Skip to content

Commit 82f4d57

Browse files
authored
Use TaskCancelledException in TMNA (#86659)
In #72157 we made it so that cancelling the task that a `TransportMasterNodeAction` was executing causes the action to fail with a `java.util.concurrent.CancellationException`. This exception is treated as a `500 Internal Server Error` which results in `WARN`-level logs, but cancelling a task is normal and expected behaviour and should not be logged (see #73524). This commit fixes this by using a `TaskCancelledException` instead, since this exception maps to a `400 Bad Request` and generates no logs.
1 parent 48e70db commit 82f4d57

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

docs/changelog/86659.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 86659
2+
summary: Use `TaskCancelledException` in TMNA
3+
area: Task Management
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/action/support/master/TransportMasterNodeAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
import org.elasticsearch.node.NodeClosedException;
3737
import org.elasticsearch.tasks.CancellableTask;
3838
import org.elasticsearch.tasks.Task;
39+
import org.elasticsearch.tasks.TaskCancelledException;
3940
import org.elasticsearch.threadpool.ThreadPool;
4041
import org.elasticsearch.transport.ConnectTransportException;
4142
import org.elasticsearch.transport.RemoteTransportException;
4243
import org.elasticsearch.transport.TransportException;
4344
import org.elasticsearch.transport.TransportService;
4445

45-
import java.util.concurrent.CancellationException;
4646
import java.util.function.Predicate;
4747

4848
/**
@@ -114,7 +114,7 @@ protected abstract void masterOperation(Task task, Request request, ClusterState
114114
private void executeMasterOperation(Task task, Request request, ClusterState state, ActionListener<Response> listener)
115115
throws Exception {
116116
if (task instanceof CancellableTask && ((CancellableTask) task).isCancelled()) {
117-
throw new CancellationException("Task was cancelled");
117+
throw new TaskCancelledException("Task was cancelled");
118118
}
119119

120120
masterOperation(task, request, state, listener);
@@ -168,7 +168,7 @@ class AsyncSingleAction {
168168

169169
protected void doStart(ClusterState clusterState) {
170170
if (isTaskCancelled()) {
171-
listener.onFailure(new CancellationException("Task was cancelled"));
171+
listener.onFailure(new TaskCancelledException("Task was cancelled"));
172172
return;
173173
}
174174
try {

server/src/test/java/org/elasticsearch/action/support/master/TransportMasterNodeActionTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.elasticsearch.rest.RestStatus;
4747
import org.elasticsearch.tasks.CancellableTask;
4848
import org.elasticsearch.tasks.Task;
49+
import org.elasticsearch.tasks.TaskCancelledException;
4950
import org.elasticsearch.tasks.TaskId;
5051
import org.elasticsearch.tasks.TaskManager;
5152
import org.elasticsearch.test.ESTestCase;
@@ -65,7 +66,6 @@
6566
import java.util.Map;
6667
import java.util.Objects;
6768
import java.util.Set;
68-
import java.util.concurrent.CancellationException;
6969
import java.util.concurrent.CountDownLatch;
7070
import java.util.concurrent.CyclicBarrier;
7171
import java.util.concurrent.ExecutionException;
@@ -594,7 +594,7 @@ protected ClusterBlockException checkBlock(Request request, ClusterState state)
594594
}
595595
setState(clusterService, newStateBuilder.build());
596596
}
597-
expectThrows(CancellationException.class, listener::actionGet);
597+
expectThrows(TaskCancelledException.class, listener::actionGet);
598598
}
599599

600600
public void testTaskCancellationOnceActionItIsDispatchedToMaster() throws Exception {
@@ -621,7 +621,7 @@ public void testTaskCancellationOnceActionItIsDispatchedToMaster() throws Except
621621

622622
releaseBlockedThreads.run();
623623

624-
expectThrows(CancellationException.class, listener::actionGet);
624+
expectThrows(TaskCancelledException.class, listener::actionGet);
625625
}
626626

627627
public void testGlobalBlocksAreCheckedAfterIndexNotFoundException() throws Exception {

0 commit comments

Comments
 (0)