Skip to content

Commit 992c788

Browse files
authored
Uncouple persistent task state and status (#31031)
This pull request removes the relationship between the state of persistent task (as stored in the cluster state) and the status of the task (as reported by the Task APIs and used in various places) that have been confusing for some time (#29608). In order to do that, a new PersistentTaskState interface is added. This interface represents the persisted state of a persistent task. The methods used to update the state of persistent tasks are renamed: updatePersistentStatus() becomes updatePersistentTaskState() and now takes a PersistentTaskState as a parameter. The Task.Status type as been changed to PersistentTaskState in all places were it make sense (in persistent task customs in cluster state and all other methods that deal with the state of an allocated persistent task).
1 parent 8c6ee7d commit 992c788

File tree

42 files changed

+404
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+404
-391
lines changed

server/src/main/java/org/elasticsearch/persistent/AllocatedPersistentTask.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.common.Nullable;
2626
import org.elasticsearch.common.unit.TimeValue;
2727
import org.elasticsearch.tasks.CancellableTask;
28-
import org.elasticsearch.tasks.Task;
2928
import org.elasticsearch.tasks.TaskId;
3029
import org.elasticsearch.tasks.TaskManager;
3130

@@ -77,8 +76,9 @@ public Status getStatus() {
7776
* <p>
7877
* This doesn't affect the status of this allocated task.
7978
*/
80-
public void updatePersistentStatus(Task.Status status, ActionListener<PersistentTasksCustomMetaData.PersistentTask<?>> listener) {
81-
persistentTasksService.updateStatus(persistentTaskId, allocationId, status, listener);
79+
public void updatePersistentTaskState(final PersistentTaskState state,
80+
final ActionListener<PersistentTasksCustomMetaData.PersistentTask<?>> listener) {
81+
persistentTasksService.sendUpdateStateRequest(persistentTaskId, allocationId, state, listener);
8282
}
8383

8484
public String getPersistentTaskId() {
@@ -116,7 +116,7 @@ public void waitForPersistentTask(final Predicate<PersistentTasksCustomMetaData.
116116
}
117117

118118
protected final boolean isCompleted() {
119-
return state.get() == State.COMPLETED;
119+
return state.get() == State.COMPLETED;
120120
}
121121

122122
boolean markAsCancelled() {

server/src/main/java/org/elasticsearch/persistent/NodePersistentTasksExecutor.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.elasticsearch.common.Nullable;
2222
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
23-
import org.elasticsearch.tasks.Task;
2423
import org.elasticsearch.threadpool.ThreadPool;
2524

2625
/**
@@ -29,16 +28,17 @@
2928
* It abstracts away the execution of tasks and greatly simplifies testing of PersistentTasksNodeService
3029
*/
3130
public class NodePersistentTasksExecutor {
31+
3232
private final ThreadPool threadPool;
3333

34-
public NodePersistentTasksExecutor(ThreadPool threadPool) {
34+
NodePersistentTasksExecutor(ThreadPool threadPool) {
3535
this.threadPool = threadPool;
3636
}
3737

38-
public <Params extends PersistentTaskParams> void executeTask(Params params,
39-
@Nullable Task.Status status,
40-
AllocatedPersistentTask task,
41-
PersistentTasksExecutor<Params> executor) {
38+
public <Params extends PersistentTaskParams> void executeTask(final Params params,
39+
final @Nullable PersistentTaskState state,
40+
final AllocatedPersistentTask task,
41+
final PersistentTasksExecutor<Params> executor) {
4242
threadPool.executor(executor.getExecutor()).execute(new AbstractRunnable() {
4343
@Override
4444
public void onFailure(Exception e) {
@@ -49,14 +49,12 @@ public void onFailure(Exception e) {
4949
@Override
5050
protected void doRun() throws Exception {
5151
try {
52-
executor.nodeOperation(task, params, status);
52+
executor.nodeOperation(task, params, state);
5353
} catch (Exception ex) {
5454
task.markAsFailed(ex);
5555
}
5656

5757
}
5858
});
59-
6059
}
61-
6260
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.persistent;
20+
21+
import org.elasticsearch.common.io.stream.NamedWriteable;
22+
import org.elasticsearch.common.xcontent.ToXContentObject;
23+
24+
/**
25+
* {@link PersistentTaskState} represents the state of the persistent tasks, as it
26+
* is persisted in the cluster state.
27+
*/
28+
public interface PersistentTaskState extends ToXContentObject, NamedWriteable {
29+
}

server/src/main/java/org/elasticsearch/persistent/PersistentTasksClusterService.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.elasticsearch.persistent.PersistentTasksCustomMetaData.PersistentTask;
3636
import org.elasticsearch.persistent.decider.AssignmentDecision;
3737
import org.elasticsearch.persistent.decider.EnableAssignmentDecider;
38-
import org.elasticsearch.tasks.Task;
3938

4039
import java.util.Objects;
4140

@@ -178,27 +177,30 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
178177
}
179178

180179
/**
181-
* Update task status
180+
* Update the state of a persistent task
182181
*
183-
* @param id the id of a persistent task
184-
* @param allocationId the expected allocation id of the persistent task
185-
* @param status new status
186-
* @param listener the listener that will be called when task is removed
182+
* @param taskId the id of a persistent task
183+
* @param taskAllocationId the expected allocation id of the persistent task
184+
* @param taskState new state
185+
* @param listener the listener that will be called when task is removed
187186
*/
188-
public void updatePersistentTaskStatus(String id, long allocationId, Task.Status status, ActionListener<PersistentTask<?>> listener) {
189-
clusterService.submitStateUpdateTask("update task status", new ClusterStateUpdateTask() {
187+
public void updatePersistentTaskState(final String taskId,
188+
final long taskAllocationId,
189+
final PersistentTaskState taskState,
190+
final ActionListener<PersistentTask<?>> listener) {
191+
clusterService.submitStateUpdateTask("update task state", new ClusterStateUpdateTask() {
190192
@Override
191193
public ClusterState execute(ClusterState currentState) {
192194
PersistentTasksCustomMetaData.Builder tasksInProgress = builder(currentState);
193-
if (tasksInProgress.hasTask(id, allocationId)) {
194-
return update(currentState, tasksInProgress.updateTaskStatus(id, status));
195+
if (tasksInProgress.hasTask(taskId, taskAllocationId)) {
196+
return update(currentState, tasksInProgress.updateTaskState(taskId, taskState));
195197
} else {
196-
if (tasksInProgress.hasTask(id)) {
197-
logger.warn("trying to update status on task {} with unexpected allocation id {}", id, allocationId);
198+
if (tasksInProgress.hasTask(taskId)) {
199+
logger.warn("trying to update state on task {} with unexpected allocation id {}", taskId, taskAllocationId);
198200
} else {
199-
logger.warn("trying to update status on non-existing task {}", id);
201+
logger.warn("trying to update state on non-existing task {}", taskId);
200202
}
201-
throw new ResourceNotFoundException("the task with id {} and allocation id {} doesn't exist", id, allocationId);
203+
throw new ResourceNotFoundException("the task with id {} and allocation id {} doesn't exist", taskId, taskAllocationId);
202204
}
203205
}
204206

@@ -209,7 +211,7 @@ public void onFailure(String source, Exception e) {
209211

210212
@Override
211213
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
212-
listener.onResponse(PersistentTasksCustomMetaData.getTaskWithId(newState, id));
214+
listener.onResponse(PersistentTasksCustomMetaData.getTaskWithId(newState, taskId));
213215
}
214216
});
215217
}

0 commit comments

Comments
 (0)