Search in sources :

Example 6 with TaskInfo

use of org.elasticsearch.tasks.TaskInfo 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 7 with TaskInfo

use of org.elasticsearch.tasks.TaskInfo in project elasticsearch by elastic.

the class TransportGetTaskAction method getRunningTaskFromNode.

/**
     * Executed on the node that should be running the task to find and return the running task. Falls back to
     * {@link #getFinishedTaskFromIndex(Task, GetTaskRequest, ActionListener)} if the task isn't still running.
     */
void getRunningTaskFromNode(Task thisTask, GetTaskRequest request, ActionListener<GetTaskResponse> listener) {
    Task runningTask = taskManager.getTask(request.getTaskId().getId());
    if (runningTask == null) {
        // Task isn't running, go look in the task index
        getFinishedTaskFromIndex(thisTask, request, listener);
    } else {
        if (request.getWaitForCompletion()) {
            // Shift to the generic thread pool and let it wait for the task to complete so we don't block any important threads.
            threadPool.generic().execute(new AbstractRunnable() {

                @Override
                protected void doRun() throws Exception {
                    taskManager.waitForTaskCompletion(runningTask, waitForCompletionTimeout(request.getTimeout()));
                    waitedForCompletion(thisTask, request, runningTask.taskInfo(clusterService.localNode().getId(), true), listener);
                }

                @Override
                public void onFailure(Exception e) {
                    listener.onFailure(e);
                }
            });
        } else {
            TaskInfo info = runningTask.taskInfo(clusterService.localNode().getId(), true);
            listener.onResponse(new GetTaskResponse(new TaskResult(false, info)));
        }
    }
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) TaskInfo(org.elasticsearch.tasks.TaskInfo) Task(org.elasticsearch.tasks.Task) TaskResult(org.elasticsearch.tasks.TaskResult) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException)

Example 8 with TaskInfo

use of org.elasticsearch.tasks.TaskInfo in project elasticsearch by elastic.

the class ListTasksResponse method toXContentGroupedByNode.

/**
     * Convert this task response to XContent grouping by executing nodes.
     */
public XContentBuilder toXContentGroupedByNode(XContentBuilder builder, Params params, DiscoveryNodes discoveryNodes) throws IOException {
    toXContentCommon(builder, params);
    builder.startObject("nodes");
    for (Map.Entry<String, List<TaskInfo>> entry : getPerNodeTasks().entrySet()) {
        DiscoveryNode node = discoveryNodes.get(entry.getKey());
        builder.startObject(entry.getKey());
        if (node != null) {
            // If the node is no longer part of the cluster, oh well, we'll just skip it's useful information.
            builder.field("name", node.getName());
            builder.field("transport_address", node.getAddress().toString());
            builder.field("host", node.getHostName());
            builder.field("ip", node.getAddress());
            builder.startArray("roles");
            for (DiscoveryNode.Role role : node.getRoles()) {
                builder.value(role.getRoleName());
            }
            builder.endArray();
            if (!node.getAttributes().isEmpty()) {
                builder.startObject("attributes");
                for (Map.Entry<String, String> attrEntry : node.getAttributes().entrySet()) {
                    builder.field(attrEntry.getKey(), attrEntry.getValue());
                }
                builder.endObject();
            }
        }
        builder.startObject("tasks");
        for (TaskInfo task : entry.getValue()) {
            builder.startObject(task.getTaskId().toString());
            task.toXContent(builder, params);
            builder.endObject();
        }
        builder.endObject();
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with TaskInfo

use of org.elasticsearch.tasks.TaskInfo in project elasticsearch by elastic.

the class ParentBulkByScrollTask method getInfoGivenSliceInfo.

@Override
public TaskInfo getInfoGivenSliceInfo(String localNodeId, List<TaskInfo> sliceInfo) {
    /* Merge the list of finished sub requests with the provided info. If a slice is both finished and in the list then we prefer the
         * finished status because we don't expect them to change after the task is finished. */
    List<StatusOrException> sliceStatuses = Arrays.asList(new StatusOrException[results.length()]);
    for (TaskInfo t : sliceInfo) {
        Status status = (Status) t.getStatus();
        sliceStatuses.set(status.getSliceId(), new StatusOrException(status));
    }
    addResultsToList(sliceStatuses);
    Status status = new Status(sliceStatuses, getReasonCancelled());
    return taskInfo(localNodeId, getDescription(), status);
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo)

Example 10 with TaskInfo

use of org.elasticsearch.tasks.TaskInfo in project elasticsearch by elastic.

the class ListTasksResponse method buildTaskGroups.

private void buildTaskGroups() {
    Map<TaskId, TaskGroup.Builder> taskGroups = new HashMap<>();
    List<TaskGroup.Builder> topLevelTasks = new ArrayList<>();
    // First populate all tasks
    for (TaskInfo taskInfo : this.tasks) {
        taskGroups.put(taskInfo.getTaskId(), TaskGroup.builder(taskInfo));
    }
    // Now go through all task group builders and add children to their parents
    for (TaskGroup.Builder taskGroup : taskGroups.values()) {
        TaskId parentTaskId = taskGroup.getTaskInfo().getParentTaskId();
        if (parentTaskId.isSet()) {
            TaskGroup.Builder parentTask = taskGroups.get(parentTaskId);
            if (parentTask != null) {
                // we found parent in the list of tasks - add it to the parent list
                parentTask.addGroup(taskGroup);
            } else {
                // we got zombie or the parent was filtered out - add it to the the top task list
                topLevelTasks.add(taskGroup);
            }
        } else {
            // top level task - add it to the top task list
            topLevelTasks.add(taskGroup);
        }
    }
    this.groups = Collections.unmodifiableList(topLevelTasks.stream().map(TaskGroup.Builder::build).collect(Collectors.toList()));
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) HashMap(java.util.HashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ArrayList(java.util.ArrayList)

Aggregations

TaskInfo (org.elasticsearch.tasks.TaskInfo)25 TaskId (org.elasticsearch.tasks.TaskId)13 ListTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)12 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 GetTaskResponse (org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse)6 Map (java.util.Map)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Tuple (org.elasticsearch.common.collect.Tuple)5 Task (org.elasticsearch.tasks.Task)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 List (java.util.List)4 CancelTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)4 BulkByScrollTask (org.elasticsearch.action.bulk.byscroll.BulkByScrollTask)4 ExecutionException (java.util.concurrent.ExecutionException)3 ActionListener (org.elasticsearch.action.ActionListener)3 FailedNodeException (org.elasticsearch.action.FailedNodeException)3 ListTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)3 GetResponse (org.elasticsearch.action.get.GetResponse)3