Search in sources :

Example 11 with ListTasksResponse

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

use of org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse 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)

Example 13 with ListTasksResponse

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

the class TransportTasksActionTests method testFindChildTasks.

public void testFindChildTasks() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch checkLatch = new CountDownLatch(1);
    ActionFuture<NodesResponse> future = startBlockingTestNodesAction(checkLatch);
    TestNode testNode = testNodes[randomIntBetween(0, testNodes.length - 1)];
    // Get the parent task
    ListTasksRequest listTasksRequest = new ListTasksRequest();
    listTasksRequest.setActions("testAction");
    ListTasksResponse response = testNode.transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(1, response.getTasks().size());
    String parentNode = response.getTasks().get(0).getTaskId().getNodeId();
    long parentTaskId = response.getTasks().get(0).getId();
    // Find tasks with common parent
    listTasksRequest = new ListTasksRequest();
    listTasksRequest.setParentTaskId(new TaskId(parentNode, parentTaskId));
    response = testNode.transportListTasksAction.execute(listTasksRequest).get();
    assertEquals(testNodes.length, response.getTasks().size());
    for (TaskInfo task : response.getTasks()) {
        assertEquals("testAction[n]", task.getAction());
        assertEquals(parentNode, task.getParentTaskId().getNodeId());
        assertEquals(parentTaskId, task.getParentTaskId().getId());
    }
    // Release all tasks and wait for response
    checkLatch.countDown();
    NodesResponse responses = future.get();
    assertEquals(0, responses.failureCount());
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 14 with ListTasksResponse

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

the class TasksIT method testTasksWaitForAllTask.

public void testTasksWaitForAllTask() throws Exception {
    // Spin up a request to wait for all tasks in the cluster to make sure it doesn't cause an infinite loop
    ListTasksResponse response = client().admin().cluster().prepareListTasks().setWaitForCompletion(true).setTimeout(timeValueSeconds(10)).get();
    // It should finish quickly and without complaint and list the list tasks themselves
    assertThat(response.getNodeFailures(), emptyCollectionOf(FailedNodeException.class));
    assertThat(response.getTaskFailures(), emptyCollectionOf(TaskOperationFailure.class));
    assertThat(response.getTasks().size(), greaterThanOrEqualTo(1));
}
Also used : TaskOperationFailure(org.elasticsearch.action.TaskOperationFailure) FailedNodeException(org.elasticsearch.action.FailedNodeException) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 15 with ListTasksResponse

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

the class TasksIT method testCanFetchIndexStatus.

/**
     * Very basic "is it plugged in" style test that indexes a document and makes sure that you can fetch the status of the process. The
     * goal here is to verify that the large moving parts that make fetching task status work fit together rather than to verify any
     * particular status results from indexing. For that, look at {@link TransportReplicationActionTests}. We intentionally don't use the
     * task recording mechanism used in other places in this test so we can make sure that the status fetching works properly over the wire.
     */
public void testCanFetchIndexStatus() throws Exception {
    // First latch waits for the task to start, second on blocks it from finishing.
    CountDownLatch taskRegistered = new CountDownLatch(1);
    CountDownLatch letTaskFinish = new CountDownLatch(1);
    Thread index = null;
    try {
        for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
            ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() {

                @Override
                public void onTaskRegistered(Task task) {
                    if (task.getAction().startsWith(IndexAction.NAME)) {
                        taskRegistered.countDown();
                        logger.debug("Blocking [{}] starting", task);
                        try {
                            assertTrue(letTaskFinish.await(10, TimeUnit.SECONDS));
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }

                @Override
                public void onTaskUnregistered(Task task) {
                }

                @Override
                public void waitForTaskCompletion(Task task) {
                }
            });
        }
        // Need to run the task in a separate thread because node client's .execute() is blocked by our task listener
        index = new Thread(() -> {
            IndexResponse indexResponse = client().prepareIndex("test", "test").setSource("test", "test").get();
            assertArrayEquals(ReplicationResponse.EMPTY, indexResponse.getShardInfo().getFailures());
        });
        index.start();
        // waiting for at least one task to be registered
        assertTrue(taskRegistered.await(10, TimeUnit.SECONDS));
        ListTasksResponse listResponse = client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*").setDetailed(true).get();
        assertThat(listResponse.getTasks(), not(empty()));
        for (TaskInfo task : listResponse.getTasks()) {
            assertNotNull(task.getStatus());
            GetTaskResponse getResponse = client().admin().cluster().prepareGetTask(task.getTaskId()).get();
            assertFalse("task should still be running", getResponse.getTask().isCompleted());
            TaskInfo fetchedWithGet = getResponse.getTask().getTask();
            assertEquals(task.getId(), fetchedWithGet.getId());
            assertEquals(task.getType(), fetchedWithGet.getType());
            assertEquals(task.getAction(), fetchedWithGet.getAction());
            assertEquals(task.getDescription(), fetchedWithGet.getDescription());
            assertEquals(task.getStatus(), fetchedWithGet.getStatus());
            assertEquals(task.getStartTime(), fetchedWithGet.getStartTime());
            assertThat(fetchedWithGet.getRunningTimeNanos(), greaterThanOrEqualTo(task.getRunningTimeNanos()));
            assertEquals(task.isCancellable(), fetchedWithGet.isCancellable());
            assertEquals(task.getParentTaskId(), fetchedWithGet.getParentTaskId());
        }
    } finally {
        letTaskFinish.countDown();
        if (index != null) {
            index.join();
        }
        assertBusy(() -> {
            assertEquals(emptyList(), client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*").get().getTasks());
        });
    }
}
Also used : Task(org.elasticsearch.tasks.Task) MockTaskManagerListener(org.elasticsearch.test.tasks.MockTaskManagerListener) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) MockTaskManager(org.elasticsearch.test.tasks.MockTaskManager) TaskInfo(org.elasticsearch.tasks.TaskInfo) SearchTransportService(org.elasticsearch.action.search.SearchTransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Aggregations

ListTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)25 TaskInfo (org.elasticsearch.tasks.TaskInfo)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ListTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)9 TaskId (org.elasticsearch.tasks.TaskId)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 ExecutionException (java.util.concurrent.ExecutionException)6 Task (org.elasticsearch.tasks.Task)6 List (java.util.List)5 CancelTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)5 Map (java.util.Map)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 FailedNodeException (org.elasticsearch.action.FailedNodeException)4 CancelTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)4 BulkByScrollTask (org.elasticsearch.action.bulk.byscroll.BulkByScrollTask)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 TaskGroup (org.elasticsearch.action.admin.cluster.node.tasks.list.TaskGroup)3 BulkByScrollResponse (org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse)3 CancellableTask (org.elasticsearch.tasks.CancellableTask)3