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 static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHORIZATION;
021
022import java.io.IOException;
023import java.net.InetAddress;
024import java.net.InetSocketAddress;
025import java.nio.ByteBuffer;
026import java.nio.channels.ReadableByteChannel;
027import java.nio.channels.WritableByteChannel;
028import java.util.Collections;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Locale;
032import java.util.Map;
033import java.util.Optional;
034import java.util.concurrent.atomic.LongAdder;
035import org.apache.commons.lang3.StringUtils;
036import org.apache.hadoop.conf.Configuration;
037import org.apache.hadoop.hbase.CallQueueTooBigException;
038import org.apache.hadoop.hbase.CellScanner;
039import org.apache.hadoop.hbase.DoNotRetryIOException;
040import org.apache.hadoop.hbase.HConstants;
041import org.apache.hadoop.hbase.Server;
042import org.apache.hadoop.hbase.conf.ConfigurationObserver;
043import org.apache.hadoop.hbase.io.ByteBuffAllocator;
044import org.apache.hadoop.hbase.monitoring.MonitoredRPCHandler;
045import org.apache.hadoop.hbase.monitoring.TaskMonitor;
046import org.apache.hadoop.hbase.namequeues.NamedQueueRecorder;
047import org.apache.hadoop.hbase.namequeues.RpcLogDetails;
048import org.apache.hadoop.hbase.regionserver.RSRpcServices;
049import org.apache.hadoop.hbase.security.HBasePolicyProvider;
050import org.apache.hadoop.hbase.security.SaslUtil;
051import org.apache.hadoop.hbase.security.SaslUtil.QualityOfProtection;
052import org.apache.hadoop.hbase.security.User;
053import org.apache.hadoop.hbase.security.UserProvider;
054import org.apache.hadoop.hbase.security.token.AuthenticationTokenSecretManager;
055import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
056import org.apache.hadoop.hbase.util.GsonUtil;
057import org.apache.hadoop.hbase.util.Pair;
058import org.apache.hadoop.security.UserGroupInformation;
059import org.apache.hadoop.security.authorize.AuthorizationException;
060import org.apache.hadoop.security.authorize.PolicyProvider;
061import org.apache.hadoop.security.authorize.ProxyUsers;
062import org.apache.hadoop.security.authorize.ServiceAuthorizationManager;
063import org.apache.hadoop.security.token.SecretManager;
064import org.apache.hadoop.security.token.TokenIdentifier;
065import org.apache.yetus.audience.InterfaceAudience;
066import org.slf4j.Logger;
067import org.slf4j.LoggerFactory;
068
069import org.apache.hbase.thirdparty.com.google.gson.Gson;
070import org.apache.hbase.thirdparty.com.google.protobuf.BlockingService;
071import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.MethodDescriptor;
072import org.apache.hbase.thirdparty.com.google.protobuf.Message;
073import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
074import org.apache.hbase.thirdparty.com.google.protobuf.TextFormat;
075
076import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
077import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
078import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.ConnectionHeader;
079
080/**
081 * An RPC server that hosts protobuf described Services.
082 */
083@InterfaceAudience.Private
084public abstract class RpcServer implements RpcServerInterface, ConfigurationObserver {
085  // LOG is being used in CallRunner and the log level is being changed in tests
086  public static final Logger LOG = LoggerFactory.getLogger(RpcServer.class);
087  protected static final CallQueueTooBigException CALL_QUEUE_TOO_BIG_EXCEPTION =
088    new CallQueueTooBigException();
089
090  private static final String MULTI_GETS = "multi.gets";
091  private static final String MULTI_MUTATIONS = "multi.mutations";
092  private static final String MULTI_SERVICE_CALLS = "multi.service_calls";
093
094  private final boolean authorize;
095  private final boolean isOnlineLogProviderEnabled;
096  protected boolean isSecurityEnabled;
097
098  public static final byte CURRENT_VERSION = 0;
099
100  /**
101   * Whether we allow a fallback to SIMPLE auth for insecure clients when security is enabled.
102   */
103  public static final String FALLBACK_TO_INSECURE_CLIENT_AUTH =
104    "hbase.ipc.server.fallback-to-simple-auth-allowed";
105
106  /**
107   * How many calls/handler are allowed in the queue.
108   */
109  protected static final int DEFAULT_MAX_CALLQUEUE_LENGTH_PER_HANDLER = 10;
110
111  protected final CellBlockBuilder cellBlockBuilder;
112
113  protected static final String AUTH_FAILED_FOR = "Auth failed for ";
114  protected static final String AUTH_SUCCESSFUL_FOR = "Auth successful for ";
115  protected static final Logger AUDITLOG =
116    LoggerFactory.getLogger("SecurityLogger." + Server.class.getName());
117  protected SecretManager<TokenIdentifier> secretManager;
118  protected final Map<String, String> saslProps;
119
120  protected ServiceAuthorizationManager authManager;
121
122  /**
123   * This is set to Call object before Handler invokes an RPC and ybdie after the call returns.
124   */
125  protected static final ThreadLocal<RpcCall> CurCall = new ThreadLocal<>();
126
127  /** Keeps MonitoredRPCHandler per handler thread. */
128  protected static final ThreadLocal<MonitoredRPCHandler> MONITORED_RPC = new ThreadLocal<>();
129
130  protected final InetSocketAddress bindAddress;
131
132  protected MetricsHBaseServer metrics;
133
134  protected final Configuration conf;
135
136  /**
137   * Maximum size in bytes of the currently queued and running Calls. If a new Call puts us over
138   * this size, then we will reject the call (after parsing it though). It will go back to the
139   * client and client will retry. Set this size with "hbase.ipc.server.max.callqueue.size". The
140   * call queue size gets incremented after we parse a call and before we add it to the queue of
141   * calls for the scheduler to use. It get decremented after we have 'run' the Call. The current
142   * size is kept in {@link #callQueueSizeInBytes}.
143   * @see #callQueueSizeInBytes
144   * @see #DEFAULT_MAX_CALLQUEUE_SIZE
145   */
146  protected final long maxQueueSizeInBytes;
147  protected static final int DEFAULT_MAX_CALLQUEUE_SIZE = 1024 * 1024 * 1024;
148
149  /**
150   * This is a running count of the size in bytes of all outstanding calls whether currently
151   * executing or queued waiting to be run.
152   */
153  protected final LongAdder callQueueSizeInBytes = new LongAdder();
154
155  protected final boolean tcpNoDelay; // if T then disable Nagle's Algorithm
156  protected final boolean tcpKeepAlive; // if T then use keepalives
157
158  /**
159   * This flag is used to indicate to sub threads when they should go down. When we call
160   * {@link #start()}, all threads started will consult this flag on whether they should keep going.
161   * It is set to false when {@link #stop()} is called.
162   */
163  volatile boolean running = true;
164
165  /**
166   * This flag is set to true after all threads are up and 'running' and the server is then opened
167   * for business by the call to {@link #start()}.
168   */
169  volatile boolean started = false;
170
171  protected AuthenticationTokenSecretManager authTokenSecretMgr = null;
172
173  protected HBaseRPCErrorHandler errorHandler = null;
174
175  public static final String MAX_REQUEST_SIZE = "hbase.ipc.max.request.size";
176
177  protected static final String WARN_RESPONSE_TIME = "hbase.ipc.warn.response.time";
178  protected static final String WARN_RESPONSE_SIZE = "hbase.ipc.warn.response.size";
179
180  /**
181   * Minimum allowable timeout (in milliseconds) in rpc request's header. This configuration exists
182   * to prevent the rpc service regarding this request as timeout immediately.
183   */
184  protected static final String MIN_CLIENT_REQUEST_TIMEOUT = "hbase.ipc.min.client.request.timeout";
185  protected static final int DEFAULT_MIN_CLIENT_REQUEST_TIMEOUT = 20;
186
187  /** Default value for above params */
188  public static final int DEFAULT_MAX_REQUEST_SIZE = DEFAULT_MAX_CALLQUEUE_SIZE / 4; // 256M
189  protected static final int DEFAULT_WARN_RESPONSE_TIME = 10000; // milliseconds
190  protected static final int DEFAULT_WARN_RESPONSE_SIZE = 100 * 1024 * 1024;
191
192  protected static final int DEFAULT_TRACE_LOG_MAX_LENGTH = 1000;
193  protected static final String TRACE_LOG_MAX_LENGTH = "hbase.ipc.trace.log.max.length";
194  protected static final String KEY_WORD_TRUNCATED = " <TRUNCATED>";
195
196  protected static final Gson GSON = GsonUtil.createGsonWithDisableHtmlEscaping().create();
197
198  protected final int maxRequestSize;
199  protected final int warnResponseTime;
200  protected final int warnResponseSize;
201
202  protected final int minClientRequestTimeout;
203
204  protected final Server server;
205  protected final List<BlockingServiceAndInterface> services;
206
207  protected final RpcScheduler scheduler;
208
209  protected UserProvider userProvider;
210
211  protected final ByteBuffAllocator bbAllocator;
212
213  protected volatile boolean allowFallbackToSimpleAuth;
214
215  /**
216   * Used to get details for scan with a scanner_id<br/>
217   * TODO try to figure out a better way and remove reference from regionserver package later.
218   */
219  private RSRpcServices rsRpcServices;
220
221  /**
222   * Use to add online slowlog responses
223   */
224  private NamedQueueRecorder namedQueueRecorder;
225
226  @FunctionalInterface
227  protected interface CallCleanup {
228    void run();
229  }
230
231  /**
232   * Datastructure for passing a {@link BlockingService} and its associated class of protobuf
233   * service interface. For example, a server that fielded what is defined in the client protobuf
234   * service would pass in an implementation of the client blocking service and then its
235   * ClientService.BlockingInterface.class. Used checking connection setup.
236   */
237  public static class BlockingServiceAndInterface {
238    private final BlockingService service;
239    private final Class<?> serviceInterface;
240
241    public BlockingServiceAndInterface(final BlockingService service,
242      final Class<?> serviceInterface) {
243      this.service = service;
244      this.serviceInterface = serviceInterface;
245    }
246
247    public Class<?> getServiceInterface() {
248      return this.serviceInterface;
249    }
250
251    public BlockingService getBlockingService() {
252      return this.service;
253    }
254  }
255
256  /**
257   * Constructs a server listening on the named port and address.
258   * @param server           hosting instance of {@link Server}. We will do authentications if an
259   *                         instance else pass null for no authentication check.
260   * @param name             Used keying this rpc servers' metrics and for naming the Listener
261   *                         thread.
262   * @param services         A list of services.
263   * @param bindAddress      Where to listen
264   * @param reservoirEnabled Enable ByteBufferPool or not.
265   */
266  public RpcServer(final Server server, final String name,
267    final List<BlockingServiceAndInterface> services, final InetSocketAddress bindAddress,
268    Configuration conf, RpcScheduler scheduler, boolean reservoirEnabled) throws IOException {
269    this.bbAllocator = ByteBuffAllocator.create(conf, reservoirEnabled);
270    this.server = server;
271    this.services = services;
272    this.bindAddress = bindAddress;
273    this.conf = conf;
274    // See declaration above for documentation on what this size is.
275    this.maxQueueSizeInBytes =
276      this.conf.getLong("hbase.ipc.server.max.callqueue.size", DEFAULT_MAX_CALLQUEUE_SIZE);
277
278    this.warnResponseTime = conf.getInt(WARN_RESPONSE_TIME, DEFAULT_WARN_RESPONSE_TIME);
279    this.warnResponseSize = conf.getInt(WARN_RESPONSE_SIZE, DEFAULT_WARN_RESPONSE_SIZE);
280    this.minClientRequestTimeout =
281      conf.getInt(MIN_CLIENT_REQUEST_TIMEOUT, DEFAULT_MIN_CLIENT_REQUEST_TIMEOUT);
282    this.maxRequestSize = conf.getInt(MAX_REQUEST_SIZE, DEFAULT_MAX_REQUEST_SIZE);
283
284    this.metrics = new MetricsHBaseServer(name, new MetricsHBaseServerWrapperImpl(this));
285    this.tcpNoDelay = conf.getBoolean("hbase.ipc.server.tcpnodelay", true);
286    this.tcpKeepAlive = conf.getBoolean("hbase.ipc.server.tcpkeepalive", true);
287
288    this.cellBlockBuilder = new CellBlockBuilder(conf);
289
290    this.authorize = conf.getBoolean(HADOOP_SECURITY_AUTHORIZATION, false);
291    this.userProvider = UserProvider.instantiate(conf);
292    this.isSecurityEnabled = userProvider.isHBaseSecurityEnabled();
293    if (isSecurityEnabled) {
294      saslProps = SaslUtil.initSaslProperties(conf.get("hbase.rpc.protection",
295        QualityOfProtection.AUTHENTICATION.name().toLowerCase(Locale.ROOT)));
296    } else {
297      saslProps = Collections.emptyMap();
298    }
299
300    this.isOnlineLogProviderEnabled = conf.getBoolean(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY,
301      HConstants.DEFAULT_ONLINE_LOG_PROVIDER_ENABLED);
302    this.scheduler = scheduler;
303  }
304
305  @Override
306  public void onConfigurationChange(Configuration newConf) {
307    initReconfigurable(newConf);
308    if (scheduler instanceof ConfigurationObserver) {
309      ((ConfigurationObserver) scheduler).onConfigurationChange(newConf);
310    }
311    if (authorize) {
312      refreshAuthManager(newConf, new HBasePolicyProvider());
313    }
314  }
315
316  protected void initReconfigurable(Configuration confToLoad) {
317    this.allowFallbackToSimpleAuth = confToLoad.getBoolean(FALLBACK_TO_INSECURE_CLIENT_AUTH, false);
318    if (isSecurityEnabled && allowFallbackToSimpleAuth) {
319      LOG.warn("********* WARNING! *********");
320      LOG.warn("This server is configured to allow connections from INSECURE clients");
321      LOG.warn("(" + FALLBACK_TO_INSECURE_CLIENT_AUTH + " = true).");
322      LOG.warn("While this option is enabled, client identities cannot be secured, and user");
323      LOG.warn("impersonation is possible!");
324      LOG.warn("For secure operation, please disable SIMPLE authentication as soon as possible,");
325      LOG.warn("by setting " + FALLBACK_TO_INSECURE_CLIENT_AUTH + " = false in hbase-site.xml");
326      LOG.warn("****************************");
327    }
328  }
329
330  Configuration getConf() {
331    return conf;
332  }
333
334  @Override
335  public boolean isStarted() {
336    return this.started;
337  }
338
339  @Override
340  public synchronized void refreshAuthManager(Configuration conf, PolicyProvider pp) {
341    // Ignore warnings that this should be accessed in a static way instead of via an instance;
342    // it'll break if you go via static route.
343    System.setProperty("hadoop.policy.file", "hbase-policy.xml");
344    this.authManager.refresh(conf, pp);
345    LOG.info("Refreshed hbase-policy.xml successfully");
346    ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
347    LOG.info("Refreshed super and proxy users successfully");
348  }
349
350  protected AuthenticationTokenSecretManager createSecretManager() {
351    if (!isSecurityEnabled) return null;
352    if (server == null) return null;
353    Configuration conf = server.getConfiguration();
354    long keyUpdateInterval = conf.getLong("hbase.auth.key.update.interval", 24 * 60 * 60 * 1000);
355    long maxAge = conf.getLong("hbase.auth.token.max.lifetime", 7 * 24 * 60 * 60 * 1000);
356    return new AuthenticationTokenSecretManager(conf, server.getZooKeeper(),
357      server.getServerName().toString(), keyUpdateInterval, maxAge);
358  }
359
360  public SecretManager<? extends TokenIdentifier> getSecretManager() {
361    return this.secretManager;
362  }
363
364  @SuppressWarnings("unchecked")
365  public void setSecretManager(SecretManager<? extends TokenIdentifier> secretManager) {
366    this.secretManager = (SecretManager<TokenIdentifier>) secretManager;
367  }
368
369  /**
370   * This is a server side method, which is invoked over RPC. On success the return response has
371   * protobuf response payload. On failure, the exception name and the stack trace are returned in
372   * the protobuf response.
373   */
374  @Override
375  public Pair<Message, CellScanner> call(RpcCall call, MonitoredRPCHandler status)
376    throws IOException {
377    try {
378      MethodDescriptor md = call.getMethod();
379      Message param = call.getParam();
380      status.setRPC(md.getName(), new Object[] { param }, call.getReceiveTime());
381      // TODO: Review after we add in encoded data blocks.
382      status.setRPCPacket(param);
383      status.resume("Servicing call");
384      // get an instance of the method arg type
385      HBaseRpcController controller = new HBaseRpcControllerImpl(call.getCellScanner());
386      controller.setCallTimeout(call.getTimeout());
387      Message result = call.getService().callBlockingMethod(md, controller, param);
388      long receiveTime = call.getReceiveTime();
389      long startTime = call.getStartTime();
390      long endTime = EnvironmentEdgeManager.currentTime();
391      int processingTime = (int) (endTime - startTime);
392      int qTime = (int) (startTime - receiveTime);
393      int totalTime = (int) (endTime - receiveTime);
394      if (LOG.isTraceEnabled()) {
395        LOG.trace(
396          "{}, response: {}, receiveTime: {}, queueTime: {}, processingTime: {}, totalTime: {}",
397          CurCall.get().toString(), TextFormat.shortDebugString(result),
398          CurCall.get().getReceiveTime(), qTime, processingTime, totalTime);
399      }
400      // Use the raw request call size for now.
401      long requestSize = call.getSize();
402      long responseSize = result.getSerializedSize();
403      if (call.isClientCellBlockSupported()) {
404        // Include the payload size in HBaseRpcController
405        responseSize += call.getResponseCellSize();
406      }
407
408      metrics.dequeuedCall(qTime);
409      metrics.processedCall(processingTime);
410      metrics.totalCall(totalTime);
411      metrics.receivedRequest(requestSize);
412      metrics.sentResponse(responseSize);
413      // log any RPC responses that are slower than the configured warn
414      // response time or larger than configured warning size
415      boolean tooSlow = (processingTime > warnResponseTime && warnResponseTime > -1);
416      boolean tooLarge = (responseSize > warnResponseSize && warnResponseSize > -1);
417      if (tooSlow || tooLarge) {
418        final String userName = call.getRequestUserName().orElse(StringUtils.EMPTY);
419        // when tagging, we let TooLarge trump TooSmall to keep output simple
420        // note that large responses will often also be slow.
421        logResponse(param, md.getName(), md.getName() + "(" + param.getClass().getName() + ")",
422          tooLarge, tooSlow, status.getClient(), startTime, processingTime, qTime, responseSize,
423          userName);
424        if (this.namedQueueRecorder != null && this.isOnlineLogProviderEnabled) {
425          // send logs to ring buffer owned by slowLogRecorder
426          final String className =
427            server == null ? StringUtils.EMPTY : server.getClass().getSimpleName();
428          this.namedQueueRecorder.addRecord(new RpcLogDetails(call, param, status.getClient(),
429            responseSize, className, tooSlow, tooLarge));
430        }
431      }
432      return new Pair<>(result, controller.cellScanner());
433    } catch (Throwable e) {
434      // The above callBlockingMethod will always return a SE. Strip the SE wrapper before
435      // putting it on the wire. Its needed to adhere to the pb Service Interface but we don't
436      // need to pass it over the wire.
437      if (e instanceof ServiceException) {
438        if (e.getCause() == null) {
439          LOG.debug("Caught a ServiceException with null cause", e);
440        } else {
441          e = e.getCause();
442        }
443      }
444
445      // increment the number of requests that were exceptions.
446      metrics.exception(e);
447
448      if (e instanceof LinkageError) throw new DoNotRetryIOException(e);
449      if (e instanceof IOException) throw (IOException) e;
450      LOG.error("Unexpected throwable object ", e);
451      throw new IOException(e.getMessage(), e);
452    }
453  }
454
455  /**
456   * Logs an RPC response to the LOG file, producing valid JSON objects for client Operations.
457   * @param param          The parameters received in the call.
458   * @param methodName     The name of the method invoked
459   * @param call           The string representation of the call
460   * @param tooLarge       To indicate if the event is tooLarge
461   * @param tooSlow        To indicate if the event is tooSlow
462   * @param clientAddress  The address of the client who made this call.
463   * @param startTime      The time that the call was initiated, in ms.
464   * @param processingTime The duration that the call took to run, in ms.
465   * @param qTime          The duration that the call spent on the queue prior to being initiated,
466   *                       in ms.
467   * @param responseSize   The size in bytes of the response buffer.
468   * @param userName       UserName of the current RPC Call
469   */
470  void logResponse(Message param, String methodName, String call, boolean tooLarge, boolean tooSlow,
471    String clientAddress, long startTime, int processingTime, int qTime, long responseSize,
472    String userName) {
473    final String className = server == null ? StringUtils.EMPTY : server.getClass().getSimpleName();
474    // base information that is reported regardless of type of call
475    Map<String, Object> responseInfo = new HashMap<>();
476    responseInfo.put("starttimems", startTime);
477    responseInfo.put("processingtimems", processingTime);
478    responseInfo.put("queuetimems", qTime);
479    responseInfo.put("responsesize", responseSize);
480    responseInfo.put("client", clientAddress);
481    responseInfo.put("class", className);
482    responseInfo.put("method", methodName);
483    responseInfo.put("call", call);
484    // The params could be really big, make sure they don't kill us at WARN
485    String stringifiedParam = ProtobufUtil.getShortTextFormat(param);
486    if (stringifiedParam.length() > 150) {
487      // Truncate to 1000 chars if TRACE is on, else to 150 chars
488      stringifiedParam = truncateTraceLog(stringifiedParam);
489    }
490    responseInfo.put("param", stringifiedParam);
491    if (param instanceof ClientProtos.ScanRequest && rsRpcServices != null) {
492      ClientProtos.ScanRequest request = ((ClientProtos.ScanRequest) param);
493      String scanDetails;
494      if (request.hasScannerId()) {
495        long scannerId = request.getScannerId();
496        scanDetails = rsRpcServices.getScanDetailsWithId(scannerId);
497      } else {
498        scanDetails = rsRpcServices.getScanDetailsWithRequest(request);
499      }
500      if (scanDetails != null) {
501        responseInfo.put("scandetails", scanDetails);
502      }
503    }
504    if (param instanceof ClientProtos.MultiRequest) {
505      int numGets = 0;
506      int numMutations = 0;
507      int numServiceCalls = 0;
508      ClientProtos.MultiRequest multi = (ClientProtos.MultiRequest) param;
509      for (ClientProtos.RegionAction regionAction : multi.getRegionActionList()) {
510        for (ClientProtos.Action action : regionAction.getActionList()) {
511          if (action.hasMutation()) {
512            numMutations++;
513          }
514          if (action.hasGet()) {
515            numGets++;
516          }
517          if (action.hasServiceCall()) {
518            numServiceCalls++;
519          }
520        }
521      }
522      responseInfo.put(MULTI_GETS, numGets);
523      responseInfo.put(MULTI_MUTATIONS, numMutations);
524      responseInfo.put(MULTI_SERVICE_CALLS, numServiceCalls);
525    }
526    final String tag =
527      (tooLarge && tooSlow) ? "TooLarge & TooSlow" : (tooSlow ? "TooSlow" : "TooLarge");
528    LOG.warn("(response" + tag + "): " + GSON.toJson(responseInfo));
529  }
530
531  /**
532   * Truncate to number of chars decided by conf hbase.ipc.trace.log.max.length if TRACE is on else
533   * to 150 chars Refer to Jira HBASE-20826 and HBASE-20942
534   * @param strParam stringifiedParam to be truncated
535   * @return truncated trace log string
536   */
537  String truncateTraceLog(String strParam) {
538    if (LOG.isTraceEnabled()) {
539      int traceLogMaxLength = getConf().getInt(TRACE_LOG_MAX_LENGTH, DEFAULT_TRACE_LOG_MAX_LENGTH);
540      int truncatedLength =
541        strParam.length() < traceLogMaxLength ? strParam.length() : traceLogMaxLength;
542      String truncatedFlag = truncatedLength == strParam.length() ? "" : KEY_WORD_TRUNCATED;
543      return strParam.subSequence(0, truncatedLength) + truncatedFlag;
544    }
545    return strParam.subSequence(0, 150) + KEY_WORD_TRUNCATED;
546  }
547
548  /**
549   * Set the handler for calling out of RPC for error conditions.
550   * @param handler the handler implementation
551   */
552  @Override
553  public void setErrorHandler(HBaseRPCErrorHandler handler) {
554    this.errorHandler = handler;
555  }
556
557  @Override
558  public HBaseRPCErrorHandler getErrorHandler() {
559    return this.errorHandler;
560  }
561
562  /**
563   * Returns the metrics instance for reporting RPC call statistics
564   */
565  @Override
566  public MetricsHBaseServer getMetrics() {
567    return metrics;
568  }
569
570  @Override
571  public void addCallSize(final long diff) {
572    this.callQueueSizeInBytes.add(diff);
573  }
574
575  /**
576   * Authorize the incoming client connection.
577   * @param user       client user
578   * @param connection incoming connection
579   * @param addr       InetAddress of incoming connection
580   * @throws AuthorizationException when the client isn't authorized to talk the protocol
581   */
582  public synchronized void authorize(UserGroupInformation user, ConnectionHeader connection,
583    InetAddress addr) throws AuthorizationException {
584    if (authorize) {
585      Class<?> c = getServiceInterface(services, connection.getServiceName());
586      authManager.authorize(user, c, getConf(), addr);
587    }
588  }
589
590  /**
591   * When the read or write buffer size is larger than this limit, i/o will be done in chunks of
592   * this size. Most RPC requests and responses would be be smaller.
593   */
594  protected static final int NIO_BUFFER_LIMIT = 64 * 1024; // should not be more than 64KB.
595
596  /**
597   * This is a wrapper around
598   * {@link java.nio.channels.ReadableByteChannel#read(java.nio.ByteBuffer)}. If the amount of data
599   * is large, it writes to channel in smaller chunks. This is to avoid jdk from creating many
600   * direct buffers as the size of ByteBuffer increases. There should not be any performance
601   * degredation.
602   * @param channel writable byte channel to write on
603   * @param buffer  buffer to write
604   * @return number of bytes written
605   * @throws java.io.IOException e
606   * @see java.nio.channels.ReadableByteChannel#read(java.nio.ByteBuffer)
607   */
608  protected int channelRead(ReadableByteChannel channel, ByteBuffer buffer) throws IOException {
609
610    int count = (buffer.remaining() <= NIO_BUFFER_LIMIT)
611      ? channel.read(buffer)
612      : channelIO(channel, null, buffer);
613    if (count > 0) {
614      metrics.receivedBytes(count);
615    }
616    return count;
617  }
618
619  /**
620   * Helper for {@link #channelRead(java.nio.channels.ReadableByteChannel, java.nio.ByteBuffer)}.
621   * Only one of readCh or writeCh should be non-null.
622   * @param readCh  read channel
623   * @param writeCh write channel
624   * @param buf     buffer to read or write into/out of
625   * @return bytes written
626   * @throws java.io.IOException e
627   * @see #channelRead(java.nio.channels.ReadableByteChannel, java.nio.ByteBuffer)
628   */
629  private static int channelIO(ReadableByteChannel readCh, WritableByteChannel writeCh,
630    ByteBuffer buf) throws IOException {
631
632    int originalLimit = buf.limit();
633    int initialRemaining = buf.remaining();
634    int ret = 0;
635
636    while (buf.remaining() > 0) {
637      try {
638        int ioSize = Math.min(buf.remaining(), NIO_BUFFER_LIMIT);
639        buf.limit(buf.position() + ioSize);
640
641        ret = (readCh == null) ? writeCh.write(buf) : readCh.read(buf);
642
643        if (ret < ioSize) {
644          break;
645        }
646
647      } finally {
648        buf.limit(originalLimit);
649      }
650    }
651
652    int nBytes = initialRemaining - buf.remaining();
653    return (nBytes > 0) ? nBytes : ret;
654  }
655
656  /**
657   * Needed for features such as delayed calls. We need to be able to store the current call so that
658   * we can complete it later or ask questions of what is supported by the current ongoing call.
659   * @return An RpcCallContext backed by the currently ongoing call (gotten from a thread local)
660   */
661  public static Optional<RpcCall> getCurrentCall() {
662    return Optional.ofNullable(CurCall.get());
663  }
664
665  /**
666   * Just return the current rpc call if it is a {@link ServerCall} and also has {@link CellScanner}
667   * attached.
668   * <p/>
669   * Mainly used for reference counting as {@link CellScanner} may reference non heap memory.
670   */
671  public static Optional<ServerCall<?>> getCurrentServerCallWithCellScanner() {
672    return getCurrentCall().filter(c -> c instanceof ServerCall)
673      .filter(c -> c.getCellScanner() != null).map(c -> (ServerCall<?>) c);
674  }
675
676  public static boolean isInRpcCallContext() {
677    return CurCall.get() != null;
678  }
679
680  /**
681   * Used by {@link org.apache.hadoop.hbase.procedure2.store.region.RegionProcedureStore}. For
682   * master's rpc call, it may generate new procedure and mutate the region which store procedure.
683   * There are some check about rpc when mutate region, such as rpc timeout check. So unset the rpc
684   * call to avoid the rpc check.
685   * @return the currently ongoing rpc call
686   */
687  public static Optional<RpcCall> unsetCurrentCall() {
688    Optional<RpcCall> rpcCall = getCurrentCall();
689    CurCall.set(null);
690    return rpcCall;
691  }
692
693  /**
694   * Used by {@link org.apache.hadoop.hbase.procedure2.store.region.RegionProcedureStore}. Set the
695   * rpc call back after mutate region.
696   */
697  public static void setCurrentCall(RpcCall rpcCall) {
698    CurCall.set(rpcCall);
699  }
700
701  /**
702   * Returns the user credentials associated with the current RPC request or not present if no
703   * credentials were provided.
704   * @return A User
705   */
706  public static Optional<User> getRequestUser() {
707    Optional<RpcCall> ctx = getCurrentCall();
708    return ctx.isPresent() ? ctx.get().getRequestUser() : Optional.empty();
709  }
710
711  /**
712   * The number of open RPC conections
713   * @return the number of open rpc connections
714   */
715  abstract public int getNumOpenConnections();
716
717  /**
718   * Returns the username for any user associated with the current RPC request or not present if no
719   * user is set.
720   */
721  public static Optional<String> getRequestUserName() {
722    return getRequestUser().map(User::getShortName);
723  }
724
725  /** Returns Address of remote client if a request is ongoing, else null */
726  public static Optional<InetAddress> getRemoteAddress() {
727    return getCurrentCall().map(RpcCall::getRemoteAddress);
728  }
729
730  /**
731   * @param serviceName Some arbitrary string that represents a 'service'.
732   * @param services    Available service instances
733   * @return Matching BlockingServiceAndInterface pair
734   */
735  protected static BlockingServiceAndInterface getServiceAndInterface(
736    final List<BlockingServiceAndInterface> services, final String serviceName) {
737    for (BlockingServiceAndInterface bs : services) {
738      if (bs.getBlockingService().getDescriptorForType().getName().equals(serviceName)) {
739        return bs;
740      }
741    }
742    return null;
743  }
744
745  /**
746   * @param serviceName Some arbitrary string that represents a 'service'.
747   * @param services    Available services and their service interfaces.
748   * @return Service interface class for <code>serviceName</code>
749   */
750  protected static Class<?> getServiceInterface(final List<BlockingServiceAndInterface> services,
751    final String serviceName) {
752    BlockingServiceAndInterface bsasi = getServiceAndInterface(services, serviceName);
753    return bsasi == null ? null : bsasi.getServiceInterface();
754  }
755
756  /**
757   * @param serviceName Some arbitrary string that represents a 'service'.
758   * @param services    Available services and their service interfaces.
759   * @return BlockingService that goes with the passed <code>serviceName</code>
760   */
761  protected static BlockingService getService(final List<BlockingServiceAndInterface> services,
762    final String serviceName) {
763    BlockingServiceAndInterface bsasi = getServiceAndInterface(services, serviceName);
764    return bsasi == null ? null : bsasi.getBlockingService();
765  }
766
767  protected static MonitoredRPCHandler getStatus() {
768    // It is ugly the way we park status up in RpcServer. Let it be for now. TODO.
769    MonitoredRPCHandler status = RpcServer.MONITORED_RPC.get();
770    if (status != null) {
771      return status;
772    }
773    status = TaskMonitor.get().createRPCStatus(Thread.currentThread().getName());
774    status.pause("Waiting for a call");
775    RpcServer.MONITORED_RPC.set(status);
776    return status;
777  }
778
779  /**
780   * Returns the remote side ip address when invoked inside an RPC Returns null incase of an error.
781   */
782  public static InetAddress getRemoteIp() {
783    RpcCall call = CurCall.get();
784    if (call != null) {
785      return call.getRemoteAddress();
786    }
787    return null;
788  }
789
790  @Override
791  public RpcScheduler getScheduler() {
792    return scheduler;
793  }
794
795  @Override
796  public ByteBuffAllocator getByteBuffAllocator() {
797    return this.bbAllocator;
798  }
799
800  @Override
801  public void setRsRpcServices(RSRpcServices rsRpcServices) {
802    this.rsRpcServices = rsRpcServices;
803  }
804
805  @Override
806  public void setNamedQueueRecorder(NamedQueueRecorder namedQueueRecorder) {
807    this.namedQueueRecorder = namedQueueRecorder;
808  }
809
810  protected boolean needAuthorization() {
811    return authorize;
812  }
813}