diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsManager.java index 5c55e2f5696fe..7ad3aa5af6899 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsManager.java @@ -7,6 +7,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexAction; @@ -78,7 +79,13 @@ public void execute(DataFrameAnalyticsTask task, DataFrameAnalyticsState current case STARTED: task.updatePersistentTaskState(reindexingState, ActionListener.wrap( updatedTask -> reindexingStateListener.onResponse(config), - reindexingStateListener::onFailure)); + error -> { + if (error instanceof ResourceNotFoundException) { + // The task has been stopped + } else { + reindexingStateListener.onFailure(error); + } + })); break; // The task has fully reindexed the documents and we should continue on with our analyses case ANALYZING: @@ -221,7 +228,13 @@ private void startAnalytics(DataFrameAnalyticsTask task, DataFrameAnalyticsConfi task.markAsCompleted(); } }), - error -> task.updateState(DataFrameAnalyticsState.FAILED, error.getMessage()) + error -> { + if (error instanceof ResourceNotFoundException) { + // Task has stopped + } else { + task.updateState(DataFrameAnalyticsState.FAILED, error.getMessage()); + } + } )); }, error -> task.updateState(DataFrameAnalyticsState.FAILED, error.getMessage()) diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java index 1e3cbdf016af0..ebfd29bed88c6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java @@ -96,7 +96,12 @@ protected void onCancelled() { @Override public void markAsCompleted() { - persistProgress(() -> super.markAsCompleted()); + // It is possible that the stop API has been called in the meantime and that + // may also cause this method to be called. We check whether we have already + // been marked completed to avoid doing it twice. + if (isCompleted() == false) { + persistProgress(() -> super.markAsCompleted()); + } } @Override