Search in sources :

Example 1 with State

use of org.opensearch.persistent.TestPersistentTasksPlugin.State 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)

Example 2 with State

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

the class PersistentTasksCustomMetadataTests method testBuilder.

public void testBuilder() {
    PersistentTasksCustomMetadata persistentTasks = null;
    String lastKnownTask = "";
    for (int i = 0; i < randomIntBetween(10, 100); i++) {
        final Builder builder;
        if (randomBoolean()) {
            builder = PersistentTasksCustomMetadata.builder();
        } else {
            builder = PersistentTasksCustomMetadata.builder(persistentTasks);
        }
        boolean changed = false;
        for (int j = 0; j < randomIntBetween(1, 10); j++) {
            switch(randomInt(3)) {
                case 0:
                    lastKnownTask = addRandomTask(builder);
                    changed = true;
                    break;
                case 1:
                    if (builder.hasTask(lastKnownTask)) {
                        changed = true;
                        builder.reassignTask(lastKnownTask, randomAssignment());
                    } else {
                        String fLastKnownTask = lastKnownTask;
                        expectThrows(ResourceNotFoundException.class, () -> builder.reassignTask(fLastKnownTask, randomAssignment()));
                    }
                    break;
                case 2:
                    if (builder.hasTask(lastKnownTask)) {
                        changed = true;
                        builder.updateTaskState(lastKnownTask, randomBoolean() ? new State(randomAlphaOfLength(10)) : null);
                    } else {
                        String fLastKnownTask = lastKnownTask;
                        expectThrows(ResourceNotFoundException.class, () -> builder.updateTaskState(fLastKnownTask, null));
                    }
                    break;
                case 3:
                    if (builder.hasTask(lastKnownTask)) {
                        changed = true;
                        builder.removeTask(lastKnownTask);
                    } else {
                        String fLastKnownTask = lastKnownTask;
                        expectThrows(ResourceNotFoundException.class, () -> builder.removeTask(fLastKnownTask));
                    }
                    break;
            }
        }
        assertEquals(changed, builder.isChanged());
        persistentTasks = builder.build();
    }
}
Also used : State(org.opensearch.persistent.TestPersistentTasksPlugin.State) ClusterState(org.opensearch.cluster.ClusterState) Builder(org.opensearch.persistent.PersistentTasksCustomMetadata.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with State

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

the class PersistentTasksCustomMetadataTests method createTestInstance.

@Override
protected PersistentTasksCustomMetadata createTestInstance() {
    int numberOfTasks = randomInt(10);
    PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
    for (int i = 0; i < numberOfTasks; i++) {
        String taskId = UUIDs.base64UUID();
        tasks.addTask(taskId, TestPersistentTasksExecutor.NAME, new TestParams(randomAlphaOfLength(10)), randomAssignment());
        if (randomBoolean()) {
            // From time to time update status
            tasks.updateTaskState(taskId, new State(randomAlphaOfLength(10)));
        }
    }
    return tasks.build();
}
Also used : State(org.opensearch.persistent.TestPersistentTasksPlugin.State) ClusterState(org.opensearch.cluster.ClusterState) Builder(org.opensearch.persistent.PersistentTasksCustomMetadata.Builder) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)

Aggregations

State (org.opensearch.persistent.TestPersistentTasksPlugin.State)3 ClusterState (org.opensearch.cluster.ClusterState)2 Builder (org.opensearch.persistent.PersistentTasksCustomMetadata.Builder)2 TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)2 Objects (java.util.Objects)1 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 PersistentTask (org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)1 TestTasksRequestBuilder (org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder)1 TaskInfo (org.opensearch.tasks.TaskInfo)1