Search in sources :

Example 1 with ListTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest 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 ListTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest in project OpenSearch by opensearch-project.

the class TasksIT method testListTasks.

public void testListTasks() throws IOException {
    ListTasksRequest request = new ListTasksRequest();
    ListTasksResponse response = execute(request, highLevelClient().tasks()::list, highLevelClient().tasks()::listAsync);
    assertThat(response, notNullValue());
    assertThat(response.getNodeFailures(), equalTo(emptyList()));
    assertThat(response.getTaskFailures(), equalTo(emptyList()));
    // It's possible that there are other tasks except 'cluster:monitor/tasks/lists[n]' and 'action":"cluster:monitor/tasks/lists'
    assertThat(response.getTasks().size(), greaterThanOrEqualTo(2));
    boolean listTasksFound = false;
    for (TaskGroup taskGroup : response.getTaskGroups()) {
        org.opensearch.tasks.TaskInfo parent = taskGroup.getTaskInfo();
        if ("cluster:monitor/tasks/lists".equals(parent.getAction())) {
            assertThat(taskGroup.getChildTasks().size(), equalTo(1));
            TaskGroup childGroup = taskGroup.getChildTasks().iterator().next();
            assertThat(childGroup.getChildTasks().isEmpty(), equalTo(true));
            org.opensearch.tasks.TaskInfo child = childGroup.getTaskInfo();
            assertThat(child.getAction(), equalTo("cluster:monitor/tasks/lists[n]"));
            assertThat(child.getParentTaskId(), equalTo(parent.getTaskId()));
            listTasksFound = true;
        }
    }
    assertTrue("List tasks were not found", listTasksFound);
}
Also used : ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TaskGroup(org.opensearch.action.admin.cluster.node.tasks.list.TaskGroup)

Example 3 with ListTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest in project OpenSearch by opensearch-project.

the class OpenSearchRestHighLevelClientTestCase method findTaskToRethrottle.

protected static TaskId findTaskToRethrottle(String actionName, String description) throws IOException {
    long start = System.nanoTime();
    ListTasksRequest request = new ListTasksRequest();
    request.setActions(actionName);
    request.setDetailed(true);
    do {
        ListTasksResponse list = highLevelClient().tasks().list(request, RequestOptions.DEFAULT);
        list.rethrowFailures("Finding tasks to rethrottle");
        List<TaskGroup> taskGroups = list.getTaskGroups().stream().filter(taskGroup -> taskGroup.getTaskInfo().getDescription().equals(description)).collect(Collectors.toList());
        assertThat("tasks are left over from the last execution of this test", taskGroups, hasSize(lessThan(2)));
        if (0 == taskGroups.size()) {
            // The parent task hasn't started yet
            continue;
        }
        TaskGroup taskGroup = taskGroups.get(0);
        assertThat(taskGroup.getChildTasks(), empty());
        return taskGroup.getTaskInfo().getTaskId();
    } while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(10));
    throw new AssertionError("Couldn't find tasks to rethrottle. Here are the running tasks " + highLevelClient().tasks().list(request, RequestOptions.DEFAULT));
}
Also used : RemoteInfoResponse(org.opensearch.client.cluster.RemoteInfoResponse) TaskGroup(org.opensearch.action.admin.cluster.node.tasks.list.TaskGroup) Arrays(java.util.Arrays) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) EntityUtils(org.apache.http.util.EntityUtils) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) AfterClass(org.junit.AfterClass) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) SearchHit(org.opensearch.search.SearchHit) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) OpenSearchRestTestCase(org.opensearch.test.rest.OpenSearchRestTestCase) Objects(java.util.Objects) Base64(java.util.Base64) List(java.util.List) CheckedRunnable(org.opensearch.common.CheckedRunnable) XContentType(org.opensearch.common.xcontent.XContentType) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) BytesReference(org.opensearch.common.bytes.BytesReference) ClusterUpdateSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) ClusterUpdateSettingsResponse(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) Booleans(org.opensearch.common.Booleans) Matchers.lessThan(org.hamcrest.Matchers.lessThan) SearchRequest(org.opensearch.action.search.SearchRequest) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections.singletonMap(java.util.Collections.singletonMap) SearchResponse(org.opensearch.action.search.SearchResponse) Before(org.junit.Before) Matchers.empty(org.hamcrest.Matchers.empty) Pipeline(org.opensearch.ingest.Pipeline) TaskId(org.opensearch.tasks.TaskId) IOException(java.io.IOException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) IOUtils(org.opensearch.core.internal.io.IOUtils) TimeUnit(java.util.concurrent.TimeUnit) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) RemoteInfoRequest(org.opensearch.client.cluster.RemoteInfoRequest) Collections(java.util.Collections) SearchModule(org.opensearch.search.SearchModule) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TaskGroup(org.opensearch.action.admin.cluster.node.tasks.list.TaskGroup)

Example 4 with ListTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest in project OpenSearch by opensearch-project.

the class RestListTasksAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final ListTasksRequest listTasksRequest = generateListTasksRequest(request);
    final String groupBy = request.param("group_by", "nodes");
    return channel -> client.admin().cluster().listTasks(listTasksRequest, listTasksResponseListener(nodesInCluster, groupBy, channel));
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) TaskId(org.opensearch.tasks.TaskId) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) Supplier(java.util.function.Supplier) RestResponse(org.opensearch.rest.RestResponse) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) Strings(org.opensearch.common.Strings) Collections.singletonList(java.util.Collections.singletonList) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) RestChannel(org.opensearch.rest.RestChannel) ActionListener(org.opensearch.action.ActionListener) BaseRestHandler(org.opensearch.rest.BaseRestHandler) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest)

Example 5 with ListTasksRequest

use of org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest 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

ListTasksRequest (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest)16 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)14 TaskId (org.opensearch.tasks.TaskId)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 IOException (java.io.IOException)6 List (java.util.List)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 CancelTasksRequest (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)5 CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)5 Task (org.opensearch.tasks.Task)5 Map (java.util.Map)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 TaskGroup (org.opensearch.action.admin.cluster.node.tasks.list.TaskGroup)4 TaskInfo (org.opensearch.tasks.TaskInfo)4 ArrayList (java.util.ArrayList)3 ActionListener (org.opensearch.action.ActionListener)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 CancellableTask (org.opensearch.tasks.CancellableTask)2 TaskCancelledException (org.opensearch.tasks.TaskCancelledException)2 StandardCharsets (java.nio.charset.StandardCharsets)1