1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.executor;
21
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.concurrent.BlockingQueue;
27 import java.util.concurrent.LinkedBlockingQueue;
28 import java.util.concurrent.ThreadPoolExecutor;
29 import java.util.concurrent.TimeUnit;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class HBaseExecutorService
47 {
48 private static final Log LOG = LogFactory.getLog(HBaseExecutorService.class);
49
50 private int corePoolSize = 1;
51
52 private int maximumPoolSize = 5;
53
54 private long keepAliveTimeInMillis = 1000;
55
56 ThreadPoolExecutor threadPoolExecutor;
57
58 BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
59
60 String name;
61
62 static Map<String, HBaseExecutorService> executorServicesMap =
63 Collections.synchronizedMap(new HashMap<String, HBaseExecutorService>());
64
65
66
67
68
69
70 public enum HBaseExecutorServiceType {
71 NONE (-1),
72 MASTER_CLOSEREGION (1),
73 MASTER_OPENREGION (2);
74
75 private final int value;
76
77 HBaseExecutorServiceType(int intValue) {
78 this.value = intValue;
79 }
80
81 public void startExecutorService(String serverName) {
82
83 if(value == NONE.value) {
84 throw new RuntimeException("Cannot start NONE executor type.");
85 }
86 String name = getExecutorName(serverName);
87 if(HBaseExecutorService.isExecutorServiceRunning(name)) {
88 LOG.debug("Executor service " + toString() + " already running on " + serverName);
89 return;
90 }
91 HBaseExecutorService.startExecutorService(name);
92 }
93
94 public HBaseExecutorService getExecutor(String serverName) {
95
96 if(value == NONE.value) {
97 return null;
98 }
99 return HBaseExecutorService.getExecutorService(getExecutorName(serverName));
100 }
101
102 public String getExecutorName(String serverName) {
103
104 if(value == NONE.value) {
105 return null;
106 }
107 return (this.toString() + "-" + serverName);
108 }
109 }
110
111
112
113
114
115
116
117
118 public static void startExecutorService(String name) {
119 if(executorServicesMap.get(name) != null) {
120 throw new RuntimeException("An executor service with the name " + name + " is already running!");
121 }
122 HBaseExecutorService hbes = new HBaseExecutorService(name);
123 executorServicesMap.put(name, hbes);
124 LOG.debug("Starting executor service: " + name);
125 }
126
127 public static boolean isExecutorServiceRunning(String name) {
128 return (executorServicesMap.containsKey(name));
129 }
130
131
132
133
134
135 public static HBaseExecutorService getExecutorService(String name) {
136 HBaseExecutorService executor = executorServicesMap.get(name);
137 if(executor == null) {
138 LOG.debug("Executor service [" + name + "] not found.");
139 }
140 return executor;
141 }
142
143 public static void shutdown() {
144 for(Entry<String, HBaseExecutorService> entry : executorServicesMap.entrySet()) {
145 entry.getValue().threadPoolExecutor.shutdown();
146 }
147 executorServicesMap.clear();
148 }
149
150 protected HBaseExecutorService(String name) {
151 this.name = name;
152
153 threadPoolExecutor = new ThreadPoolExecutor(
154 corePoolSize,
155 maximumPoolSize,
156 keepAliveTimeInMillis,
157 TimeUnit.MILLISECONDS,
158 workQueue
159 );
160
161 threadPoolExecutor.setThreadFactory(new NamedThreadFactory(name));
162 }
163
164
165
166
167
168 public void submit(Runnable event) {
169 threadPoolExecutor.execute(event);
170 }
171 }