Search in sources :

Example 1 with PersistentTask

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

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

the class PersistentTasksExecutorIT method testPersistentActionWithNonClusterStateCondition.

public void testPersistentActionWithNonClusterStateCondition() throws Exception {
    PersistentTasksClusterService persistentTasksClusterService = internalCluster().getInstance(PersistentTasksClusterService.class, internalCluster().getMasterName());
    // Speed up rechecks to a rate that is quicker than what settings would allow
    persistentTasksClusterService.setRecheckInterval(TimeValue.timeValueMillis(1));
    TestPersistentTasksExecutor.setNonClusterStateCondition(false);
    PersistentTasksService persistentTasksService = internalCluster().getInstance(PersistentTasksService.class);
    PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
    TestParams testParams = new TestParams("Blah");
    persistentTasksService.sendStartRequest(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, testParams, future);
    String taskId = future.get().getId();
    assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks(), empty());
    TestPersistentTasksExecutor.setNonClusterStateCondition(true);
    waitForTaskToStart();
    TaskInfo taskInfo = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    // Verifying the task can now be assigned
    assertThat(taskInfo.getTaskId().getNodeId(), notNullValue());
    // Remove the persistent task
    PlainActionFuture<PersistentTask<?>> removeFuture = new PlainActionFuture<>();
    persistentTasksService.sendRemoveRequest(taskId, removeFuture);
    assertEquals(removeFuture.get().getId(), taskId);
}
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)

Example 3 with PersistentTask

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

the class PersistentTasksExecutorIT method testPersistentActionWithNoAvailableNode.

public void testPersistentActionWithNoAvailableNode() throws Exception {
    PersistentTasksService persistentTasksService = internalCluster().getInstance(PersistentTasksService.class);
    PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
    TestParams testParams = new TestParams("Blah");
    testParams.setExecutorNodeAttr("test");
    persistentTasksService.sendStartRequest(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, testParams, future);
    String taskId = future.get().getId();
    Settings nodeSettings = Settings.builder().put(nodeSettings(0)).put("node.attr.test_attr", "test").build();
    String newNode = internalCluster().startNode(nodeSettings);
    String newNodeId = internalCluster().clusterService(newNode).localNode().getId();
    waitForTaskToStart();
    TaskInfo taskInfo = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    // Verifying the task runs on the new node
    assertThat(taskInfo.getTaskId().getNodeId(), equalTo(newNodeId));
    internalCluster().stopRandomNode(settings -> "test".equals(settings.get("node.attr.test_attr")));
    assertBusy(() -> {
        // Wait for the task to disappear completely
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks(), empty());
    });
    // Remove the persistent task
    PlainActionFuture<PersistentTask<?>> removeFuture = new PlainActionFuture<>();
    persistentTasksService.sendRemoveRequest(taskId, removeFuture);
    assertEquals(removeFuture.get().getId(), taskId);
}
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) Settings(org.opensearch.common.settings.Settings)

Example 4 with PersistentTask

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

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