Search in sources :

Example 1 with TaskResult

use of org.opensearch.tasks.TaskResult 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 2 with TaskResult

use of org.opensearch.tasks.TaskResult 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 3 with TaskResult

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

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() {
                    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.opensearch.common.util.concurrent.AbstractRunnable) TaskInfo(org.opensearch.tasks.TaskInfo) Task(org.opensearch.tasks.Task) TaskResult(org.opensearch.tasks.TaskResult) OpenSearchException(org.opensearch.OpenSearchException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IOException(java.io.IOException)

Example 4 with TaskResult

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

the class TransportGetTaskAction method onGetFinishedTaskFromIndex.

/**
 * Called with the {@linkplain GetResponse} from loading the task from the results index. Called on the node that once had the task if
 * that node is part of the cluster or on the coordinating node if the node wasn't part of the cluster.
 */
void onGetFinishedTaskFromIndex(GetResponse response, ActionListener<GetTaskResponse> listener) throws IOException {
    if (false == response.isExists()) {
        listener.onFailure(new ResourceNotFoundException("task [{}] isn't running and hasn't stored its results", response.getId()));
        return;
    }
    if (response.isSourceEmpty()) {
        listener.onFailure(new OpenSearchException("Stored task status for [{}] didn't contain any source!", response.getId()));
        return;
    }
    try (XContentParser parser = XContentHelper.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, response.getSourceAsBytesRef())) {
        TaskResult result = TaskResult.PARSER.apply(parser, null);
        listener.onResponse(new GetTaskResponse(result));
    }
}
Also used : TaskResult(org.opensearch.tasks.TaskResult) OpenSearchException(org.opensearch.OpenSearchException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with TaskResult

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

the class TasksIT method testNodeNotFoundButTaskFound.

public void testNodeNotFoundButTaskFound() throws Exception {
    // Save a fake task that looks like it is from a node that isn't part of the cluster
    CyclicBarrier b = new CyclicBarrier(2);
    TaskResultsService resultsService = internalCluster().getInstance(TaskResultsService.class);
    resultsService.storeResult(new TaskResult(new TaskInfo(new TaskId("fake", 1), "test", "test", "", null, 0, 0, false, false, TaskId.EMPTY_TASK_ID, Collections.emptyMap()), new RuntimeException("test")), new ActionListener<Void>() {

        @Override
        public void onResponse(Void response) {
            try {
                b.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception e) {
            throw new RuntimeException(e);
        }
    });
    b.await();
    // Now we can find it!
    GetTaskResponse response = expectFinishedTask(new TaskId("fake:1"));
    assertEquals("test", response.getTask().getTask().getAction());
    assertNotNull(response.getTask().getError());
    assertNull(response.getTask().getResponse());
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) TaskId(org.opensearch.tasks.TaskId) TaskResult(org.opensearch.tasks.TaskResult) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse) TaskResultsService(org.opensearch.tasks.TaskResultsService) OpenSearchException(org.opensearch.OpenSearchException) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) ReceiveTimeoutTransportException(org.opensearch.transport.ReceiveTimeoutTransportException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

TaskResult (org.opensearch.tasks.TaskResult)5 TaskInfo (org.opensearch.tasks.TaskInfo)4 OpenSearchException (org.opensearch.OpenSearchException)3 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)3 GetTaskResponse (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse)3 TaskId (org.opensearch.tasks.TaskId)3 IOException (java.io.IOException)2 GetTaskRequest (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest)2 Tuple (org.opensearch.common.collect.Tuple)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 OpenSearchTimeoutException (org.opensearch.OpenSearchTimeoutException)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 AbstractRunnable (org.opensearch.common.util.concurrent.AbstractRunnable)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)1 Task (org.opensearch.tasks.Task)1 TaskResultsService (org.opensearch.tasks.TaskResultsService)1