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());
}
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());
}
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));
}
Aggregations