Class OrderedExecutor
java.lang.Object
org.apache.bookkeeper.common.util.OrderedExecutor
- All Implemented Interfaces:
Executor,ExecutorService
- Direct Known Subclasses:
OrderedScheduler
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstract builder class to buildOrderedScheduler.static classA builder class for an OrderedExecutor.(package private) static classDecorator class for a callable that preserves MDC context.(package private) static classDecorator class for a runnable that preserves MDC context.protected classDecorator class for a callable that measure the execution time.protected classDecorator class for a runnable that measure the execution time. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final boolean(package private) final boolean(package private) final int(package private) final Stringstatic final int(package private) final boolean(package private) final Random(package private) final OpStatsLogger(package private) final OpStatsLogger(package private) final long[](package private) final ExecutorService[](package private) final booleanprotected static final long(package private) final long -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedOrderedExecutor(String baseName, int numThreads, ThreadFactory threadFactory, StatsLogger statsLogger, boolean traceTaskExecution, boolean preserveMdcForTaskExecution, long warnTimeMicroSec, int maxTasksInQueue, boolean enableBusyWait, boolean enableThreadScopedMetrics) Constructs Safe executor. -
Method Summary
Modifier and TypeMethodDescriptionprotected ExecutorServiceaddExecutorDecorators(ExecutorService executor) booleanawaitTermination(long timeout, TimeUnit unit) chooseThread(long orderingKey) skip hashcode generation in this special case.chooseThread(Object orderingKey) protected static intchooseThreadIdx(long orderingKey, int numThreads) protected ExecutorServicecreateSingleThreadExecutor(ThreadFactory factory) voidvoidexecuteOrdered(int orderingKey, Runnable r) Schedules a one time action to execute with an ordering guarantee on the key.voidexecuteOrdered(long orderingKey, Runnable r) Schedules a one time action to execute with an ordering guarantee on the key.voidexecuteOrdered(Object orderingKey, Runnable r) Schedules a one time action to execute with an ordering guarantee on the key.voidforceShutdown(long timeout, TimeUnit unit) Force threads shutdown (cancel active requests) after specified delay, to be used after shutdown() rejects new requests.protected ExecutorServicegetBoundedExecutor(ExecutorService executor) longgetThreadID(long orderingKey) invokeAll(Collection<? extends Callable<T>> tasks) invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) <T> TinvokeAny(Collection<? extends Callable<T>> tasks) <T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) booleanbooleanstatic OrderedExecutor.BuilderbooleanFlag describing executor's expectation in regards of MDC.voidshutdown()Future<?><T> Future<T><T> Future<T><T> com.google.common.util.concurrent.ListenableFuture<T>submitOrdered(long orderingKey, Callable<T> task) protected <T> Callable<T>timedCallable(Callable<T> c) protected <T> Collection<? extends Callable<T>>timedCallables(Collection<? extends Callable<T>> tasks) protected Runnable
-
Field Details
-
NO_TASK_LIMIT
public static final int NO_TASK_LIMIT- See Also:
-
WARN_TIME_MICRO_SEC_DEFAULT
protected static final long WARN_TIME_MICRO_SEC_DEFAULT -
name
-
threads
-
threadIds
final long[] threadIds -
rand
-
taskExecutionStats
-
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 Details
-
OrderedExecutor
protected OrderedExecutor(String baseName, int numThreads, 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 Details
-
newBuilder
-
createSingleThreadExecutor
-
getBoundedExecutor
-
addExecutorDecorators
-
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
Schedules a one time action to execute with an ordering guarantee on the key.- Parameters:
orderingKey-r-
-
executeOrdered
Schedules a one time action to execute with an ordering guarantee on the key.- Parameters:
orderingKey-r-
-
executeOrdered
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, Callable<T> task) -
getThreadID
public long getThreadID(long orderingKey) -
chooseThread
-
chooseThread
-
chooseThread
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
-
timedCallable
-
timedCallables
protected <T> Collection<? extends Callable<T>> timedCallables(Collection<? extends Callable<T>> tasks) -
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
execute
-
shutdown
public void shutdown()- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
- Specified by:
shutdownNowin interfaceExecutorService
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdownin interfaceExecutorService
-
awaitTermination
- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminatedin interfaceExecutorService
-
forceShutdown
Force threads shutdown (cancel active requests) after specified delay, to be used after shutdown() rejects new requests.
-