Search in sources :

Example 21 with TaskInfo

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

the class CancellableTasksTests method testBasicTaskCancellation.

public void testBasicTaskCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch responseLatch = new CountDownLatch(1);
    boolean waitForActionToStart = randomBoolean();
    logger.info("waitForActionToStart is set to {}", waitForActionToStart);
    final AtomicReference<NodesResponse> responseReference = new AtomicReference<>();
    final AtomicReference<Throwable> throwableReference = new AtomicReference<>();
    int blockedNodesCount = randomIntBetween(0, nodesCount);
    Task mainTask = startCancellableTestNodesAction(waitForActionToStart, blockedNodesCount, 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();
        }
    });
    // Cancel main task
    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[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction.execute(request).get();
    // Awaiting for the main task to finish
    responseLatch.await();
    if (response.getTasks().size() == 0) {
        // We didn't cancel the request and it finished successfully
        // That should be rare and can be only in case we didn't block on a single node
        assertEquals(0, blockedNodesCount);
        // Make sure that the request was successful
        assertNull(throwableReference.get());
        assertNotNull(responseReference.get());
        assertEquals(nodesCount, responseReference.get().getNodes().size());
        assertEquals(0, responseReference.get().failureCount());
    } else {
        // We canceled the request, in this case it should have fail, but we should get partial response
        assertNull(throwableReference.get());
        assertEquals(nodesCount, responseReference.get().failureCount() + responseReference.get().getNodes().size());
        // and we should have at least as many failures as the number of blocked operations
        // (we might have cancelled some non-blocked operations before they even started and that's ok)
        assertThat(responseReference.get().failureCount(), greaterThanOrEqualTo(blockedNodesCount));
        // We should have the information about the cancelled task in the cancel operation response
        assertEquals(1, response.getTasks().size());
        assertEquals(mainTask.getId(), response.getTasks().get(0).getId());
        // Verify that all cancelled tasks reported that they support cancellation
        for (TaskInfo taskInfo : response.getTasks()) {
            assertTrue(taskInfo.isCancellable());
        }
    }
    // Make sure that tasks are no longer running
    ListTasksResponse listTasksResponse = testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction.execute(new ListTasksRequest().setTaskId(new TaskId(testNodes[0].getNodeId(), mainTask.getId()))).get();
    assertEquals(0, listTasksResponse.getTasks().size());
    // Make sure that there are no leftover bans, the ban removal is async, so we might return from the cancellation
    // while the ban is still there, but it should disappear shortly
    assertBusy(() -> {
        for (int i = 0; i < testNodes.length; i++) {
            assertEquals("No bans on the node " + i, 0, testNodes[i].transportService.getTaskManager().getBanCount());
        }
    });
}
Also used : CancellableTask(org.elasticsearch.tasks.CancellableTask) 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TaskCancelledException(org.elasticsearch.tasks.TaskCancelledException) TaskInfo(org.elasticsearch.tasks.TaskInfo) ListTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)

Example 22 with TaskInfo

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

the class TasksIT method testListTasksWaitForCompletion.

public void testListTasksWaitForCompletion() throws Exception {
    waitForCompletionTestCase(randomBoolean(), id -> {
        return client().admin().cluster().prepareListTasks().setActions(TestTaskPlugin.TestTaskAction.NAME).setWaitForCompletion(true).execute();
    }, response -> {
        assertThat(response.getNodeFailures(), empty());
        assertThat(response.getTaskFailures(), empty());
        assertThat(response.getTasks(), hasSize(1));
        TaskInfo task = response.getTasks().get(0);
        assertEquals(TestTaskPlugin.TestTaskAction.NAME, task.getAction());
    });
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo)

Example 23 with TaskInfo

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

the class CancelTests method testCancel.

/**
     * Executes the cancellation test
     */
