Search in sources :

Example 16 with Task

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

the class TransportTasksActionTests method startBlockingTestNodesAction.

private Task startBlockingTestNodesAction(CountDownLatch checkLatch, NodesRequest request, ActionListener<NodesResponse> listener) throws InterruptedException {
    CountDownLatch actionLatch = new CountDownLatch(nodesCount);
    TestNodesAction[] actions = new TestNodesAction[nodesCount];
    for (int i = 0; i < testNodes.length; i++) {
        final int node = i;
        actions[i] = new TestNodesAction(CLUSTER_SETTINGS, "testAction", threadPool, testNodes[i].clusterService, testNodes[i].transportService) {

            @Override
            protected NodeResponse nodeOperation(NodeRequest request) {
                logger.info("Action on node {}", node);
                actionLatch.countDown();
                try {
                    checkLatch.await();
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
                logger.info("Action on node {} finished", node);
                return new NodeResponse(testNodes[node].discoveryNode());
            }
        };
    }
    // Make sure no tasks are running
    for (TestNode node : testNodes) {
        assertEquals(0, node.transportService.getTaskManager().getTasks().size());
    }
    Task task = actions[0].execute(request, listener);
    logger.info("Awaiting for all actions to start");
    assertTrue(actionLatch.await(10, TimeUnit.SECONDS));
    logger.info("Done waiting for all actions to start");
    return task;
}
Also used : Task(org.elasticsearch.tasks.Task) CountDownLatch(java.util.concurrent.CountDownLatch) BaseNodeRequest(org.elasticsearch.action.support.nodes.BaseNodeRequest)

Example 17 with Task

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

Example 18 with Task

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

the class TasksIT method waitForCompletionTestCase.

/**
     * Test wait for completion.
     * @param storeResult should the task store its results
     * @param wait start waiting for a task. Accepts that id of the task to wait for and returns a future waiting for it.
     * @param validator validate the response and return the task ids that were found
     */
private <T> void waitForCompletionTestCase(boolean storeResult, Function<TaskId, ListenableActionFuture<T>> wait, Consumer<T> validator) throws Exception {
    // Start blocking test task
    ListenableActionFuture<TestTaskPlugin.NodesResponse> future = TestTaskPlugin.TestTaskAction.INSTANCE.newRequestBuilder(client()).setShouldStoreResult(storeResult).execute();
    ListenableActionFuture<T> waitResponseFuture;
    TaskId taskId;
    try {
        taskId = waitForTestTaskStartOnAllNodes();
        // Wait for the task to start
        assertBusy(() -> client().admin().cluster().prepareGetTask(taskId).get());
        // Register listeners so we can be sure the waiting started
        CountDownLatch waitForWaitingToStart = new CountDownLatch(1);
        for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
            ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() {

                @Override
                public void waitForTaskCompletion(Task task) {
                    waitForWaitingToStart.countDown();
                }

                @Override
                public void onTaskRegistered(Task task) {
                }

                @Override
                public void onTaskUnregistered(Task task) {
                }
            });
        }
        // Spin up a request to wait for the test task to finish
        waitResponseFuture = wait.apply(taskId);
        /* Wait for the wait to start. This should count down just *before* we wait for completion but after the list/get has got a
             * reference to the running task. Because we unblock immediately after this the task may no longer be running for us to wait
             * on which is fine. */
        waitForWaitingToStart.await();
    } finally {
        // Unblock the request so the wait for completion request can finish
        TestTaskPlugin.UnblockTestTasksAction.INSTANCE.newRequestBuilder(client()).get();
    }
    // Now that the task is unblocked the list response will come back
    T waitResponse = waitResponseFuture.get();
    validator.accept(waitResponse);
    TestTaskPlugin.NodesResponse response = future.get();
    assertEquals(emptyList(), response.failures());
}
Also used : Task(org.elasticsearch.tasks.Task) TaskId(org.elasticsearch.tasks.TaskId) MockTaskManagerListener(org.elasticsearch.test.tasks.MockTaskManagerListener) CountDownLatch(java.util.concurrent.CountDownLatch) MockTaskManager(org.elasticsearch.test.tasks.MockTaskManager) SearchTransportService(org.elasticsearch.action.search.SearchTransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService)

Example 19 with Task

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

the class TasksIT method testTransportBroadcastReplicationTasks.

