Class OrderedExecutor

  • All Implemented Interfaces:
    java.util.concurrent.Executor, java.util.concurrent.ExecutorService
    Direct Known Subclasses:
    OrderedScheduler

    public class OrderedExecutor
    extends java.lang.Object
    implements java.util.concurrent.ExecutorService
    This class supports submitting tasks with an ordering key, so that tasks submitted with the same key will always be executed in order, but tasks across different keys can be unordered. This retains parallelism while retaining the basic amount of ordering we want (e.g. , per ledger handle). Ordering is achieved by hashing the key objects to threads by their Object.hashCode() method.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected OrderedExecutor​(java.lang.String baseName, int numThreads, java.util.concurrent.ThreadFactory threadFactory, StatsLogger statsLogger, boolean traceTaskExecution, boolean preserveMdcForTaskExecution, long warnTimeMicroSec, int maxTasksInQueue, boolean enableBusyWait, boolean enableThreadScopedMetrics)
      Constructs Safe executor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.util.concurrent.ExecutorService addExecutorDecorators​(java.util.concurrent.ExecutorService executor)  
      boolean awaitTermination​(long timeout, java.util.concurrent.TimeUnit unit)
      java.util.concurrent.ExecutorService chooseThread()  
      java.util.concurrent.ExecutorService chooseThread​(long orderingKey)
      skip hashcode generation in this special case.
      java.util.concurrent.ExecutorService chooseThread​(java.lang.Object orderingKey)  
      protected static int chooseThreadIdx​(long orderingKey, int numThreads)  
      protected java.util.concurrent.ExecutorService createSingleThreadExecutor​(java.util.concurrent.ThreadFactory factory)  
      void execute​(java.lang.Runnable command)
      void executeOrdered​(int orderingKey, java.lang.Runnable r)
      Schedules a one time action to execute with an ordering guarantee on the key.
      void executeOrdered​(long orderingKey, java.lang.Runnable r)
      Schedules a one time action to execute with an ordering guarantee on the key.
      void executeOrdered​(java.lang.Object orderingKey, java.lang.Runnable r)
      Schedules a one time action to execute with an ordering guarantee on the key.
      void forceShutdown​(long timeout, java.util.concurrent.TimeUnit unit)
      Force threads shutdown (cancel active requests) after specified delay, to be used after shutdown() rejects new requests.
      protected java.util.concurrent.ExecutorService getBoundedExecutor​(java.util.concurrent.ExecutorService executor)  
      long getThreadID​(long orderingKey)  
      <T> java.util.List<java.util.concurrent.Future<T>> invokeAll​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
      <T> java.util.List<java.util.concurrent.Future<T>> invokeAll​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit)
      <T> T invokeAny​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
      <T> T invokeAny​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit)
      boolean isShutdown()
      boolean isTerminated()
      static OrderedExecutor.Builder newBuilder()  
      boolean preserveMdc()
      Flag describing executor's expectation in regards of MDC.
      void shutdown()
      java.util.List<java.lang.Runnable> shutdownNow()
      java.util.concurrent.Future<?> submit​(java.lang.Runnable task)
      <T> java.util.concurrent.Future<T> submit​(java.lang.Runnable task, T result)
      <T> java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)
      <T> com.google.common.util.concurrent.ListenableFuture<T> submitOrdered​(long orderingKey, java.util.concurrent.Callable<T> task)  
      protected <T> java.util.concurrent.Callable<T> timedCallable​(java.util.concurrent.Callable<T> c)  
      protected <T> java.util.Collection<? extends java.util.concurrent.Callable<T>> timedCallables​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)  
      protected java.lang.Runnable timedRunnable​(java.lang.Runnable r)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • WARN_TIME_MICRO_SEC_DEFAULT

        protected static final long WARN_TIME_MICRO_SEC_DEFAULT
      • name

        final java.lang.String name
      • threads

        final java.util.concurrent.ExecutorService[] threads
      • threadIds

        final long[] threadIds
      • rand

        final java.util.Random rand
      • traceTaskExecution

        final boolean traceTaskExecution
      • preserveMdcForTaskExecution

        final boolean preserveMdcForTaskExecution
      • warnTimeMicroSec

        final long warnTimeMicroSec
      • maxTasksInQueue

        final int maxTasksInQueue
      • enableBusyWait

        final boolean enableBusyWait
      • enableThreadScopedMetrics

        final boolean enableThreadScopedMetrics
    • Constructor Detail

      • OrderedExecutor

        protected OrderedExecutor​(java.lang.String baseName,
                                  int numThreads,
                                  java.util.concurrent.ThreadFactory threadFactory,
                                  StatsLogger statsLogger,
                                  boolean traceTaskExecution,
                                  boolean preserveMdcForTaskExecution,
                                  long warnTimeMicroSec,
                                  int maxTasksInQueue,
                                  boolean enableBusyWait,
                                  boolean enableThreadScopedMetrics)
        Constructs Safe executor.
        Parameters:
        numThreads - - number of threads
        baseName - - base name of executor threads
        threadFactory - - for constructing threads
        statsLogger - - for reporting executor stats
        traceTaskExecution - - should we stat task execution
        preserveMdcForTaskExecution - - should we preserve MDC for task execution
        warnTimeMicroSec - - log long task exec warning after this interval
        maxTasksInQueue - - maximum items allowed in a thread queue. -1 for no limit
    • Method Detail

      • createSingleThreadExecutor

        protected java.util.concurrent.ExecutorService createSingleThreadExecutor​(java.util.concurrent.ThreadFactory factory)
      • getBoundedExecutor

        protected java.util.concurrent.ExecutorService getBoundedExecutor​(java.util.concurrent.ExecutorService executor)
      • addExecutorDecorators

        protected java.util.concurrent.ExecutorService addExecutorDecorators​(java.util.concurrent.ExecutorService executor)
      • preserveMdc

        public boolean preserveMdc()
        Flag describing executor's expectation in regards of MDC. All tasks submitted through executor's submit/execute methods will automatically respect this.
        Returns:
        true if runnable/callable is expected to preserve MDC, false otherwise.
      • executeOrdered

        public void executeOrdered​(java.lang.Object orderingKey,
                                   java.lang.Runnable r)
        Schedules a one time action to execute with an ordering guarantee on the key.
        Parameters:
        orderingKey -
        r -
      • executeOrdered

        public void executeOrdered​(long orderingKey,
                                   java.lang.Runnable r)
        Schedules a one time action to execute with an ordering guarantee on the key.
        Parameters:
        orderingKey -
        r -
      • executeOrdered

        public void executeOrdered​(int orderingKey,
                                   java.lang.Runnable r)
        Schedules a one time action to execute with an ordering guarantee on the key.
        Parameters:
        orderingKey -
        r -
      • submitOrdered

        public <T> com.google.common.util.concurrent.ListenableFuture<T> submitOrdered​(long orderingKey,
                                                                                       java.util.concurrent.Callable<T> task)
      • getThreadID

        public long getThreadID​(long orderingKey)
      • chooseThread

        public java.util.concurrent.ExecutorService chooseThread()
      • chooseThread

        public java.util.concurrent.ExecutorService chooseThread​(java.lang.Object orderingKey)
      • chooseThread

        public java.util.concurrent.ExecutorService chooseThread​(long orderingKey)
        skip hashcode generation in this special case.
        Parameters:
        orderingKey - long ordering key
        Returns:
        the thread for executing this order key
      • chooseThreadIdx

        protected static int chooseThreadIdx​(long orderingKey,
                                             int numThreads)
      • timedRunnable

        protected java.lang.Runnable timedRunnable​(java.lang.Runnable r)
      • timedCallable

        protected <T> java.util.concurrent.Callable<T> timedCallable​(java.util.concurrent.Callable<T> c)
      • timedCallables

        protected <T> java.util.Collection<? extends java.util.concurrent.Callable<T>> timedCallables​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
      • submit

        public <T> java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
      • submit

        public <T> java.util.concurrent.Future<T> submit​(java.lang.Runnable task,
                                                         T result)
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
      • submit

        public java.util.concurrent.Future<?> submit​(java.lang.Runnable task)
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
      • invokeAll

        public <T> java.util.List<java.util.concurrent.Future<T>> invokeAll​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
                                                                     throws java.lang.InterruptedException
        Specified by:
        invokeAll in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
      • invokeAll

        public <T> java.util.List<java.util.concurrent.Future<T>> invokeAll​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks,
                                                                            long timeout,
                                                                            java.util.concurrent.TimeUnit unit)
                                                                     throws java.lang.InterruptedException
        Specified by:
        invokeAll in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
      • invokeAny

        public <T> T invokeAny​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
                        throws java.lang.InterruptedException,
                               java.util.concurrent.ExecutionException
        Specified by:
        invokeAny in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
      • invokeAny

        public <T> T invokeAny​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks,
                               long timeout,
                               java.util.concurrent.TimeUnit unit)
                        throws java.lang.InterruptedException,
                               java.util.concurrent.ExecutionException,
                               java.util.concurrent.TimeoutException
        Specified by:
        invokeAny in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException
      • execute

        public void execute​(java.lang.Runnable command)
        Specified by:
        execute in interface java.util.concurrent.Executor
      • shutdown

        public void shutdown()
        Specified by:
        shutdown in interface java.util.concurrent.ExecutorService
      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
        Specified by:
        shutdownNow in interface java.util.concurrent.ExecutorService
      • isShutdown

        public boolean isShutdown()
        Specified by:
        isShutdown in interface java.util.concurrent.ExecutorService
      • awaitTermination

        public boolean awaitTermination​(long timeout,
                                        java.util.concurrent.TimeUnit unit)
                                 throws java.lang.InterruptedException
        Specified by:
        awaitTermination in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
      • isTerminated

        public boolean isTerminated()
        Specified by:
        isTerminated in interface java.util.concurrent.ExecutorService
      • forceShutdown

        public void forceShutdown​(long timeout,
                                  java.util.concurrent.TimeUnit unit)
        Force threads shutdown (cancel active requests) after specified delay, to be used after shutdown() rejects new requests.