Search in sources :

Example 1 with TaskInfo

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()));
    }
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) AbstractResponseTestCase(org.opensearch.client.AbstractResponseTestCase) Version(org.opensearch.Version) StreamOutput(org.opensearch.common.io.stream.StreamOutput) HashMap(java.util.HashMap) OpenSearchException(org.opensearch.OpenSearchException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Map(java.util.Map) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) StreamInput(org.opensearch.common.io.stream.StreamInput) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) TaskId(org.opensearch.tasks.TaskId) IOException(java.io.IOException) Task(org.opensearch.tasks.Task) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) TaskInfo(org.opensearch.tasks.TaskInfo) List(java.util.List) TaskOperationFailure(org.opensearch.action.TaskOperationFailure) XContentType(org.opensearch.common.xcontent.XContentType) Collections(java.util.Collections) TaskId(org.opensearch.tasks.TaskId) TaskInfo(org.opensearch.tasks.TaskInfo) TaskOperationFailure(org.opensearch.action.TaskOperationFailure) OpenSearchException(org.opensearch.OpenSearchException)

Example 2 with TaskInfo

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;
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse)

Example 3 with TaskInfo

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);
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) Tuple(org.opensearch.common.collect.Tuple)

Example 4 with TaskInfo

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());
}
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 5 with TaskInfo

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));
    }
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) Tuple(org.opensearch.common.collect.Tuple)

Aggregations

TaskInfo (org.opensearch.tasks.TaskInfo)44 TaskId (org.opensearch.tasks.TaskId)23 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)16 Task (org.opensearch.tasks.Task)10 ArrayList (java.util.ArrayList)9 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)9 List (java.util.List)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)7 GetTaskResponse (org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse)7 PersistentTask (org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)7 TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)7 TransportService (org.opensearch.transport.TransportService)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 OpenSearchException (org.opensearch.OpenSearchException)6 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)5