1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.ipc;
19
20 import org.apache.hadoop.conf.Configuration;
21 import org.apache.hadoop.hbase.DaemonThreadFactory;
22
23 import java.io.IOException;
24 import java.util.concurrent.ArrayBlockingQueue;
25 import java.util.concurrent.ThreadPoolExecutor;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicInteger;
28
29
30
31
32
33
34 public class FifoRpcScheduler extends RpcScheduler {
35 private final int handlerCount;
36 private final int maxQueueLength;
37 private final AtomicInteger queueSize = new AtomicInteger(0);
38 private ThreadPoolExecutor executor;
39
40 public FifoRpcScheduler(Configuration conf, int handlerCount) {
41 this.handlerCount = handlerCount;
42 this.maxQueueLength = conf.getInt(RpcScheduler.IPC_SERVER_MAX_CALLQUEUE_LENGTH,
43 handlerCount * RpcServer.DEFAULT_MAX_CALLQUEUE_LENGTH_PER_HANDLER);
44 }
45
46 @Override
47 public void init(Context context) {
48
49 }
50
51 @Override
52 public void start() {
53 this.executor = new ThreadPoolExecutor(
54 handlerCount,
55 handlerCount,
56 60,
57 TimeUnit.SECONDS,
58 new ArrayBlockingQueue<Runnable>(maxQueueLength),
59 new DaemonThreadFactory("FifoRpcScheduler.handler"),
60 new ThreadPoolExecutor.CallerRunsPolicy());
61 }
62
63 @Override
64 public void stop() {
65 this.executor.shutdown();
66 }
67
68 @Override
69 public boolean dispatch(final CallRunner task) throws IOException, InterruptedException {
70
71 int queued = queueSize.getAndIncrement();
72 if (maxQueueLength > 0 && queued >= maxQueueLength) {
73 queueSize.decrementAndGet();
74 return false;
75 }
76 executor.submit(new Runnable() {
77 @Override
78 public void run() {
79 task.setStatus(RpcServer.getStatus());
80 task.run();
81 queueSize.decrementAndGet();
82 }
83 });
84 return true;
85 }
86
87 @Override
88 public int getGeneralQueueLength() {
89 return executor.getQueue().size();
90 }
91
92 @Override
93 public int getPriorityQueueLength() {
94 return 0;
95 }
96
97 @Override
98 public int getReplicationQueueLength() {
99 return 0;
100 }
101
102 @Override
103 public int getActiveRpcHandlerCount() {
104 return executor.getActiveCount();
105 }
106
107 @Override
108 public long getNumGeneralCallsDropped() {
109 return 0;
110 }
111
112 @Override
113 public long getNumLifoModeSwitches() {
114 return 0;
115 }
116 }