Search in sources :

Example 1 with TaskId

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

the class RequestConvertersTests method testRethrottle.

public void testRethrottle() {
    TaskId taskId = new TaskId(randomAlphaOfLength(10), randomIntBetween(1, 100));
    RethrottleRequest rethrottleRequest;
    Float requestsPerSecond;
    Map<String, String> expectedParams = new HashMap<>();
    if (frequently()) {
        requestsPerSecond = (float) randomDoubleBetween(0.0, 100.0, true);
        rethrottleRequest = new RethrottleRequest(taskId, requestsPerSecond);
        expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, Float.toString(requestsPerSecond));
    } else {
        rethrottleRequest = new RethrottleRequest(taskId);
        expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, "-1");
    }
    expectedParams.put("group_by", "none");
    List<Tuple<String, Supplier<Request>>> variants = new ArrayList<>();
    variants.add(new Tuple<String, Supplier<Request>>("_reindex", () -> RequestConverters.rethrottleReindex(rethrottleRequest)));
    variants.add(new Tuple<String, Supplier<Request>>("_update_by_query", () -> RequestConverters.rethrottleUpdateByQuery(rethrottleRequest)));
    variants.add(new Tuple<String, Supplier<Request>>("_delete_by_query", () -> RequestConverters.rethrottleDeleteByQuery(rethrottleRequest)));
    for (Tuple<String, Supplier<Request>> variant : variants) {
        Request request = variant.v2().get();
        assertEquals("/" + variant.v1() + "/" + taskId + "/_rethrottle", request.getEndpoint());
        assertEquals(HttpPost.METHOD_NAME, request.getMethod());
        assertEquals(expectedParams, request.getParameters());
        assertNull(request.getEntity());
    }
    // test illegal RethrottleRequest values
    Exception e = expectThrows(NullPointerException.class, () -> new RethrottleRequest(null, 1.0f));
    assertEquals("taskId cannot be null", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> new RethrottleRequest(new TaskId("taskId", 1), -5.0f));
    assertEquals("requestsPerSecond needs to be positive value but was [-5.0]", e.getMessage());
}
Also used : TaskId(org.opensearch.tasks.TaskId) HashMap(java.util.HashMap) RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Supplier(java.util.function.Supplier) Tuple(org.opensearch.common.collect.Tuple)

Example 2 with TaskId

use of org.opensearch.tasks.TaskId 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 3 with TaskId

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

the class CRUDDocumentationIT method testReindexRethrottle.

