Package io.deephaven.io.sched
Class YASchedulerImpl
- java.lang.Object
-
- io.deephaven.io.sched.YASchedulerImpl
-
- All Implemented Interfaces:
Scheduler
public class YASchedulerImpl extends java.lang.Object implements 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
-
-
Constructor Summary
Constructors Constructor Description YASchedulerImpl(java.lang.String name, java.nio.channels.Selector selector, Logger log)YASchedulerImpl(java.lang.String name, java.nio.channels.Selector selector, Logger log, boolean doTimingStats, boolean doSpinSelect)YASchedulerImpl(java.nio.channels.Selector selector, Logger log)The constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancelJob(Job job)Cancel a job's selection key with the scheduler.voidclose()Shuts down the scheduler, calling close() on the underlying Selector instance.longcurrentTimeMillis()Return 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, java.nio.channels.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.java.util.Set<Job>junitGetAllJobs()return the set of all jobs known to the scheduler, in whatever statejava.util.ArrayList<java.nio.channels.SelectionKey>junitGetAllKeys()Return the selection keys currently known to the scheduler.java.util.Map<java.nio.channels.SelectableChannel,Job>junitGetChannelsAndJobs()Return a map containing all channels and the jobs to which they are associated.java.util.ArrayList<java.nio.channels.SelectionKey>junitGetReadyKeys()Return the selection keys currently known to the scheduler.java.nio.channels.SelectorjunitGetSelector()Return a reference to the selectorjava.util.ArrayList<Job>junitGetTimeoutQueue()Return the contents of the timeout queue, in deadline orderbooleanjunitTestTimeoutQueueInvariant()Return true if the timeout queue invariant holds.booleanwork(long timeout, io.deephaven.base.Procedure.Nullary handoff)Wait for jobs to become ready, then invoke() them all.
-
-
-
Field Detail
-
name
protected final java.lang.String name
the scheduler name, for debug and stats output
-
log
protected final Logger log
the logger
-
-
Constructor Detail
-
YASchedulerImpl
public YASchedulerImpl(java.nio.channels.Selector selector, Logger log) throws java.io.IOExceptionThe constructor.- Throws:
java.io.IOException
-
YASchedulerImpl
public YASchedulerImpl(java.lang.String name, java.nio.channels.Selector selector, Logger log) throws java.io.IOException- Throws:
java.io.IOException
-
YASchedulerImpl
public YASchedulerImpl(java.lang.String name, java.nio.channels.Selector selector, Logger log, boolean doTimingStats, boolean doSpinSelect)
-
-
Method Detail
-
currentTimeMillis
public long currentTimeMillis()
Return the scheduler's idea of the current time.- Specified by:
currentTimeMillisin interfaceScheduler
-
installJob
public void installJob(Job job, long deadline, java.nio.channels.SelectableChannel channel, int interest)
Install a job in association with a channel and an interest set.- Specified by:
installJobin interfaceScheduler
-
installJob
public void installJob(Job job, long deadline)
Install a job with only an associated deadline (removing any channel association)- Specified by:
installJobin interfaceScheduler
-
cancelJob
public void cancelJob(Job job)
Cancel a job's selection key with the scheduler.
-
work
public boolean work(long timeout, io.deephaven.base.Procedure.Nullary handoff)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
public java.nio.channels.Selector junitGetSelector()
Description copied from interface:SchedulerReturn a reference to the selector- Specified by:
junitGetSelectorin interfaceScheduler
-
junitGetAllJobs
public java.util.Set<Job> junitGetAllJobs()
return the set of all jobs known to the scheduler, in whatever state- Specified by:
junitGetAllJobsin interfaceScheduler
-
junitGetTimeoutQueue
public java.util.ArrayList<Job> junitGetTimeoutQueue()
Return the contents of the timeout queue, in deadline order- Specified by:
junitGetTimeoutQueuein interfaceScheduler- Returns:
- the jobs in the timeout queue
-
junitGetAllKeys
public java.util.ArrayList<java.nio.channels.SelectionKey> junitGetAllKeys()
Return the selection keys currently known to the scheduler.- Specified by:
junitGetAllKeysin interfaceScheduler
-
junitGetReadyKeys
public java.util.ArrayList<java.nio.channels.SelectionKey> junitGetReadyKeys()
Return the selection keys currently known to the scheduler.- Specified by:
junitGetReadyKeysin interfaceScheduler
-
junitGetChannelsAndJobs
public java.util.Map<java.nio.channels.SelectableChannel,Job> 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
-
-