Search in sources :

Example 1 with CancelTasksResponse

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse in project OpenSearch by opensearch-project.

the class TasksIT method testTasksCancellation.

public void testTasksCancellation() throws Exception {
    // Start blocking test task
    // Get real client (the plugin is not registered on transport nodes)
    TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
    ActionFuture<TestTaskPlugin.NodesResponse> future = client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request);
    logger.info("--> started test tasks");
    // Wait for the task to start on all nodes
    assertBusy(() -> assertEquals(internalCluster().size(), client().admin().cluster().prepareListTasks().setActions(TestTaskPlugin.TestTaskAction.NAME + "[n]").get().getTasks().size()));
    logger.info("--> cancelling the main test task");
    CancelTasksResponse cancelTasksResponse = client().admin().cluster().prepareCancelTasks().setActions(TestTaskPlugin.TestTaskAction.NAME).get();
    assertEquals(1, cancelTasksResponse.getTasks().size());
    future.get();
    logger.info("--> checking that test tasks are not running");
    assertEquals(0, client().admin().cluster().prepareListTasks().setActions(TestTaskPlugin.TestTaskAction.NAME + "*").get().getTasks().size());
}
Also used : CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)

Example 2 with CancelTasksResponse

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse in project OpenSearch by opensearch-project.

the class CancellableTasksIT method testCancelTaskMultipleTimes.

public void testCancelTaskMultipleTimes() throws Exception {
    Set<DiscoveryNode> nodes = StreamSupport.stream(clusterService().state().nodes().spliterator(), false).collect(Collectors.toSet());
    TestRequest rootRequest = generateTestRequest(nodes, 0, randomIntBetween(1, 3));
    ActionFuture<TestResponse> mainTaskFuture = client().execute(TransportTestAction.ACTION, rootRequest);
    TaskId taskId = getRootTaskId(rootRequest);
    allowPartialRequest(rootRequest);
    CancelTasksResponse resp = client().admin().cluster().prepareCancelTasks().setTaskId(taskId).waitForCompletion(false).get();
    assertThat(resp.getTaskFailures(), empty());
    assertThat(resp.getNodeFailures(), empty());
    ActionFuture<CancelTasksResponse> cancelFuture = client().admin().cluster().prepareCancelTasks().setTaskId(taskId).waitForCompletion(true).execute();
    assertFalse(cancelFuture.isDone());
    allowEntireRequest(rootRequest);
    assertThat(cancelFuture.actionGet().getTaskFailures(), empty());
    assertThat(cancelFuture.actionGet().getTaskFailures(), empty());
    waitForRootTask(mainTaskFuture);
    CancelTasksResponse cancelError = client().admin().cluster().prepareCancelTasks().setTaskId(taskId).waitForCompletion(randomBoolean()).get();
    assertThat(cancelError.getNodeFailures(), hasSize(1));
    final Throwable notFound = ExceptionsHelper.unwrap(cancelError.getNodeFailures().get(0), ResourceNotFoundException.class);
    assertThat(notFound.getMessage(), equalTo("task [" + taskId + "] is not found"));
    ensureAllBansRemoved();
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TaskId(org.opensearch.tasks.TaskId) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)

Example 3 with CancelTasksResponse

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse 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 4 with CancelTasksResponse

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse in project OpenSearch by opensearch-project.

the class SearchCancellationIT method cancelSearch.

private void cancelSearch(String action) {
    ListTasksResponse listTasksResponse = client().admin().cluster().prepareListTasks().setActions(action).get();
    assertThat(listTasksResponse.getTasks(), hasSize(1));
    TaskInfo searchTask = listTasksResponse.getTasks().get(0);
    logger.info("Cancelling search");
    CancelTasksResponse cancelTasksResponse = client().admin().cluster().prepareCancelTasks().setTaskId(searchTask.getTaskId()).get();
    assertThat(cancelTasksResponse.getTasks(), hasSize(1));
    assertThat(cancelTasksResponse.getTasks().get(0).getTaskId(), equalTo(searchTask.getTaskId()));
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 5 with CancelTasksResponse

use of org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse in project OpenSearch by opensearch-project.

the class CancellableTasksTests method testNonExistingTaskCancellation.

public void testNonExistingTaskCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    // Cancel a task that doesn't exist
    CancelTasksRequest request = new CancelTasksRequest();
    request.setReason("Testing Cancellation");
    request.setActions("do-not-match-anything");
    request.setNodes(randomSubsetOf(randomIntBetween(1, testNodes.length - 1), testNodes).stream().map(TestNode::getNodeId).toArray(String[]::new));
    // And send the cancellation request to a random node
    CancelTasksResponse response = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(1, testNodes.length - 1)].transportCancelTasksAction, request);
    // Shouldn't have cancelled anything
    assertThat(response.getTasks().size(), equalTo(0));
    assertBusy(() -> {
        // Make sure that main task is no longer running
        ListTasksResponse listTasksResponse = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction, new ListTasksRequest().setActions(CancelTasksAction.NAME + "*"));
        assertEquals(0, listTasksResponse.getTasks().size());
    });
}
Also used : CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Aggregations

CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)12 TaskId (org.opensearch.tasks.TaskId)8 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 Task (org.opensearch.tasks.Task)6 IOException (java.io.IOException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 CancelTasksRequest (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)5 ListTasksRequest (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)4 CancellableTask (org.opensearch.tasks.CancellableTask)4 TaskCancelledException (org.opensearch.tasks.TaskCancelledException)4 TaskInfo (org.opensearch.tasks.TaskInfo)4 ArrayList (java.util.ArrayList)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ActionListener (org.opensearch.action.ActionListener)2 ConnectException (java.net.ConnectException)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1