|
51 | 51 | import java.util.concurrent.atomic.AtomicReference; |
52 | 52 |
|
53 | 53 | import static org.elasticsearch.test.ClusterServiceUtils.setState; |
| 54 | +import static org.hamcrest.Matchers.equalTo; |
54 | 55 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
55 | 56 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
56 | 57 |
|
@@ -299,6 +300,54 @@ public void onFailure(Exception e) { |
299 | 300 | }); |
300 | 301 | } |
301 | 302 |
|
| 303 | + public void testChildTasksCancellation() throws Exception { |
| 304 | + setupTestNodes(Settings.EMPTY); |
| 305 | + connectNodes(testNodes); |
| 306 | + CountDownLatch responseLatch = new CountDownLatch(1); |
| 307 | + final AtomicReference<NodesResponse> responseReference = new AtomicReference<>(); |
| 308 | + final AtomicReference<Throwable> throwableReference = new AtomicReference<>(); |
| 309 | + Task mainTask = startCancellableTestNodesAction(true, nodesCount, new ActionListener<NodesResponse>() { |
| 310 | + @Override |
| 311 | + public void onResponse(NodesResponse listTasksResponse) { |
| 312 | + responseReference.set(listTasksResponse); |
| 313 | + responseLatch.countDown(); |
| 314 | + } |
| 315 | + |
| 316 | + @Override |
| 317 | + public void onFailure(Exception e) { |
| 318 | + throwableReference.set(e); |
| 319 | + responseLatch.countDown(); |
| 320 | + } |
| 321 | + }); |
| 322 | + |
| 323 | + // Cancel all child tasks without cancelling the main task, which should quit on its own |
| 324 | + CancelTasksRequest request = new CancelTasksRequest(); |
| 325 | + request.setReason("Testing Cancellation"); |
| 326 | + request.setParentTaskId(new TaskId(testNodes[0].discoveryNode.getId(), mainTask.getId())); |
| 327 | + // And send the cancellation request to a random node |
| 328 | + CancelTasksResponse response = testNodes[randomIntBetween(1, testNodes.length - 1)].transportCancelTasksAction.execute(request) |
| 329 | + .get(); |
| 330 | + |
| 331 | + // Awaiting for the main task to finish |
| 332 | + responseLatch.await(); |
| 333 | + |
| 334 | + // Should have cancelled tasks on all nodes |
| 335 | + assertThat(response.getTasks().size(), equalTo(testNodes.length)); |
| 336 | + |
| 337 | + assertBusy(() -> { |
| 338 | + try { |
| 339 | + // Make sure that main task is no longer running |
| 340 | + ListTasksResponse listTasksResponse = testNodes[randomIntBetween(0, testNodes.length - 1)] |
| 341 | + .transportListTasksAction.execute(new ListTasksRequest().setTaskId( |
| 342 | + new TaskId(testNodes[0].discoveryNode.getId(), mainTask.getId()))).get(); |
| 343 | + assertEquals(0, listTasksResponse.getTasks().size()); |
| 344 | + |
| 345 | + } catch (ExecutionException | InterruptedException ex) { |
| 346 | + throw new RuntimeException(ex); |
| 347 | + } |
| 348 | + }); |
| 349 | + } |
| 350 | + |
302 | 351 | public void testTaskCancellationOnCoordinatingNodeLeavingTheCluster() throws Exception { |
303 | 352 | setupTestNodes(Settings.EMPTY); |
304 | 353 | connectNodes(testNodes); |
|
0 commit comments