Search in sources :

Example 11 with ListTasksRequest

use of org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest in project elasticsearch by elastic.

the class CancellableTasksTests method testBasicTaskCancellation.

public void testBasicTaskCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch responseLatch = new CountDownLatch(1);
    boolean waitForActionToStart = randomBoolean();
    logger.info("waitForActionToStart is set to {}", waitForActionToStart);
    final AtomicReference<NodesResponse> responseReference = new AtomicReference<>();
    final AtomicReference<Throwable> throwableReference = new AtomicReference<>();
    int blockedNodesCount = randomIntBetween(0, nodesCount);
    Task mainTask = startCancellableTestNodesAction(waitForActionToStart, blockedNodesCount, new ActionListener<NodesResponse>() {

        @Override
        public void onResponse(NodesResponse listTasksResponse) {
            responseReference.set(listTasksResponse);
            responseLatch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            throwableReference.set(e);
            responseLatch.countDown();
        }
    });
    // Cancel main task
    CancelTasksRequest request = new CancelTasksRequest();
    request.setReason("Testing Cancellation");
    request.setTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()));
    // And send the cancellation request to a random node
    CancelTasksResponse response = testNodes[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction.execute(request).get();
    // Awaiting for the main task to finish
    responseLatch.await();
    if (response.getTasks().size() == 0) {
        // We didn't cancel the request and it finished successfully
        // That should be rare and can be only in case we didn't block on a single node
        assertEquals(0, blockedNodesCount);
        // Make sure that the request was successful
        assertNull(throwableReference.get());
        assertNotNull(responseReference.get());
        assertEquals(nodesCount, responseReference.get().getNodes().size());
        assertEquals(0, responseReference.get().failureCount());
    } else {
        // We canceled the request, in this case it should have fail, but we should get partial response
        assertNull(throwableReference.get());
        assertEquals(nodesCount, responseReference.get().failureCount() + responseReference.get().getNodes().size());
        // and we should have at least as many failures as the number of blocked operations
        // (we might have cancelled some non-blocked operations before they even started and that's ok)
        assertThat(responseReference.get().failureCount(), greaterThanOrEqualTo(blockedNodesCount));
        // We should have the information about the cancelled task in the cancel operation response
        assertEquals(1, response.getTasks().size());
        assertEquals(mainTask.getId(), response.getTasks().get(0).getId());
        // Verify that all cancelled tasks reported that they support cancellation
        for (TaskInfo taskInfo : response.getTasks()) {
            assertTrue(taskInfo.isCancellable());
        }
    }
    // Make sure that tasks are no longer running
    ListTasksResponse listTasksResponse = testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction.execute(new ListTasksRequest().setTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()))).get();
    assertEquals(0, listTasksResponse.getTasks().size());
    // Make sure that there are no leftover bans, the ban removal is async, so we might return from the cancellation
    // while the ban is still there, but it should disappear shortly
    assertBusy(() -> {
        for (int i = 0; i < testNodes.length; i++) {
            assertEquals("No bans on the node " + i, 0, testNodes[i].transportService.getTaskManager().getBanCount());
        }
    });
}
Also used : CancellableTask(org.elasticsearch.tasks.CancellableTask) Task(org.elasticsearch.tasks.Task) TaskId(org.elasticsearch.tasks.TaskId) CancelTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) CancelTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TaskCancelledException(org.elasticsearch.tasks.TaskCancelledException) TaskInfo(org.elasticsearch.tasks.TaskInfo) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)

Aggregations

ListTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)11 ListTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 TaskId (org.elasticsearch.tasks.TaskId)7 IOException (java.io.IOException)6 ExecutionException (java.util.concurrent.ExecutionException)5 Task (org.elasticsearch.tasks.Task)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 CancelTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)4 CancelTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 CancellableTask (org.elasticsearch.tasks.CancellableTask)3 TaskCancelledException (org.elasticsearch.tasks.TaskCancelledException)3 TaskInfo (org.elasticsearch.tasks.TaskInfo)3 FailedNodeException (org.elasticsearch.action.FailedNodeException)2 TimeValue (org.elasticsearch.common.unit.TimeValue)2 Supplier (java.util.function.Supplier)1