forked from YahooArchive/oozie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CallableQueueService.java
681 lines (620 loc) · 24.7 KB
/
CallableQueueService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/**
* Copyright (c) 2010 Yahoo! Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. See accompanying LICENSE file.
*/
package org.apache.oozie.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.hadoop.conf.Configuration;
import org.apache.oozie.client.OozieClient.SYSTEM_MODE;
import org.apache.oozie.util.Instrumentable;
import org.apache.oozie.util.Instrumentation;
import org.apache.oozie.util.PollablePriorityDelayQueue;
import org.apache.oozie.util.PriorityDelayQueue;
import org.apache.oozie.util.XCallable;
import org.apache.oozie.util.XLog;
import org.apache.oozie.util.PriorityDelayQueue.QueueElement;
/**
* The callable queue service queues {@link XCallable}s for asynchronous execution.
* <p/>
* Callables can be queued for immediate execution or for delayed execution (some time in the future).
* <p/>
* Callables are consumed from the queue for execution based on their priority.
* <p/>
* When the queues (for immediate execution and for delayed execution) are full, the callable queue service stops
* queuing callables.
* <p/>
* A thread-pool is used to execute the callables asynchronously.
* <p/>
* The following configuration parameters control the callable queue service:
* <p/>
* {@link #CONF_QUEUE_SIZE} size of the immediate execution queue. Defaulf value is 10000.
* <p/>
* {@link #CONF_THREADS} number of threads in the thread-pool used for asynchronous command execution. When this number
* of threads is reached, commands remain the queue until threads become available. Sets up a priority queue for the
* execution of Commands via a ThreadPool. Sets up a Delayed Queue to handle actions which will be ready for execution
* sometime in the future.
*/
public class CallableQueueService implements Service, Instrumentable {
private static final String INSTRUMENTATION_GROUP = "callablequeue";
private static final String INSTR_IN_QUEUE_TIME_TIMER = "time.in.queue";
private static final String INSTR_EXECUTED_COUNTER = "executed";
private static final String INSTR_FAILED_COUNTER = "failed";
private static final String INSTR_QUEUED_COUNTER = "queued";
private static final String INSTR_QUEUE_SIZE_SAMPLER = "queue.size";
private static final String INSTR_THREADS_ACTIVE_SAMPLER = "threads.active";
public static final String CONF_PREFIX = Service.CONF_PREFIX + "CallableQueueService.";
public static final String CONF_QUEUE_SIZE = CONF_PREFIX + "queue.size";
public static final String CONF_THREADS = CONF_PREFIX + "threads";
public static final String CONF_CALLABLE_CONCURRENCY = CONF_PREFIX + "callable.concurrency";
public static final String CONF_CALLABLE_NEXT_ELIGIBLE = CONF_PREFIX + "callable.next.eligible";
public static final int CONCURRENCY_DELAY = 500;
public static final int SAFE_MODE_DELAY = 60000;
private final Map<String, AtomicInteger> activeCallables = new HashMap<String, AtomicInteger>();
private final Map<String, Date> uniqueCallables = new ConcurrentHashMap<String, Date>();
private int maxCallableConcurrency;
private boolean callableBegin(XCallable<?> callable) {
synchronized (activeCallables) {
AtomicInteger counter = activeCallables.get(callable.getType());
if (counter == null) {
counter = new AtomicInteger(1);
activeCallables.put(callable.getType(), counter);
return true;
}
else {
int i = counter.incrementAndGet();
return i <= maxCallableConcurrency;
}
}
}
private void callableEnd(XCallable<?> callable) {
synchronized (activeCallables) {
AtomicInteger counter = activeCallables.get(callable.getType());
if (counter == null) {
throw new IllegalStateException("It should not happen");
}
else {
counter.decrementAndGet();
}
}
}
private boolean callableReachMaxConcurrency(XCallable<?> callable) {
synchronized (activeCallables) {
AtomicInteger counter = activeCallables.get(callable.getType());
if (counter == null) {
return true;
}
else {
int i = counter.get();
return i < maxCallableConcurrency;
}
}
}
// Callables are wrapped with the this wrapper for execution, for logging
// and instrumentation.
// The wrapper implements Runnable and Comparable to be able to work with an
// executor and a priority queue.
class CallableWrapper extends PriorityDelayQueue.QueueElement<XCallable<?>> implements Runnable {
private Instrumentation.Cron cron;
public CallableWrapper(XCallable<?> callable, long delay) {
super(callable, callable.getPriority(), delay, TimeUnit.MILLISECONDS);
cron = new Instrumentation.Cron();
cron.start();
}
public void run() {
if (Services.get().getSystemMode() == SYSTEM_MODE.SAFEMODE) {
log.info("Oozie is in SAFEMODE, requeuing callable [{0}] with [{1}]ms delay", getElement().getType(),
SAFE_MODE_DELAY);
setDelay(SAFE_MODE_DELAY, TimeUnit.MILLISECONDS);
removeFromUniqueCallables();
queue(this, true);
return;
}
XCallable<?> callable = getElement();
try {
if (callableBegin(callable)) {
cron.stop();
addInQueueCron(cron);
XLog.Info.get().clear();
XLog log = XLog.getLog(getClass());
log.trace("executing callable [{0}]", callable.getName());
removeFromUniqueCallables();
try {
callable.call();
incrCounter(INSTR_EXECUTED_COUNTER, 1);
log.trace("executed callable [{0}]", callable.getName());
}
catch (Exception ex) {
incrCounter(INSTR_FAILED_COUNTER, 1);
log.warn("exception callable [{0}], {1}", callable.getName(), ex.getMessage(), ex);
}
finally {
XLog.Info.get().clear();
}
}
else {
log.warn("max concurrency for callable [{0}] exceeded, requeueing with [{1}]ms delay", callable
.getType(), CONCURRENCY_DELAY);
setDelay(CONCURRENCY_DELAY, TimeUnit.MILLISECONDS);
removeFromUniqueCallables();
queue(this, true);
incrCounter(callable.getType() + "#exceeded.concurrency", 1);
}
}
finally {
callableEnd(callable);
}
}
/**
* @return String the queue dump
*/
@Override
public String toString() {
return "delay=" + getDelay(TimeUnit.MILLISECONDS) + ", elements=" + getElement().toString();
}
/**
* Filter the duplicate callables from the list before queue this.
* <p/>
* If it is single callable, checking if key is in unique map or not.
* <p/>
* If it is composite callable, remove duplicates callables from the composite.
*
* @return true if this callable should be queued
*/
public boolean filterDuplicates() {
XCallable<?> callable = getElement();
if (callable instanceof CompositeCallable) {
return ((CompositeCallable) callable).removeDuplicates();
}
else {
return uniqueCallables.containsKey(callable.getKey()) == false;
}
}
/**
* Add the keys to the set
*/
public void addToUniqueCallables() {
XCallable<?> callable = getElement();
if (callable instanceof CompositeCallable) {
((CompositeCallable) callable).addToUniqueCallables();
}
else {
((ConcurrentHashMap<String, Date>) uniqueCallables).putIfAbsent(callable.getKey(), new Date());
}
}
/**
* Remove the keys from the set
*/
public void removeFromUniqueCallables() {
XCallable<?> callable = getElement();
if (callable instanceof CompositeCallable) {
((CompositeCallable) callable).removeFromUniqueCallables();
}
else {
uniqueCallables.remove(callable.getKey());
}
}
}
class CompositeCallable implements XCallable<Void> {
private List<XCallable<?>> callables;
private String name;
private int priority;
private long createdTime;
public CompositeCallable(List<? extends XCallable<?>> callables) {
this.callables = new ArrayList<XCallable<?>>(callables);
priority = 0;
createdTime = Long.MAX_VALUE;
StringBuilder sb = new StringBuilder();
String separator = "[";
for (XCallable<?> callable : callables) {
priority = Math.max(priority, callable.getPriority());
createdTime = Math.min(createdTime, callable.getCreatedTime());
sb.append(separator).append(callable.getName());
separator = ",";
}
sb.append("]");
name = sb.toString();
}
@Override
public String getName() {
return name;
}
@Override
public String getType() {
return "#composite#" + callables.get(0).getType();
}
@Override
public String getKey() {
return "#composite#" + callables.get(0).getKey();
}
@Override
public int getPriority() {
return priority;
}
@Override
public long getCreatedTime() {
return createdTime;
}
public Void call() throws Exception {
XLog log = XLog.getLog(getClass());
for (XCallable<?> callable : callables) {
log.trace("executing callable [{0}]", callable.getName());
try {
callable.call();
incrCounter(INSTR_EXECUTED_COUNTER, 1);
log.trace("executed callable [{0}]", callable.getName());
}
catch (Exception ex) {
incrCounter(INSTR_FAILED_COUNTER, 1);
log.warn("exception callable [{0}], {1}", callable.getName(), ex.getMessage(), ex);
}
}
// ticking -1 not to count the call to the composite callable
incrCounter(INSTR_EXECUTED_COUNTER, -1);
return null;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
if (callables.size() == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
int size = callables.size();
for (int i = 0; i < size; i++) {
XCallable<?> callable = callables.get(i);
sb.append("(");
sb.append(callable.toString());
if (i + 1 == size) {
sb.append(")");
}
else {
sb.append("),");
}
}
return sb.toString();
}
/**
* Remove the duplicate callables from the list before queue them
*
* @return true if callables should be queued
*/
public boolean removeDuplicates() {
Set<String> set = new HashSet<String>();
List<XCallable<?>> filteredCallables = new ArrayList<XCallable<?>>();
if (callables.size() == 0) {
return false;
}
for (XCallable<?> callable : callables) {
if (!uniqueCallables.containsKey(callable.getKey()) && !set.contains(callable.getKey())) {
filteredCallables.add(callable);
set.add(callable.getKey());
}
}
callables = filteredCallables;
if (callables.size() == 0) {
return false;
}
return true;
}
/**
* Add the keys to the set
*/
public void addToUniqueCallables() {
for (XCallable<?> callable : callables) {
((ConcurrentHashMap<String, Date>) uniqueCallables).putIfAbsent(callable.getKey(), new Date());
}
}
/**
* Remove the keys from the set
*/
public void removeFromUniqueCallables() {
for (XCallable<?> callable : callables) {
uniqueCallables.remove(callable.getKey());
}
}
}
private XLog log = XLog.getLog(getClass());
private int queueSize;
private PriorityDelayQueue<CallableWrapper> queue;
private AtomicLong delayQueueExecCounter = new AtomicLong(0);
private ThreadPoolExecutor executor;
private Instrumentation instrumentation;
/**
* Convenience method for instrumentation counters.
*
* @param name counter name.
* @param count count to increment the counter.
*/
private void incrCounter(String name, int count) {
if (instrumentation != null) {
instrumentation.incr(INSTRUMENTATION_GROUP, name, count);
}
}
private void addInQueueCron(Instrumentation.Cron cron) {
if (instrumentation != null) {
instrumentation.addCron(INSTRUMENTATION_GROUP, INSTR_IN_QUEUE_TIME_TIMER, cron);
}
}
/**
* Initialize the command queue service.
*
* @param services services instance.
*/
@Override
@SuppressWarnings("unchecked")
public void init(Services services) {
Configuration conf = services.getConf();
queueSize = conf.getInt(CONF_QUEUE_SIZE, 10000);
int threads = conf.getInt(CONF_THREADS, 10);
boolean callableNextEligible = conf.getBoolean(CONF_CALLABLE_NEXT_ELIGIBLE, true);
if (!callableNextEligible) {
queue = new PriorityDelayQueue<CallableWrapper>(3, 1000 * 30, TimeUnit.MILLISECONDS, queueSize) {
@Override
protected void debug(String msgTemplate, Object... msgArgs) {
log.trace(msgTemplate, msgArgs);
}
};
}
else {
// If the head of this queue has already reached max concurrency, continuously find next one
// which has not yet reach max concurrency.Overrided method 'eligibleToPoll' to check if the
// element of this queue has reached the maximum concurrency.
queue = new PollablePriorityDelayQueue<CallableWrapper>(3, 1000 * 30, TimeUnit.MILLISECONDS,
queueSize) {
@Override
protected void debug(String msgTemplate, Object... msgArgs) {
log.trace(msgTemplate, msgArgs);
}
@Override
protected boolean eligibleToPoll(QueueElement<?> element) {
if (element != null) {
CallableWrapper wrapper = (CallableWrapper) element;
if (element.getElement() != null) {
return callableReachMaxConcurrency(wrapper.getElement());
}
}
return false;
}
};
}
// IMPORTANT: The ThreadPoolExecutor does not always the execute
// commands out of the queue, there are
// certain conditions where commands are pushed directly to a thread.
// As we are using a queue with DELAYED semantics (i.e. execute the
// command in 5 mins) we need to make
// sure that the commands are always pushed to the queue.
// To achieve this (by looking a the ThreadPoolExecutor.execute()
// implementation, we are making the pool
// minimum size equals to the maximum size (thus threads are keep always
// running) and we are warming up
// all those threads (the for loop that runs dummy runnables).
executor = new ThreadPoolExecutor(threads, threads, 10, TimeUnit.SECONDS, (BlockingQueue) queue);
for (int i = 0; i < threads; i++) {
executor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(100);
}
catch (InterruptedException ex) {
log.warn("Could not warm up threadpool {0}", ex.getMessage(), ex);
}
}
});
}
maxCallableConcurrency = conf.getInt(CONF_CALLABLE_CONCURRENCY, 3);
}
/**
* Destroy the command queue service.
*/
@Override
public void destroy() {
try {
long limit = System.currentTimeMillis() + 30 * 1000;// 30 seconds
executor.shutdown();
queue.clear();
while (!executor.awaitTermination(1000, TimeUnit.MILLISECONDS)) {
log.info("Waiting for executor to shutdown");
if (System.currentTimeMillis() > limit) {
log.warn("Gave up, continuing without waiting for executor to shutdown");
break;
}
}
}
catch (InterruptedException ex) {
log.warn(ex);
}
}
/**
* Return the public interface for command queue service.
*
* @return {@link CallableQueueService}.
*/
@Override
public Class<? extends Service> getInterface() {
return CallableQueueService.class;
}
/**
* @return int size of queue
*/
public synchronized int queueSize() {
return queue.size();
}
private synchronized boolean queue(CallableWrapper wrapper, boolean ignoreQueueSize) {
if (!ignoreQueueSize && queue.size() >= queueSize) {
log.warn("queue if full, ignoring queuing for [{0}]", wrapper.getElement());
return false;
}
if (!executor.isShutdown()) {
if (wrapper.filterDuplicates()) {
wrapper.addToUniqueCallables();
try {
executor.execute(wrapper);
}
catch (RejectedExecutionException ree) {
wrapper.removeFromUniqueCallables();
throw ree;
}
}
}
else {
log.warn("Executor shutting down, ignoring queueing of [{0}]", wrapper.getElement());
}
return true;
}
/**
* Queue a callable for asynchronous execution.
*
* @param callable callable to queue.
* @return <code>true</code> if the callable was queued, <code>false</code> if the queue is full and the callable
* was not queued.
*/
public boolean queue(XCallable<?> callable) {
return queue(callable, 0);
}
/**
* Queue a list of callables for serial execution.
* <p/>
* Useful to serialize callables that may compete with each other for resources.
* <p/>
* All callables will be processed with the priority of the highest priority of all callables.
*
* @param callables callables to be executed by the composite callable.
* @return <code>true</code> if the callables were queued, <code>false</code> if the queue is full and the callables
* were not queued.
*/
@SuppressWarnings("unchecked")
public boolean queueSerial(List<? extends XCallable<?>> callables) {
return queueSerial(callables, 0);
}
/**
* Queue a callable for asynchronous execution sometime in the future.
*
* @param callable callable to queue for delayed execution
* @param delay time, in milliseconds, that the callable should be delayed.
* @return <code>true</code> if the callable was queued, <code>false</code> if the queue is full and the callable
* was not queued.
*/
public synchronized boolean queue(XCallable<?> callable, long delay) {
if (callable == null) {
return true;
}
boolean queued = false;
if (Services.get().getSystemMode() == SYSTEM_MODE.SAFEMODE) {
log.warn("[queue] System is in SAFEMODE. Hence no callable is queued. current queue size " + queue.size());
}
else {
queued = queue(new CallableWrapper(callable, delay), false);
if (queued) {
incrCounter(INSTR_QUEUED_COUNTER, 1);
}
else {
log.warn("Could not queue callable");
}
}
return queued;
}
/**
* Queue a list of callables for serial execution sometime in the future.
* <p/>
* Useful to serialize callables that may compete with each other for resources.
* <p/>
* All callables will be processed with the priority of the highest priority of all callables.
*
* @param callables callables to be executed by the composite callable.
* @param delay time, in milliseconds, that the callable should be delayed.
* @return <code>true</code> if the callables were queued, <code>false</code> if the queue is full and the callables
* were not queued.
*/
@SuppressWarnings("unchecked")
public synchronized boolean queueSerial(List<? extends XCallable<?>> callables, long delay) {
boolean queued;
if (callables == null || callables.size() == 0) {
queued = true;
}
else if (callables.size() == 1) {
queued = queue(callables.get(0), delay);
}
else {
XCallable<?> callable = new CompositeCallable(callables);
queued = queue(callable, delay);
if (queued) {
incrCounter(INSTR_QUEUED_COUNTER, callables.size());
}
}
return queued;
}
/**
* Instruments the callable queue service.
*
* @param instr instance to instrument the callable queue service to.
*/
public void instrument(Instrumentation instr) {
instrumentation = instr;
instr.addSampler(INSTRUMENTATION_GROUP, INSTR_QUEUE_SIZE_SAMPLER, 60, 1, new Instrumentation.Variable<Long>() {
public Long getValue() {
return (long) queue.size();
}
});
instr.addSampler(INSTRUMENTATION_GROUP, INSTR_THREADS_ACTIVE_SAMPLER, 60, 1,
new Instrumentation.Variable<Long>() {
public Long getValue() {
return (long) executor.getActiveCount();
}
});
}
/**
* Get the list of strings of queue dump
*
* @return the list of string that representing each CallableWrapper
*/
public List<String> getQueueDump() {
List<String> list = new ArrayList<String>();
for (QueueElement<CallableWrapper> qe : queue) {
if (qe.toString() == null) {
continue;
}
list.add(qe.toString());
}
return list;
}
/**
* Get the list of strings of uniqueness map dump
*
* @return the list of string that representing the key of each command in the queue
*/
public List<String> getUniqueDump() {
List<String> list = new ArrayList<String>();
for (Entry<String, Date> entry : uniqueCallables.entrySet()) {
list.add(entry.toString());
}
return list;
}
}