public void testTransportBroadcastReplicationTasks() {
    // main task
    registerTaskManageListeners(RefreshAction.NAME);
    // shard level tasks
    registerTaskManageListeners(RefreshAction.NAME + "[s]");
    // primary and replica shard tasks
    registerTaskManageListeners(RefreshAction.NAME + "[s][*]");
    createIndex("test");
    // Make sure all shards are allocated
    ensureGreen("test");
    client().admin().indices().prepareRefresh("test").get();
    // the refresh operation should produce one main task
    NumShards numberOfShards = getNumShards("test");
    logger.debug("number of shards, total: [{}], primaries: [{}] ", numberOfShards.totalNumShards, numberOfShards.numPrimaries);
    logger.debug("main events {}", numberOfEvents(RefreshAction.NAME, Tuple::v1));
    logger.debug("main event node {}", findEvents(RefreshAction.NAME, Tuple::v1).get(0).getTaskId().getNodeId());
    logger.debug("[s] events {}", numberOfEvents(RefreshAction.NAME + "[s]", Tuple::v1));
    logger.debug("[s][*] events {}", numberOfEvents(RefreshAction.NAME + "[s][*]", Tuple::v1));
    logger.debug("nodes with the index {}", internalCluster().nodesInclude("test"));
    assertEquals(1, numberOfEvents(RefreshAction.NAME, Tuple::v1));
    // Because it's broadcast replication action we will have as many [s] level requests
    // as we have primary shards on the coordinating node plus we will have one task per primary outside of the
    // coordinating node due to replication.
    // If all primaries are on the coordinating node, the number of tasks should be equal to the number of primaries
    // If all primaries are not on the coordinating node, the number of tasks should be equal to the number of primaries times 2
    assertThat(numberOfEvents(RefreshAction.NAME + "[s]", Tuple::v1), greaterThanOrEqualTo(numberOfShards.numPrimaries));
    assertThat(numberOfEvents(RefreshAction.NAME + "[s]", Tuple::v1), lessThanOrEqualTo(numberOfShards.numPrimaries * 2));
    // Verify that all [s] events have the proper parent
    // This is complicated because if the shard task runs on the same node it has main task as a parent
    // but if it runs on non-coordinating node it would have another intermediate [s] task on the coordinating node as a parent
    TaskInfo mainTask = findEvents(RefreshAction.NAME, Tuple::v1).get(0);
    List<TaskInfo> sTasks = findEvents(RefreshAction.NAME + "[s]", Tuple::v1);
    for (TaskInfo taskInfo : sTasks) {
        if (mainTask.getTaskId().getNodeId().equals(taskInfo.getTaskId().getNodeId())) {
            // This shard level task runs on the same node as a parent task - it should have the main task as a direct parent
            assertParentTask(Collections.singletonList(taskInfo), mainTask);
        } else {
            String description = taskInfo.getDescription();
            // This shard level task runs on another node - it should have a corresponding shard level task on the node where main task
            // is running
            List<TaskInfo> sTasksOnRequestingNode = findEvents(RefreshAction.NAME + "[s]", event -> event.v1() && mainTask.getTaskId().getNodeId().equals(event.v2().getTaskId().getNodeId()) && description.equals(event.v2().getDescription()));
            // There should be only one parent task
            assertEquals(1, sTasksOnRequestingNode.size());
            assertParentTask(Collections.singletonList(taskInfo), sTasksOnRequestingNode.get(0));
        }
    }
    // we will have as many [s][p] and [s][r] tasks as we have primary and replica shards
    assertEquals(numberOfShards.totalNumShards, numberOfEvents(RefreshAction.NAME + "[s][*]", Tuple::v1));
    // we the [s][p] and [s][r] tasks should have a corresponding [s] task on the same node as a parent
    List<TaskInfo> spEvents = findEvents(RefreshAction.NAME + "[s][*]", Tuple::v1);
    for (TaskInfo taskInfo : spEvents) {
        List<TaskInfo> sTask;
        if (taskInfo.getAction().endsWith("[s][p]")) {
            // A [s][p] level task should have a corresponding [s] level task on the same node
            sTask = findEvents(RefreshAction.NAME + "[s]", event -> event.v1() && taskInfo.getTaskId().getNodeId().equals(event.v2().getTaskId().getNodeId()) && taskInfo.getDescription().equals(event.v2().getDescription()));
        } else {
            // A [s][r] level task should have a corresponding [s] level task on the a different node (where primary is located)
            sTask = findEvents(RefreshAction.NAME + "[s]", event -> event.v1() && taskInfo.getParentTaskId().getNodeId().equals(event.v2().getTaskId().getNodeId()) && taskInfo.getDescription().equals(event.v2().getDescription()));
        }
        // There should be only one parent task
        assertEquals(1, sTask.size());
        assertParentTask(Collections.singletonList(taskInfo), sTask.get(0));
    }
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) MockTaskManagerListener(org.elasticsearch.test.tasks.MockTaskManagerListener) GetResponse(org.elasticsearch.action.get.GetResponse) Arrays(java.util.Arrays) FailedNodeException(org.elasticsearch.action.FailedNodeException) ValidateQueryAction(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryAction) ListTasksAction(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction) Matchers.not(org.hamcrest.Matchers.not) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) UpgradeAction(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeAction) TaskResult(org.elasticsearch.tasks.TaskResult) Settings(org.elasticsearch.common.settings.Settings) TaskOperationFailure(org.elasticsearch.action.TaskOperationFailure) Collections.singleton(java.util.Collections.singleton) BulkAction(org.elasticsearch.action.bulk.BulkAction) Map(java.util.Map) TimeValue.timeValueSeconds(org.elasticsearch.common.unit.TimeValue.timeValueSeconds) SearchResponse(org.elasticsearch.action.search.SearchResponse) TaskResultsService(org.elasticsearch.tasks.TaskResultsService) Matchers.emptyCollectionOf(org.hamcrest.Matchers.emptyCollectionOf) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) ReceiveTimeoutTransportException(org.elasticsearch.transport.ReceiveTimeoutTransportException) CyclicBarrier(java.util.concurrent.CyclicBarrier) FieldStatsAction(org.elasticsearch.action.fieldstats.FieldStatsAction) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Collections.emptyList(java.util.Collections.emptyList) Matchers.allOf(org.hamcrest.Matchers.allOf) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) Collection(java.util.Collection) TransportReplicationActionTests(org.elasticsearch.action.support.replication.TransportReplicationActionTests) CancelTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) Matchers.startsWith(org.hamcrest.Matchers.startsWith) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SearchTransportService(org.elasticsearch.action.search.SearchTransportService) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) MockTaskManager(org.elasticsearch.test.tasks.MockTaskManager) Task(org.elasticsearch.tasks.Task) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentType(org.elasticsearch.common.xcontent.XContentType) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashMap(java.util.HashMap) ListenableActionFuture(org.elasticsearch.action.ListenableActionFuture) TaskId(org.elasticsearch.tasks.TaskId) RefreshAction(org.elasticsearch.action.admin.indices.refresh.RefreshAction) Function(java.util.function.Function) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) WriteRequest(org.elasticsearch.action.support.WriteRequest) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Regex(org.elasticsearch.common.regex.Regex) TimeValue.timeValueMillis(org.elasticsearch.common.unit.TimeValue.timeValueMillis) Matchers.hasSize(org.hamcrest.Matchers.hasSize) IndexResponse(org.elasticsearch.action.index.IndexResponse) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) ElasticsearchAssertions.assertThrows(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows) SearchAction(org.elasticsearch.action.search.SearchAction) TaskInfo(org.elasticsearch.tasks.TaskInfo) ClusterHealthAction(org.elasticsearch.action.admin.cluster.health.ClusterHealthAction) Matchers.empty(org.hamcrest.Matchers.empty) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Plugin(org.elasticsearch.plugins.Plugin) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) IndexAction(org.elasticsearch.action.index.IndexAction) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) Tuple(org.elasticsearch.common.collect.Tuple) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) ElasticsearchAssertions.assertAcked(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked) ElasticsearchAssertions.assertNoFailures(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures) Matchers.containsString(org.hamcrest.Matchers.containsString) Tuple(org.elasticsearch.common.collect.Tuple)

