Search in sources :

Example 6 with TaskInfo

use of org.opensearch.tasks.TaskInfo 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());
}
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) Tuple(org.opensearch.common.collect.Tuple) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with TaskInfo

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

the class CancellableTasksIT method testCancelOrphanedTasks.

public void testCancelOrphanedTasks() throws Exception {
    final String nodeWithRootTask = internalCluster().startDataOnlyNode();
    Set<DiscoveryNode> nodes = StreamSupport.stream(clusterService().state().nodes().spliterator(), false).collect(Collectors.toSet());
    TestRequest rootRequest = generateTestRequest(nodes, 0, between(1, 3));
    client(nodeWithRootTask).execute(TransportTestAction.ACTION, rootRequest);
    allowPartialRequest(rootRequest);
    try {
        internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeWithRootTask));
        assertBusy(() -> {
            for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
                for (CancellableTask task : transportService.getTaskManager().getCancellableTasks().values()) {
                    if (task.getAction().equals(TransportTestAction.ACTION.name())) {
                        final TaskInfo taskInfo = task.taskInfo(transportService.getLocalNode().getId(), false);
                        assertTrue(taskInfo.toString(), task.isCancelled());
                        assertNotNull(taskInfo.toString(), task.getReasonCancelled());
                        assertThat(taskInfo.toString(), task.getReasonCancelled(), equalTo("channel was closed"));
                    }
                }
            }
        }, 30, TimeUnit.SECONDS);
    } finally {
        allowEntireRequest(rootRequest);
        ensureAllBansRemoved();
    }
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) CancellableTask(org.opensearch.tasks.CancellableTask) TransportService(org.opensearch.transport.TransportService) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 8 with TaskInfo

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

the class CancellableTasksIT method getRootTaskId.

static TaskId getRootTaskId(TestRequest request) throws Exception {
    SetOnce<TaskId> taskId = new SetOnce<>();
    assertBusy(() -> {
        ListTasksResponse listTasksResponse = client().admin().cluster().prepareListTasks().setActions(TransportTestAction.ACTION.name()).setDetailed(true).get();
        List<TaskInfo> tasks = listTasksResponse.getTasks().stream().filter(t -> t.getDescription().equals(request.taskDescription())).collect(Collectors.toList());
        assertThat(tasks, hasSize(1));
        taskId.set(tasks.get(0).getTaskId());
    });
    return taskId.get();
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) ActionPlugin(org.opensearch.plugins.ActionPlugin) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) ActionRequest(org.opensearch.action.ActionRequest) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) GroupedActionListener(org.opensearch.action.support.GroupedActionListener) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LatchedActionListener(org.opensearch.action.LatchedActionListener) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) ActionResponse(org.opensearch.action.ActionResponse) ActionType(org.opensearch.action.ActionType) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) CancellableTask(org.opensearch.tasks.CancellableTask) NodeClient(org.opensearch.client.node.NodeClient) Collection(java.util.Collection) ExceptionsHelper(org.opensearch.ExceptionsHelper) Set(java.util.Set) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) TaskManager(org.opensearch.tasks.TaskManager) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ActionFilters(org.opensearch.action.support.ActionFilters) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TaskCancelledException(org.opensearch.tasks.TaskCancelledException) Matchers.anyOf(org.hamcrest.Matchers.anyOf) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportException(org.opensearch.transport.TransportException) HandledTransportAction(org.opensearch.action.support.HandledTransportAction) ActionRunnable(org.opensearch.action.ActionRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) StreamOutput(org.opensearch.common.io.stream.StreamOutput) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) InternalTestCluster(org.opensearch.test.InternalTestCluster) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) Matchers.hasSize(org.hamcrest.Matchers.hasSize) StreamSupport(java.util.stream.StreamSupport) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.empty(org.hamcrest.Matchers.empty) SetOnce(org.apache.lucene.util.SetOnce) TaskId(org.opensearch.tasks.TaskId) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) IOException(java.io.IOException) Plugin(org.opensearch.plugins.Plugin) ActionFuture(org.opensearch.action.ActionFuture) TimeUnit(java.util.concurrent.TimeUnit) Sets(org.opensearch.common.util.set.Sets) TaskInfo(org.opensearch.tasks.TaskInfo) Collections(java.util.Collections) TaskId(org.opensearch.tasks.TaskId) SetOnce(org.apache.lucene.util.SetOnce) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 9 with TaskInfo

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

the class CancellableTasksIT method testBanOnlyNodesWithOutstandingDescendantTasks.

