Search in sources :

Example 1 with ListTasksRequest

use of org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest 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 2 with ListTasksRequest

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

the class TransportTasksActionTests method testTasksDescriptions.

public void testTasksDescriptions() throws Exception {
    long minimalStartTime = System.currentTimeMillis();
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch checkLatch = new CountDownLatch(1);
    ActionFuture<NodesResponse> future = startBlockingTestNodesAction(checkLatch);
    long maximumStartTimeNanos = System.nanoTime();
    // Check task counts using transport with filtering
    TestNode testNode = testNodes[randomIntBetween(0, testNodes.length - 1)];
    ListTasksRequest listTasksRequest = new ListTasksRequest();
    // only pick node actions
    listTasksRequest.setActions("testAction[n]");
    ListTasksResponse response = testNode.transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(testNodes.length, response.getPerNodeTasks().size());
    for (Map.Entry<String, List<TaskInfo>> entry : response.getPerNodeTasks().entrySet()) {
        assertEquals(1, entry.getValue().size());
        assertNull(entry.getValue().get(0).getDescription());
    }
    // Check task counts using transport with detailed description
    long minimalDurationNanos = System.nanoTime() - maximumStartTimeNanos;
    // same request only with detailed description
    listTasksRequest.setDetailed(true);
    response = testNode.transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(testNodes.length, response.getPerNodeTasks().size());
    for (Map.Entry<String, List<TaskInfo>> entry : response.getPerNodeTasks().entrySet()) {
        assertEquals(1, entry.getValue().size());
        assertEquals("CancellableNodeRequest[Test Request, true]", entry.getValue().get(0).getDescription());
        assertThat(entry.getValue().get(0).getStartTime(), greaterThanOrEqualTo(minimalStartTime));
        assertThat(entry.getValue().get(0).getRunningTimeNanos(), greaterThanOrEqualTo(minimalDurationNanos));
    }
    // Release all tasks and wait for response
    checkLatch.countDown();
    NodesResponse responses = future.get();
    assertEquals(0, responses.failureCount());
}
Also used : ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) Map(java.util.Map)

Example 3 with ListTasksRequest

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

the class TransportTasksActionTests method testTasksToXContentGrouping.

@SuppressWarnings("unchecked")
public void testTasksToXContentGrouping() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    // Get the parent task
    ListTasksRequest listTasksRequest = new ListTasksRequest();
    listTasksRequest.setActions(ListTasksAction.NAME + "*");
    ListTasksResponse response = testNodes[0].transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(testNodes.length + 1, response.getTasks().size());
    Map<String, Object> byNodes = serialize(response, true);
    byNodes = (Map<String, Object>) byNodes.get("nodes");
    // One element on the top level
    assertEquals(testNodes.length, byNodes.size());
    Map<String, Object> firstNode = (Map<String, Object>) byNodes.get(testNodes[0].getNodeId());
    firstNode = (Map<String, Object>) firstNode.get("tasks");
    // two tasks for the first node
    assertEquals(2, firstNode.size());
    for (int i = 1; i < testNodes.length; i++) {
        Map<String, Object> otherNode = (Map<String, Object>) byNodes.get(testNodes[i].getNodeId());
        otherNode = (Map<String, Object>) otherNode.get("tasks");
        // one tasks for the all other nodes
        assertEquals(1, otherNode.size());
    }
    // Group by parents
    Map<String, Object> byParent = serialize(response, false);
    byParent = (Map<String, Object>) byParent.get("tasks");
    // One element on the top level
    // Only one top level task
    assertEquals(1, byParent.size());
    Map<String, Object> topTask = (Map<String, Object>) byParent.values().iterator().next();
    List<Object> children = (List<Object>) topTask.get("children");
    // two tasks for the first node
    assertEquals(testNodes.length, children.size());
    for (int i = 0; i < testNodes.length; i++) {
        Map<String, Object> child = (Map<String, Object>) children.get(i);
        assertNull(child.get("children"));
    }
}
Also used : ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) Map(java.util.Map)

Example 4 with ListTasksRequest

use of org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest 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 5 with ListTasksRequest

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

the class TransportTasksActionTests method testRunningTasksCount.

