001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.oozie.service; 020 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.Date; 024import java.util.HashMap; 025import java.util.HashSet; 026import java.util.LinkedHashSet; 027import java.util.List; 028import java.util.Map; 029import java.util.Map.Entry; 030import java.util.Set; 031import java.util.concurrent.BlockingQueue; 032import java.util.concurrent.Callable; 033import java.util.concurrent.ConcurrentHashMap; 034import java.util.concurrent.Future; 035import java.util.concurrent.RunnableFuture; 036import java.util.concurrent.ThreadPoolExecutor; 037import java.util.concurrent.TimeUnit; 038import java.util.concurrent.atomic.AtomicInteger; 039 040import org.apache.hadoop.conf.Configuration; 041import org.apache.oozie.client.OozieClient.SYSTEM_MODE; 042import org.apache.oozie.util.Instrumentable; 043import org.apache.oozie.util.Instrumentation; 044import org.apache.oozie.util.NamedThreadFactory; 045import org.apache.oozie.util.PollablePriorityDelayQueue; 046import org.apache.oozie.util.PriorityDelayQueue; 047import org.apache.oozie.util.PriorityDelayQueue.QueueElement; 048import org.apache.oozie.util.StringUtils; 049import org.apache.oozie.util.XCallable; 050import org.apache.oozie.util.XLog; 051 052import com.google.common.collect.ImmutableSet; 053 054/** 055 * The callable queue service queues {@link XCallable}s for asynchronous execution. 056 * <p> 057 * Callables can be queued for immediate execution or for delayed execution (some time in the future). 058 * <p> 059 * Callables are consumed from the queue for execution based on their priority. 060 * <p> 061 * When the queues (for immediate execution and for delayed execution) are full, the callable queue service stops 062 * queuing callables. 063 * <p> 064 * A thread-pool is used to execute the callables asynchronously. 065 * <p> 066 * The following configuration parameters control the callable queue service: 067 * <p> 068 * {@link #CONF_QUEUE_SIZE} size of the immediate execution queue. Defaulf value is 10000. 069 * <p> 070 * {@link #CONF_THREADS} number of threads in the thread-pool used for asynchronous command execution. When this number 071 * of threads is reached, commands remain the queue until threads become available. Sets up a priority queue for the 072 * execution of Commands via a ThreadPool. Sets up a Delayed Queue to handle actions which will be ready for execution 073 * sometime in the future. 074 */ 075public class CallableQueueService implements Service, Instrumentable { 076 private static final String INSTRUMENTATION_GROUP = "callablequeue"; 077 private static final String INSTR_IN_QUEUE_TIME_TIMER = "time.in.queue"; 078 private static final String INSTR_EXECUTED_COUNTER = "executed"; 079 private static final String INSTR_FAILED_COUNTER = "failed"; 080 private static final String INSTR_QUEUED_COUNTER = "queued"; 081 private static final String INSTR_QUEUE_SIZE_SAMPLER = "queue.size"; 082 private static final String INSTR_THREADS_ACTIVE_SAMPLER = "threads.active"; 083 084 public static final String CONF_PREFIX = Service.CONF_PREFIX + "CallableQueueService."; 085 086 public static final String CONF_QUEUE_SIZE = CONF_PREFIX + "queue.size"; 087 public static final String CONF_THREADS = CONF_PREFIX + "threads"; 088 public static final String CONF_CALLABLE_CONCURRENCY = CONF_PREFIX + "callable.concurrency"; 089 public static final String CONF_CALLABLE_NEXT_ELIGIBLE = CONF_PREFIX + "callable.next.eligible"; 090 public static final String CONF_CALLABLE_INTERRUPT_TYPES = CONF_PREFIX + "InterruptTypes"; 091 public static final String CONF_CALLABLE_INTERRUPT_MAP_MAX_SIZE = CONF_PREFIX + "InterruptMapMaxSize"; 092 093 public static final int CONCURRENCY_DELAY = 500; 094 095 public static final int SAFE_MODE_DELAY = 60000; 096 097 private final Map<String, AtomicInteger> activeCallables = new HashMap<String, AtomicInteger>(); 098 099 private final Map<String, Date> uniqueCallables = new ConcurrentHashMap<String, Date>(); 100 101 private final ConcurrentHashMap<String, Set<XCallable<?>>> interruptCommandsMap = new ConcurrentHashMap<>(); 102 103 private Set<String> interruptTypes; 104 105 private int interruptMapMaxSize; 106 107 private int maxCallableConcurrency; 108 109 private boolean callableBegin(XCallable<?> callable) { 110 synchronized (activeCallables) { 111 AtomicInteger counter = activeCallables.get(callable.getType()); 112 if (counter == null) { 113 counter = new AtomicInteger(1); 114 activeCallables.put(callable.getType(), counter); 115 return true; 116 } 117 else { 118 int i = counter.incrementAndGet(); 119 return i <= maxCallableConcurrency; 120 } 121 } 122 } 123 124 private void callableEnd(XCallable<?> callable) { 125 synchronized (activeCallables) { 126 AtomicInteger counter = activeCallables.get(callable.getType()); 127 if (counter == null) { 128 throw new IllegalStateException("It should not happen"); 129 } 130 else { 131 counter.decrementAndGet(); 132 } 133 } 134 } 135 136 private boolean callableReachMaxConcurrency(XCallable<?> callable) { 137 synchronized (activeCallables) { 138 AtomicInteger counter = activeCallables.get(callable.getType()); 139 if (counter == null) { 140 return true; 141 } 142 else { 143 int i = counter.get(); 144 return i < maxCallableConcurrency; 145 } 146 } 147 } 148 149 // Callables are wrapped with the this wrapper for execution, for logging 150 // and instrumentation. 151 // The wrapper implements Runnable and Comparable to be able to work with an 152 // executor and a priority queue. 153 public class CallableWrapper<E> extends PriorityDelayQueue.QueueElement<E> implements Runnable, Callable<E> { 154 private Instrumentation.Cron cron; 155 156 public CallableWrapper(XCallable<E> callable, long delay) { 157 super(callable, callable.getPriority(), delay, TimeUnit.MILLISECONDS); 158 cron = new Instrumentation.Cron(); 159 cron.start(); 160 } 161 162 public void run() { 163 XCallable<?> callable = null; 164 try { 165 removeFromUniqueCallables(); 166 if (Services.get().getSystemMode() == SYSTEM_MODE.SAFEMODE) { 167 log.info("Oozie is in SAFEMODE, requeuing callable [{0}] with [{1}]ms delay", getElement().getType(), 168 SAFE_MODE_DELAY); 169 setDelay(SAFE_MODE_DELAY, TimeUnit.MILLISECONDS); 170 queue(this, true); 171 return; 172 } 173 callable = getElement(); 174 if (callableBegin(callable)) { 175 cron.stop(); 176 addInQueueCron(cron); 177 XLog log = XLog.getLog(getClass()); 178 log.trace("executing callable [{0}]", callable.getName()); 179 180 try { 181 //FutureTask.run() will invoke cllable.call() 182 super.run(); 183 incrCounter(INSTR_EXECUTED_COUNTER, 1); 184 log.trace("executed callable [{0}]", callable.getName()); 185 } 186 catch (Exception ex) { 187 incrCounter(INSTR_FAILED_COUNTER, 1); 188 log.warn("exception callable [{0}], {1}", callable.getName(), ex.getMessage(), ex); 189 } 190 } 191 else { 192 log.warn("max concurrency for callable [{0}] exceeded, requeueing with [{1}]ms delay", callable 193 .getType(), CONCURRENCY_DELAY); 194 setDelay(CONCURRENCY_DELAY, TimeUnit.MILLISECONDS); 195 queue(this, true); 196 incrCounter(callable.getType() + "#exceeded.concurrency", 1); 197 } 198 } 199 catch (Throwable t) { 200 incrCounter(INSTR_FAILED_COUNTER, 1); 201 log.warn("exception callable [{0}], {1}", callable == null ? "N/A" : callable.getName(), 202 t.getMessage(), t); 203 } 204 finally { 205 if (callable != null) { 206 callableEnd(callable); 207 } 208 } 209 } 210 211 /** 212 * Filter the duplicate callables from the list before queue this. 213 * <p> 214 * If it is single callable, checking if key is in unique map or not. 215 * <p> 216 * If it is composite callable, remove duplicates callables from the composite. 217 * 218 * @return true if this callable should be queued 219 */ 220 public boolean filterDuplicates() { 221 XCallable<?> callable = getElement(); 222 if (callable instanceof CompositeCallable) { 223 return ((CompositeCallable) callable).removeDuplicates(); 224 } 225 else { 226 return uniqueCallables.containsKey(callable.getKey()) == false; 227 } 228 } 229 230 /** 231 * Add the keys to the set 232 */ 233 public void addToUniqueCallables() { 234 XCallable<?> callable = getElement(); 235 if (callable instanceof CompositeCallable) { 236 ((CompositeCallable) callable).addToUniqueCallables(); 237 } 238 else { 239 ((ConcurrentHashMap<String, Date>) uniqueCallables).putIfAbsent(callable.getKey(), new Date()); 240 } 241 } 242 243 /** 244 * Remove the keys from the set 245 */ 246 public void removeFromUniqueCallables() { 247 XCallable<?> callable = getElement(); 248 if (callable instanceof CompositeCallable) { 249 ((CompositeCallable) callable).removeFromUniqueCallables(); 250 } 251 else { 252 uniqueCallables.remove(callable.getKey()); 253 } 254 } 255 256 //this will not get called, bcz newTaskFor of threadpool will convert it in futureTask which is a runnable. 257 // futureTask will call the cllable.call from run method. so we override run to call super.run method. 258 @Override 259 public E call() throws Exception { 260 return null; 261 } 262 } 263 264 class CompositeCallable implements XCallable<Void> { 265 private List<XCallable<?>> callables; 266 private String name; 267 private int priority; 268 private long createdTime; 269 270 public CompositeCallable(List<? extends XCallable<?>> callables) { 271 this.callables = new ArrayList<XCallable<?>>(callables); 272 priority = 0; 273 createdTime = Long.MAX_VALUE; 274 StringBuilder sb = new StringBuilder(); 275 String separator = "["; 276 for (XCallable<?> callable : callables) { 277 priority = Math.max(priority, callable.getPriority()); 278 createdTime = Math.min(createdTime, callable.getCreatedTime()); 279 sb.append(separator).append(callable.getName()); 280 separator = ","; 281 } 282 sb.append("]"); 283 name = StringUtils.intern(sb.toString()); 284 } 285 286 @Override 287 public String getName() { 288 return name; 289 } 290 291 @Override 292 public String getType() { 293 return "#composite#" + callables.get(0).getType(); 294 } 295 296 @Override 297 public String getKey() { 298 return "#composite#" + callables.get(0).getKey(); 299 } 300 301 @Override 302 public String getEntityKey() { 303 return "#composite#" + callables.get(0).getEntityKey(); 304 } 305 306 @Override 307 public int getPriority() { 308 return priority; 309 } 310 311 @Override 312 public long getCreatedTime() { 313 return createdTime; 314 } 315 316 @Override 317 public void setInterruptMode(boolean mode) { 318 } 319 320 @Override 321 public boolean inInterruptMode() { 322 return false; 323 } 324 325 public List<XCallable<?>> getCallables() { 326 return this.callables; 327 } 328 329 public Void call() throws Exception { 330 XLog log = XLog.getLog(getClass()); 331 332 for (XCallable<?> callable : callables) { 333 log.trace("executing callable [{0}]", callable.getName()); 334 try { 335 callable.call(); 336 incrCounter(INSTR_EXECUTED_COUNTER, 1); 337 log.trace("executed callable [{0}]", callable.getName()); 338 } 339 catch (Exception ex) { 340 incrCounter(INSTR_FAILED_COUNTER, 1); 341 log.warn("exception callable [{0}], {1}", callable.getName(), ex.getMessage(), ex); 342 } 343 } 344 345 // ticking -1 not to count the call to the composite callable 346 incrCounter(INSTR_EXECUTED_COUNTER, -1); 347 return null; 348 } 349 350 /* 351 * (non-Javadoc) 352 * 353 * @see java.lang.Object#toString() 354 */ 355 @Override 356 public String toString() { 357 if (callables.size() == 0) { 358 return null; 359 } 360 StringBuilder sb = new StringBuilder(); 361 int size = callables.size(); 362 for (int i = 0; i < size; i++) { 363 XCallable<?> callable = callables.get(i); 364 sb.append("("); 365 sb.append(callable.toString()); 366 if (i + 1 == size) { 367 sb.append(")"); 368 } 369 else { 370 sb.append("),"); 371 } 372 } 373 return sb.toString(); 374 } 375 376 /** 377 * Remove the duplicate callables from the list before queue them 378 * 379 * @return true if callables should be queued 380 */ 381 public boolean removeDuplicates() { 382 Set<String> set = new HashSet<String>(); 383 List<XCallable<?>> filteredCallables = new ArrayList<XCallable<?>>(); 384 if (callables.size() == 0) { 385 return false; 386 } 387 for (XCallable<?> callable : callables) { 388 if (!uniqueCallables.containsKey(callable.getKey()) && !set.contains(callable.getKey())) { 389 filteredCallables.add(callable); 390 set.add(callable.getKey()); 391 } 392 } 393 callables = filteredCallables; 394 if (callables.size() == 0) { 395 return false; 396 } 397 return true; 398 } 399 400 /** 401 * Add the keys to the set 402 */ 403 public void addToUniqueCallables() { 404 for (XCallable<?> callable : callables) { 405 ((ConcurrentHashMap<String, Date>) uniqueCallables).putIfAbsent(callable.getKey(), new Date()); 406 } 407 } 408 409 /** 410 * Remove the keys from the set 411 */ 412 public void removeFromUniqueCallables() { 413 for (XCallable<?> callable : callables) { 414 uniqueCallables.remove(callable.getKey()); 415 } 416 } 417 } 418 419 private XLog log = XLog.getLog(getClass()); 420 421 private int queueSize; 422 private PriorityDelayQueue<CallableWrapper> queue; 423 private ThreadPoolExecutor executor; 424 private Instrumentation instrumentation; 425 426 /** 427 * Convenience method for instrumentation counters. 428 * 429 * @param name counter name. 430 * @param count count to increment the counter. 431 */ 432 private void incrCounter(String name, int count) { 433 if (instrumentation != null) { 434 instrumentation.incr(INSTRUMENTATION_GROUP, name, count); 435 } 436 } 437 438 private void addInQueueCron(Instrumentation.Cron cron) { 439 if (instrumentation != null) { 440 instrumentation.addCron(INSTRUMENTATION_GROUP, INSTR_IN_QUEUE_TIME_TIMER, cron); 441 } 442 } 443 444 /** 445 * Initialize the command queue service. 446 * 447 * @param services services instance. 448 */ 449 @Override 450 @SuppressWarnings({ "unchecked", "rawtypes" }) 451 public void init(Services services) { 452 Configuration conf = services.getConf(); 453 454 queueSize = ConfigurationService.getInt(conf, CONF_QUEUE_SIZE); 455 int threads = ConfigurationService.getInt(conf, CONF_THREADS); 456 boolean callableNextEligible = ConfigurationService.getBoolean(conf, CONF_CALLABLE_NEXT_ELIGIBLE); 457 458 interruptTypes = new HashSet<>(); 459 for (String type : ConfigurationService.getStrings(conf, CONF_CALLABLE_INTERRUPT_TYPES)) { 460 log.debug("Adding interrupt type [{0}]", type); 461 interruptTypes.add(type); 462 } 463 interruptTypes = ImmutableSet.copyOf(interruptTypes); 464 465 if (!callableNextEligible) { 466 queue = new PriorityDelayQueue<CallableWrapper>(3, 1000 * 30, TimeUnit.MILLISECONDS, queueSize) { 467 @Override 468 protected void debug(String msgTemplate, Object... msgArgs) { 469 log.trace(msgTemplate, msgArgs); 470 } 471 }; 472 } 473 else { 474 // If the head of this queue has already reached max concurrency, 475 // continuously find next one 476 // which has not yet reach max concurrency.Overrided method 477 // 'eligibleToPoll' to check if the 478 // element of this queue has reached the maximum concurrency. 479 queue = new PollablePriorityDelayQueue<CallableWrapper>(3, 1000 * 30, TimeUnit.MILLISECONDS, queueSize) { 480 @Override 481 protected void debug(String msgTemplate, Object... msgArgs) { 482 log.trace(msgTemplate, msgArgs); 483 } 484 485 @Override 486 protected boolean eligibleToPoll(QueueElement<?> element) { 487 if (element != null) { 488 CallableWrapper wrapper = (CallableWrapper) element; 489 if (element.getElement() != null) { 490 return callableReachMaxConcurrency(wrapper.getElement()); 491 } 492 } 493 return false; 494 } 495 496 }; 497 } 498 499 interruptMapMaxSize = ConfigurationService.getInt(conf, CONF_CALLABLE_INTERRUPT_MAP_MAX_SIZE); 500 501 // IMPORTANT: The ThreadPoolExecutor does not always the execute 502 // commands out of the queue, there are 503 // certain conditions where commands are pushed directly to a thread. 504 // As we are using a queue with DELAYED semantics (i.e. execute the 505 // command in 5 mins) we need to make 506 // sure that the commands are always pushed to the queue. 507 // To achieve this (by looking a the ThreadPoolExecutor.execute() 508 // implementation, we are making the pool 509 // minimum size equals to the maximum size (thus threads are keep always 510 // running) and we are warming up 511 // all those threads (the for loop that runs dummy runnables). 512 executor = new ThreadPoolExecutor(threads, threads, 10, TimeUnit.SECONDS, (BlockingQueue) queue, 513 new NamedThreadFactory("CallableQueue")) { 514 protected void beforeExecute(Thread t, Runnable r) { 515 super.beforeExecute(t,r); 516 XLog.Info.get().clear(); 517 } 518 protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { 519 return (RunnableFuture<T>)callable; 520 } 521 }; 522 523 for (int i = 0; i < threads; i++) { 524 executor.execute(new Runnable() { 525 public void run() { 526 try { 527 Thread.sleep(100); 528 } 529 catch (InterruptedException ex) { 530 log.warn("Could not warm up threadpool {0}", ex.getMessage(), ex); 531 } 532 } 533 }); 534 } 535 536 maxCallableConcurrency = ConfigurationService.getInt(conf, CONF_CALLABLE_CONCURRENCY); 537 } 538 539 /** 540 * Destroy the command queue service. 541 */ 542 @Override 543 public void destroy() { 544 try { 545 long limit = System.currentTimeMillis() + 30 * 1000;// 30 seconds 546 executor.shutdown(); 547 queue.clear(); 548 while (!executor.awaitTermination(1000, TimeUnit.MILLISECONDS)) { 549 log.info("Waiting for executor to shutdown"); 550 if (System.currentTimeMillis() > limit) { 551 log.warn("Gave up, continuing without waiting for executor to shutdown"); 552 break; 553 } 554 } 555 } 556 catch (InterruptedException ex) { 557 log.warn(ex); 558 } 559 } 560 561 /** 562 * Return the public interface for command queue service. 563 * 564 * @return {@link CallableQueueService}. 565 */ 566 @Override 567 public Class<? extends Service> getInterface() { 568 return CallableQueueService.class; 569 } 570 571 /** 572 * @return int size of queue 573 */ 574 public synchronized int queueSize() { 575 return queue.size(); 576 } 577 578 private synchronized boolean queue(CallableWrapper wrapper, boolean ignoreQueueSize) { 579 if (!ignoreQueueSize && queue.size() >= queueSize) { 580 log.warn("queue full, ignoring queuing for [{0}]", wrapper.getElement().getKey()); 581 return false; 582 } 583 if (!executor.isShutdown()) { 584 if (wrapper.filterDuplicates()) { 585 wrapper.addToUniqueCallables(); 586 try { 587 executor.execute(wrapper); 588 } 589 catch (Throwable ree) { 590 wrapper.removeFromUniqueCallables(); 591 throw new RuntimeException(ree); 592 } 593 } 594 } 595 else { 596 log.warn("Executor shutting down, ignoring queueing of [{0}]", wrapper.getElement().getKey()); 597 } 598 return true; 599 } 600 601 /** 602 * Queue a callable for asynchronous execution. 603 * 604 * @param callable callable to queue. 605 * @return <code>true</code> if the callable was queued, <code>false</code> if the queue is full and the callable 606 * was not queued. 607 */ 608 public boolean queue(XCallable<?> callable) { 609 return queue(callable, 0); 610 } 611 612 /** 613 * Queue a list of callables for serial execution. 614 * <p> 615 * Useful to serialize callables that may compete with each other for resources. 616 * <p> 617 * All callables will be processed with the priority of the highest priority of all callables. 618 * 619 * @param callables callables to be executed by the composite callable. 620 * @return <code>true</code> if the callables were queued, <code>false</code> if the queue is full and the callables 621 * were not queued. 622 */ 623 public boolean queueSerial(List<? extends XCallable<?>> callables) { 624 return queueSerial(callables, 0); 625 } 626 627 /** 628 * Queue a callable for asynchronous execution sometime in the future. 629 * 630 * @param callable callable to queue for delayed execution 631 * @param delay time, in milliseconds, that the callable should be delayed. 632 * @return <code>true</code> if the callable was queued, <code>false</code> 633 * if the queue is full and the callable was not queued. 634 */ 635 public synchronized boolean queue(XCallable<?> callable, long delay) { 636 if (callable == null) { 637 return true; 638 } 639 boolean queued = false; 640 if (Services.get().getSystemMode() == SYSTEM_MODE.SAFEMODE) { 641 log.warn("[queue] System is in SAFEMODE. Hence no callable is queued. current queue size " + queue.size()); 642 } 643 else { 644 checkInterruptTypes(callable); 645 queued = queue(new CallableWrapper(callable, delay), false); 646 if (queued) { 647 incrCounter(INSTR_QUEUED_COUNTER, 1); 648 } 649 else { 650 log.warn("Could not queue callable"); 651 } 652 } 653 return queued; 654 } 655 656 /** 657 * Queue a list of callables for serial execution sometime in the future. 658 * <p> 659 * Useful to serialize callables that may compete with each other for resources. 660 * <p> 661 * All callables will be processed with the priority of the highest priority of all callables. 662 * 663 * @param callables callables to be executed by the composite callable. 664 * @param delay time, in milliseconds, that the callable should be delayed. 665 * @return <code>true</code> if the callables were queued, <code>false</code> if the queue is full and the callables 666 * were not queued. 667 */ 668 public synchronized boolean queueSerial(List<? extends XCallable<?>> callables, long delay) { 669 boolean queued; 670 if (callables == null || callables.size() == 0) { 671 queued = true; 672 } 673 else if (callables.size() == 1) { 674 queued = queue(callables.get(0), delay); 675 } 676 else { 677 XCallable<?> callable = new CompositeCallable(callables); 678 queued = queue(callable, delay); 679 if (queued) { 680 incrCounter(INSTR_QUEUED_COUNTER, callables.size()); 681 } 682 } 683 return queued; 684 } 685 686 /** 687 * Instruments the callable queue service. 688 * 689 * @param instr instance to instrument the callable queue service to. 690 */ 691 public void instrument(Instrumentation instr) { 692 instrumentation = instr; 693 instr.addSampler(INSTRUMENTATION_GROUP, INSTR_QUEUE_SIZE_SAMPLER, 60, 1, new Instrumentation.Variable<Long>() { 694 public Long getValue() { 695 return (long) queue.size(); 696 } 697 }); 698 instr.addSampler(INSTRUMENTATION_GROUP, INSTR_THREADS_ACTIVE_SAMPLER, 60, 1, 699 new Instrumentation.Variable<Long>() { 700 public Long getValue() { 701 return (long) executor.getActiveCount(); 702 } 703 }); 704 } 705 706 /** 707 * check the interrupt map for the existence of an interrupt commands if 708 * exist a List of Interrupt Callable for the same lock key will bereturned, 709 * otherwise it will return null 710 */ 711 public Set<XCallable<?>> checkInterrupts(String lockKey) { 712 713 if (lockKey != null) { 714 return interruptCommandsMap.remove(lockKey); 715 } 716 return null; 717 } 718 719 /** 720 * check if the callable is of an interrupt type and insert it into the map 721 * accordingly 722 * 723 * @param callable 724 */ 725 public void checkInterruptTypes(XCallable<?> callable) { 726 if ((callable instanceof CompositeCallable) && (((CompositeCallable) callable).getCallables() != null)) { 727 for (XCallable<?> singleCallable : ((CompositeCallable) callable).getCallables()) { 728 if (interruptTypes.contains(singleCallable.getType())) { 729 insertCallableIntoInterruptMap(singleCallable); 730 } 731 } 732 } 733 else if (interruptTypes.contains(callable.getType())) { 734 insertCallableIntoInterruptMap(callable); 735 } 736 } 737 738 /** 739 * insert a new callable in the Interrupt Command Map add a new element to 740 * the list or create a new list accordingly 741 * 742 * @param callable 743 */ 744 public void insertCallableIntoInterruptMap(XCallable<?> callable) { 745 if (interruptCommandsMap.size() < interruptMapMaxSize) { 746 Set<XCallable<?>> newSet = Collections.synchronizedSet(new LinkedHashSet<XCallable<?>>()); 747 Set<XCallable<?>> interruptSet = interruptCommandsMap.putIfAbsent(callable.getEntityKey(), newSet); 748 if (interruptSet == null) { 749 interruptSet = newSet; 750 } 751 if (interruptSet.add(callable)) { 752 log.trace("Inserting an interrupt element [{0}] to the interrupt map", callable.toString()); 753 } else { 754 log.trace("Interrupt element [{0}] already present", callable.toString()); 755 } 756 } 757 else { 758 log.warn( 759 "The interrupt map reached max size of [{0}], an interrupt element [{1}] will not added to the map [{1}]", 760 interruptCommandsMap.size(), callable.toString()); 761 } 762 } 763 764 /** 765 * Get the list of strings of queue dump 766 * 767 * @return the list of string that representing each CallableWrapper 768 */ 769 public List<String> getQueueDump() { 770 List<String> list = new ArrayList<String>(); 771 for (QueueElement<CallableWrapper> qe : queue) { 772 if (qe.toString() == null) { 773 continue; 774 } 775 list.add(qe.toString()); 776 } 777 return list; 778 } 779 780 /** 781 * Get the list of strings of uniqueness map dump 782 * 783 * @return the list of string that representing the key of each command in the queue 784 */ 785 public List<String> getUniqueDump() { 786 List<String> list = new ArrayList<String>(); 787 for (Entry<String, Date> entry : uniqueCallables.entrySet()) { 788 list.add(entry.toString()); 789 } 790 return list; 791 } 792 793 // Refer executor.invokeAll 794 public <T> List<Future<T>> invokeAll(List<CallableWrapper<T>> tasks) 795 throws InterruptedException { 796 return executor.invokeAll(tasks); 797 } 798 799 public Set<String> getInterruptTypes() { 800 return interruptTypes; 801 } 802 803}