private void testCancel(String action, AbstractBulkByScrollRequestBuilder<?, ?> builder, CancelAssertion assertion, Matcher<String> taskDescriptionMatcher) throws Exception {
    createIndex(INDEX);
    // Total number of documents created for this test (~10 per primary shard per shard)
    int numDocs = getNumShards(INDEX).numPrimaries * 10 * builder.request().getSlices();
    ALLOWED_OPERATIONS.release(numDocs);
    indexRandom(true, false, true, IntStream.range(0, numDocs).mapToObj(i -> client().prepareIndex(INDEX, TYPE, String.valueOf(i)).setSource("n", i)).collect(Collectors.toList()));
    // Checks that the all documents have been indexed and correctly counted
    assertHitCount(client().prepareSearch(INDEX).setSize(0).get(), numDocs);
    assertThat(ALLOWED_OPERATIONS.drainPermits(), equalTo(0));
    // Scroll by 1 so that cancellation is easier to control
    builder.source().setSize(1);
    /* Allow a random number of the documents less the number of workers to be modified by the reindex action. That way at least one
         * worker is blocked. */
    int numModifiedDocs = randomIntBetween(builder.request().getSlices() * 2, numDocs);
    ALLOWED_OPERATIONS.release(numModifiedDocs - builder.request().getSlices());
    // Now execute the reindex action...
    ListenableActionFuture<? extends BulkByScrollResponse> future = builder.execute();
    /* ... and waits for the indexing operation listeners to block. It is important to realize that some of the workers might have
         * exhausted their slice while others might have quite a bit left to work on. We can't control that. */
    awaitBusy(() -> ALLOWED_OPERATIONS.hasQueuedThreads() && ALLOWED_OPERATIONS.availablePermits() == 0);
    // Status should show the task running
    TaskInfo mainTask = findTaskToCancel(action, builder.request().getSlices());
    BulkByScrollTask.Status status = (BulkByScrollTask.Status) mainTask.getStatus();
    assertNull(status.getReasonCancelled());
    // Description shouldn't be empty
    assertThat(mainTask.getDescription(), taskDescriptionMatcher);
    // Cancel the request while the reindex action is blocked by the indexing operation listeners.
    // This will prevent further requests from being sent.
    ListTasksResponse cancelTasksResponse = client().admin().cluster().prepareCancelTasks().setTaskId(mainTask.getTaskId()).get();
    cancelTasksResponse.rethrowFailures("Cancel");
    assertThat(cancelTasksResponse.getTasks(), hasSize(1));
    // The status should now show canceled. The request will still be in the list because it is (or its children are) still blocked.
    mainTask = client().admin().cluster().prepareGetTask(mainTask.getTaskId()).get().getTask().getTask();
    status = (BulkByScrollTask.Status) mainTask.getStatus();
    assertEquals(CancelTasksRequest.DEFAULT_REASON, status.getReasonCancelled());
    if (builder.request().getSlices() > 1) {
        boolean foundCancelled = false;
        ListTasksResponse sliceList = client().admin().cluster().prepareListTasks().setParentTaskId(mainTask.getTaskId()).setDetailed(true).get();
        sliceList.rethrowFailures("Fetch slice tasks");
        for (TaskInfo slice : sliceList.getTasks()) {
            BulkByScrollTask.Status sliceStatus = (BulkByScrollTask.Status) slice.getStatus();
            if (sliceStatus.getReasonCancelled() == null)
                continue;
            assertEquals(CancelTasksRequest.DEFAULT_REASON, sliceStatus.getReasonCancelled());
            foundCancelled = true;
        }
        assertTrue("Didn't find at least one sub task that was cancelled", foundCancelled);
    }
    // Unblock the last operations
    ALLOWED_OPERATIONS.release(builder.request().getSlices());
    // Checks that no more operations are executed
    assertBusy(() -> {
        if (builder.request().getSlices() == 1) {
            /* We can only be sure that we've drained all the permits if we only use a single worker. Otherwise some worker may have
                 * exhausted all of its documents before we blocked. */
            assertEquals(0, ALLOWED_OPERATIONS.availablePermits());
        }
        assertEquals(0, ALLOWED_OPERATIONS.getQueueLength());
    });
    // And check the status of the response
    BulkByScrollResponse response = future.get();
    assertThat(response.getReasonCancelled(), equalTo("by user request"));
    assertThat(response.getBulkFailures(), emptyIterable());
    assertThat(response.getSearchFailures(), emptyIterable());
    if (builder.request().getSlices() >= 1) {
        // If we have more than one worker we might not have made all the modifications
        numModifiedDocs -= ALLOWED_OPERATIONS.availablePermits();
    }
    flushAndRefresh(INDEX);
    assertion.assertThat(response, numDocs, numModifiedDocs);
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) BulkByScrollTask(org.elasticsearch.action.bulk.byscroll.BulkByScrollTask) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) BulkByScrollResponse(org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse)