public void testRunningTasksCount() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch checkLatch = new CountDownLatch(1);
    CountDownLatch responseLatch = new CountDownLatch(1);
    final AtomicReference<NodesResponse> responseReference = new AtomicReference<>();
    Task mainTask = startBlockingTestNodesAction(checkLatch, new ActionListener<NodesResponse>() {

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

        @Override
        public void onFailure(Exception e) {
            logger.warn("Couldn't get list of tasks", e);
            responseLatch.countDown();
        }
    });
    // Check task counts using taskManager
    Map<Long, Task> localTasks = testNodes[0].transportService.getTaskManager().getTasks();
    // all node tasks + 1 coordinating task
    assertEquals(2, localTasks.size());
    Task coordinatingTask = localTasks.get(Collections.min(localTasks.keySet()));
    Task subTask = localTasks.get(Collections.max(localTasks.keySet()));
    assertThat(subTask.getAction(), endsWith("[n]"));
    assertThat(coordinatingTask.getAction(), not(endsWith("[n]")));
    for (int i = 1; i < testNodes.length; i++) {
        Map<Long, Task> remoteTasks = testNodes[i].transportService.getTaskManager().getTasks();
        assertEquals(1, remoteTasks.size());
        Task remoteTask = remoteTasks.values().iterator().next();
        assertThat(remoteTask.getAction(), endsWith("[n]"));
    }
    // Check task counts using transport
    int testNodeNum = randomIntBetween(0, testNodes.length - 1);
    TestNode testNode = testNodes[testNodeNum];
    ListTasksRequest listTasksRequest = new ListTasksRequest();
    // pick all test actions
    listTasksRequest.setActions("testAction*");
    logger.info("Listing currently running tasks using node [{}]", testNodeNum);
    ListTasksResponse response = testNode.transportListTasksAction.execute(listTasksRequest).get();
    logger.info("Checking currently running tasks");
    assertEquals(testNodes.length, response.getPerNodeTasks().size());
    // Coordinating node
    assertEquals(2, response.getPerNodeTasks().get(testNodes[0].getNodeId()).size());
    // Other nodes node
    for (int i = 1; i < testNodes.length; i++) {
        assertEquals(1, response.getPerNodeTasks().get(testNodes[i].getNodeId()).size());
    }
    // There should be a single main task when grouped by tasks
    assertEquals(1, response.getTaskGroups().size());
    // And as many child tasks as we have nodes
    assertEquals(testNodes.length, response.getTaskGroups().get(0).getChildTasks().size());
    // Check task counts using transport with filtering
    testNode = testNodes[randomIntBetween(0, testNodes.length - 1)];
    listTasksRequest = new ListTasksRequest();
    // only pick node actions
    listTasksRequest.setActions("testAction[n]");
    response = testNode.transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(testNodes.length, response.getPerNodeTasks().size());
    for (Map.Entry<String, List<TaskInfo>> entry : response.getPerNodeTasks().entrySet()) {
        assertEquals(1, entry.getValue().size());
        assertNull(entry.getValue().get(0).getDescription());
    }
    // Since the main task is not in the list - all tasks should be by themselves
    assertEquals(testNodes.length, response.getTaskGroups().size());
    for (TaskGroup taskGroup : response.getTaskGroups()) {
        assertEquals(0, taskGroup.getChildTasks().size());
    }
    // Check task counts using transport with detailed description
    // same request only with detailed description
    listTasksRequest.setDetailed(true);
    response = testNode.transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(testNodes.length, response.getPerNodeTasks().size());
    for (Map.Entry<String, List<TaskInfo>> entry : response.getPerNodeTasks().entrySet()) {
        assertEquals(1, entry.getValue().size());
        assertEquals("CancellableNodeRequest[Test Request, true]", entry.getValue().get(0).getDescription());
    }
    // Make sure that the main task on coordinating node is the task that was returned to us by execute()
    // only pick the main task
    listTasksRequest.setActions("testAction");
    response = testNode.transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(1, response.getTasks().size());
    assertEquals(mainTask.getId(), response.getTasks().get(0).getId());
    // Release all tasks and wait for response
    checkLatch.countDown();
    assertTrue(responseLatch.await(10, TimeUnit.SECONDS));
    NodesResponse responses = responseReference.get();
    assertEquals(0, responses.failureCount());
    // Make sure that we don't have any lingering tasks
    for (TestNode node : testNodes) {
        assertEquals(0, node.transportService.getTaskManager().getTasks().size());
    }
}
Also used : Task(org.elasticsearch.tasks.Task) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) TaskGroup(org.elasticsearch.action.admin.cluster.node.tasks.list.TaskGroup)

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