Search in sources :

Example 1 with CancelTasksResponse

use of org.opensearch.client.tasks.CancelTasksResponse in project OpenSearch by opensearch-project.

the class TasksIT method testCancelTasks.

public void testCancelTasks() throws IOException {
    ListTasksRequest listRequest = new ListTasksRequest();
    ListTasksResponse listResponse = execute(listRequest, highLevelClient().tasks()::list, highLevelClient().tasks()::listAsync);
    // in this case, probably no task will actually be cancelled.
    // this is ok, that case is covered in TasksIT.testTasksCancellation
    org.opensearch.tasks.TaskInfo firstTask = listResponse.getTasks().get(0);
    String node = listResponse.getPerNodeTasks().keySet().iterator().next();
    CancelTasksRequest cancelTasksRequest = new CancelTasksRequest.Builder().withTaskId(new TaskId(node, firstTask.getId())).build();
    CancelTasksResponse response = execute(cancelTasksRequest, highLevelClient().tasks()::cancel, highLevelClient().tasks()::cancelAsync);
    // Since the task may or may not have been cancelled, assert that we received a response only
    // The actual testing of task cancellation is covered by TasksIT.testTasksCancellation
    assertThat(response, notNullValue());
}
Also used : TaskId(org.opensearch.client.tasks.TaskId) CancelTasksResponse(org.opensearch.client.tasks.CancelTasksResponse) CancelTasksRequest(org.opensearch.client.tasks.CancelTasksRequest) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Example 2 with CancelTasksResponse

use of org.opensearch.client.tasks.CancelTasksResponse in project OpenSearch by opensearch-project.

the class TasksClientDocumentationIT method testCancelTasks.

@SuppressWarnings("unused")
public void testCancelTasks() throws IOException {
    RestHighLevelClient client = highLevelClient();
    {
        // tag::cancel-tasks-request
        CancelTasksRequest request = new org.opensearch.client.tasks.CancelTasksRequest.Builder().withNodesFiltered(Arrays.asList("nodeId1", "nodeId2")).withActionsFiltered(Collections.singletonList("cluster:*")).build();
        // end::cancel-tasks-request
        // tag::cancel-tasks-request-filter
        CancelTasksRequest byTaskIdRequest = // <1>
        new org.opensearch.client.tasks.CancelTasksRequest.Builder().withTaskId(// <2>
        new org.opensearch.client.tasks.TaskId("myNode", 44L)).withWaitForCompletion(// <3>
        true).build();
    // end::cancel-tasks-request-filter
    }
    CancelTasksRequest request = new org.opensearch.client.tasks.CancelTasksRequest.Builder().build();
    // tag::cancel-tasks-execute
    CancelTasksResponse response = client.tasks().cancel(request, RequestOptions.DEFAULT);
    // end::cancel-tasks-execute
    assertThat(response, notNullValue());
    // tag::cancel-tasks-response-tasks
    // <1>
    List<org.opensearch.client.tasks.TaskInfo> tasks = response.getTasks();
    // end::cancel-tasks-response-tasks
    // tag::cancel-tasks-response-calc
    // <1>
    Map<String, List<org.opensearch.client.tasks.TaskInfo>> perNodeTasks = response.getPerNodeTasks();
    // <2>
    List<org.opensearch.client.tasks.TaskGroup> groups = response.getTaskGroups();
    // end::cancel-tasks-response-calc
    // tag::cancel-tasks-response-failures
    // <1>
    List<org.opensearch.client.tasks.OpenSearchException> nodeFailures = response.getNodeFailures();
    // <2>
    List<org.opensearch.client.tasks.TaskOperationFailure> taskFailures = response.getTaskFailures();
    // end::cancel-tasks-response-failures
    assertThat(response.getNodeFailures(), equalTo(emptyList()));
    assertThat(response.getTaskFailures(), equalTo(emptyList()));
}
Also used : TaskId(org.opensearch.tasks.TaskId) CancelTasksResponse(org.opensearch.client.tasks.CancelTasksResponse) CancelTasksRequest(org.opensearch.client.tasks.CancelTasksRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) TaskInfo(org.opensearch.tasks.TaskInfo) TaskOperationFailure(org.opensearch.action.TaskOperationFailure) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) OpenSearchException(org.opensearch.OpenSearchException) TaskGroup(org.opensearch.action.admin.cluster.node.tasks.list.TaskGroup)

Aggregations

CancelTasksRequest (org.opensearch.client.tasks.CancelTasksRequest)2 CancelTasksResponse (org.opensearch.client.tasks.CancelTasksResponse)2 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 OpenSearchException (org.opensearch.OpenSearchException)1 TaskOperationFailure (org.opensearch.action.TaskOperationFailure)1 ListTasksRequest (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest)1 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)1 TaskGroup (org.opensearch.action.admin.cluster.node.tasks.list.TaskGroup)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1 TaskId (org.opensearch.client.tasks.TaskId)1 TaskId (org.opensearch.tasks.TaskId)1 TaskInfo (org.opensearch.tasks.TaskInfo)1