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.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 theirObject.hashCode()
method.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
OrderedExecutor.AbstractBuilder<T extends OrderedExecutor>
Abstract builder class to buildOrderedScheduler
.static class
OrderedExecutor.Builder
A builder class for an OrderedExecutor.(package private) static class
OrderedExecutor.ContextPreservingCallable<T>
Decorator class for a callable that preserves MDC context.(package private) static class
OrderedExecutor.ContextPreservingRunnable
Decorator class for a runnable that preserves MDC context.protected class
OrderedExecutor.TimedCallable<T>
Decorator class for a callable that measure the execution time.protected class
OrderedExecutor.TimedRunnable
Decorator class for a runnable that measure the execution time.
-
Field Summary
Fields Modifier and Type Field Description (package private) boolean
enableBusyWait
(package private) boolean
enableThreadScopedMetrics
(package private) int
maxTasksInQueue
(package private) java.lang.String
name
static int
NO_TASK_LIMIT
(package private) boolean
preserveMdcForTaskExecution
(package private) java.util.Random
rand
(package private) OpStatsLogger
taskExecutionStats
(package private) OpStatsLogger
taskPendingStats
(package private) long[]
threadIds
(package private) java.util.concurrent.ExecutorService[]
threads
(package private) boolean
traceTaskExecution
protected static long
WARN_TIME_MICRO_SEC_DEFAULT
(package private) long
warnTimeMicroSec
-
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)
-
-
-
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:
submit
in interfacejava.util.concurrent.ExecutorService
-
submit
public <T> java.util.concurrent.Future<T> submit(java.lang.Runnable task, T result)
- Specified by:
submit
in interfacejava.util.concurrent.ExecutorService
-
submit
public java.util.concurrent.Future<?> submit(java.lang.Runnable task)
- Specified by:
submit
in 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:
invokeAll
in 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:
invokeAll
in 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:
invokeAny
in interfacejava.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 interfacejava.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 interfacejava.util.concurrent.Executor
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfacejava.util.concurrent.ExecutorService
-
shutdownNow
public java.util.List<java.lang.Runnable> shutdownNow()
- Specified by:
shutdownNow
in interfacejava.util.concurrent.ExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown
in interfacejava.util.concurrent.ExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
- Specified by:
awaitTermination
in interfacejava.util.concurrent.ExecutorService
- Throws:
java.lang.InterruptedException
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated
in 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.
-
-