001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import java.io.IOException;
021import java.util.Map;
022import org.apache.hadoop.hbase.ExtendedCellScannable;
023import org.apache.hadoop.hbase.ExtendedCellScanner;
024import org.apache.hadoop.hbase.HBaseInterfaceAudience;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.RegionInfo;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.yetus.audience.InterfaceStability;
030import org.checkerframework.checker.nullness.qual.Nullable;
031
032import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback;
033import org.apache.hbase.thirdparty.com.google.protobuf.RpcController;
034
035/**
036 * Optionally carries Cells across the proxy/service interface down into ipc. On its way out it
037 * optionally carries a set of result Cell data. We stick the Cells here when we want to avoid
038 * having to protobuf them (for performance reasons). This class is used ferrying data across the
039 * proxy/protobuf service chasm. Also does call timeout and on client-side, carries the target
040 * RegionInfo we're making the call against if relevant (useful adding info to exceptions and logs).
041 * Used by client and server ipc'ing.
042 */
043@InterfaceAudience.LimitedPrivate({ HBaseInterfaceAudience.COPROC, HBaseInterfaceAudience.PHOENIX,
044  HBaseInterfaceAudience.REPLICATION })
045@InterfaceStability.Evolving
046public interface HBaseRpcController extends RpcController, ExtendedCellScannable {
047
048  /**
049   * Only used to send cells to rpc server, the returned cells should be set by
050   * {@link #setDone(ExtendedCellScanner)}.
051   */
052  void setCellScanner(ExtendedCellScanner cellScanner);
053
054  /**
055   * Set the priority for this operation.
056   * @param priority Priority for this request; should fall roughly in the range
057   *                 {@link HConstants#NORMAL_QOS} to {@link HConstants#HIGH_QOS}
058   * @deprecated Since 3.0.0, will be remove in 4.0.0. Use {@link #setPriority(int, TableName)}
059   *             instead.
060   */
061  @Deprecated
062  void setPriority(int priority);
063
064  /**
065   * Set the priority for this operation.
066   * @param tn Set priority based off the table we are going against.
067   * @deprecated Since 3.0.0, will be remove in 4.0.0. Use {@link #setPriority(int, TableName)}
068   *             instead.
069   */
070  @Deprecated
071  void setPriority(final TableName tn);
072
073  /**
074   * Set the priority for this rpc request.
075   * <p>
076   * For keep compatibility, here we declare the default method where we call
077   * {@link #setPriority(int)} and then {@link #setPriority(TableName)}.
078   * <p>
079   * The default implementation in HBase follow the below rules:
080   * <ol>
081   * <li>If user set a priority explicitly, then just use it.</li>
082   * <li>For system table, use {@link HConstants#SYSTEMTABLE_QOS}.</li>
083   * <li>For other tables, use {@link HConstants#NORMAL_QOS}.</li>
084   * </ol>
085   * @param priority  the priority set by user, can be {@link HConstants#PRIORITY_UNSET}.
086   * @param tableName the table we operate on, can be null.
087   */
088  default void setPriority(int priority, @Nullable TableName tableName) {
089    setPriority(priority);
090    setPriority(tableName);
091  }
092
093  /** Returns The priority of this request */
094  int getPriority();
095
096  int getCallTimeout();
097
098  void setCallTimeout(int callTimeout);
099
100  boolean hasCallTimeout();
101
102  /**
103   * Get the map of request attributes
104   */
105  Map<String, byte[]> getRequestAttributes();
106
107  /**
108   * Set the map of request attributes
109   */
110  void setRequestAttributes(Map<String, byte[]> requestAttributes);
111
112  /**
113   * Set failed with an exception to pass on. For use in async rpc clients
114   * @param e exception to set with
115   */
116  void setFailed(IOException e);
117
118  /**
119   * Return the failed exception, null if not failed.
120   */
121  IOException getFailed();
122
123  /**
124   * <b>IMPORTANT:</b> always call this method if the call finished without any exception to tell
125   * the {@code HBaseRpcController} that we are done.
126   */
127  void setDone(ExtendedCellScanner cellScanner);
128
129  /**
130   * A little different from the basic RpcController:
131   * <ol>
132   * <li>You can register multiple callbacks to an {@code HBaseRpcController}.</li>
133   * <li>The callback will not be called if the rpc call is finished without any cancellation.</li>
134   * <li>You can call me at client side also.</li>
135   * </ol>
136   */
137  @Override
138  void notifyOnCancel(RpcCallback<Object> callback);
139
140  interface CancellationCallback {
141    void run(boolean cancelled) throws IOException;
142  }
143
144  /**
145   * If not cancelled, add the callback to cancellation callback list. And then execute the action
146   * with the cancellation state as a parameter. The implementation should guarantee that the
147   * cancellation state does not change during this call.
148   */
149  void notifyOnCancel(RpcCallback<Object> callback, CancellationCallback action) throws IOException;
150
151  /** Returns True if this Controller is carrying the RPC target Region's RegionInfo. */
152  default boolean hasRegionInfo() {
153    return false;
154  }
155
156  /** Returns Target Region's RegionInfo or null if not available or pertinent. */
157  default RegionInfo getRegionInfo() {
158    return null;
159  }
160
161  /** Sets Region's table name. */
162  default void setTableName(TableName tableName) {
163
164  }
165
166  /** Returns Region's table name or null if not available or pertinent. */
167  default TableName getTableName() {
168    return null;
169  }
170}