View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.ipc;
19  
20  import java.util.Deque;
21  import java.util.concurrent.BlockingQueue;
22  import java.util.concurrent.ConcurrentLinkedDeque;
23  import java.util.concurrent.atomic.AtomicInteger;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.Abortable;
26  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * RPC Executor that extends {@link RWQueueRpcExecutor} with fast-path feature, used in
34   * {@link FastPathBalancedQueueRpcExecutor}.
35   */
36  @InterfaceAudience.LimitedPrivate({ HBaseInterfaceAudience.COPROC, HBaseInterfaceAudience.PHOENIX})
37  @InterfaceStability.Evolving
38  public class FastPathRWQueueRpcExecutor extends RWQueueRpcExecutor {
39    private static final Logger LOG = LoggerFactory.getLogger(RWQueueRpcExecutor.class);
40  
41    private final Deque<FastPathRpcHandler> readHandlerStack = new ConcurrentLinkedDeque<>();
42    private final Deque<FastPathRpcHandler> writeHandlerStack = new ConcurrentLinkedDeque<>();
43    private final Deque<FastPathRpcHandler> scanHandlerStack = new ConcurrentLinkedDeque<>();
44  
45    public FastPathRWQueueRpcExecutor(String name, int handlerCount, int maxQueueLength,
46      PriorityFunction priority, Configuration conf, Abortable abortable) {
47      super(name, handlerCount, maxQueueLength, priority, conf, abortable);
48    }
49  
50    @Override
51    protected RpcHandler getHandler(final String name, final double handlerFailureThreshhold,
52      final int handlerCount, final BlockingQueue<CallRunner> q,
53      final AtomicInteger activeHandlerCount, final AtomicInteger failedHandlerCount,
54      final Abortable abortable) {
55      Deque<FastPathRpcHandler> handlerStack = name.contains("read") ? readHandlerStack :
56        name.contains("write") ? writeHandlerStack : scanHandlerStack;
57      return new FastPathRpcHandler(name, handlerFailureThreshhold, handlerCount, q,
58        activeHandlerCount, failedHandlerCount, abortable, handlerStack);
59    }
60  
61    @Override
62    public boolean dispatch(final CallRunner callTask) throws InterruptedException {
63      RpcServer.Call call = callTask.getCall();
64      boolean shouldDispatchToWriteQueue = isWriteRequest(call.getHeader(), call.param);
65      boolean shouldDispatchToScanQueue = shouldDispatchToScanQueue(callTask);
66      FastPathRpcHandler handler = shouldDispatchToWriteQueue ? writeHandlerStack.poll() :
67        shouldDispatchToScanQueue ? scanHandlerStack.poll() : readHandlerStack.poll();
68      return handler != null ? handler.loadCallRunner(callTask) :
69        dispatchTo(shouldDispatchToWriteQueue, shouldDispatchToScanQueue, callTask);
70    }
71  }