public void testBanOnlyNodesWithOutstandingDescendantTasks() throws Exception {
    if (randomBoolean()) {
        internalCluster().startNodes(randomIntBetween(1, 3));
    }
    Set<DiscoveryNode> nodes = StreamSupport.stream(clusterService().state().nodes().spliterator(), false).collect(Collectors.toSet());
    final TestRequest rootRequest = generateTestRequest(nodes, 0, between(1, 4));
    ActionFuture<TestResponse> rootTaskFuture = client().execute(TransportTestAction.ACTION, rootRequest);
    Set<TestRequest> pendingRequests = allowPartialRequest(rootRequest);
    TaskId rootTaskId = getRootTaskId(rootRequest);
    ActionFuture<CancelTasksResponse> cancelFuture = client().admin().cluster().prepareCancelTasks().setTaskId(rootTaskId).waitForCompletion(true).execute();
    if (randomBoolean()) {
        List<TaskInfo> runningTasks = client().admin().cluster().prepareListTasks().setActions(TransportTestAction.ACTION.name()).setDetailed(true).get().getTasks();
        for (TaskInfo subTask : randomSubsetOf(runningTasks)) {
            client().admin().cluster().prepareCancelTasks().setTaskId(subTask.getTaskId()).waitForCompletion(false).get();
        }
    }
    assertBusy(() -> {
        for (DiscoveryNode node : nodes) {
            TaskManager taskManager = internalCluster().getInstance(TransportService.class, node.getName()).getTaskManager();
            Set<TaskId> expectedBans = new HashSet<>();
            for (TestRequest req : pendingRequests) {
                if (req.node.equals(node)) {
                    List<Task> childTasks = taskManager.getTasks().values().stream().filter(t -> t.getParentTaskId() != null && t.getDescription().equals(req.taskDescription())).collect(Collectors.toList());
                    assertThat(childTasks, hasSize(1));
                    CancellableTask childTask = (CancellableTask) childTasks.get(0);
                    assertTrue(childTask.isCancelled());
                    expectedBans.add(childTask.getParentTaskId());
                }
            }
            assertThat(taskManager.getBannedTaskIds(), equalTo(expectedBans));
        }
    }, 30, TimeUnit.SECONDS);
    allowEntireRequest(rootRequest);
    cancelFuture.actionGet();
    waitForRootTask(rootTaskFuture);
    ensureAllBansRemoved();
}
Also used : ActionPlugin(org.opensearch.plugins.ActionPlugin) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) ActionRequest(org.opensearch.action.ActionRequest) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) GroupedActionListener(org.opensearch.action.support.GroupedActionListener) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LatchedActionListener(org.opensearch.action.LatchedActionListener) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) ActionResponse(org.opensearch.action.ActionResponse) ActionType(org.opensearch.action.ActionType) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) CancellableTask(org.opensearch.tasks.CancellableTask) NodeClient(org.opensearch.client.node.NodeClient) Collection(java.util.Collection) ExceptionsHelper(org.opensearch.ExceptionsHelper) Set(java.util.Set) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) TaskManager(org.opensearch.tasks.TaskManager) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ActionFilters(org.opensearch.action.support.ActionFilters) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TaskCancelledException(org.opensearch.tasks.TaskCancelledException) Matchers.anyOf(org.hamcrest.Matchers.anyOf) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportException(org.opensearch.transport.TransportException) HandledTransportAction(org.opensearch.action.support.HandledTransportAction) ActionRunnable(org.opensearch.action.ActionRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) StreamOutput(org.opensearch.common.io.stream.StreamOutput) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) InternalTestCluster(org.opensearch.test.InternalTestCluster) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) Matchers.hasSize(org.hamcrest.Matchers.hasSize) StreamSupport(java.util.stream.StreamSupport) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.empty(org.hamcrest.Matchers.empty) SetOnce(org.apache.lucene.util.SetOnce) TaskId(org.opensearch.tasks.TaskId) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) IOException(java.io.IOException) Plugin(org.opensearch.plugins.Plugin) ActionFuture(org.opensearch.action.ActionFuture) TimeUnit(java.util.concurrent.TimeUnit) Sets(org.opensearch.common.util.set.Sets) TaskInfo(org.opensearch.tasks.TaskInfo) Collections(java.util.Collections) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) CancellableTask(org.opensearch.tasks.CancellableTask) Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) TaskInfo(org.opensearch.tasks.TaskInfo) TaskManager(org.opensearch.tasks.TaskManager) CancellableTask(org.opensearch.tasks.CancellableTask) TransportService(org.opensearch.transport.TransportService) HashSet(java.util.HashSet)

Example 10 with TaskInfo

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

the class TransportGetTaskAction method getRunningTaskFromNode.

/**
 * Executed on the node that should be running the task to find and return the running task. Falls back to
 * {@link #getFinishedTaskFromIndex(Task, GetTaskRequest, ActionListener)} if the task isn't still running.
 */
void getRunningTaskFromNode(Task thisTask, GetTaskRequest request, ActionListener<GetTaskResponse> listener) {
    Task runningTask = taskManager.getTask(request.getTaskId().getId());
    if (runningTask == null) {
        // Task isn't running, go look in the task index
        getFinishedTaskFromIndex(thisTask, request, listener);
    } else {
        if (request.getWaitForCompletion()) {
            // Shift to the generic thread pool and let it wait for the task to complete so we don't block any important threads.
            threadPool.generic().execute(new AbstractRunnable() {

                @Override
                protected void doRun() {
                    taskManager.waitForTaskCompletion(runningTask, waitForCompletionTimeout(request.getTimeout()));
                    waitedForCompletion(thisTask, request, runningTask.taskInfo(clusterService.localNode().getId(), true), listener);
                }

                @Override
                public void onFailure(Exception e) {
                    listener.onFailure(e);
                }
            });
        } else {
            TaskInfo info = runningTask.taskInfo(clusterService.localNode().getId(), true);
            listener.onResponse(new GetTaskResponse(new TaskResult(false, info)));
        }
    }
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) TaskInfo(org.opensearch.tasks.TaskInfo) Task(org.opensearch.tasks.Task) TaskResult(org.opensearch.tasks.TaskResult) OpenSearchException(org.opensearch.OpenSearchException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IOException(java.io.IOException)

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