use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class CancelTasksResponseTests method assertInstances.
@Override
protected void assertInstances(ByNodeCancelTasksResponse serverTestInstance, org.opensearch.client.tasks.CancelTasksResponse clientInstance) {
// checking tasks
List<TaskInfo> sTasks = serverTestInstance.getTasks();
List<org.opensearch.client.tasks.TaskInfo> cTasks = clientInstance.getTasks();
Map<org.opensearch.client.tasks.TaskId, org.opensearch.client.tasks.TaskInfo> cTasksMap = cTasks.stream().collect(Collectors.toMap(org.opensearch.client.tasks.TaskInfo::getTaskId, Function.identity()));
for (TaskInfo ti : sTasks) {
org.opensearch.client.tasks.TaskInfo taskInfo = cTasksMap.get(new org.opensearch.client.tasks.TaskId(ti.getTaskId().getNodeId(), ti.getTaskId().getId()));
assertEquals(ti.getAction(), taskInfo.getAction());
assertEquals(ti.getDescription(), taskInfo.getDescription());
assertEquals(new HashMap<>(ti.getHeaders()), new HashMap<>(taskInfo.getHeaders()));
assertEquals(ti.getType(), taskInfo.getType());
assertEquals(ti.getStartTime(), taskInfo.getStartTime());
assertEquals(ti.getRunningTimeNanos(), taskInfo.getRunningTimeNanos());
assertEquals(ti.isCancellable(), taskInfo.isCancellable());
assertEquals(ti.isCancelled(), taskInfo.isCancelled());
assertEquals(ti.getParentTaskId().getNodeId(), taskInfo.getParentTaskId().getNodeId());
assertEquals(ti.getParentTaskId().getId(), taskInfo.getParentTaskId().getId());
FakeTaskStatus status = (FakeTaskStatus) ti.getStatus();
assertEquals(status.code, taskInfo.getStatus().get("code"));
assertEquals(status.status, taskInfo.getStatus().get("status"));
}
// checking failures
List<OpenSearchException> serverNodeFailures = serverTestInstance.getNodeFailures();
List<org.opensearch.client.tasks.OpenSearchException> cNodeFailures = clientInstance.getNodeFailures();
List<String> sExceptionsMessages = serverNodeFailures.stream().map(x -> org.opensearch.client.tasks.OpenSearchException.buildMessage("exception", x.getMessage(), null)).collect(Collectors.toList());
List<String> cExceptionsMessages = cNodeFailures.stream().map(org.opensearch.client.tasks.OpenSearchException::getMsg).collect(Collectors.toList());
assertEquals(new HashSet<>(sExceptionsMessages), new HashSet<>(cExceptionsMessages));
List<TaskOperationFailure> sTaskFailures = serverTestInstance.getTaskFailures();
List<org.opensearch.client.tasks.TaskOperationFailure> cTaskFailures = clientInstance.getTaskFailures();
Map<Long, org.opensearch.client.tasks.TaskOperationFailure> cTasksFailuresMap = cTaskFailures.stream().collect(Collectors.toMap(org.opensearch.client.tasks.TaskOperationFailure::getTaskId, Function.identity()));
for (TaskOperationFailure tof : sTaskFailures) {
org.opensearch.client.tasks.TaskOperationFailure failure = cTasksFailuresMap.get(tof.getTaskId());
assertEquals(tof.getNodeId(), failure.getNodeId());
assertTrue(failure.getReason().getMsg().contains("runtime_exception"));
assertTrue(failure.getStatus().contains("" + tof.getStatus().name()));
}
}
use of org.opensearch.tasks.TaskInfo 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.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class TasksIT method testTransportBulkTasks.
public void testTransportBulkTasks() {
// main task
registerTaskManagerListeners(BulkAction.NAME);
// shard task
registerTaskManagerListeners(BulkAction.NAME + "[s]");
// shard task on primary
registerTaskManagerListeners(BulkAction.NAME + "[s][p]");
// shard task on replica
registerTaskManagerListeners(BulkAction.NAME + "[s][r]");
createIndex("test");
// Make sure all shards are allocated to catch replication tasks
ensureGreen("test");
// ensures the mapping is available on all nodes so we won't retry the request (in case replicas don't have the right mapping).
client().admin().indices().preparePutMapping("test").setSource("foo", "type=keyword").get();
client().prepareBulk().add(client().prepareIndex("test").setId("test_id").setSource("{\"foo\": \"bar\"}", XContentType.JSON)).get();
// the bulk operation should produce one main task
List<TaskInfo> topTask = findEvents(BulkAction.NAME, Tuple::v1);
assertEquals(1, topTask.size());
assertEquals("requests[1], indices[test]", topTask.get(0).getDescription());
// we should also get 1 or 2 [s] operation with main operation as a parent
// in case the primary is located on the coordinating node we will have 1 operation, otherwise - 2
List<TaskInfo> shardTasks = findEvents(BulkAction.NAME + "[s]", Tuple::v1);
assertThat(shardTasks.size(), allOf(lessThanOrEqualTo(2), greaterThanOrEqualTo(1)));
// Select the effective shard task
TaskInfo shardTask;
if (shardTasks.size() == 1) {
// we have only one task - it's going to be the parent task for all [s][p] and [s][r] tasks
shardTask = shardTasks.get(0);
// and it should have the main task as a parent
assertParentTask(shardTask, findEvents(BulkAction.NAME, Tuple::v1).get(0));
} else {
if (shardTasks.get(0).getParentTaskId().equals(shardTasks.get(1).getTaskId())) {
// task 1 is the parent of task 0, that means that task 0 will control [s][p] and [s][r] tasks
shardTask = shardTasks.get(0);
// in turn the parent of the task 1 should be the main task
assertParentTask(shardTasks.get(1), findEvents(BulkAction.NAME, Tuple::v1).get(0));
} else {
// otherwise task 1 will control [s][p] and [s][r] tasks
shardTask = shardTasks.get(1);
// in turn the parent of the task 0 should be the main task
assertParentTask(shardTasks.get(0), findEvents(BulkAction.NAME, Tuple::v1).get(0));
}
}
assertThat(shardTask.getDescription(), startsWith("requests[1], index[test]["));
// we should also get one [s][p] operation with shard operation as a parent
assertEquals(1, numberOfEvents(BulkAction.NAME + "[s][p]", Tuple::v1));
assertParentTask(findEvents(BulkAction.NAME + "[s][p]", Tuple::v1), shardTask);
// we should get as many [s][r] operations as we have replica shards
// they all should have the same shard task as a parent
assertEquals(getNumShards("test").numReplicas, numberOfEvents(BulkAction.NAME + "[s][r]", Tuple::v1));
assertParentTask(findEvents(BulkAction.NAME + "[s][r]", Tuple::v1), shardTask);
}
use of org.opensearch.tasks.TaskInfo 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.tasks.TaskInfo in project OpenSearch by opensearch-project.
the class TasksIT method testSearchTaskDescriptions.
public void testSearchTaskDescriptions() {
// main task
registerTaskManagerListeners(SearchAction.NAME);
// shard task
registerTaskManagerListeners(SearchAction.NAME + "[*]");
createIndex("test");
// Make sure all shards are allocated to catch replication tasks
ensureGreen("test");
client().prepareIndex("test").setId("test_id").setSource("{\"foo\": \"bar\"}", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
Map<String, String> headers = new HashMap<>();
headers.put(Task.X_OPAQUE_ID, "my_id");
headers.put("Foo-Header", "bar");
headers.put("Custom-Task-Header", "my_value");
assertSearchResponse(client().filterWithHeader(headers).prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).get());
// the search operation should produce one main task
List<TaskInfo> mainTask = findEvents(SearchAction.NAME, Tuple::v1);
assertEquals(1, mainTask.size());
assertThat(mainTask.get(0).getDescription(), startsWith("indices[test], search_type["));
assertThat(mainTask.get(0).getDescription(), containsString("\"query\":{\"match_all\""));
assertTaskHeaders(mainTask.get(0));
// check that if we have any shard-level requests they all have non-zero length description
List<TaskInfo> shardTasks = findEvents(SearchAction.NAME + "[*]", Tuple::v1);
for (TaskInfo taskInfo : shardTasks) {
assertThat(taskInfo.getParentTaskId(), notNullValue());
assertEquals(mainTask.get(0).getTaskId(), taskInfo.getParentTaskId());
assertTaskHeaders(taskInfo);
switch(taskInfo.getAction()) {
case SearchTransportService.QUERY_ACTION_NAME:
case SearchTransportService.DFS_ACTION_NAME:
assertTrue(taskInfo.getDescription(), Regex.simpleMatch("shardId[[test][*]]", taskInfo.getDescription()));
break;
case SearchTransportService.QUERY_ID_ACTION_NAME:
assertTrue(taskInfo.getDescription(), Regex.simpleMatch("id[*], indices[test]", taskInfo.getDescription()));
break;
case SearchTransportService.FETCH_ID_ACTION_NAME:
assertTrue(taskInfo.getDescription(), Regex.simpleMatch("id[*], size[1], lastEmittedDoc[null]", taskInfo.getDescription()));
break;
case SearchTransportService.QUERY_CAN_MATCH_NAME:
assertTrue(taskInfo.getDescription(), Regex.simpleMatch("shardId[[test][*]]", taskInfo.getDescription()));
break;
default:
fail("Unexpected action [" + taskInfo.getAction() + "] with description [" + taskInfo.getDescription() + "]");
}
// assert that all task descriptions have non-zero length
assertThat(taskInfo.getDescription().length(), greaterThan(0));
}
}
Aggregations