Skip to content

Commit df4e911

Browse files
committed
Protect against project deletion
1 parent 33e8ee1 commit df4e911

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ public void waitForPersistentTaskCondition(
188188
*
189189
* @param projectId the project ID
190190
* @param taskId the persistent task id
191-
* @param predicate the persistent task predicate to evaluate
191+
* @param predicate the persistent task predicate to evaluate, must be able to handle {@code null} input which means either the project
192+
* does not exist or persistent tasks for the project do not exist
192193
* @param timeout a timeout for waiting
193194
* @param listener the callback listener
194195
*/
@@ -202,7 +203,8 @@ public void waitForPersistentTaskCondition(
202203
ClusterStateObserver.waitForState(clusterService, threadPool.getThreadContext(), new ClusterStateObserver.Listener() {
203204
@Override
204205
public void onNewClusterState(ClusterState state) {
205-
listener.onResponse(PersistentTasksCustomMetadata.getTaskWithId(state.metadata().getProject(projectId), taskId));
206+
final var project = state.metadata().projects().get(projectId);
207+
listener.onResponse(project == null ? null : PersistentTasksCustomMetadata.getTaskWithId(project, taskId));
206208
}
207209

208210
@Override
@@ -214,13 +216,15 @@ public void onClusterServiceClose() {
214216
public void onTimeout(TimeValue timeout) {
215217
listener.onTimeout(timeout);
216218
}
217-
},
218-
clusterState -> predicate.test(
219-
PersistentTasksCustomMetadata.getTaskWithId(clusterState.metadata().getProject(projectId), taskId)
220-
),
221-
timeout,
222-
logger
223-
);
219+
}, clusterState -> {
220+
final var project = clusterState.metadata().projects().get(projectId);
221+
if (project == null) {
222+
logger.debug("project [{}] not found while waiting for persistent task [{}] to pass predicate", projectId, taskId);
223+
return predicate.test(null);
224+
} else {
225+
return predicate.test(PersistentTasksCustomMetadata.getTaskWithId(project, taskId));
226+
}
227+
}, timeout, logger);
224228
}
225229

226230
// visible for testing

0 commit comments

Comments
 (0)