1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.procedure;
19
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.concurrent.ConcurrentMap;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.RejectedExecutionException;
29 import java.util.concurrent.SynchronousQueue;
30 import java.util.concurrent.ThreadPoolExecutor;
31 import java.util.concurrent.TimeUnit;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.classification.InterfaceAudience;
36 import org.apache.hadoop.classification.InterfaceStability;
37 import org.apache.hadoop.hbase.DaemonThreadFactory;
38 import org.apache.hadoop.hbase.errorhandling.ForeignException;
39 import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
40
41 import com.google.common.collect.MapMaker;
42
43
44
45
46
47
48
49 @InterfaceAudience.Public
50 @InterfaceStability.Evolving
51 public class ProcedureCoordinator {
52 private static final Log LOG = LogFactory.getLog(ProcedureCoordinator.class);
53
54 final static long KEEP_ALIVE_MILLIS_DEFAULT = 5000;
55 final static long TIMEOUT_MILLIS_DEFAULT = 60000;
56 final static long WAKE_MILLIS_DEFAULT = 500;
57
58 private final ProcedureCoordinatorRpcs rpcs;
59 private final ExecutorService pool;
60 private final long wakeTimeMillis;
61 private final long timeoutMillis;
62
63
64 private final ConcurrentMap<String, Procedure> procedures =
65 new MapMaker().concurrencyLevel(4).weakValues().makeMap();
66
67
68
69
70
71
72
73
74
75
76 public ProcedureCoordinator(ProcedureCoordinatorRpcs rpcs, ThreadPoolExecutor pool) {
77 this(rpcs, pool, TIMEOUT_MILLIS_DEFAULT, WAKE_MILLIS_DEFAULT);
78 }
79
80
81
82
83
84
85
86
87
88
89
90 public ProcedureCoordinator(ProcedureCoordinatorRpcs rpcs, ThreadPoolExecutor pool,
91 long timeoutMillis, long wakeTimeMillis) {
92 this.timeoutMillis = timeoutMillis;
93 this.wakeTimeMillis = wakeTimeMillis;
94 this.rpcs = rpcs;
95 this.pool = pool;
96 this.rpcs.start(this);
97 }
98
99
100
101
102
103
104
105 public static ThreadPoolExecutor defaultPool(String coordName, int opThreads) {
106 return defaultPool(coordName, opThreads, KEEP_ALIVE_MILLIS_DEFAULT);
107 }
108
109
110
111
112
113
114
115
116 public static ThreadPoolExecutor defaultPool(String coordName, int opThreads,
117 long keepAliveMillis) {
118 return new ThreadPoolExecutor(1, opThreads, keepAliveMillis, TimeUnit.MILLISECONDS,
119 new SynchronousQueue<Runnable>(),
120 new DaemonThreadFactory("(" + coordName + ")-proc-coordinator-pool"));
121 }
122
123
124
125
126
127 public void close() throws IOException {
128
129 pool.shutdownNow();
130 rpcs.close();
131 }
132
133
134
135
136
137
138
139
140
141
142 boolean submitProcedure(Procedure proc) {
143
144 if (proc == null) {
145 return false;
146 }
147 String procName = proc.getName();
148
149
150 synchronized (procedures) {
151 Procedure oldProc = procedures.get(procName);
152 if (oldProc != null) {
153
154 if (oldProc.completedLatch.getCount() != 0) {
155 LOG.warn("Procedure " + procName + " currently running. Rejecting new request");
156 return false;
157 }
158 LOG.debug("Procedure " + procName + " was in running list but was completed. Accepting new attempt.");
159 procedures.remove(procName);
160 }
161 }
162
163
164 Future<Void> f = null;
165 try {
166 synchronized (procedures) {
167 f = this.pool.submit(proc);
168
169 this.procedures.put(procName, proc);
170 }
171 return true;
172 } catch (RejectedExecutionException e) {
173 LOG.warn("Procedure " + procName + " rejected by execution pool. Propagating error and " +
174 "cancelling operation.", e);
175
176 proc.receive(new ForeignException(procName, e));
177
178
179 if (f != null) {
180 f.cancel(true);
181 }
182 }
183 return false;
184 }
185
186
187
188
189
190
191
192
193 void rpcConnectionFailure(final String message, final IOException cause) {
194 Collection<Procedure> toNotify = procedures.values();
195
196 for (Procedure proc : toNotify) {
197 if (proc == null) {
198 continue;
199 }
200
201 proc.receive(new ForeignException(proc.getName(), cause));
202 }
203 }
204
205
206
207
208
209
210 public void abortProcedure(String procName, ForeignException reason) {
211
212 synchronized(procedures) {
213 Procedure proc = procedures.get(procName);
214 if (proc == null) {
215 return;
216 }
217 proc.receive(reason);
218 }
219 }
220
221
222
223
224
225
226
227
228 Procedure createProcedure(ForeignExceptionDispatcher fed, String procName, byte[] procArgs,
229 List<String> expectedMembers) {
230
231 return new Procedure(this, fed, wakeTimeMillis, timeoutMillis,
232 procName, procArgs, expectedMembers);
233 }
234
235
236
237
238
239
240
241
242
243 public Procedure startProcedure(ForeignExceptionDispatcher fed, String procName, byte[] procArgs,
244 List<String> expectedMembers) throws RejectedExecutionException {
245 Procedure proc = createProcedure(fed, procName, procArgs, expectedMembers);
246 if (!this.submitProcedure(proc)) {
247 LOG.error("Failed to submit procedure '" + procName + "'");
248 return null;
249 }
250 return proc;
251 }
252
253
254
255
256
257
258
259 void memberAcquiredBarrier(String procName, final String member) {
260 Procedure proc = procedures.get(procName);
261 if (proc != null) {
262 proc.barrierAcquiredByMember(member);
263 }
264 }
265
266
267
268
269
270
271
272 void memberFinishedBarrier(String procName, final String member) {
273 Procedure proc = procedures.get(procName);
274 if (proc != null) {
275 proc.barrierReleasedByMember(member);
276 }
277 }
278
279
280
281
282 ProcedureCoordinatorRpcs getRpcs() {
283 return rpcs;
284 }
285
286
287
288
289
290
291
292 public Procedure getProcedure(String name) {
293 return procedures.get(name);
294 }
295
296
297
298
299 public Set<String> getProcedureNames() {
300 return new HashSet<String>(procedures.keySet());
301 }
302 }