Search in sources :

Example 1 with CancelTasksResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse in project elasticsearch by elastic.

the class SearchCancellationIT method cancelSearch.

private void cancelSearch(String action) {
    ListTasksResponse listTasksResponse = client().admin().cluster().prepareListTasks().setActions(action).get();
    assertThat(listTasksResponse.getTasks(), hasSize(1));
    TaskInfo searchTask = listTasksResponse.getTasks().get(0);
    logger.info("Cancelling search");
    CancelTasksResponse cancelTasksResponse = client().admin().cluster().prepareCancelTasks().setTaskId(searchTask.getTaskId()).get();
    assertThat(cancelTasksResponse.getTasks(), hasSize(1));
    assertThat(cancelTasksResponse.getTasks().get(0).getTaskId(), equalTo(searchTask.getTaskId()));
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) CancelTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 2 with CancelTasksResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse in project elasticsearch by elastic.

the class CancellableTasksTests method testTaskCancellationOnCoordinatingNodeLeavingTheCluster.

public void testTaskCancellationOnCoordinatingNodeLeavingTheCluster() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch responseLatch = new CountDownLatch(1);
    boolean simulateBanBeforeLeaving = randomBoolean();
    final AtomicReference<NodesResponse> responseReference = new AtomicReference<>();
    final AtomicReference<Throwable> throwableReference = new AtomicReference<>();
    int blockedNodesCount = randomIntBetween(0, nodesCount - 1);
    // We shouldn't block on the first node since it's leaving the cluster anyway so it doesn't matter
    List<TestNode> blockOnNodes = randomSubsetOf(blockedNodesCount, Arrays.copyOfRange(testNodes, 1, nodesCount));
    Task mainTask = startCancellableTestNodesAction(true, blockOnNodes, new CancellableNodesRequest("Test Request"), 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();
        }
    });
    String mainNode = testNodes[0].getNodeId();
    // Make sure that tasks are running
    ListTasksResponse listTasksResponse = testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction.execute(new ListTasksRequest().setParentTaskId(new TaskId(mainNode, mainTask.getId()))).get();
    assertThat(listTasksResponse.getTasks().size(), greaterThanOrEqualTo(blockOnNodes.size()));
    // Simulate the coordinating node leaving the cluster
    DiscoveryNode[] discoveryNodes = new DiscoveryNode[testNodes.length - 1];
    for (int i = 1; i < testNodes.length; i++) {
        discoveryNodes[i - 1] = testNodes[i].discoveryNode();
    }
    DiscoveryNode master = discoveryNodes[0];
    for (int i = 1; i < testNodes.length; i++) {
        // Notify only nodes that should remain in the cluster
        setState(testNodes[i].clusterService, ClusterStateCreationUtils.state(testNodes[i].discoveryNode(), master, discoveryNodes));
    }
    if (simulateBanBeforeLeaving) {
        logger.info("--> Simulate issuing cancel request on the node that is about to leave the cluster");
        // Simulate issuing cancel request on the node that is about to leave the cluster
        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[0].transportCancelTasksAction.execute(request).get();
        logger.info("--> Done simulating issuing cancel request on the node that is about to leave the cluster");
        // This node still thinks that's part of the cluster, so cancelling should look successful
        if (response.getTasks().size() == 0) {
            logger.error("!!!!");
        }
        assertThat(response.getTasks().size(), lessThanOrEqualTo(1));
        assertThat(response.getTaskFailures().size(), lessThanOrEqualTo(1));
        assertThat(response.getTaskFailures().size() + response.getTasks().size(), lessThanOrEqualTo(1));
    }
    for (int i = 1; i < testNodes.length; i++) {
        assertEquals("No bans on the node " + i, 0, testNodes[i].transportService.getTaskManager().getBanCount());
    }
    // Close the first node
    testNodes[0].close();
    assertBusy(() -> {
        // Make sure that tasks are no longer running
        try {
            ListTasksResponse listTasksResponse1 = testNodes[randomIntBetween(1, testNodes.length - 1)].transportListTasksAction.execute(new ListTasksRequest().setTaskId(new TaskId(mainNode, mainTask.getId()))).get();
            assertEquals(0, listTasksResponse1.getTasks().size());
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        } catch (ExecutionException ex2) {
            fail("shouldn't be here");
        }
    });
    // Wait for clean up
    responseLatch.await();
}
Also used : CancellableTask(org.elasticsearch.tasks.CancellableTask) Task(org.elasticsearch.tasks.Task) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) 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) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) ExecutionException(java.util.concurrent.ExecutionException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TaskCancelledException(org.elasticsearch.tasks.TaskCancelledException) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)

