Search in sources :

Example 31 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class TasksClientDocumentationIT method testListTasks.

@SuppressWarnings("unused")
public void testListTasks() throws IOException {
    RestHighLevelClient client = highLevelClient();
    {
        // tag::list-tasks-request
        ListTasksRequest request = new ListTasksRequest();
        // end::list-tasks-request
        // tag::list-tasks-request-filter
        // <1>
        request.setActions("cluster:*");
        // <2>
        request.setNodes("nodeId1", "nodeId2");
        // <3>
        request.setParentTaskId(new TaskId("parentTaskId", 42));
        // end::list-tasks-request-filter
        // tag::list-tasks-request-detailed
        // <1>
        request.setDetailed(true);
        // end::list-tasks-request-detailed
        // tag::list-tasks-request-wait-completion
        // <1>
        request.setWaitForCompletion(true);
        // <2>
        request.setTimeout(TimeValue.timeValueSeconds(50));
        // <3>
        request.setTimeout("50s");
    // end::list-tasks-request-wait-completion
    }
    ListTasksRequest request = new ListTasksRequest();
    // tag::list-tasks-execute
    ListTasksResponse response = client.tasks().list(request, RequestOptions.DEFAULT);
    // end::list-tasks-execute
    assertThat(response, notNullValue());
    // tag::list-tasks-response-tasks
    // <1>
    List<TaskInfo> tasks = response.getTasks();
    // end::list-tasks-response-tasks
    // tag::list-tasks-response-calc
    // <1>
    Map<String, List<TaskInfo>> perNodeTasks = response.getPerNodeTasks();
    // <2>
    List<TaskGroup> groups = response.getTaskGroups();
    // end::list-tasks-response-calc
    // tag::list-tasks-response-failures
    // <1>
    List<OpenSearchException> nodeFailures = response.getNodeFailures();
    // <2>
    List<TaskOperationFailure> taskFailures = response.getTaskFailures();
    // end::list-tasks-response-failures
    assertThat(response.getNodeFailures(), equalTo(emptyList()));
    assertThat(response.getTaskFailures(), equalTo(emptyList()));
    assertThat(response.getTasks().size(), greaterThanOrEqualTo(2));
}
Also used : TaskId(org.opensearch.tasks.TaskId) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TaskInfo(org.opensearch.tasks.TaskInfo) TaskOperationFailure(org.opensearch.action.TaskOperationFailure) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) OpenSearchException(org.opensearch.OpenSearchException) TaskGroup(org.opensearch.action.admin.cluster.node.tasks.list.TaskGroup)

Example 32 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class GetTaskResponseTests method randomTaskInfo.

static TaskInfo randomTaskInfo() {
    TaskId taskId = randomTaskId();
    String type = randomAlphaOfLength(5);
    String action = randomAlphaOfLength(5);
    Task.Status status = randomBoolean() ? randomRawTaskStatus() : null;
    String description = randomBoolean() ? randomAlphaOfLength(5) : null;
    long startTime = randomLong();
    long runningTimeNanos = randomLong();
    boolean cancellable = randomBoolean();
    boolean cancelled = cancellable == true ? randomBoolean() : false;
    TaskId parentTaskId = randomBoolean() ? TaskId.EMPTY_TASK_ID : randomTaskId();
    Map<String, String> headers = randomBoolean() ? Collections.emptyMap() : Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5));
    return new TaskInfo(taskId, type, action, description, status, startTime, runningTimeNanos, cancellable, cancelled, parentTaskId, headers);
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId)

Example 33 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class TasksIT method testListTasksWaitForCompletion.

public void testListTasksWaitForCompletion() throws Exception {
    waitForCompletionTestCase(randomBoolean(), id -> client().admin().cluster().prepareListTasks().setActions(TestTaskPlugin.TestTaskAction.NAME).setWaitForCompletion(true).execute(), response -> {
        assertThat(response.getNodeFailures(), empty());
        assertThat(response.getTaskFailures(), empty());
        assertThat(response.getTasks(), hasSize(1));
        TaskInfo task = response.getTasks().get(0);
        assertEquals(TestTaskPlugin.TestTaskAction.NAME, task.getAction());
    });
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo)

Example 34 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class TasksIT method testCanFetchIndexStatus.

/**
 * Very basic "is it plugged in" style test that indexes a document and makes sure that you can fetch the status of the process. The
 * goal here is to verify that the large moving parts that make fetching task status work fit together rather than to verify any
 * particular status results from indexing. For that, look at {@link TransportReplicationActionTests}. We intentionally don't use the
 * task recording mechanism used in other places in this test so we can make sure that the status fetching works properly over the wire.
 */
