Search in sources :

Example 6 with PersistentTask

use of org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask in project OpenSearch by opensearch-project.

the class PersistentTasksCustomMetadataTests method testSerializationContext.

@SuppressWarnings("unchecked")
public void testSerializationContext() throws Exception {
    PersistentTasksCustomMetadata testInstance = createTestInstance();
    for (int i = 0; i < randomInt(10); i++) {
        testInstance = (PersistentTasksCustomMetadata) makeTestChanges(testInstance);
    }
    ToXContent.MapParams params = new ToXContent.MapParams(Collections.singletonMap(Metadata.CONTEXT_MODE_PARAM, randomFrom(CONTEXT_MODE_SNAPSHOT, CONTEXT_MODE_GATEWAY)));
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference shuffled = toShuffledXContent(testInstance, xContentType, params, false);
    PersistentTasksCustomMetadata newInstance;
    try (XContentParser parser = createParser(XContentFactory.xContent(xContentType), shuffled)) {
        newInstance = doParseInstance(parser);
    }
    assertNotSame(newInstance, testInstance);
    assertEquals(testInstance.tasks().size(), newInstance.tasks().size());
    for (PersistentTask<?> testTask : testInstance.tasks()) {
        PersistentTask<TestParams> newTask = (PersistentTask<TestParams>) newInstance.getTask(testTask.getId());
        assertNotNull(newTask);
        // Things that should be serialized
        assertEquals(testTask.getTaskName(), newTask.getTaskName());
        assertEquals(testTask.getId(), newTask.getId());
        assertEquals(testTask.getState(), newTask.getState());
        assertEquals(testTask.getParams(), newTask.getParams());
        // Things that shouldn't be serialized
        assertEquals(0, newTask.getAllocationId());
        assertNull(newTask.getExecutorNode());
    }
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) XContentParser(org.opensearch.common.xcontent.XContentParser) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)

Example 7 with PersistentTask

use of org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask in project OpenSearch by opensearch-project.

the class PersistentTasksNodeServiceTests method testRegisterTaskFails.

public void testRegisterTaskFails() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    final Client mockClient = mock(Client.class);
    final ThreadPool threadPool = mock(ThreadPool.class);
    when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY));
    when(threadPool.generic()).thenReturn(OpenSearchExecutors.newDirectExecutorService());
    when(mockClient.threadPool()).thenReturn(threadPool);
    when(mockClient.settings()).thenReturn(Settings.EMPTY);
    PersistentTasksService persistentTasksService = new PersistentTasksService(null, null, mockClient) {

        @Override
        public void sendCompletionRequest(String taskId, long taskAllocationId, Exception taskFailure, ActionListener<PersistentTask<?>> listener) {
            assertThat(taskFailure, instanceOf(RuntimeException.class));
            assertThat(taskFailure.getMessage(), equalTo("Something went wrong"));
            listener.onResponse(mock(PersistentTask.class));
            latch.countDown();
        }
    };
    @SuppressWarnings("unchecked") PersistentTasksExecutor<TestParams> action = mock(PersistentTasksExecutor.class);
    when(action.getExecutor()).thenReturn(ThreadPool.Names.SAME);
    when(action.getTaskName()).thenReturn(TestPersistentTasksExecutor.NAME);
    when(action.createTask(anyLong(), anyString(), anyString(), any(), any(), any())).thenThrow(new RuntimeException("Something went wrong"));
    PersistentTasksExecutorRegistry registry = new PersistentTasksExecutorRegistry(Collections.singletonList(action));
    MockExecutor executor = new MockExecutor();
    PersistentTasksNodeService coordinator = new PersistentTasksNodeService(persistentTasksService, registry, new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet()), executor);
    ClusterState state = createInitialClusterState(0, Settings.EMPTY);
    PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
    tasks.addTask(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, new TestParams("this_param"), new Assignment("this_node", "test assignment on this node"));
    Metadata.Builder metadata = Metadata.builder(state.metadata());
    metadata.putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build());
    ClusterState newClusterState = ClusterState.builder(state).metadata(metadata).build();
    coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
    // Failed to start the task, make sure it wasn't invoked further
    assertThat(executor.executions.size(), equalTo(0));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Metadata(org.opensearch.cluster.metadata.Metadata) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask) Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) TaskManager(org.opensearch.tasks.TaskManager) ActionListener(org.opensearch.action.ActionListener) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) Client(org.opensearch.client.Client)

Example 8 with PersistentTask

use of org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask in project OpenSearch by opensearch-project.

the class PersistentTasksService method waitForPersistentTaskCondition.

