Search in sources :

Example 1 with GetTaskRequest

use of org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest 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 GetTaskRequest

use of org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest 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 GetTaskRequest

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

the class RestGetTaskAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    TaskId taskId = new TaskId(request.param("task_id"));
    boolean waitForCompletion = request.paramAsBoolean("wait_for_completion", false);
    TimeValue timeout = request.paramAsTime("timeout", null);
    GetTaskRequest getTaskRequest = new GetTaskRequest();
    getTaskRequest.setTaskId(taskId);
    getTaskRequest.setWaitForCompletion(waitForCompletion);
    getTaskRequest.setTimeout(timeout);
    return channel -> client.admin().cluster().getTask(getTaskRequest, new RestToXContentListener<>(channel));
}
Also used : List(java.util.List) TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) TaskId(org.opensearch.tasks.TaskId) IOException(java.io.IOException) GetTaskRequest(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Collections.singletonList(java.util.Collections.singletonList) GetTaskRequest(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest) TaskId(org.opensearch.tasks.TaskId) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

GetTaskRequest (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskRequest)3 TaskId (org.opensearch.tasks.TaskId)3 GetTaskResponse (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse)2 Tuple (org.opensearch.common.collect.Tuple)2 TaskInfo (org.opensearch.tasks.TaskInfo)2 TaskResult (org.opensearch.tasks.TaskResult)2 IOException (java.io.IOException)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 NodeClient (org.opensearch.client.node.NodeClient)1 TimeValue (org.opensearch.common.unit.TimeValue)1 BaseRestHandler (org.opensearch.rest.BaseRestHandler)1 RestRequest (org.opensearch.rest.RestRequest)1 GET (org.opensearch.rest.RestRequest.Method.GET)1 RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)1 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)1