Search in sources :

Example 1 with GetTaskResponse

use of org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project OpenSearch by opensearch-project.

the class TasksIT method expectFinishedTask.

/**
 * Fetch the task status from the list tasks API using it's "fallback to get from the task index" behavior. Asserts some obvious stuff
 * about the fetched task and returns a map of it's status.
 */
private GetTaskResponse expectFinishedTask(TaskId taskId) throws IOException {
    GetTaskResponse response = client().admin().cluster().prepareGetTask(taskId).get();
    assertTrue("the task should have been completed before fetching", response.getTask().isCompleted());
    TaskInfo info = response.getTask().getTask();
    assertEquals(taskId, info.getTaskId());
    // The test task doesn't have any status
    assertNull(info.getStatus());
    return response;
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse)

Example 2 with GetTaskResponse

use of org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project OpenSearch by opensearch-project.

the class TasksIT method testTaskStoringFailureResult.

public void testTaskStoringFailureResult() throws Exception {
    // we need this to get task id of the process
    registerTaskManagerListeners(TestTaskPlugin.TestTaskAction.NAME);
    TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
    request.setShouldFail(true);
    request.setShouldStoreResult(true);
    request.setShouldBlock(false);
    // Start non-blocking test task that should fail
    assertFutureThrows(client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request), IllegalStateException.class);
    List<TaskInfo> events = findEvents(TestTaskPlugin.TestTaskAction.NAME, Tuple::v1);
    assertEquals(1, events.size());
    TaskInfo failedTaskInfo = events.get(0);
    TaskId failedTaskId = failedTaskInfo.getTaskId();
    TaskResult taskResult = client().admin().cluster().getTask(new GetTaskRequest().setTaskId(failedTaskId)).get().getTask();
    assertTrue(taskResult.isCompleted());
    assertNull(taskResult.getResponse());
    assertEquals(failedTaskInfo.getTaskId(), taskResult.getTask().getTaskId());
    assertEquals(failedTaskInfo.getType(), taskResult.getTask().getType());
    assertEquals(failedTaskInfo.getAction(), taskResult.getTask().getAction());
    assertEquals(failedTaskInfo.getDescription(), taskResult.getTask().getDescription());
    assertEquals(failedTaskInfo.getStartTime(), taskResult.getTask().getStartTime());
    assertEquals(failedTaskInfo.getHeaders(), taskResult.getTask().getHeaders());
    Map<?, ?> error = (Map<?, ?>) taskResult.getErrorAsMap();
    assertEquals("Simulating operation failure", error.get("reason"));
    assertEquals("illegal_state_exception", error.get("type"));
    GetTaskResponse getResponse = expectFinishedTask(failedTaskId);
    assertNull(getResponse.getTask().getResponse());
    assertEquals(error, getResponse.getTask().getErrorAsMap());
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) GetTaskRequest(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest) TaskId(org.opensearch.tasks.TaskId) TaskResult(org.opensearch.tasks.TaskResult) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.opensearch.common.collect.Tuple)

Example 3 with GetTaskResponse

use of org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project OpenSearch by opensearch-project.

the class TasksIT method testTaskStoringSuccessfulResult.

public void testTaskStoringSuccessfulResult() throws Exception {
    // we need this to get task id of the process
    registerTaskManagerListeners(TestTaskPlugin.TestTaskAction.NAME);
    // Start non-blocking test task
    TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
    request.setShouldStoreResult(true);
    request.setShouldBlock(false);
    TaskId parentTaskId = new TaskId("parent_node", randomLong());
    request.setParentTask(parentTaskId);
    client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request).get();
    List<TaskInfo> events = findEvents(TestTaskPlugin.TestTaskAction.NAME, Tuple::v1);
    assertEquals(1, events.size());
    TaskInfo taskInfo = events.get(0);
    TaskId taskId = taskInfo.getTaskId();
    TaskResult taskResult = client().admin().cluster().getTask(new GetTaskRequest().setTaskId(taskId)).get().getTask();
    assertTrue(taskResult.isCompleted());
    assertNull(taskResult.getError());
    assertEquals(taskInfo.getTaskId(), taskResult.getTask().getTaskId());
    assertEquals(taskInfo.getParentTaskId(), taskResult.getTask().getParentTaskId());
    assertEquals(taskInfo.getType(), taskResult.getTask().getType());
    assertEquals(taskInfo.getAction(), taskResult.getTask().getAction());
    assertEquals(taskInfo.getDescription(), taskResult.getTask().getDescription());
    assertEquals(taskInfo.getStartTime(), taskResult.getTask().getStartTime());
    assertEquals(taskInfo.getHeaders(), taskResult.getTask().getHeaders());
    Map<?, ?> result = taskResult.getResponseAsMap();
    assertEquals("0", result.get("failure_count").toString());
    assertNoFailures(client().admin().indices().prepareRefresh(TaskResultsService.TASK_INDEX).get());
    SearchResponse searchResponse = client().prepareSearch(TaskResultsService.TASK_INDEX).setSource(SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("task.action", taskInfo.getAction()))).get();
    assertEquals(1L, searchResponse.getHits().getTotalHits().value);
    searchResponse = client().prepareSearch(TaskResultsService.TASK_INDEX).setSource(SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("task.node", taskInfo.getTaskId().getNodeId()))).get();
    assertEquals(1L, searchResponse.getHits().getTotalHits().value);
    GetTaskResponse getResponse = expectFinishedTask(taskId);
    assertEquals(result, getResponse.getTask().getResponseAsMap());
    assertNull(getResponse.getTask().getError());
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) GetTaskRequest(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest) TaskId(org.opensearch.tasks.TaskId) TaskResult(org.opensearch.tasks.TaskResult) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse) Tuple(org.opensearch.common.collect.Tuple) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with GetTaskResponse

