Search in sources :

Example 1 with TaskId

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

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

the class TasksIT method testGetValidTask.

public void testGetValidTask() throws Exception {
    // Run a Reindex to create a task
    final String sourceIndex = "source1";
    final String destinationIndex = "dest";
    Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
    createIndex(sourceIndex, settings);
    createIndex(destinationIndex, settings);
    BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status());
    final ReindexRequest reindexRequest = new ReindexRequest().setSourceIndices(sourceIndex).setDestIndex(destinationIndex);
    final TaskSubmissionResponse taskSubmissionResponse = highLevelClient().submitReindexTask(reindexRequest, RequestOptions.DEFAULT);
    final String taskId = taskSubmissionResponse.getTask();
    assertNotNull(taskId);
    TaskId childTaskId = new TaskId(taskId);
    GetTaskRequest gtr = new GetTaskRequest(childTaskId.getNodeId(), childTaskId.getId());
    gtr.setWaitForCompletion(randomBoolean());
    Optional<GetTaskResponse> getTaskResponse = execute(gtr, highLevelClient().tasks()::get, highLevelClient().tasks()::getAsync);
    assertTrue(getTaskResponse.isPresent());
    GetTaskResponse taskResponse = getTaskResponse.get();
    if (gtr.getWaitForCompletion()) {
        assertTrue(taskResponse.isCompleted());
    }
    org.opensearch.tasks.TaskInfo info = taskResponse.getTaskInfo();
    assertTrue(info.isCancellable());
    assertEquals("reindex from [source1] to [dest]", info.getDescription());
    assertEquals("indices:data/write/reindex", info.getAction());
    if (taskResponse.isCompleted() == false) {
        assertBusy(checkTaskCompletionStatus(client(), taskId));
    }
}
Also used : GetTaskRequest(org.opensearch.client.tasks.GetTaskRequest) TaskSubmissionResponse(org.opensearch.client.tasks.TaskSubmissionResponse) TaskId(org.opensearch.client.tasks.TaskId) GetTaskResponse(org.opensearch.client.tasks.GetTaskResponse) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) Settings(org.opensearch.common.settings.Settings)

Aggregations

TaskId (org.opensearch.client.tasks.TaskId)2 ListTasksRequest (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksRequest)1 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)1 BulkRequest (org.opensearch.action.bulk.BulkRequest)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 CancelTasksRequest (org.opensearch.client.tasks.CancelTasksRequest)1 CancelTasksResponse (org.opensearch.client.tasks.CancelTasksResponse)1 GetTaskRequest (org.opensearch.client.tasks.GetTaskRequest)1 GetTaskResponse (org.opensearch.client.tasks.GetTaskResponse)1 TaskSubmissionResponse (org.opensearch.client.tasks.TaskSubmissionResponse)1 Settings (org.opensearch.common.settings.Settings)1 ReindexRequest (org.opensearch.index.reindex.ReindexRequest)1