Example 3 with CancelTasksResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse in project elasticsearch by elastic.

the class TransportTasksActionTests method testCancellingTasksThatDontSupportCancellation.

public void testCancellingTasksThatDontSupportCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch checkLatch = new CountDownLatch(1);
    CountDownLatch responseLatch = new CountDownLatch(1);
    Task task = startBlockingTestNodesAction(checkLatch, new ActionListener<NodesResponse>() {

        @Override
        public void onResponse(NodesResponse nodeResponses) {
            responseLatch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            responseLatch.countDown();
        }
    });
    // only pick the main action
    String actionName = "testAction";
    // Try to cancel main task using action name
    CancelTasksRequest request = new CancelTasksRequest();
    request.setNodes(testNodes[0].getNodeId());
    request.setReason("Testing Cancellation");
    request.setActions(actionName);
    CancelTasksResponse response = testNodes[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction.execute(request).get();
    // Shouldn't match any tasks since testAction doesn't support cancellation
    assertEquals(0, response.getTasks().size());
    assertEquals(0, response.getTaskFailures().size());
    assertEquals(0, response.getNodeFailures().size());
    // Try to cancel main task using id
    request = new CancelTasksRequest();
    request.setReason("Testing Cancellation");
    request.setTaskId(new TaskId(testNodes[0].getNodeId(), task.getId()));
    response = testNodes[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction.execute(request).get();
    // Shouldn't match any tasks since testAction doesn't support cancellation
    assertEquals(0, response.getTasks().size());
    assertEquals(0, response.getTaskFailures().size());
    assertEquals(1, response.getNodeFailures().size());
    assertThat(response.getNodeFailures().get(0).getDetailedMessage(), containsString("doesn't support cancellation"));
    // Make sure that task is still running
    ListTasksRequest listTasksRequest = new ListTasksRequest();
    listTasksRequest.setActions(actionName);
    ListTasksResponse listResponse = testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(1, listResponse.getPerNodeTasks().size());
    // Verify that tasks are marked as non-cancellable
    for (TaskInfo taskInfo : listResponse.getTasks()) {
        assertFalse(taskInfo.isCancellable());
    }
    // Release all tasks and wait for response
    checkLatch.countDown();
    responseLatch.await(10, TimeUnit.SECONDS);
}
Also used : 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) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) FailedNodeException(org.elasticsearch.action.FailedNodeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TaskInfo(org.elasticsearch.tasks.TaskInfo) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)

Example 4 with CancelTasksResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse in project elasticsearch by elastic.

the class CancellableTasksTests method testChildTasksCancellation.

public void testChildTasksCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch responseLatch = new CountDownLatch(1);
    final AtomicReference<NodesResponse> responseReference = new AtomicReference<>();
    final AtomicReference<Throwable> throwableReference = new AtomicReference<>();
    Task mainTask = startCancellableTestNodesAction(true, nodesCount, 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 all child tasks without cancelling the main task, which should quit on its own
    CancelTasksRequest request = new CancelTasksRequest();
    request.setReason("Testing Cancellation");
    request.setParentTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()));
    // And send the cancellation request to a random node
    CancelTasksResponse response = testNodes[randomIntBetween(1, testNodes.length - 1)].transportCancelTasksAction.execute(request).get();
    // Awaiting for the main task to finish
    responseLatch.await();
    // Should have cancelled tasks on all nodes
    assertThat(response.getTasks().size(), equalTo(testNodes.length));
    assertBusy(() -> {
        try {
            // Make sure that main task is 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());
        } catch (ExecutionException | InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    });
}
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) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with CancelTasksResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse 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

CancelTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)6 ListTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)5 IOException (java.io.IOException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExecutionException (java.util.concurrent.ExecutionException)4 CancelTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)4 ListTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)4 Task (org.elasticsearch.tasks.Task)4 TaskId (org.elasticsearch.tasks.TaskId)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CancellableTask (org.elasticsearch.tasks.CancellableTask)3 TaskCancelledException (org.elasticsearch.tasks.TaskCancelledException)3 TaskInfo (org.elasticsearch.tasks.TaskInfo)3 FailedNodeException (org.elasticsearch.action.FailedNodeException)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1