Class PersistentTasksExecutor<Params extends PersistentTaskParams>

java.lang.Object
org.elasticsearch.persistent.PersistentTasksExecutor<Params>
Direct Known Subclasses:
HealthNodeTaskExecutor

public abstract class PersistentTasksExecutor<Params extends PersistentTaskParams> extends Object
An executor of tasks that can survive restart of requesting or executing node. These tasks are using cluster state rather than only transport service to send requests and responses.
  • Field Details

  • Constructor Details

    • PersistentTasksExecutor

      protected PersistentTasksExecutor(String taskName, Executor executor)
  • Method Details

    • getTaskName

      public String getTaskName()
    • scope

    • getAssignment

      public final PersistentTasksCustomMetadata.Assignment getAssignment(Params params, Collection<DiscoveryNode> candidateNodes, ClusterState clusterState, @Nullable ProjectId projectId)
      Returns the node id where the params has to be executed,

      The default implementation returns the least loaded data node from amongst the collection of candidate nodes

    • doGetAssignment

      protected PersistentTasksCustomMetadata.Assignment doGetAssignment(Params params, Collection<DiscoveryNode> candidateNodes, ClusterState clusterState, @Nullable ProjectId projectId)
      Returns the node id where the params has to be executed,

      The default implementation returns the least loaded data node from amongst the collection of candidate nodes.

      If scope() returns CLUSTER, then ProjectId will be null. If scope() returns PROJECT, then ProjectId will not be null.

    • selectLeastLoadedNode

      protected DiscoveryNode selectLeastLoadedNode(ClusterState clusterState, Collection<DiscoveryNode> candidateNodes, Predicate<DiscoveryNode> selector)
      Finds the least loaded node from amongs the candidate node collection that satisfies the selector criteria
    • automaticReassignmentOnShutdown

      public boolean automaticReassignmentOnShutdown()
      Whether this task should proactively be reassigned when its executing node is marked for shutdown. Automated reassignment allows for gap-free reassignment on a node (single cluster state update).

      Returns true by default. Executors that manage their own shutdown handling must override this method to return false to preserve their existing behavior.

      Migration requirement: before removing a return false override, ensure that neither the executor nor any associated plugin or cluster-state listener independently handles node shutdown for this task (for example, by calling AllocatedPersistentTask.markAsLocallyAborted(java.lang.String)). Such bespoke handlers would race with the framework-level reassignment. If the handler fires first it will unassign the task and trigger a two-step unassign-then-reassign cycle, which is exactly the gap this mechanism is designed to eliminate.

    • validate

      public void validate(Params params, ClusterState clusterState, @Nullable ProjectId projectId)
      Checks the current cluster state for compatibility with the params

      Throws an exception if the supplied params cannot be executed on the cluster in the current state.

    • createTask

      protected AllocatedPersistentTask createTask(long id, String type, String action, TaskId parentTaskId, PersistentTasksCustomMetadata.PersistentTask<Params> taskInProgress, Map<String,String> headers)
      Creates a AllocatedPersistentTask for communicating with task manager
    • getDescription

      protected String getDescription(PersistentTasksCustomMetadata.PersistentTask<Params> taskInProgress)
      Returns task description that will be available via task manager
    • nodeOperation

      protected abstract void nodeOperation(AllocatedPersistentTask task, Params params, @Nullable PersistentTaskState state)
      This operation will be executed on the executor node.

      NOTE: The nodeOperation has to throw an exception, trigger task.markAsCompleted() or task.completeAndNotifyIfNeeded() methods to indicate that the persistent task has finished.

    • getExecutor

      public Executor getExecutor()