Skip to content

Commit 4cdc4bd

Browse files
committed
ML: better handle task state race condition (#38040)
1 parent ab49681 commit 4cdc4bd

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetUpgradeModeAction.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
package org.elasticsearch.xpack.ml.action;
77

8+
import org.elasticsearch.ElasticsearchException;
89
import org.elasticsearch.ElasticsearchStatusException;
910
import org.elasticsearch.ElasticsearchTimeoutException;
11+
import org.elasticsearch.ResourceNotFoundException;
1012
import org.elasticsearch.action.ActionListener;
1113
import org.elasticsearch.action.support.ActionFilters;
1214
import org.elasticsearch.action.support.master.AcknowledgedResponse;
@@ -34,11 +36,13 @@
3436
import org.elasticsearch.xpack.core.ml.action.SetUpgradeModeAction;
3537
import org.elasticsearch.xpack.ml.utils.TypedChainTaskExecutor;
3638

39+
import java.util.Comparator;
3740
import java.util.List;
3841
import java.util.Set;
3942
import java.util.concurrent.atomic.AtomicBoolean;
4043
import java.util.stream.Collectors;
4144

45+
import static org.elasticsearch.ExceptionsHelper.rethrowAndSuppress;
4246
import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN;
4347
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
4448
import static org.elasticsearch.xpack.core.ml.MlTasks.AWAITING_UPGRADE;
@@ -120,9 +124,20 @@ protected void masterOperation(SetUpgradeModeAction.Request request, ClusterStat
120124
.cluster()
121125
.prepareListTasks()
122126
.setActions(DATAFEED_TASK_NAME + "[c]", JOB_TASK_NAME + "[c]")
127+
// There is a chance that we failed un-allocating a task due to allocation_id being changed
128+
// This call will timeout in that case and return an error
123129
.setWaitForCompletion(true)
124130
.setTimeout(request.timeout()).execute(ActionListener.wrap(
125-
r -> wrappedListener.onResponse(new AcknowledgedResponse(true)),
131+
r -> {
132+
try {
133+
// Handle potential node timeouts,
134+
// these should be considered failures as tasks as still potentially executing
135+
rethrowAndSuppress(r.getNodeFailures());
136+
wrappedListener.onResponse(new AcknowledgedResponse(true));
137+
} catch (ElasticsearchException ex) {
138+
wrappedListener.onFailure(ex);
139+
}
140+
},
126141
wrappedListener::onFailure));
127142
},
128143
wrappedListener::onFailure
@@ -244,10 +259,19 @@ private void unassignPersistentTasks(PersistentTasksCustomMetaData tasksCustomMe
244259
.stream()
245260
.filter(persistentTask -> (persistentTask.getTaskName().equals(MlTasks.JOB_TASK_NAME) ||
246261
persistentTask.getTaskName().equals(MlTasks.DATAFEED_TASK_NAME)))
262+
// We want to always have the same ordering of which tasks we un-allocate first.
263+
// However, the order in which the distributed tasks handle the un-allocation event is not guaranteed.
264+
.sorted(Comparator.comparing(PersistentTask::getTaskName))
247265
.collect(Collectors.toList());
248266

249267
TypedChainTaskExecutor<PersistentTask<?>> chainTaskExecutor =
250-
new TypedChainTaskExecutor<>(client.threadPool().executor(executor()), r -> true, ex -> true);
268+
new TypedChainTaskExecutor<>(client.threadPool().executor(executor()),
269+
r -> true,
270+
// Another process could modify tasks and thus we cannot find them via the allocation_id and name
271+
// If the task was removed from the node, all is well
272+
// We handle the case of allocation_id changing later in this transport class by timing out waiting for task completion
273+
// Consequently, if the exception is ResourceNotFoundException, continue execution; circuit break otherwise.
274+
ex -> ex instanceof ResourceNotFoundException == false);
251275

252276
for (PersistentTask<?> task : datafeedAndJobTasks) {
253277
chainTaskExecutor.add(

0 commit comments

Comments
 (0)