Example 20 with Task

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

the class TransportBulkActionTookTests method createAction.

private TransportBulkAction createAction(boolean controlled, AtomicLong expected) {
    CapturingTransport capturingTransport = new CapturingTransport();
    TransportService transportService = new TransportService(clusterService.getSettings(), capturingTransport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> clusterService.localNode(), null);
    transportService.start();
    transportService.acceptIncomingRequests();
    IndexNameExpressionResolver resolver = new Resolver(Settings.EMPTY);
    ActionFilters actionFilters = new ActionFilters(new HashSet<>());
    TransportCreateIndexAction createIndexAction = new TransportCreateIndexAction(Settings.EMPTY, transportService, clusterService, threadPool, null, actionFilters, resolver);
    if (controlled) {
        return new TestTransportBulkAction(Settings.EMPTY, threadPool, transportService, clusterService, null, createIndexAction, actionFilters, resolver, null, expected::get) {

            @Override
            void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses) {
                expected.set(1000000);
                super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses);
            }
        };
    } else {
        return new TestTransportBulkAction(Settings.EMPTY, threadPool, transportService, clusterService, null, createIndexAction, actionFilters, resolver, null, System::nanoTime) {

            @Override
            void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses) {
                long elapsed = spinForAtLeastOneMillisecond();
                expected.set(elapsed);
                super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses);
            }
        };
    }
}
Also used : Task(org.elasticsearch.tasks.Task) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) ActionFilters(org.elasticsearch.action.support.ActionFilters) TransportCreateIndexAction(org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction) ActionListener(org.elasticsearch.action.ActionListener) TransportService(org.elasticsearch.transport.TransportService) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)

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