Search in sources :

Example 1 with TestTasksRequestBuilder

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder in project OpenSearch by opensearch-project.

the class PersistentTasksExecutorIT method testCreatePersistentTaskWithDuplicateId.

public void testCreatePersistentTaskWithDuplicateId() throws Exception {
    PersistentTasksService persistentTasksService = internalCluster().getInstance(PersistentTasksService.class);
    PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
    String taskId = UUIDs.base64UUID();
    persistentTasksService.sendStartRequest(taskId, TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future);
    future.get();
    PlainActionFuture<PersistentTask<TestParams>> future2 = new PlainActionFuture<>();
    persistentTasksService.sendStartRequest(taskId, TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future2);
    assertFutureThrows(future2, ResourceAlreadyExistsException.class);
    assertBusy(() -> {
        // Wait for the task to start
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().size(), equalTo(1));
    });
    TaskInfo firstRunningTask = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    logger.info("Completing the running task");
    // Fail the running task and make sure it restarts properly
    assertThat(new TestTasksRequestBuilder(client()).setOperation("finish").setTaskId(firstRunningTask.getTaskId()).get().getTasks().size(), equalTo(1));
    logger.info("Waiting for persistent task with id {} to disappear", firstRunningTask.getId());
    assertBusy(() -> {
        // Wait for the task to disappear completely
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks(), empty());
    });
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) TestTasksRequestBuilder(org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)

Example 2 with TestTasksRequestBuilder

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder in project OpenSearch by opensearch-project.

the class PersistentTasksExecutorIT method testPersistentActionFailure.

public void testPersistentActionFailure() throws Exception {
    PersistentTasksService persistentTasksService = internalCluster().getInstance(PersistentTasksService.class);
    PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
    persistentTasksService.sendStartRequest(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future);
    long allocationId = future.get().getAllocationId();
    assertBusy(() -> {
        // Wait for the task to start
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().size(), equalTo(1));
    });
    TaskInfo firstRunningTask = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    logger.info("Found running task with id {} and parent {}", firstRunningTask.getId(), firstRunningTask.getParentTaskId());
    // Verifying parent
    assertThat(firstRunningTask.getParentTaskId().getId(), equalTo(allocationId));
    assertThat(firstRunningTask.getParentTaskId().getNodeId(), equalTo("cluster"));
    logger.info("Failing the running task");
    // Fail the running task and make sure it restarts properly
    assertThat(new TestTasksRequestBuilder(client()).setOperation("fail").setTaskId(firstRunningTask.getTaskId()).get().getTasks().size(), equalTo(1));
    logger.info("Waiting for persistent task with id {} to disappear", firstRunningTask.getId());
    assertBusy(() -> {
        // Wait for the task to disappear completely
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks(), empty());
    });
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) TestTasksRequestBuilder(org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)

Example 3 with TestTasksRequestBuilder

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder in project OpenSearch by opensearch-project.

the class PersistentTasksExecutorIT method testPersistentActionStatusUpdate.

public void testPersistentActionStatusUpdate() throws Exception {
    PersistentTasksService persistentTasksService = internalCluster().getInstance(PersistentTasksService.class);
    PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
    persistentTasksService.sendStartRequest(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future);
    String taskId = future.get().getId();
    waitForTaskToStart();
    TaskInfo firstRunningTask = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    PersistentTasksCustomMetadata tasksInProgress = internalCluster().clusterService().state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    assertThat(tasksInProgress.tasks().size(), equalTo(1));
    assertThat(tasksInProgress.tasks().iterator().next().getState(), nullValue());
    int numberOfUpdates = randomIntBetween(1, 10);
    for (int i = 0; i < numberOfUpdates; i++) {
        logger.info("Updating the task states");
        // Complete the running task and make sure it finishes properly
        assertThat(new TestTasksRequestBuilder(client()).setOperation("update_status").setTaskId(firstRunningTask.getTaskId()).get().getTasks().size(), equalTo(1));
        int finalI = i;
        WaitForPersistentTaskFuture<?> future1 = new WaitForPersistentTaskFuture<>();
        persistentTasksService.waitForPersistentTaskCondition(taskId, task -> task != null && task.getState() != null && task.getState().toString() != null && task.getState().toString().equals("{\"phase\":\"phase " + (finalI + 1) + "\"}"), TimeValue.timeValueSeconds(10), future1);
        assertThat(future1.get().getId(), equalTo(taskId));
    }
    WaitForPersistentTaskFuture<?> future1 = new WaitForPersistentTaskFuture<>();
    persistentTasksService.waitForPersistentTaskCondition(taskId, task -> false, TimeValue.timeValueMillis(10), future1);
    assertFutureThrows(future1, IllegalStateException.class, "timed out after 10ms");
    PlainActionFuture<PersistentTask<?>> failedUpdateFuture = new PlainActionFuture<>();
    persistentTasksService.sendUpdateStateRequest(taskId, -2, new State("should fail"), failedUpdateFuture);
    assertFutureThrows(failedUpdateFuture, ResourceNotFoundException.class, "the task with id " + taskId + " and allocation id -2 doesn't exist");
    // Wait for the task to disappear
    WaitForPersistentTaskFuture<?> future2 = new WaitForPersistentTaskFuture<>();
    persistentTasksService.waitForPersistentTaskCondition(taskId, Objects::isNull, TimeValue.timeValueSeconds(10), future2);
    logger.info("Completing the running task");
    // Complete the running task and make sure it finishes properly
    assertThat(new TestTasksRequestBuilder(client()).setOperation("finish").setTaskId(firstRunningTask.getTaskId()).get().getTasks().size(), equalTo(1));
    assertThat(future2.get(), nullValue());
}
Also used : TestTasksRequestBuilder(org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask) TaskInfo(org.opensearch.tasks.TaskInfo) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) State(org.opensearch.persistent.TestPersistentTasksPlugin.State) Objects(java.util.Objects) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)

Aggregations

PlainActionFuture (org.opensearch.action.support.PlainActionFuture)3 PersistentTask (org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)3 TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)3 TestTasksRequestBuilder (org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder)3 TaskInfo (org.opensearch.tasks.TaskInfo)3 Objects (java.util.Objects)1 State (org.opensearch.persistent.TestPersistentTasksPlugin.State)1