Skip to content

Commit 3120049

Browse files
committed
Use removeTask instead of finishTask in PersistentTasksClusterService (#29055)
The method `PersistentTasksClusterService.finishTask()` has been modified since it was added and does not use any `removeOncompletion` flag anymore. Its behavior is now similar to `removeTask()` and can be replaced by this one. When a non existing task is removed, the cluster state update task will fail and its `source` will still indicate `finish persistent task`/`remove persistent task`.
1 parent bd54917 commit 3120049

File tree

3 files changed

+5
-30
lines changed

3 files changed

+5
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void completePersistentTask(String id, long allocationId, Exception failu
117117
public ClusterState execute(ClusterState currentState) throws Exception {
118118
PersistentTasksCustomMetaData.Builder tasksInProgress = builder(currentState);
119119
if (tasksInProgress.hasTask(id, allocationId)) {
120-
tasksInProgress.finishTask(id);
120+
tasksInProgress.removeTask(id);
121121
return update(currentState, tasksInProgress);
122122
} else {
123123
if (tasksInProgress.hasTask(id)) {

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ public Builder reassignTask(String taskId, Assignment assignment) {
609609
changed = true;
610610
tasks.put(taskId, new PersistentTask<>(taskInProgress, getNextAllocationId(), assignment));
611611
} else {
612-
throw new ResourceNotFoundException("cannot reassign task with id {" + taskId + "}, the task no longer exits");
612+
throw new ResourceNotFoundException("cannot reassign task with id {" + taskId + "}, the task no longer exists");
613613
}
614614
return this;
615615
}
@@ -623,7 +623,7 @@ public Builder updateTaskStatus(String taskId, Status status) {
623623
changed = true;
624624
tasks.put(taskId, new PersistentTask<>(taskInProgress, status));
625625
} else {
626-
throw new ResourceNotFoundException("cannot update task with id {" + taskId + "}, the task no longer exits");
626+
throw new ResourceNotFoundException("cannot update task with id {" + taskId + "}, the task no longer exists");
627627
}
628628
return this;
629629
}
@@ -635,23 +635,7 @@ public Builder removeTask(String taskId) {
635635
if (tasks.remove(taskId) != null) {
636636
changed = true;
637637
} else {
638-
throw new ResourceNotFoundException("cannot remove task with id {" + taskId + "}, the task no longer exits");
639-
}
640-
return this;
641-
}
642-
643-
/**
644-
* Finishes the task
645-
* <p>
646-
* If the task is marked with removeOnCompletion flag, it is removed from the list, otherwise it is stopped.
647-
*/
648-
public Builder finishTask(String taskId) {
649-
PersistentTask<?> taskInProgress = tasks.get(taskId);
650-
if (taskInProgress != null) {
651-
changed = true;
652-
tasks.remove(taskId);
653-
} else {
654-
throw new ResourceNotFoundException("cannot finish task with id {" + taskId + "}, the task no longer exits");
638+
throw new ResourceNotFoundException("cannot remove task with id {" + taskId + "}, the task no longer exists");
655639
}
656640
return this;
657641
}

server/src/test/java/org/elasticsearch/persistent/PersistentTasksCustomMetaDataTests.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void testBuilder() {
191191
}
192192
boolean changed = false;
193193
for (int j = 0; j < randomIntBetween(1, 10); j++) {
194-
switch (randomInt(4)) {
194+
switch (randomInt(3)) {
195195
case 0:
196196
lastKnownTask = addRandomTask(builder);
197197
changed = true;
@@ -223,15 +223,6 @@ public void testBuilder() {
223223
expectThrows(ResourceNotFoundException.class, () -> builder.removeTask(fLastKnownTask));
224224
}
225225
break;
226-
case 4:
227-
if (builder.hasTask(lastKnownTask)) {
228-
changed = true;
229-
builder.finishTask(lastKnownTask);
230-
} else {
231-
String fLastKnownTask = lastKnownTask;
232-
expectThrows(ResourceNotFoundException.class, () -> builder.finishTask(fLastKnownTask));
233-
}
234-
break;
235226
}
236227
}
237228
assertEquals(changed, builder.isChanged());

0 commit comments

Comments
 (0)