diff --git a/server/src/main/java/org/elasticsearch/persistent/PersistentTasksService.java b/server/src/main/java/org/elasticsearch/persistent/PersistentTasksService.java index f17677a6e2db3..717ee6da6863b 100644 --- a/server/src/main/java/org/elasticsearch/persistent/PersistentTasksService.java +++ b/server/src/main/java/org/elasticsearch/persistent/PersistentTasksService.java @@ -130,28 +130,32 @@ public void removeTask(long taskId, PersistentTaskOperationListener listener) { } /** - * Waits for the persistent task with giving id (taskId) to achieve the desired status. + * Checks if the persistent task with giving id (taskId) has the desired state and if it doesn't + * waits of it. */ public void waitForPersistentTaskStatus(long taskId, Predicate> predicate, @Nullable TimeValue timeout, WaitForPersistentTaskStatusListener listener) { ClusterStateObserver stateObserver = new ClusterStateObserver(clusterService, timeout, logger, threadPool.getThreadContext()); - stateObserver.waitForNextChange(new ClusterStateObserver.Listener() { - @Override - public void onNewClusterState(ClusterState state) { - listener.onResponse(taskId); - } - - @Override - public void onClusterServiceClose() { - listener.onFailure(new NodeClosedException(clusterService.localNode())); - - } - - @Override - public void onTimeout(TimeValue timeout) { - listener.onTimeout(timeout); - } - }, clusterState -> predicate.test(PersistentTasksCustomMetaData.getTaskWithId(clusterState, taskId))); + if (predicate.test(PersistentTasksCustomMetaData.getTaskWithId(stateObserver.setAndGetObservedState(), taskId))) { + listener.onResponse(taskId); + } else { + stateObserver.waitForNextChange(new ClusterStateObserver.Listener() { + @Override + public void onNewClusterState(ClusterState state) { + listener.onResponse(taskId); + } + + @Override + public void onClusterServiceClose() { + listener.onFailure(new NodeClosedException(clusterService.localNode())); + } + + @Override + public void onTimeout(TimeValue timeout) { + listener.onTimeout(timeout); + } + }, clusterState -> predicate.test(PersistentTasksCustomMetaData.getTaskWithId(clusterState, taskId))); + } } public interface WaitForPersistentTaskStatusListener extends PersistentTaskOperationListener {