Class SimpleBatchedExecutor<Task extends ClusterStateTaskListener,TaskResult>
- All Implemented Interfaces:
ClusterStateTaskExecutor<Task>
- Direct Known Subclasses:
ReservedStateUpdateTaskExecutor
TaskResult
.
This allows executing the tasks in the batch as a series of executions, each taking an input cluster state
and producing a new cluster state that serves as the input of the next task in the batch.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.elasticsearch.cluster.ClusterStateTaskExecutor
ClusterStateTaskExecutor.BatchExecutionContext<T extends ClusterStateTaskListener>, ClusterStateTaskExecutor.TaskContext<T extends ClusterStateTaskListener>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionafterBatchExecution
(ClusterState clusterState, boolean clusterStateChanged) Called once all tasks in the batch have finished execution.void
Called after the new cluster state is published.final void
clusterStatePublished
(ClusterState newClusterState) Callback invoked after new cluster state is published.final ClusterState
execute
(ClusterStateTaskExecutor.BatchExecutionContext<Task> batchExecutionContext) Update the cluster state based on the current state and the given tasks.abstract Tuple
<ClusterState, TaskResult> executeTask
(Task task, ClusterState clusterState) Executes the provided task from the batch.abstract void
taskSucceeded
(Task task, TaskResult taskResult) Called if executing a task in the batch finished successfully, and before the execution of the next task in the batch.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.elasticsearch.cluster.ClusterStateTaskExecutor
describeTasks, runOnlyOnMaster
-
Constructor Details
-
SimpleBatchedExecutor
public SimpleBatchedExecutor()
-
-
Method Details
-
executeTask
public abstract Tuple<ClusterState,TaskResult> executeTask(Task task, ClusterState clusterState) throws Exception Executes the provided task from the batch.- Parameters:
task
- The task to be executed.clusterState
- The cluster state on which the task should be executed.- Returns:
- A tuple consisting of the resulting cluster state after executing this task, and the result of the task execution.
The returned cluster state serves as the cluster state on which the next task in the batch will run. The returned
task result is provided to the
taskSucceeded(Task, TaskResult)
implementation. - Throws:
Exception
-
afterBatchExecution
Called once all tasks in the batch have finished execution. It should return a cluster state that reflects the execution of all the tasks.- Parameters:
clusterState
- The cluster state resulting from the execution of all the tasks.clusterStateChanged
- WhetherclusterState
is different from the cluster state before executing the tasks in the batch.- Returns:
- The resulting cluster state after executing all the tasks.
-
taskSucceeded
Called if executing a task in the batch finished successfully, and before the execution of the next task in the batch.- Parameters:
task
- The task that successfully finished execution.taskResult
- The result returned from the successful execution of the task.
-
clusterStatePublished
Description copied from interface:ClusterStateTaskExecutor
Callback invoked after new cluster state is published. Note that this method is not invoked if the cluster state was not updated. Note that this method will be executed using system context.- Specified by:
clusterStatePublished
in interfaceClusterStateTaskExecutor<Task extends ClusterStateTaskListener>
- Parameters:
newClusterState
- The new state which was published.
-
clusterStatePublished
public void clusterStatePublished()Called after the new cluster state is published. Note that this method is not invoked if the cluster state was not updated. -
execute
public final ClusterState execute(ClusterStateTaskExecutor.BatchExecutionContext<Task> batchExecutionContext) throws Exception Description copied from interface:ClusterStateTaskExecutor
Update the cluster state based on the current state and the given tasks. ReturnbatchExecutionContext.initialState()
to avoid publishing any update.If this method throws an exception then the cluster state is unchanged and every task's
ClusterStateTaskListener.onFailure(java.lang.Exception)
method is called.A common implementation pattern is to iterate through the tasks, constructing a new and updated
ClusterState
for each one. This works ok but beware that constructing a whole newClusterState
can be somewhat expensive, and there may sometimes be surprisingly many tasks to process in the batch. If it's possible to accumulate the effects of the tasks at a lower level then you should do that instead.Returning
batchExecutionContext.initialState()
is an important and useful optimisation in most cases, but note that this fast-path exposes APIs to the risk of stale reads in the vicinity of a master failover: a nodeN
that handles such a no-op task batch does not verify with its peers that it's still the master, and if it's not the master then another nodeM
may already have become master and updated the state in a way that would be inconsistent with the response thatN
sends back to clients.- Specified by:
execute
in interfaceClusterStateTaskExecutor<Task extends ClusterStateTaskListener>
- Returns:
- The resulting cluster state after executing all the tasks. If
batchExecutionContext.initialState()
is returned then no update is published. - Throws:
Exception
-