Class DeterministicTaskQueue

java.lang.Object
org.elasticsearch.common.util.concurrent.DeterministicTaskQueue

public class DeterministicTaskQueue extends Object
Permits the testing of async processes by interleaving all the tasks on a single thread in a pseudo-random (deterministic) fashion, letting each task spawn future tasks, and simulating the passage of time. Tasks can be scheduled directly via scheduleNow(java.lang.Runnable) and scheduleAt(long, java.lang.Runnable), or can be executed using the thread pool returned from getThreadPool(). The scheduling of tasks can be made more variable with setExecutionDelayVariabilityMillis(long) to simulate a system that is not running tasks in a timely fashion e.g. due to overload or network delays.
  • Field Details

  • Constructor Details

    • DeterministicTaskQueue

      public DeterministicTaskQueue(org.elasticsearch.common.settings.Settings settings, Random random)
    • DeterministicTaskQueue

      public DeterministicTaskQueue()
  • Method Details

    • getExecutionDelayVariabilityMillis

      public long getExecutionDelayVariabilityMillis()
    • setExecutionDelayVariabilityMillis

      public void setExecutionDelayVariabilityMillis(long executionDelayVariabilityMillis)
    • runAllRunnableTasks

      public void runAllRunnableTasks()
    • runAllTasks

      public void runAllTasks()
    • runAllTasksInTimeOrder

      public void runAllTasksInTimeOrder()
    • hasRunnableTasks

      public boolean hasRunnableTasks()
      Returns:
      whether there are any runnable tasks.
    • hasDeferredTasks

      public boolean hasDeferredTasks()
      Returns:
      whether there are any deferred tasks, i.e. tasks that are scheduled for the future.
    • getCurrentTimeMillis

      public long getCurrentTimeMillis()
      Returns:
      the current (simulated) time, in milliseconds.
    • runRandomTask

      public void runRandomTask()
      Runs an arbitrary runnable task.
    • scheduleNow

      public void scheduleNow(Runnable task)
      Schedule a task for immediate execution.
    • scheduleAt

      public void scheduleAt(long executionTimeMillis, Runnable task)
      Schedule a task for future execution.
    • advanceTime

      public void advanceTime()
      Advance the current time to the time of the next deferred task, and update the sets of deferred and runnable tasks accordingly.
    • getThreadPool

      public org.elasticsearch.threadpool.ThreadPool getThreadPool()
      Returns:
      A ThreadPool that uses this task queue.
    • getThreadPool

      public org.elasticsearch.threadpool.ThreadPool getThreadPool(Function<Runnable,Runnable> runnableWrapper)
      Returns:
      A ThreadPool that uses this task queue and wraps Runnables in the given wrapper.
    • getLatestDeferredExecutionTime

      public long getLatestDeferredExecutionTime()
    • getNodeIdForLogContext

      public static String getNodeIdForLogContext(org.elasticsearch.cluster.node.DiscoveryNode node)
    • onNodeLog

      public static Runnable onNodeLog(org.elasticsearch.cluster.node.DiscoveryNode node, Runnable runnable)