Search in sources :

Example 16 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

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 slice)
    int numDocs = getNumShards(INDEX).numPrimaries * 10 * builder.request().getSlices();
    ALLOWED_OPERATIONS.release(numDocs);
    logger.debug("setting up [{}] docs", numDocs);
    indexRandom(true, false, true, IntStream.range(0, numDocs).mapToObj(i -> client().prepareIndex().setIndex(INDEX).setId(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);
    logger.debug("chose to modify [{}] out of [{}] docs", numModifiedDocs, numDocs);
    ALLOWED_OPERATIONS.release(numModifiedDocs - builder.request().getSlices());
    // Now execute the reindex action...
    ActionFuture<? extends BulkByScrollResponse> future = builder.execute();
    /* ... and wait 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. */
    logger.debug("waiting for updates to be blocked");
    assertBusy(() -> assertTrue("updates blocked", ALLOWED_OPERATIONS.hasQueuedThreads() && ALLOWED_OPERATIONS.availablePermits() == 0), 1, TimeUnit.MINUTES);
    // 10 seconds is usually fine but on heavily loaded machines this can take a while
    // 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 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();
    logger.debug("asserting that parent is marked canceled {}", status);
    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");
        logger.debug("finding at least one canceled child among {}", sliceList.getTasks());
        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);
    }
    logger.debug("unblocking the blocked update");
    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;
    try {
        response = future.get(30, TimeUnit.SECONDS);
    } catch (Exception e) {
        if (ExceptionsHelper.unwrapCausesAndSuppressed(e, t -> t instanceof TaskCancelledException).isPresent()) {
            // the scroll request was cancelled
            return;
        }
        String tasks = client().admin().cluster().prepareListTasks().setParentTaskId(mainTask.getTaskId()).setDetailed(true).get().toString();
        throw new RuntimeException("Exception while waiting for the response. Running tasks: " + tasks, e);
    }
    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 : ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TaskCancelledException(org.opensearch.tasks.TaskCancelledException) TaskInfo(org.opensearch.tasks.TaskInfo) TaskCancelledException(org.opensearch.tasks.TaskCancelledException)

Example 17 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class CancelTests method findTaskToCancel.

public static 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.opensearch.tasks.TaskInfo) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 18 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class PersistentTasksExecutorIT method testCreatePersistentTaskWithDuplicateId.

public void testCreatePersistentTaskWithDuplicateId() throws Exception {
    PersistentTasksService persistentTasksService = internalCluster().getInstance(PersistentTasksService.class);
    PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
    String taskId = UUIDs.base64UUID();
    persistentTasksService.sendStartRequest(taskId, TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future);
    future.get();
    PlainActionFuture<PersistentTask<TestParams>> future2 = new PlainActionFuture<>();
    persistentTasksService.sendStartRequest(taskId, TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future2);
    assertFutureThrows(future2, ResourceAlreadyExistsException.class);
    assertBusy(() -> {
        // Wait for the task to start
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().size(), equalTo(1));
    });
    TaskInfo firstRunningTask = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    logger.info("Completing the running task");
    // Fail the running task and make sure it restarts properly
    assertThat(new TestTasksRequestBuilder(client()).setOperation("finish").setTaskId(firstRunningTask.getTaskId()).get().getTasks().size(), equalTo(1));
    logger.info("Waiting for persistent task with id {} to disappear", firstRunningTask.getId());
    assertBusy(() -> {
        // Wait for the task to disappear completely
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks(), empty());
    });
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) TestTasksRequestBuilder(org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)

Example 19 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class PersistentTasksExecutorIT method testPersistentActionWithNonClusterStateCondition.

public void testPersistentActionWithNonClusterStateCondition() throws Exception {
    PersistentTasksClusterService persistentTasksClusterService = internalCluster().getInstance(PersistentTasksClusterService.class, internalCluster().getMasterName());
    // Speed up rechecks to a rate that is quicker than what settings would allow
    persistentTasksClusterService.setRecheckInterval(TimeValue.timeValueMillis(1));
    TestPersistentTasksExecutor.setNonClusterStateCondition(false);
    PersistentTasksService persistentTasksService = internalCluster().getInstance(PersistentTasksService.class);
    PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
    TestParams testParams = new TestParams("Blah");
    persistentTasksService.sendStartRequest(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, testParams, future);
    String taskId = future.get().getId();
    assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks(), empty());
    TestPersistentTasksExecutor.setNonClusterStateCondition(true);
    waitForTaskToStart();
    TaskInfo taskInfo = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    // Verifying the task can now be assigned
    assertThat(taskInfo.getTaskId().getNodeId(), notNullValue());
    // Remove the persistent task
    PlainActionFuture<PersistentTask<?>> removeFuture = new PlainActionFuture<>();
    persistentTasksService.sendRemoveRequest(taskId, removeFuture);
    assertEquals(removeFuture.get().getId(), taskId);
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)

Example 20 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class PersistentTasksExecutorIT method testPersistentActionWithNoAvailableNode.

public void testPersistentActionWithNoAvailableNode() throws Exception {
    PersistentTasksService persistentTasksService = internalCluster().getInstance(PersistentTasksService.class);
    PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
    TestParams testParams = new TestParams("Blah");
    testParams.setExecutorNodeAttr("test");
    persistentTasksService.sendStartRequest(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, testParams, future);
    String taskId = future.get().getId();
    Settings nodeSettings = Settings.builder().put(nodeSettings(0)).put("node.attr.test_attr", "test").build();
    String newNode = internalCluster().startNode(nodeSettings);
    String newNodeId = internalCluster().clusterService(newNode).localNode().getId();
    waitForTaskToStart();
    TaskInfo taskInfo = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    // Verifying the task runs on the new node
    assertThat(taskInfo.getTaskId().getNodeId(), equalTo(newNodeId));
    internalCluster().stopRandomNode(settings -> "test".equals(settings.get("node.attr.test_attr")));
    assertBusy(() -> {
        // Wait for the task to disappear completely
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks(), empty());
    });
    // Remove the persistent task
    PlainActionFuture<PersistentTask<?>> removeFuture = new PlainActionFuture<>();
    persistentTasksService.sendRemoveRequest(taskId, removeFuture);
    assertEquals(removeFuture.get().getId(), taskId);
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask) Settings(org.opensearch.common.settings.Settings)

Aggregations

TaskInfo (org.opensearch.tasks.TaskInfo)47 TaskId (org.opensearch.tasks.TaskId)24 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)19 Task (org.opensearch.tasks.Task)11 CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 OpenSearchException (org.opensearch.OpenSearchException)7 GetTaskResponse (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse)7 PersistentTask (org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)7 TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)7 TransportService (org.opensearch.transport.TransportService)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ActionListener (org.opensearch.action.ActionListener)6 TaskOperationFailure (org.opensearch.action.TaskOperationFailure)6