public void testCanFetchIndexStatus() throws Exception {
    // First latch waits for the task to start, second on blocks it from finishing.
    CountDownLatch taskRegistered = new CountDownLatch(1);
    CountDownLatch letTaskFinish = new CountDownLatch(1);
    Thread index = null;
    try {
        for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
            ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() {

                @Override
                public void onTaskRegistered(Task task) {
                    if (task.getAction().startsWith(IndexAction.NAME)) {
                        taskRegistered.countDown();
                        logger.debug("Blocking [{}] starting", task);
                        try {
                            assertTrue(letTaskFinish.await(10, TimeUnit.SECONDS));
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }

                @Override
                public void onTaskUnregistered(Task task) {
                }

                @Override
                public void waitForTaskCompletion(Task task) {
                }
            });
        }
        // Need to run the task in a separate thread because node client's .execute() is blocked by our task listener
        index = new Thread(() -> {
            IndexResponse indexResponse = client().prepareIndex("test").setSource("test", "test").get();
            assertArrayEquals(ReplicationResponse.EMPTY, indexResponse.getShardInfo().getFailures());
        });
        index.start();
        // waiting for at least one task to be registered
        assertTrue(taskRegistered.await(10, TimeUnit.SECONDS));
        ListTasksResponse listResponse = client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*").setDetailed(true).get();
        assertThat(listResponse.getTasks(), not(empty()));
        for (TaskInfo task : listResponse.getTasks()) {
            assertNotNull(task.getStatus());
            GetTaskResponse getResponse = client().admin().cluster().prepareGetTask(task.getTaskId()).get();
            assertFalse("task should still be running", getResponse.getTask().isCompleted());
            TaskInfo fetchedWithGet = getResponse.getTask().getTask();
            assertEquals(task.getId(), fetchedWithGet.getId());
            assertEquals(task.getType(), fetchedWithGet.getType());
            assertEquals(task.getAction(), fetchedWithGet.getAction());
            assertEquals(task.getDescription(), fetchedWithGet.getDescription());
            assertEquals(task.getStatus(), fetchedWithGet.getStatus());
            assertEquals(task.getStartTime(), fetchedWithGet.getStartTime());
            assertThat(fetchedWithGet.getRunningTimeNanos(), greaterThanOrEqualTo(task.getRunningTimeNanos()));
            assertEquals(task.isCancellable(), fetchedWithGet.isCancellable());
            assertEquals(task.getParentTaskId(), fetchedWithGet.getParentTaskId());
        }
    } finally {
        letTaskFinish.countDown();
        if (index != null) {
            index.join();
        }
        assertBusy(() -> {
            assertEquals(emptyList(), client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*").get().getTasks());
        });
    }
}
Also used : Task(org.opensearch.tasks.Task) MockTaskManagerListener(org.opensearch.test.tasks.MockTaskManagerListener) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) MockTaskManager(org.opensearch.test.tasks.MockTaskManager) TaskInfo(org.opensearch.tasks.TaskInfo) SearchTransportService(org.opensearch.action.search.SearchTransportService) TransportService(org.opensearch.transport.TransportService) MockTransportService(org.opensearch.test.transport.MockTransportService) IndexResponse(org.opensearch.action.index.IndexResponse)

Example 35 with TaskInfo

use of org.opensearch.tasks.TaskInfo in project OpenSearch by opensearch-project.

the class TasksIT method testNodeNotFoundButTaskFound.

public void testNodeNotFoundButTaskFound() throws Exception {
    // Save a fake task that looks like it is from a node that isn't part of the cluster
    CyclicBarrier b = new CyclicBarrier(2);
    TaskResultsService resultsService = internalCluster().getInstance(TaskResultsService.class);
    resultsService.storeResult(new TaskResult(new TaskInfo(new TaskId("fake", 1), "test", "test", "", null, 0, 0, false, false, TaskId.EMPTY_TASK_ID, Collections.emptyMap()), new RuntimeException("test")), new ActionListener<Void>() {

        @Override
        public void onResponse(Void response) {
            try {
                b.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception e) {
            throw new RuntimeException(e);
        }
    });
    b.await();
    // Now we can find it!
    GetTaskResponse response = expectFinishedTask(new TaskId("fake:1"));
    assertEquals("test", response.getTask().getTask().getAction());
    assertNotNull(response.getTask().getError());
    assertNull(response.getTask().getResponse());
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) TaskId(org.opensearch.tasks.TaskId) TaskResult(org.opensearch.tasks.TaskResult) GetTaskResponse(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskResponse) TaskResultsService(org.opensearch.tasks.TaskResultsService) OpenSearchException(org.opensearch.OpenSearchException) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) ReceiveTimeoutTransportException(org.opensearch.transport.ReceiveTimeoutTransportException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

TaskInfo (org.opensearch.tasks.TaskInfo)47 TaskId (org.opensearch.tasks.TaskId)24 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)19 Task (org.opensearch.tasks.Task)11 CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 OpenSearchException (org.opensearch.OpenSearchException)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 ActionListener (org.opensearch.action.ActionListener)6 TaskOperationFailure (org.opensearch.action.TaskOperationFailure)6