Class TaskManager

java.lang.Object
org.elasticsearch.tasks.TaskManager
All Implemented Interfaces:
ClusterStateApplier

public class TaskManager extends Object implements ClusterStateApplier
Task Manager service for keeping track of currently running tasks on the nodes
  • Constructor Details

  • Method Details

    • setTaskResultsService

      public void setTaskResultsService(TaskResultsService taskResultsService)
    • setTaskCancellationService

      public void setTaskCancellationService(TaskCancellationService taskCancellationService)
    • register

      public Task register(String type, String action, TaskAwareRequest request)
      Registers a task without parent task
    • registerAndExecute

      public <Request extends ActionRequest, Response extends ActionResponse> Task registerAndExecute(String type, TransportAction<Request,Response> action, Request request, Transport.Connection localConnection, BiConsumer<Task,Response> onResponse, BiConsumer<Task,Exception> onFailure)
    • cancel

      public void cancel(CancellableTask task, String reason, Runnable listener)
      Cancels a task

      After starting cancellation on the parent task, the task manager tries to cancel all children tasks of the current task. Once cancellation of the children tasks is done, the listener is triggered. If the task is completed or unregistered from TaskManager, then the listener is called immediately.

    • unregister

      public Task unregister(Task task)
      Unregister the task
    • registerChildConnection

      public org.elasticsearch.core.Releasable registerChildConnection(long taskId, Transport.Connection childConnection)
      Register a connection on which a child task will execute on the target connection. The returned Releasable must be called to unregister the child connection once the child task is completed or failed.
    • storeResult

      public <Response extends ActionResponse> void storeResult(Task task, Exception error, ActionListener<Response> listener)
      Stores the task failure
    • storeResult

      public <Response extends ActionResponse> void storeResult(Task task, Response response, ActionListener<Response> listener)
      Stores the task result
    • getTasks

      public Map<Long,Task> getTasks()
      Returns the list of currently running tasks on the node
    • getCancellableTasks

      public Map<Long,CancellableTask> getCancellableTasks()
      Returns the list of currently running tasks on the node that can be cancelled
    • getTask

      public Task getTask(long id)
      Returns a task with given id, or null if the task is not found.
    • getCancellableTask

      public CancellableTask getCancellableTask(long id)
      Returns a cancellable task with given id, or null if the task is not found.
    • setBan

      public List<CancellableTask> setBan(TaskId parentTaskId, String reason, TransportChannel channel)
      Bans all tasks with the specified parent task from execution, cancels all tasks that are currently executing.

      This method is called when a parent task that has children is cancelled.

      Returns:
      a list of pending cancellable child tasks
    • removeBan

      public void removeBan(TaskId parentTaskId)
      Removes the ban for the specified parent task.

      This method is called when a previously banned task finally cancelled

    • getBannedTaskIds

      public Set<TaskId> getBannedTaskIds()
    • assertCancellableTaskConsistency

      public boolean assertCancellableTaskConsistency()
    • startBanOnChildTasks

      public Collection<Transport.Connection> startBanOnChildTasks(long taskId, String reason, Runnable onChildTasksCompleted)
      Start rejecting new child requests as the parent task was cancelled.
      Parameters:
      taskId - the parent task id
      onChildTasksCompleted - called when all child tasks are completed or failed
      Returns:
      a set of current connections that have outstanding child tasks
    • applyClusterState

      public void applyClusterState(ClusterChangedEvent event)
      Description copied from interface: ClusterStateApplier
      Called when a new cluster state (ClusterChangedEvent.state() needs to be applied. The cluster state to be applied is already committed when this method is called, so an applier must therefore be prepared to deal with any state it receives without throwing an exception. Throwing an exception from an applier is very bad because it will stop the application of this state before it has reached all the other appliers, and will likely result in another attempt to apply the same (or very similar) cluster state which might continue until this node is removed from the cluster.
      Specified by:
      applyClusterState in interface ClusterStateApplier
    • waitForTaskCompletion

      public void waitForTaskCompletion(Task task, long untilInNanos)
      Blocks the calling thread, waiting for the task to vanish from the TaskManager.
    • startTrackingCancellableChannelTask

      public org.elasticsearch.core.Releasable startTrackingCancellableChannelTask(TcpChannel channel, CancellableTask task)
      Start tracking a cancellable task with its tcp channel, so if the channel gets closed we can get a set of pending tasks associated that channel and cancel them as these results won't be retrieved by the parent task.
      Returns:
      a releasable that should be called when this pending task is completed
    • cancelTaskAndDescendants

      public void cancelTaskAndDescendants(CancellableTask task, String reason, boolean waitForCompletion, ActionListener<Void> listener)