Skip to content

Instantly share code, notes, and snippets.

@libbkmz
Last active July 22, 2021 16:04
Show Gist options
  • Save libbkmz/ba94a0103b151345c94d07c241903c3a to your computer and use it in GitHub Desktop.
Save libbkmz/ba94a0103b151345c94d07c241903c3a to your computer and use it in GitHub Desktop.
diff --git a/server/src/main/java/org/elasticsearch/tasks/TaskManager.java b/server/src/main/java/org/elasticsearch/tasks/TaskManager.java
index 10fb4eced77..4be7707d886 100644
--- a/server/src/main/java/org/elasticsearch/tasks/TaskManager.java
+++ b/server/src/main/java/org/elasticsearch/tasks/TaskManager.java
@@ -81,6 +81,8 @@ public class TaskManager implements ClusterStateApplier {
private final ConcurrentMapLong<CancellableTaskHolder> cancellableTasks = ConcurrentCollections
.newConcurrentMapLongWithAggressiveConcurrency();
+ private final ConcurrentMapLong<Set<CancellableTaskHolder>> cancellableChildTasks = ConcurrentCollections
+ .newConcurrentMapLongWithAggressiveConcurrency();
private final AtomicLong taskIdGenerator = new AtomicLong();
@@ -147,6 +149,19 @@ public class TaskManager implements ClusterStateApplier {
CancellableTask cancellableTask = (CancellableTask) task;
CancellableTaskHolder holder = new CancellableTaskHolder(cancellableTask);
CancellableTaskHolder oldHolder = cancellableTasks.put(task.getId(), holder);
+ TaskId parent_id = task.getParentTaskId();
+ if (parent_id.isSet())
+ {
+ if (cancellableChildTasks.containsKey(parent_id.getId()))
+ cancellableChildTasks.get(parent_id.getId()).add(holder);
+ else
+ {
+ HashSet<CancellableTaskHolder> a = new HashSet<>();
+ a.add(holder);
+ cancellableChildTasks.put(parent_id.getId(), a);
+ }
+ }
+
assert oldHolder == null;
// Check if this task was banned before we start it. The empty check is used to avoid
// computing the hash code of the parent taskId as most of the time bannedParents is empty.
@@ -188,6 +203,8 @@ public class TaskManager implements ClusterStateApplier {
logger.trace("unregister task for id: {}", task.getId());
if (task instanceof CancellableTask) {
CancellableTaskHolder holder = cancellableTasks.remove(task.getId());
+ if (task.getParentTaskId().isSet())
+ cancellableChildTasks.get(task.getParentTaskId().getId()).remove(holder);
if (holder != null) {
holder.finish();
return holder.getTask();
@@ -372,10 +389,12 @@ public class TaskManager implements ClusterStateApplier {
}
}
}
- return cancellableTasks.values().stream()
- .filter(t -> t.hasParent(parentTaskId))
- .map(t -> t.task)
- .collect(Collectors.toList());
+ if (cancellableChildTasks.containsKey(parentTaskId.getId()))
+ return cancellableChildTasks.get(parentTaskId.getId()).stream()
+ .map(t -> t.task)
+ .collect(Collectors.toList());
+ else
+ return Collections.emptyList();
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment