Class OrderedExecutor
- java.lang.Object
-
- org.apache.bookkeeper.common.util.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.ExecutorServiceThis 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 theirObject.hashCode()method.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOrderedExecutor.AbstractBuilder<T extends OrderedExecutor>Abstract builder class to buildOrderedScheduler.static classOrderedExecutor.BuilderA builder class for an OrderedExecutor.(package private) static classOrderedExecutor.ContextPreservingCallable<T>Decorator class for a callable that preserves MDC context.(package private) static classOrderedExecutor.ContextPreservingRunnableDecorator class for a runnable that preserves MDC context.protected classOrderedExecutor.TimedCallable<T>Decorator class for a callable that measure the execution time.protected classOrderedExecutor.TimedRunnableDecorator class for a runnable that measure the execution time.
-
Field Summary
Fields Modifier and Type Field Description (package private) booleanenableBusyWait(package private) booleanenableThreadScopedMetrics(package private) intmaxTasksInQueue(package private) java.lang.Stringnamestatic intNO_TASK_LIMIT(package private) booleanpreserveMdcForTaskExecution(package private) java.util.Randomrand(package private) OpStatsLoggertaskExecutionStats(package private) OpStatsLoggertaskPendingStats(package private) long[]threadIds(package private) java.util.concurrent.ExecutorService[]threads(package private) booleantraceTaskExecutionprotected static longWARN_TIME_MICRO_SEC_DEFAULT(package private) longwarnTimeMicroSec
-
Constructor Summary
Constructors Modifier Constructor Description protectedOrderedExecutor(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.ExecutorServiceaddExecutorDecorators(java.util.concurrent.ExecutorService executor)booleanawaitTermination(long timeout, java.util.concurrent.TimeUnit unit)java.util.concurrent.ExecutorServicechooseThread()java.util.concurrent.ExecutorServicechooseThread(long orderingKey)skip hashcode generation in this special case.java.util.concurrent.ExecutorServicechooseThread(java.lang.Object orderingKey)protected static intchooseThreadIdx(long orderingKey, int numThreads)protected java.util.concurrent.ExecutorServicecreateSingleThreadExecutor(java.util.concurrent.ThreadFactory factory)voidexecute(java.lang.Runnable command)voidexecuteOrdered(int orderingKey, java.lang.Runnable r)Schedules a one time action to execute with an ordering guarantee on the key.voidexecuteOrdered(long orderingKey, java.lang.Runnable r)Schedules a one time action to execute with an ordering guarantee on the key.voidexecuteOrdered(java.lang.Object orderingKey, java.lang.Runnable r)Schedules a one time action to execute with an ordering guarantee on the key.voidforceShutdown(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.ExecutorServicegetBoundedExecutor(java.util.concurrent.ExecutorService executor)longgetThreadID(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> TinvokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)<T> TinvokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit)booleanisShutdown()booleanisTerminated()static OrderedExecutor.BuildernewBuilder()booleanpreserveMdc()Flag describing executor's expectation in regards of MDC.voidshutdown()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.RunnabletimedRunnable(java.lang.Runnable r)
-
-
-
Field Detail
-
NO_TASK_LIMIT
public static final int NO_TASK_LIMIT
- See Also:
- Constant Field Values
-
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
-
taskExecutionStats
final OpStatsLogger taskExecutionStats
-
taskPendingStats
final OpStatsLogger taskPendingStats
-
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 threadsbaseName- - base name of executor threadsthreadFactory- - for constructing threadsstatsLogger- - for reporting executor statstraceTaskExecution- - should we stat task executionpreserveMdcForTaskExecution- - should we preserve MDC for task executionwarnTimeMicroSec- - log long task exec warning after this intervalmaxTasksInQueue- - maximum items allowed in a thread queue. -1 for no limit
-
-
Method Detail
-
newBuilder
public static OrderedExecutor.Builder newBuilder()
-
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:
submitin interfacejava.util.concurrent.ExecutorService
-
submit
public <T> java.util.concurrent.Future<T> submit(java.lang.Runnable task, T result)- Specified by:
submitin interfacejava.util.concurrent.ExecutorService
-
submit
public java.util.concurrent.Future<?> submit(java.lang.Runnable task)
- Specified by:
submitin interfacejava.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:
invokeAllin interfacejava.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:
invokeAllin interfacejava.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:
invokeAnyin interfacejava.util.concurrent.ExecutorService- Throws:
java.lang.InterruptedExceptionjava.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:
invokeAnyin interfacejava.util.concurrent.ExecutorService- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException
-
execute
public void execute(java.lang.Runnable command)
- Specified by:
executein interfacejava.util.concurrent.Executor
-
shutdown
public void shutdown()
- Specified by:
shutdownin interfacejava.util.concurrent.ExecutorService
-
shutdownNow
public java.util.List<java.lang.Runnable> shutdownNow()
- Specified by:
shutdownNowin interfacejava.util.concurrent.ExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdownin interfacejava.util.concurrent.ExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException- Specified by:
awaitTerminationin interfacejava.util.concurrent.ExecutorService- Throws:
java.lang.InterruptedException
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminatedin interfacejava.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.
-
-