/**
 * Waits for a given persistent task to comply with a given predicate, then call back the listener accordingly.
 *
 * @param taskId the persistent task id
 * @param predicate the persistent task predicate to evaluate
 * @param timeout a timeout for waiting
 * @param listener the callback listener
 */
public void waitForPersistentTaskCondition(final String taskId, final Predicate<PersistentTask<?>> predicate, @Nullable final TimeValue timeout, final WaitForPersistentTaskListener<?> listener) {
    final Predicate<ClusterState> clusterStatePredicate = clusterState -> predicate.test(PersistentTasksCustomMetadata.getTaskWithId(clusterState, taskId));
    final ClusterStateObserver observer = new ClusterStateObserver(clusterService, timeout, logger, threadPool.getThreadContext());
    final ClusterState clusterState = observer.setAndGetObservedState();
    if (clusterStatePredicate.test(clusterState)) {
        listener.onResponse(PersistentTasksCustomMetadata.getTaskWithId(clusterState, taskId));
    } else {
        observer.waitForNextChange(new ClusterStateObserver.Listener() {

            @Override
            public void onNewClusterState(ClusterState state) {
                listener.onResponse(PersistentTasksCustomMetadata.getTaskWithId(state, taskId));
            }

            @Override
            public void onClusterServiceClose() {
                listener.onFailure(new NodeClosedException(clusterService.localNode()));
            }

            @Override
            public void onTimeout(TimeValue timeout) {
                listener.onTimeout(timeout);
            }
        }, clusterStatePredicate);
    }
}
Also used : Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) Predicate(java.util.function.Predicate) ThreadPool(org.opensearch.threadpool.ThreadPool) TaskId(org.opensearch.tasks.TaskId) ActionRequest(org.opensearch.action.ActionRequest) Nullable(org.opensearch.common.Nullable) ClusterState(org.opensearch.cluster.ClusterState) Logger(org.apache.logging.log4j.Logger) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) ClusterService(org.opensearch.cluster.service.ClusterService) NodeClosedException(org.opensearch.node.NodeClosedException) ActionType(org.opensearch.action.ActionType) ActionListener(org.opensearch.action.ActionListener) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) LogManager(org.apache.logging.log4j.LogManager) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) OriginSettingClient(org.opensearch.client.OriginSettingClient) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask) ClusterState(org.opensearch.cluster.ClusterState) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) NodeClosedException(org.opensearch.node.NodeClosedException) TimeValue(org.opensearch.common.unit.TimeValue)

Example 9 with PersistentTask

use of org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask 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 10 with PersistentTask

use of org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask in project OpenSearch by opensearch-project.

the class PersistentTasksExecutorIT method testPersistentActionCompletion.

public void testPersistentActionCompletion() 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);
    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]").setDetailed(true).get().getTasks().get(0);
    logger.info("Found running task with id {} and parent {}", firstRunningTask.getId(), firstRunningTask.getParentTaskId());
    // Verifying parent and description
    assertThat(firstRunningTask.getParentTaskId().getId(), equalTo(allocationId));
    assertThat(firstRunningTask.getParentTaskId().getNodeId(), equalTo("cluster"));
    assertThat(firstRunningTask.getDescription(), equalTo("id=" + taskId));
    if (randomBoolean()) {
        logger.info("Simulating errant completion notification");
        // try sending completion request with incorrect allocation id
        PlainActionFuture<PersistentTask<?>> failedCompletionNotificationFuture = new PlainActionFuture<>();
        persistentTasksService.sendCompletionRequest(taskId, Long.MAX_VALUE, null, failedCompletionNotificationFuture);
        assertFutureThrows(failedCompletionNotificationFuture, ResourceNotFoundException.class);
        // Make sure that the task is still running
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").setDetailed(true).get().getTasks().size(), equalTo(1));
    }
    stopOrCancelTask(firstRunningTask.getTaskId());
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)

Aggregations

PersistentTask (org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)14 TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)11 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)8 TaskInfo (org.opensearch.tasks.TaskInfo)7 ClusterState (org.opensearch.cluster.ClusterState)5 ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)4 ActionListener (org.opensearch.action.ActionListener)3 Client (org.opensearch.client.Client)3 Metadata (org.opensearch.cluster.metadata.Metadata)3 ClusterService (org.opensearch.cluster.service.ClusterService)3 TestTasksRequestBuilder (org.opensearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder)3 IOException (java.io.IOException)2 Mockito.anyString (org.mockito.Mockito.anyString)2 CancelTasksResponse (org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)2 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)2 Settings (org.opensearch.common.settings.Settings)2 TaskId (org.opensearch.tasks.TaskId)2 TaskManager (org.opensearch.tasks.TaskManager)2 ClusterServiceUtils.createClusterService (org.opensearch.test.ClusterServiceUtils.createClusterService)2