Example 24 with TaskInfo

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

the class CancelTests method findTaskToCancel.

private TaskInfo findTaskToCancel(String actionName, int workerCount) {
    ListTasksResponse tasks;
    long start = System.nanoTime();
    do {
        tasks = client().admin().cluster().prepareListTasks().setActions(actionName).setDetailed(true).get();
        tasks.rethrowFailures("Find tasks to cancel");
        for (TaskInfo taskInfo : tasks.getTasks()) {
            // Skip tasks with a parent because those are children of the task we want to cancel
            if (false == taskInfo.getParentTaskId().isSet()) {
                return taskInfo;
            }
        }
    } while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(10));
    throw new AssertionError("Couldn't find task to rethrottle after waiting tasks=" + tasks.getTasks());
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 25 with TaskInfo

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

the class InternalTestCluster method assertShardIndexCounter.

private void assertShardIndexCounter() throws Exception {
    assertBusy(() -> {
        final Collection<NodeAndClient> nodesAndClients = nodes.values();
        for (NodeAndClient nodeAndClient : nodesAndClients) {
            IndicesService indexServices = getInstance(IndicesService.class, nodeAndClient.name);
            for (IndexService indexService : indexServices) {
                for (IndexShard indexShard : indexService) {
                    int activeOperationsCount = indexShard.getActiveOperationsCount();
                    if (activeOperationsCount > 0) {
                        TaskManager taskManager = getInstance(TransportService.class, nodeAndClient.name).getTaskManager();
                        DiscoveryNode localNode = getInstance(ClusterService.class, nodeAndClient.name).localNode();
                        List<TaskInfo> taskInfos = taskManager.getTasks().values().stream().filter(task -> task instanceof ReplicationTask).map(task -> task.taskInfo(localNode.getId(), true)).collect(Collectors.toList());
                        ListTasksResponse response = new ListTasksResponse(taskInfos, Collections.emptyList(), Collections.emptyList());
                        try {
                            XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint().value(response);
                            throw new AssertionError("expected index shard counter on shard " + indexShard.shardId() + " on node " + nodeAndClient.name + " to be 0 but was " + activeOperationsCount + ". Current replication tasks on node:\n" + builder.string());
                        } catch (IOException e) {
                            throw new RuntimeException("caught exception while building response [" + response + "]", e);
                        }
                    }
                }
            }
        }
    });
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) Arrays(java.util.Arrays) Nullable(org.elasticsearch.common.Nullable) Releasables(org.elasticsearch.common.lease.Releasables) NetworkModule(org.elasticsearch.common.network.NetworkModule) ZenDiscovery(org.elasticsearch.discovery.zen.ZenDiscovery) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) NodeValidationException(org.elasticsearch.node.NodeValidationException) SearchService(org.elasticsearch.search.SearchService) RecoverySettings(org.elasticsearch.indices.recovery.RecoverySettings) ClusterState(org.elasticsearch.cluster.ClusterState) Future(java.util.concurrent.Future) RandomNumbers(com.carrotsearch.randomizedtesting.generators.RandomNumbers) Map(java.util.Map) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) Path(java.nio.file.Path) RandomStrings(com.carrotsearch.randomizedtesting.generators.RandomStrings) ServiceDisruptionScheme(org.elasticsearch.test.disruption.ServiceDisruptionScheme) Transport(org.elasticsearch.transport.Transport) MappingUpdatedAction(org.elasticsearch.cluster.action.index.MappingUpdatedAction) Set(java.util.Set) PageCacheRecycler(org.elasticsearch.common.util.PageCacheRecycler) ESTestCase.assertBusy(org.elasticsearch.test.ESTestCase.assertBusy) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) TransportSettings(org.elasticsearch.transport.TransportSettings) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) ThrottlingAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) RandomPicks(com.carrotsearch.randomizedtesting.generators.RandomPicks) DiskThresholdSettings(org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings) TransportClient(org.elasticsearch.client.transport.TransportClient) ClusterService(org.elasticsearch.cluster.service.ClusterService) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) ArrayList(java.util.ArrayList) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TcpTransport(org.elasticsearch.transport.TcpTransport) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) IndicesService(org.elasticsearch.indices.IndicesService) TransportService(org.elasticsearch.transport.TransportService) Loggers(org.elasticsearch.common.logging.Loggers) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) FileSystemUtils(org.elasticsearch.common.io.FileSystemUtils) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) Client(org.elasticsearch.client.Client) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) NodeService(org.elasticsearch.node.NodeService) SeedUtils(com.carrotsearch.randomizedtesting.SeedUtils) ExecutionException(java.util.concurrent.ExecutionException) LuceneTestCase.rarely(org.apache.lucene.util.LuceneTestCase.rarely) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) IndicesFieldDataCache(org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache) TreeMap(java.util.TreeMap) CommonStatsFlags(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags) Assert(org.junit.Assert) ScriptService(org.elasticsearch.script.ScriptService) Builder(org.elasticsearch.common.settings.Settings.Builder) ElasticsearchAssertions.assertAcked(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked) ElasticsearchException(org.elasticsearch.ElasticsearchException) Environment(org.elasticsearch.env.Environment) Random(java.util.Random) TEST_NIGHTLY(org.apache.lucene.util.LuceneTestCase.TEST_NIGHTLY) Assert.assertThat(org.junit.Assert.assertThat) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SysGlobals(com.carrotsearch.randomizedtesting.SysGlobals) Assert.fail(org.junit.Assert.fail) ClusterName(org.elasticsearch.cluster.ClusterName) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) Flag(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag) NavigableMap(java.util.NavigableMap) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Engine(org.elasticsearch.index.engine.Engine) ESTestCase.randomFrom(org.elasticsearch.test.ESTestCase.randomFrom) MockTransportClient(org.elasticsearch.transport.MockTransportClient) List(java.util.List) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Matchers.equalTo(org.hamcrest.Matchers.equalTo) DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING(org.elasticsearch.discovery.zen.ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING) ElectMasterService(org.elasticsearch.discovery.zen.ElectMasterService) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) CommitStats(org.elasticsearch.index.engine.CommitStats) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) Function(java.util.function.Function) Strings(org.elasticsearch.common.Strings) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) HashSet(java.util.HashSet) TimeValue(org.elasticsearch.common.unit.TimeValue) Node(org.elasticsearch.node.Node) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ExecutorService(java.util.concurrent.ExecutorService) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) TaskInfo(org.elasticsearch.tasks.TaskInfo) MockNode(org.elasticsearch.node.MockNode) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) Iterator(java.util.Iterator) Plugin(org.elasticsearch.plugins.Plugin) ESTestCase.awaitBusy(org.elasticsearch.test.ESTestCase.awaitBusy) TimeUnit(java.util.concurrent.TimeUnit) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Closeable(java.io.Closeable) TaskManager(org.elasticsearch.tasks.TaskManager) Collections(java.util.Collections) ReplicationTask(org.elasticsearch.action.support.replication.ReplicationTask) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) IOException(java.io.IOException) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskManager(org.elasticsearch.tasks.TaskManager) ClusterService(org.elasticsearch.cluster.service.ClusterService) ReplicationTask(org.elasticsearch.action.support.replication.ReplicationTask) TransportService(org.elasticsearch.transport.TransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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