@SuppressWarnings("unused")
public void testReindexRethrottle() throws Exception {
    RestHighLevelClient client = highLevelClient();
    TaskId taskId = new TaskId("oTUltX4IQMOUUVeiohTt8A:124");
    {
        // tag::rethrottle-disable-request
        // <1>
        RethrottleRequest request = new RethrottleRequest(taskId);
    // end::rethrottle-disable-request
    }
    {
        // tag::rethrottle-request
        // <1>
        RethrottleRequest request = new RethrottleRequest(taskId, 100.0f);
    // end::rethrottle-request
    }
    {
        RethrottleRequest request = new RethrottleRequest(taskId);
        // tag::rethrottle-request-execution
        // <1>
        client.reindexRethrottle(request, RequestOptions.DEFAULT);
        // <2>
        client.updateByQueryRethrottle(request, RequestOptions.DEFAULT);
        // <3>
        client.deleteByQueryRethrottle(request, RequestOptions.DEFAULT);
    // end::rethrottle-request-execution
    }
    ActionListener<ListTasksResponse> listener;
    // tag::rethrottle-request-async-listener
    listener = new ActionListener<ListTasksResponse>() {

        @Override
        public void onResponse(ListTasksResponse response) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::rethrottle-request-async-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(3);
    listener = new LatchedActionListener<>(listener, latch);
    RethrottleRequest request = new RethrottleRequest(taskId);
    // tag::rethrottle-execute-async
    client.reindexRethrottleAsync(request, RequestOptions.DEFAULT, // <1>
    listener);
    client.updateByQueryRethrottleAsync(request, RequestOptions.DEFAULT, // <2>
    listener);
    client.deleteByQueryRethrottleAsync(request, RequestOptions.DEFAULT, // <3>
    listener);
    // end::rethrottle-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : RethrottleRequest(org.opensearch.client.RethrottleRequest) TaskId(org.opensearch.tasks.TaskId) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchException(org.opensearch.OpenSearchException)

Example 4 with TaskId

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

the class TasksRequestConvertersTests method testCancelTasks.

public void testCancelTasks() {
    Map<String, String> expectedParams = new HashMap<>();
    org.opensearch.client.tasks.TaskId taskId = new org.opensearch.client.tasks.TaskId(randomAlphaOfLength(5), randomNonNegativeLong());
    org.opensearch.client.tasks.TaskId parentTaskId = new org.opensearch.client.tasks.TaskId(randomAlphaOfLength(5), randomNonNegativeLong());
    CancelTasksRequest.Builder builder = new CancelTasksRequest.Builder().withTaskId(taskId).withParentTaskId(parentTaskId);
    expectedParams.put("task_id", taskId.toString());
    expectedParams.put("parent_task_id", parentTaskId.toString());
    if (randomBoolean()) {
        boolean waitForCompletion = randomBoolean();
        builder.withWaitForCompletion(waitForCompletion);
        expectedParams.put("wait_for_completion", Boolean.toString(waitForCompletion));
    }
    Request httpRequest = TasksRequestConverters.cancelTasks(builder.build());
    assertThat(httpRequest, notNullValue());
    assertThat(httpRequest.getMethod(), equalTo(HttpPost.METHOD_NAME));
    assertThat(httpRequest.getEntity(), nullValue());
    assertThat(httpRequest.getEndpoint(), equalTo("/_tasks/_cancel"));
    assertThat(httpRequest.getParameters(), equalTo(expectedParams));
}
Also used : TaskId(org.opensearch.tasks.TaskId) HashMap(java.util.HashMap) CancelTasksRequest(org.opensearch.client.tasks.CancelTasksRequest) ListTasksRequest(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest) CancelTasksRequest(org.opensearch.client.tasks.CancelTasksRequest)

Example 5 with TaskId

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

the class CancelTasksResponseTests method createServerTestInstance.

@Override
protected CancelTasksResponseTests.ByNodeCancelTasksResponse createServerTestInstance(XContentType xContentType) {
    List<org.opensearch.tasks.TaskInfo> tasks = new ArrayList<>();
    List<TaskOperationFailure> taskFailures = new ArrayList<>();
    List<OpenSearchException> nodeFailures = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 4); i++) {
        taskFailures.add(new TaskOperationFailure(randomAlphaOfLength(4), (long) i, new RuntimeException(randomAlphaOfLength(4))));
    }
    for (int i = 0; i < randomIntBetween(1, 4); i++) {
        nodeFailures.add(new OpenSearchException(new RuntimeException(randomAlphaOfLength(10))));
    }
    for (int i = 0; i < 4; i++) {
        boolean cancellable = randomBoolean();
        boolean cancelled = cancellable == true ? randomBoolean() : false;
        tasks.add(new org.opensearch.tasks.TaskInfo(new TaskId(NODE_ID, (long) i), randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(10), new FakeTaskStatus(randomAlphaOfLength(4), randomInt()), randomLongBetween(1, 3), randomIntBetween(5, 10), cancellable, cancelled, new TaskId("node1", randomLong()), Collections.singletonMap("x-header-of", "some-value")));
    }
    return new ByNodeCancelTasksResponse(tasks, taskFailures, nodeFailures);
}
Also used : TaskId(org.opensearch.tasks.TaskId) ArrayList(java.util.ArrayList) TaskInfo(org.opensearch.tasks.TaskInfo) TaskInfo(org.opensearch.tasks.TaskInfo) TaskOperationFailure(org.opensearch.action.TaskOperationFailure) OpenSearchException(org.opensearch.OpenSearchException)

Aggregations

TaskId (org.opensearch.tasks.TaskId)64 TaskInfo (org.opensearch.tasks.TaskInfo)23 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)16 Task (org.opensearch.tasks.Task)16 IOException (java.io.IOException)13 List (java.util.List)13 ActionListener (org.opensearch.action.ActionListener)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)10 ArrayList (java.util.ArrayList)9 CancelTasksRequest (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)9 ListTasksRequest (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest)9 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)9 SearchRequest (org.opensearch.action.search.SearchRequest)8 CancellableTask (org.opensearch.tasks.CancellableTask)8 TaskManager (org.opensearch.tasks.TaskManager)8 ThreadPool (org.opensearch.threadpool.ThreadPool)8 TransportService (org.opensearch.transport.TransportService)8 TimeUnit (java.util.concurrent.TimeUnit)7 Collections (java.util.Collections)6