Search in sources :

Example 21 with TaskInfo

use of org.opensearch.tasks.TaskInfo 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 22 with TaskInfo

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

the class BulkByScrollTask method taskInfoGivenSubtaskInfo.

/**
 * Build the status for this task given a snapshot of the information of running slices. This is only supported if the task is
 * set as a leader for slice subtasks
 */
public TaskInfo taskInfoGivenSubtaskInfo(String localNodeId, List<TaskInfo> sliceInfo) {
    if (isLeader() == false) {
        throw new IllegalStateException("This task is not set to be a leader of other slice subtasks");
    }
    List<BulkByScrollTask.StatusOrException> sliceStatuses = Arrays.asList(new BulkByScrollTask.StatusOrException[leaderState.getSlices()]);
    for (TaskInfo t : sliceInfo) {
        BulkByScrollTask.Status status = (BulkByScrollTask.Status) t.getStatus();
        sliceStatuses.set(status.getSliceId(), new BulkByScrollTask.StatusOrException(status));
    }
    Status status = leaderState.getStatus(sliceStatuses);
    return taskInfo(localNodeId, getDescription(), status);
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo)

Example 23 with TaskInfo

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

the class TransportTasksActionTests method testCancellingTasksThatDontSupportCancellation.

public void testCancellingTasksThatDontSupportCancellation() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch checkLatch = new CountDownLatch(1);
    CountDownLatch responseLatch = new CountDownLatch(1);
    Task task = startBlockingTestNodesAction(checkLatch, ActionListener.wrap(responseLatch::countDown));
    // only pick the main action
    String actionName = "internal:testAction";
    // Try to cancel main task using action name
    CancelTasksRequest request = new CancelTasksRequest();
    request.setNodes(testNodes[0].getNodeId());
    request.setReason("Testing Cancellation");
    request.setActions(actionName);
    CancelTasksResponse response = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction, request);
    // Shouldn't match any tasks since testAction doesn't support cancellation
    assertEquals(0, response.getTasks().size());
    assertEquals(0, response.getTaskFailures().size());
    assertEquals(0, response.getNodeFailures().size());
    // Try to cancel main task using id
    request = new CancelTasksRequest();
    request.setReason("Testing Cancellation");
    request.setTaskId(new TaskId(testNodes[0].getNodeId(), task.getId()));
    response = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(0, testNodes.length - 1)].transportCancelTasksAction, request);
    // Shouldn't match any tasks since testAction doesn't support cancellation
    assertEquals(0, response.getTasks().size());
    assertEquals(0, response.getTaskFailures().size());
    assertEquals(1, response.getNodeFailures().size());
    assertThat(response.getNodeFailures().get(0).getDetailedMessage(), containsString("doesn't support cancellation"));
    // Make sure that task is still running
    ListTasksRequest listTasksRequest = new ListTasksRequest();
    listTasksRequest.setActions(actionName);
    ListTasksResponse listResponse = ActionTestUtils.executeBlocking(testNodes[randomIntBetween(0, testNodes.length - 1)].transportListTasksAction, listTasksRequest);
    assertEquals(1, listResponse.getPerNodeTasks().size());
    // Verify that tasks are marked as non-cancellable
    for (TaskInfo taskInfo : listResponse.getTasks()) {
        assertFalse(taskInfo.isCancellable());
    }
    // Release all tasks and wait for response
    checkLatch.countDown();
    responseLatch.await(10, TimeUnit.SECONDS);
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId) 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) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 24 with TaskInfo

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

the class TransportTasksActionTests method testFindChildTasks.

public void testFindChildTasks() throws Exception {
    setupTestNodes(Settings.EMPTY);
    connectNodes(testNodes);
    CountDownLatch checkLatch = new CountDownLatch(1);
    ActionFuture<NodesResponse> future = startBlockingTestNodesAction(checkLatch);
    TestNode testNode = testNodes[randomIntBetween(0, testNodes.length - 1)];
    // Get the parent task
    ListTasksRequest listTasksRequest = new ListTasksRequest();
    listTasksRequest.setActions("internal:testAction");
    ListTasksResponse response = ActionTestUtils.executeBlocking(testNode.transportListTasksAction, listTasksRequest);
    assertEquals(1, response.getTasks().size());
    String parentNode = response.getTasks().get(0).getTaskId().getNodeId();
    long parentTaskId = response.getTasks().get(0).getId();
    // Find tasks with common parent
    listTasksRequest = new ListTasksRequest();
    listTasksRequest.setParentTaskId(new TaskId(parentNode, parentTaskId));
    response = ActionTestUtils.executeBlocking(testNode.transportListTasksAction, listTasksRequest);
    assertEquals(testNodes.length, response.getTasks().size());
    for (TaskInfo task : response.getTasks()) {
        assertEquals("internal:testAction[n]", task.getAction());
        assertEquals(parentNode, task.getParentTaskId().getNodeId());
        assertEquals(parentTaskId, task.getParentTaskId().getId());
    }
    // Release all tasks and wait for response
    checkLatch.countDown();
    NodesResponse responses = future.get();
    assertEquals(0, responses.failureCount());
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) TaskId(org.opensearch.tasks.TaskId) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 25 with TaskInfo

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

the class TaskTests method testCancellableOptionWhenCancelledFalse.

public void testCancellableOptionWhenCancelledFalse() {
    String nodeId = randomAlphaOfLength(10);
    long taskId = randomIntBetween(0, 100000);
    long startTime = randomNonNegativeLong();
    long runningTime = randomNonNegativeLong();
    boolean cancellable = true;
    boolean cancelled = false;
    TaskInfo taskInfo = new TaskInfo(new TaskId(nodeId, taskId), "test_type", "test_action", "test_description", null, startTime, runningTime, cancellable, cancelled, TaskId.EMPTY_TASK_ID, Collections.singletonMap("foo", "bar"));
    String taskInfoString = taskInfo.toString();
    Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(taskInfoString.getBytes(StandardCharsets.UTF_8)), true).v2();
    assertEquals(map.get("cancellable"), cancellable);
    assertEquals(map.get("cancelled"), cancelled);
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) BytesArray(org.opensearch.common.bytes.BytesArray) TaskId(org.opensearch.tasks.TaskId)

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