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