Search in sources :

Example 6 with Task

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

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

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

the class TransportMasterNodeActionTests method testLocalOperationWithoutBlocks.

public void testLocalOperationWithoutBlocks() throws ExecutionException, InterruptedException {
    final boolean masterOperationFailure = randomBoolean();
    Request request = new Request();
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    final Exception exception = new Exception();
    final Response response = new Response();
    setState(clusterService, ClusterStateCreationUtils.state(localNode, localNode, allNodes));
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool) {

        @Override
        protected void masterOperation(Task task, Request request, ClusterState state, ActionListener<Response> listener) throws Exception {
            if (masterOperationFailure) {
                listener.onFailure(exception);
            } else {
                listener.onResponse(response);
            }
        }
    }.execute(request, listener);
    assertTrue(listener.isDone());
    if (masterOperationFailure) {
        try {
            listener.get();
            fail("Expected exception but returned proper result");
        } catch (ExecutionException ex) {
            assertThat(ex.getCause(), equalTo(exception));
        }
    } else {
        assertThat(listener.get(), equalTo(response));
    }
}
Also used : ActionResponse(org.elasticsearch.action.ActionResponse) ClusterState(org.elasticsearch.cluster.ClusterState) Task(org.elasticsearch.tasks.Task) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ExecutionException(java.util.concurrent.ExecutionException) ElasticsearchException(org.elasticsearch.ElasticsearchException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) NotMasterException(org.elasticsearch.cluster.NotMasterException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ExecutionException(java.util.concurrent.ExecutionException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException)

Example 9 with Task

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

the class TransportActionFilterChainTests method testTooManyContinueProcessingRequest.

public void testTooManyContinueProcessingRequest() throws ExecutionException, InterruptedException {
    final int additionalContinueCount = randomInt(10);
    RequestTestFilter testFilter = new RequestTestFilter(randomInt(), new RequestCallback() {

        @Override
        public <Request extends ActionRequest, Response extends ActionResponse> void execute(Task task, String action, Request request, ActionListener<Response> listener, ActionFilterChain<Request, Response> actionFilterChain) {
            for (int i = 0; i <= additionalContinueCount; i++) {
                actionFilterChain.proceed(task, action, request, listener);
            }
        }
    });
    Set<ActionFilter> filters = new HashSet<>();
    filters.add(testFilter);
    String actionName = randomAsciiOfLength(randomInt(30));
    ActionFilters actionFilters = new ActionFilters(filters);
    TransportAction<TestRequest, TestResponse> transportAction = new TransportAction<TestRequest, TestResponse>(Settings.EMPTY, actionName, null, actionFilters, null, new TaskManager(Settings.EMPTY)) {

        @Override
        protected void doExecute(TestRequest request, ActionListener<TestResponse> listener) {
            listener.onResponse(new TestResponse());
        }
    };
    final CountDownLatch latch = new CountDownLatch(additionalContinueCount + 1);
    final AtomicInteger responses = new AtomicInteger();
    final List<Throwable> failures = new CopyOnWriteArrayList<>();
    transportAction.execute(new TestRequest(), new ActionListener<TestResponse>() {

        @Override
        public void onResponse(TestResponse testResponse) {
            responses.incrementAndGet();
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            failures.add(e);
            latch.countDown();
        }
    });
    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("timeout waiting for the filter to notify the listener as many times as expected");
    }
    assertThat(testFilter.runs.get(), equalTo(1));
    assertThat(testFilter.lastActionName, equalTo(actionName));
    assertThat(responses.get(), equalTo(1));
    assertThat(failures.size(), equalTo(additionalContinueCount));
    for (Throwable failure : failures) {
        assertThat(failure, instanceOf(IllegalStateException.class));
    }
}
Also used : Task(org.elasticsearch.tasks.Task) HashSet(java.util.HashSet) ActionRequest(org.elasticsearch.action.ActionRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) ActionResponse(org.elasticsearch.action.ActionResponse) TaskManager(org.elasticsearch.tasks.TaskManager) ActionListener(org.elasticsearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 10 with Task

use of org.elasticsearch.tasks.Task 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)

Aggregations

Task (org.elasticsearch.tasks.Task)24 CountDownLatch (java.util.concurrent.CountDownLatch)13 IOException (java.io.IOException)10 ExecutionException (java.util.concurrent.ExecutionException)8 ActionListener (org.elasticsearch.action.ActionListener)8 ListTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 TransportService (org.elasticsearch.transport.TransportService)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 ActionFilters (org.elasticsearch.action.support.ActionFilters)6 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)6 Settings (org.elasticsearch.common.settings.Settings)6 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)5 CancelTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)5 ListTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)5 ClusterState (org.elasticsearch.cluster.ClusterState)5 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)5 ClusterService (org.elasticsearch.cluster.service.ClusterService)5 TaskId (org.elasticsearch.tasks.TaskId)5