Package io.deephaven.io.sched
Class YASchedulerImpl
java.lang.Object
io.deephaven.io.sched.YASchedulerImpl
- All Implemented Interfaces:
Scheduler
Yet Another implementation of the Scheduler interface -- the best one yet.
This class provides a singleton wrapper for scheduling invocations of multiple Job instances from a single thread.
Job are scheduled in accordance with an interest set on a java.nio.Channel, deadline based time scheduling, and/or
custom criteria defined by the Jobs' implementation of the ready() method.
Jobs are instantiated by the application and made known to the scheduler by one of the installJob() methods. A
previously installed job can be removed from the scheduler with the cancelJob() method. The installJob() and
cancelJob() methods are thread-safe. It is allowed to call installJob() on a job that is already installed, or
cancelJob() on a job that is not current in the scheduler. In the former case, the channel and/or deadline will be
updated accordingly; in the latter, the call will be ignored.
Once the job is installed, the scheduler promises to call exactly one of its invoke(), timedOut() or cancelled()
methods exactly once. The invoke() method will be called only if the job was (last) installed with a channel and
non-zero interest set. The timedOut() method can be called for any job, since all jobs have an associated deadline
(although the timeout value can be set to Integer.MAX_VALUE to make if effectively infinite). The cancelled() method
is called only if the job is removed by a cancelJob() call before either the channe is ready or the deadline expires.
After the job is called back, the scheduler forgets about the job completely, unless the application installs it
again. That is, from the scheduler's point of view *all* jobs are one-shots. This design is based on the observation
that it is easier to reschedule jobs on every invocation in the style of a tail-recursive loop, as opposed to
maintaining persistent state in the scheduler.
The application must drive the scheduler by calling the work() method in a loop. The work() method is *not*
thread-safe; the application must either call it from a single thread or synchronize calls accordingly.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.deephaven.io.sched.Scheduler
Scheduler.ExecutorAdaptor, Scheduler.Null -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionYASchedulerImpl(String name, Selector selector, Logger log) YASchedulerImpl(String name, Selector selector, Logger log, boolean doTimingStats, boolean doSpinSelect) YASchedulerImpl(Selector selector, Logger log) The constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidCancel a job's selection key with the scheduler.voidclose()Shuts down the scheduler, calling close() on the underlying Selector instance.longReturn the scheduler's idea of the current time.voidinstallJob(Job job, long deadline) Install a job with only an associated deadline (removing any channel association)voidinstallJob(Job job, long deadline, SelectableChannel channel, int interest) Install a job in association with a channel and an interest set.booleanisClosed()Return true if the scheduler is closed, or in the process of closing.return the set of all jobs known to the scheduler, in whatever stateReturn the selection keys currently known to the scheduler.Return a map containing all channels and the jobs to which they are associated.Return the selection keys currently known to the scheduler.Return a reference to the selectorReturn the contents of the timeout queue, in deadline orderbooleanReturn true if the timeout queue invariant holds.booleanWait for jobs to become ready, then invoke() them all.
-
Field Details
-
name
the scheduler name, for debug and stats output -
log
the logger
-
-
Constructor Details
-
YASchedulerImpl
The constructor.- Throws:
IOException
-
YASchedulerImpl
- Throws:
IOException
-
YASchedulerImpl
-
-
Method Details
-
currentTimeMillis
public long currentTimeMillis()Return the scheduler's idea of the current time.- Specified by:
currentTimeMillisin interfaceScheduler
-
installJob
Install a job in association with a channel and an interest set.- Specified by:
installJobin interfaceScheduler
-
installJob
Install a job with only an associated deadline (removing any channel association)- Specified by:
installJobin interfaceScheduler
-
cancelJob
Cancel a job's selection key with the scheduler. -
work
Wait for jobs to become ready, then invoke() them all. This method will form the core of the main loop of a scheduler-driven application. The method first waits until: -- the given timeout expires, -- the earliest job-specific timeout expires, or -- one or more jobs becomes ready Note that this method is not synchronized. The application must ensure that it is never called concurrently by more than one thread. -
close
public void close()Shuts down the scheduler, calling close() on the underlying Selector instance. -
isClosed
public boolean isClosed()Return true if the scheduler is closed, or in the process of closing. -
junitGetSelector
Description copied from interface:SchedulerReturn a reference to the selector- Specified by:
junitGetSelectorin interfaceScheduler
-
junitGetAllJobs
return the set of all jobs known to the scheduler, in whatever state- Specified by:
junitGetAllJobsin interfaceScheduler
-
junitGetTimeoutQueue
Return the contents of the timeout queue, in deadline order- Specified by:
junitGetTimeoutQueuein interfaceScheduler- Returns:
- the jobs in the timeout queue
-
junitGetAllKeys
Return the selection keys currently known to the scheduler.- Specified by:
junitGetAllKeysin interfaceScheduler
-
junitGetReadyKeys
Return the selection keys currently known to the scheduler.- Specified by:
junitGetReadyKeysin interfaceScheduler
-
junitGetChannelsAndJobs
Return a map containing all channels and the jobs to which they are associated.- Specified by:
junitGetChannelsAndJobsin interfaceScheduler
-
junitTestTimeoutQueueInvariant
public boolean junitTestTimeoutQueueInvariant()Return true if the timeout queue invariant holds.- Specified by:
junitTestTimeoutQueueInvariantin interfaceScheduler
-