use of org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project OpenSearch by opensearch-project.

the class TaskStorageRetryIT method testRetry.

public void testRetry() throws Exception {
    logger.info("block the write executor");
    CyclicBarrier barrier = new CyclicBarrier(2);
    getInstanceFromNode(ThreadPool.class).executor(ThreadPool.Names.WRITE).execute(() -> {
        try {
            barrier.await();
            logger.info("blocking the write executor");
            barrier.await();
            logger.info("unblocked the write executor");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    barrier.await();
    Task task;
    PlainListenableActionFuture<TestTaskPlugin.NodesResponse> future = PlainListenableActionFuture.newListenableFuture();
    try {
        logger.info("start a task that will store its results");
        TestTaskPlugin.NodesRequest req = new TestTaskPlugin.NodesRequest("foo");
        req.setShouldStoreResult(true);
        req.setShouldBlock(false);
        task = nodeClient().executeLocally(TestTaskPlugin.TestTaskAction.INSTANCE, req, future);
        logger.info("verify that the task has started and is still running");
        assertBusy(() -> {
            GetTaskResponse runningTask = client().admin().cluster().prepareGetTask(new TaskId(nodeClient().getLocalNodeId(), task.getId())).get();
            assertNotNull(runningTask.getTask());
            assertFalse(runningTask.getTask().isCompleted());
            assertEquals(emptyMap(), runningTask.getTask().getErrorAsMap());
            assertEquals(emptyMap(), runningTask.getTask().getResponseAsMap());
            assertFalse(future.isDone());
        });
    } finally {
        logger.info("unblock the write executor");
        barrier.await();
    }
    logger.info("wait for the task to finish");
    future.get(10, TimeUnit.SECONDS);
    logger.info("check that it was written successfully");
    GetTaskResponse finishedTask = client().admin().cluster().prepareGetTask(new TaskId(nodeClient().getLocalNodeId(), task.getId())).get();
    assertTrue(finishedTask.getTask().isCompleted());
    assertEquals(emptyMap(), finishedTask.getTask().getErrorAsMap());
    assertEquals(singletonMap("failure_count", 0), finishedTask.getTask().getResponseAsMap());
}
Also used : Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 5 with GetTaskResponse

use of org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project OpenSearch by opensearch-project.

the class ReindexDocumentationIT method testTasks.

public void testTasks() throws Exception {
    final Client client = client();
    final ReindexRequestBuilder builder = reindexAndPartiallyBlock();
    {
        // tag::update-by-query-list-tasks
        ListTasksResponse tasksList = client.admin().cluster().prepareListTasks().setActions(UpdateByQueryAction.NAME).setDetailed(true).get();
        for (TaskInfo info : tasksList.getTasks()) {
            TaskId taskId = info.getTaskId();
            BulkByScrollTask.Status status = (BulkByScrollTask.Status) info.getStatus();
        // do stuff
        }
    // end::update-by-query-list-tasks
    }
    TaskInfo mainTask = CancelTests.findTaskToCancel(ReindexAction.NAME, builder.request().getSlices());
    BulkByScrollTask.Status status = (BulkByScrollTask.Status) mainTask.getStatus();
    assertNull(status.getReasonCancelled());
    TaskId taskId = mainTask.getTaskId();
    {
        // tag::update-by-query-get-task
        GetTaskResponse get = client.admin().cluster().prepareGetTask(taskId).get();
    // end::update-by-query-get-task
    }
    {
        // tag::update-by-query-cancel-task
        // Cancel all update-by-query requests
        client.admin().cluster().prepareCancelTasks().setActions(UpdateByQueryAction.NAME).get().getTasks();
        // Cancel a specific update-by-query request
        client.admin().cluster().prepareCancelTasks().setTaskId(taskId).get().getTasks();
    // end::update-by-query-cancel-task
    }
    {
        // tag::update-by-query-rethrottle
        new RethrottleRequestBuilder(client, RethrottleAction.INSTANCE).setTaskId(taskId).setRequestsPerSecond(2.0f).get();
    // end::update-by-query-rethrottle
    }
    // unblocking the blocked update
    ALLOWED_OPERATIONS.release(builder.request().getSlices());
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) TaskId(org.opensearch.tasks.TaskId) ReindexRequestBuilder(org.opensearch.index.reindex.ReindexRequestBuilder) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse) BulkByScrollTask(org.opensearch.index.reindex.BulkByScrollTask) Client(org.opensearch.client.Client) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) RethrottleRequestBuilder(org.opensearch.index.reindex.RethrottleRequestBuilder)

Aggregations

GetTaskResponse (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse)7 TaskInfo (org.opensearch.tasks.TaskInfo)6 TaskId (org.opensearch.tasks.TaskId)5 TaskResult (org.opensearch.tasks.TaskResult)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 GetTaskRequest (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest)2 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)2 Tuple (org.opensearch.common.collect.Tuple)2 Task (org.opensearch.tasks.Task)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 OpenSearchException (org.opensearch.OpenSearchException)1 OpenSearchTimeoutException (org.opensearch.OpenSearchTimeoutException)1 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 SearchTransportService (org.opensearch.action.search.SearchTransportService)1