Class OrderedScheduler
- java.lang.Object
-
- org.apache.bookkeeper.common.util.OrderedExecutor
-
- org.apache.bookkeeper.common.util.OrderedScheduler
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,java.util.concurrent.ExecutorService
,java.util.concurrent.ScheduledExecutorService
public class OrderedScheduler extends OrderedExecutor implements java.util.concurrent.ScheduledExecutorService
This class provides 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 (package private) class
OrderedScheduler.OrderedSchedulerDecoratedThread
static class
OrderedScheduler.SchedulerBuilder
Builder to build ordered scheduler.-
Nested classes/interfaces inherited from class org.apache.bookkeeper.common.util.OrderedExecutor
OrderedExecutor.AbstractBuilder<T extends OrderedExecutor>, OrderedExecutor.Builder, OrderedExecutor.ContextPreservingCallable<T>, OrderedExecutor.ContextPreservingRunnable, OrderedExecutor.TimedCallable<T>, OrderedExecutor.TimedRunnable
-
-
Field Summary
-
Fields inherited from class org.apache.bookkeeper.common.util.OrderedExecutor
enableBusyWait, enableThreadScopedMetrics, maxTasksInQueue, name, NO_TASK_LIMIT, preserveMdcForTaskExecution, rand, taskExecutionStats, taskPendingStats, threadIds, threads, traceTaskExecution, WARN_TIME_MICRO_SEC_DEFAULT, warnTimeMicroSec
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected com.google.common.util.concurrent.ListeningScheduledExecutorService
addExecutorDecorators(java.util.concurrent.ExecutorService executor)
com.google.common.util.concurrent.ListeningScheduledExecutorService
chooseThread()
com.google.common.util.concurrent.ListeningScheduledExecutorService
chooseThread(long orderingKey)
skip hashcode generation in this special case.com.google.common.util.concurrent.ListeningScheduledExecutorService
chooseThread(java.lang.Object orderingKey)
protected java.util.concurrent.ExecutorService
createSingleThreadExecutor(java.util.concurrent.ThreadFactory factory)
protected com.google.common.util.concurrent.ListeningScheduledExecutorService
getBoundedExecutor(java.util.concurrent.ExecutorService executor)
static OrderedScheduler.SchedulerBuilder
newSchedulerBuilder()
Create a builder to build ordered scheduler.java.util.concurrent.ScheduledFuture<?>
schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)
<V> java.util.concurrent.ScheduledFuture<V>
schedule(java.util.concurrent.Callable<V> callable, long delay, java.util.concurrent.TimeUnit unit)
java.util.concurrent.ScheduledFuture<?>
scheduleAtFixedRate(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
java.util.concurrent.ScheduledFuture<?>
scheduleAtFixedRateOrdered(java.lang.Object orderingKey, java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period.java.util.concurrent.ScheduledFuture<?>
scheduleOrdered(java.lang.Object orderingKey, java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.java.util.concurrent.ScheduledFuture<?>
scheduleWithFixedDelay(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit)
java.util.concurrent.ScheduledFuture<?>
scheduleWithFixedDelayOrdered(java.lang.Object orderingKey, java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.<T> com.google.common.util.concurrent.ListenableFuture<T>
submitOrdered(java.lang.Object orderingKey, java.util.concurrent.Callable<T> callable)
schedules a one time action to execute with an ordering guarantee on the key.-
Methods inherited from class org.apache.bookkeeper.common.util.OrderedExecutor
awaitTermination, chooseThreadIdx, execute, executeOrdered, executeOrdered, executeOrdered, forceShutdown, getThreadID, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, newBuilder, preserveMdc, shutdown, shutdownNow, submit, submit, submit, submitOrdered, timedCallable, timedCallables, timedRunnable
-
-
-
-
Method Detail
-
newSchedulerBuilder
public static OrderedScheduler.SchedulerBuilder newSchedulerBuilder()
Create a builder to build ordered scheduler.- Returns:
- builder to build ordered scheduler.
-
createSingleThreadExecutor
protected java.util.concurrent.ExecutorService createSingleThreadExecutor(java.util.concurrent.ThreadFactory factory)
- Overrides:
createSingleThreadExecutor
in classOrderedExecutor
-
getBoundedExecutor
protected com.google.common.util.concurrent.ListeningScheduledExecutorService getBoundedExecutor(java.util.concurrent.ExecutorService executor)
- Overrides:
getBoundedExecutor
in classOrderedExecutor
-
addExecutorDecorators
protected com.google.common.util.concurrent.ListeningScheduledExecutorService addExecutorDecorators(java.util.concurrent.ExecutorService executor)
- Overrides:
addExecutorDecorators
in classOrderedExecutor
-
chooseThread
public com.google.common.util.concurrent.ListeningScheduledExecutorService chooseThread()
- Overrides:
chooseThread
in classOrderedExecutor
-
chooseThread
public com.google.common.util.concurrent.ListeningScheduledExecutorService chooseThread(java.lang.Object orderingKey)
- Overrides:
chooseThread
in classOrderedExecutor
-
chooseThread
public com.google.common.util.concurrent.ListeningScheduledExecutorService chooseThread(long orderingKey)
Description copied from class:OrderedExecutor
skip hashcode generation in this special case.- Overrides:
chooseThread
in classOrderedExecutor
- Parameters:
orderingKey
- long ordering key- Returns:
- the thread for executing this order key
-
submitOrdered
public <T> com.google.common.util.concurrent.ListenableFuture<T> submitOrdered(java.lang.Object orderingKey, java.util.concurrent.Callable<T> callable)
schedules a one time action to execute with an ordering guarantee on the key.- Parameters:
orderingKey
-callable
-
-
scheduleOrdered
public java.util.concurrent.ScheduledFuture<?> scheduleOrdered(java.lang.Object orderingKey, java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.- Parameters:
orderingKey
- - the key used for orderingcommand
- - the Runnable to executedelay
- - the time from now to delay executionunit
- - the time unit of the delay parameter- Returns:
- a ScheduledFuture representing pending completion of the task and whose get() method will return null upon completion
-
scheduleAtFixedRateOrdered
public java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRateOrdered(java.lang.Object orderingKey, java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period.For more details check
ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
.- Parameters:
orderingKey
- - the key used for orderingcommand
- - the Runnable to executeinitialDelay
- - the time to delay first executionperiod
- - the period between successive executionsunit
- - the time unit of the initialDelay and period parameters- Returns:
- a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
-
scheduleWithFixedDelayOrdered
public java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelayOrdered(java.lang.Object orderingKey, java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.For more details check
ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
.- Parameters:
orderingKey
- - the key used for orderingcommand
- - the Runnable to executeinitialDelay
- - the time to delay first executiondelay
- - the delay between the termination of one execution and the commencement of the nextunit
- - the time unit of the initialDelay and delay parameters- Returns:
- a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
-
schedule
public java.util.concurrent.ScheduledFuture<?> schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)
- Specified by:
schedule
in interfacejava.util.concurrent.ScheduledExecutorService
-
schedule
public <V> java.util.concurrent.ScheduledFuture<V> schedule(java.util.concurrent.Callable<V> callable, long delay, java.util.concurrent.TimeUnit unit)
- Specified by:
schedule
in interfacejava.util.concurrent.ScheduledExecutorService
-
scheduleAtFixedRate
public java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
- Specified by:
scheduleAtFixedRate
in interfacejava.util.concurrent.ScheduledExecutorService
-
scheduleWithFixedDelay
public java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit)
- Specified by:
scheduleWithFixedDelay
in interfacejava.util.concurrent.ScheduledExecutorService
-
-