Skip to content

Commit e69d6c4

Browse files
committed
print reason why parent task was cancelled
Signed-off-by: kkewwei <kkewwei@163.com>
1 parent f14b5c8 commit e69d6c4

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1212
- Add batching supported processor base type AbstractBatchingProcessor ([#14554](https://github.com/opensearch-project/OpenSearch/pull/14554))
1313
- Fix race condition while parsing derived fields from search definition ([14445](https://github.com/opensearch-project/OpenSearch/pull/14445))
1414
- Add allowlist setting for ingest-common and search-pipeline-common processors ([#14439](https://github.com/opensearch-project/OpenSearch/issues/14439))
15+
- Print reason why parent task was cancelled ([#14604](https://github.com/opensearch-project/OpenSearch/issues/14604))
1516

1617
### Dependencies
1718
- Bump `org.gradle.test-retry` from 1.5.8 to 1.5.9 ([#13442](https://github.com/opensearch-project/OpenSearch/pull/13442))

server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/node/tasks/CancellableTasksIT.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@ public void testFailedToStartChildTaskAfterCancelled() throws Exception {
327327
mainAction.startSubTask(taskId, subRequest, future);
328328
TransportException te = expectThrows(TransportException.class, future::actionGet);
329329
assertThat(te.getCause(), instanceOf(TaskCancelledException.class));
330-
assertThat(te.getCause().getMessage(), equalTo("The parent task was cancelled, shouldn't start any child tasks"));
330+
assertThat(
331+
te.getCause().getMessage(),
332+
equalTo("The parent task was cancelled, shouldn't start any child tasks, by user request")
333+
);
331334
allowEntireRequest(rootRequest);
332335
waitForRootTask(rootTaskFuture);
333336
ensureAllBansRemoved();
@@ -386,7 +389,7 @@ static void waitForRootTask(ActionFuture<TestResponse> rootTask) {
386389
assertThat(
387390
cause.getMessage(),
388391
anyOf(
389-
equalTo("The parent task was cancelled, shouldn't start any child tasks"),
392+
equalTo("The parent task was cancelled, shouldn't start any child tasks, by user request"),
390393
containsString("Task cancelled before it started:"),
391394
equalTo("Task was cancelled while executing")
392395
)

server/src/main/java/org/opensearch/tasks/TaskCancellationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void cancelTaskAndDescendants(CancellableTask task, String reason, boolean waitF
9292
Collection<DiscoveryNode> childrenNodes = taskManager.startBanOnChildrenNodes(task.getId(), () -> {
9393
logger.trace("child tasks of parent [{}] are completed", taskId);
9494
groupedListener.onResponse(null);
95-
});
95+
}, reason);
9696
taskManager.cancel(task, reason, () -> {
9797
logger.trace("task [{}] is cancelled", taskId);
9898
groupedListener.onResponse(null);

server/src/main/java/org/opensearch/tasks/TaskManager.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,10 @@ public Set<TaskId> getBannedTaskIds() {
517517
* @param onChildTasksCompleted called when all child tasks are completed or failed
518518
* @return the set of current nodes that have outstanding child tasks
519519
*/
520-
public Collection<DiscoveryNode> startBanOnChildrenNodes(long taskId, Runnable onChildTasksCompleted) {
520+
public Collection<DiscoveryNode> startBanOnChildrenNodes(long taskId, Runnable onChildTasksCompleted, String reason) {
521521
final CancellableTaskHolder holder = cancellableTasks.get(taskId);
522522
if (holder != null) {
523-
return holder.startBan(onChildTasksCompleted);
523+
return holder.startBan(onChildTasksCompleted, reason);
524524
} else {
525525
onChildTasksCompleted.run();
526526
return Collections.emptySet();
@@ -585,6 +585,7 @@ private static class CancellableTaskHolder {
585585
private List<Runnable> cancellationListeners = null;
586586
private Map<DiscoveryNode, Integer> childTasksPerNode = null;
587587
private boolean banChildren = false;
588+
private String banReason;
588589
private List<Runnable> childTaskCompletedListeners = null;
589590

590591
CancellableTaskHolder(CancellableTask task) {
@@ -662,7 +663,7 @@ public CancellableTask getTask() {
662663

663664
synchronized void registerChildNode(DiscoveryNode node) {
664665
if (banChildren) {
665-
throw new TaskCancelledException("The parent task was cancelled, shouldn't start any child tasks");
666+
throw new TaskCancelledException("The parent task was cancelled, shouldn't start any child tasks, " + banReason);
666667
}
667668
if (childTasksPerNode == null) {
668669
childTasksPerNode = new HashMap<>();
@@ -686,11 +687,13 @@ void unregisterChildNode(DiscoveryNode node) {
686687
notifyListeners(listeners);
687688
}
688689

689-
Set<DiscoveryNode> startBan(Runnable onChildTasksCompleted) {
690+
Set<DiscoveryNode> startBan(Runnable onChildTasksCompleted, String reason) {
690691
final Set<DiscoveryNode> pendingChildNodes;
691692
final Runnable toRun;
692693
synchronized (this) {
693694
banChildren = true;
695+
assert reason != null;
696+
banReason = reason;
694697
if (childTasksPerNode == null) {
695698
pendingChildNodes = Collections.emptySet();
696699
} else {

server/src/test/java/org/opensearch/action/admin/cluster/node/tasks/CancellableTasksTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ public void testRegisterAndExecuteChildTaskWhileParentTaskIsBeingCanceled() thro
428428
);
429429
assertThat(cancelledException.getMessage(), startsWith("Task cancelled before it started:"));
430430
CountDownLatch latch = new CountDownLatch(1);
431-
taskManager.startBanOnChildrenNodes(parentTaskId.getId(), latch::countDown);
431+
taskManager.startBanOnChildrenNodes(parentTaskId.getId(), latch::countDown, cancelledException.getMessage());
432432
assertTrue("onChildTasksCompleted() is not invoked", latch.await(1, TimeUnit.SECONDS));
433433
}
434434

0 commit comments

Comments
 (0)