Search in sources :

Example 11 with TestParams

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestParams 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 12 with TestParams

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

Example 13 with TestParams

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

the class PersistentTasksExecutorIT method testUnassignRunningPersistentTask.

public void testUnassignRunningPersistentTask() 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));
    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);
    PersistentTask<TestParams> task = future.get();
    String taskId = task.getId();
    Settings nodeSettings = Settings.builder().put(nodeSettings(0)).put("node.attr.test_attr", "test").build();
    internalCluster().startNode(nodeSettings);
    waitForTaskToStart();
    PlainActionFuture<PersistentTask<?>> unassignmentFuture = new PlainActionFuture<>();
    // Disallow re-assignment after it is unallocated to verify master and node state
    TestPersistentTasksExecutor.setNonClusterStateCondition(false);
    persistentTasksClusterService.unassignPersistentTask(taskId, task.getAllocationId() + 1, "unassignment test", unassignmentFuture);
    PersistentTask<?> unassignedTask = unassignmentFuture.get();
    assertThat(unassignedTask.getId(), equalTo(taskId));
    assertThat(unassignedTask.getAssignment().getExplanation(), equalTo("unassignment test"));
    assertThat(unassignedTask.getAssignment().getExecutorNode(), is(nullValue()));
    assertBusy(() -> {
        // Verify that the task is NOT running on the node
        List<TaskInfo> tasks = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks();
        assertThat(tasks.size(), equalTo(0));
        // Verify that the task is STILL in internal cluster state
        assertClusterStateHasTask(taskId);
    });
    // Allow it to be reassigned again to the same node
    TestPersistentTasksExecutor.setNonClusterStateCondition(true);
    // Verify it starts again
    waitForTaskToStart();
    assertClusterStateHasTask(taskId);
    // Complete or cancel the running task
    TaskInfo taskInfo = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().get(0);
    stopOrCancelTask(taskInfo.getTaskId());
}
Also used : PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask) TaskInfo(org.opensearch.tasks.TaskInfo) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) Settings(org.opensearch.common.settings.Settings)

Example 14 with TestParams

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

the class PersistentTasksExecutorFullRestartIT method testFullClusterRestart.

public void testFullClusterRestart() throws Exception {
    PersistentTasksService service = internalCluster().getInstance(PersistentTasksService.class);
    int numberOfTasks = randomIntBetween(1, 10);
    String[] taskIds = new String[numberOfTasks];
    List<PlainActionFuture<PersistentTask<TestParams>>> futures = new ArrayList<>(numberOfTasks);
    for (int i = 0; i < numberOfTasks; i++) {
        PlainActionFuture<PersistentTask<TestParams>> future = new PlainActionFuture<>();
        futures.add(future);
        taskIds[i] = UUIDs.base64UUID();
        service.sendStartRequest(taskIds[i], TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future);
    }
    for (int i = 0; i < numberOfTasks; i++) {
        assertThat(futures.get(i).get().getId(), equalTo(taskIds[i]));
    }
    PersistentTasksCustomMetadata tasksInProgress = internalCluster().clusterService().state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    assertThat(tasksInProgress.tasks().size(), equalTo(numberOfTasks));
    // Make sure that at least one of the tasks is running
    assertBusy(() -> {
        // Wait for the task to start
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().size(), greaterThan(0));
    });
    // Restart cluster
    internalCluster().fullRestart();
    ensureYellow();
    tasksInProgress = internalCluster().clusterService().state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    assertThat(tasksInProgress.tasks().size(), equalTo(numberOfTasks));
    // Check that cluster state is correct
    for (int i = 0; i < numberOfTasks; i++) {
        PersistentTask<?> task = tasksInProgress.getTask(taskIds[i]);
        assertNotNull(task);
    }
    logger.info("Waiting for {} tasks to start", numberOfTasks);
    assertBusy(() -> {
        // Wait for all tasks to start
        assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get().getTasks().size(), equalTo(numberOfTasks));
    });
    logger.info("Complete all tasks");
    // Complete the running task and make sure it finishes properly
    assertThat(new TestPersistentTasksPlugin.TestTasksRequestBuilder(client()).setOperation("finish").get().getTasks().size(), equalTo(numberOfTasks));
    assertBusy(() -> {
        // Make sure the task is removed from the cluster state
        assertThat(((PersistentTasksCustomMetadata) internalCluster().clusterService().state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE)).tasks(), empty());
    });
}
Also used : ArrayList(java.util.ArrayList) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)

Example 15 with TestParams

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

the class EnableAssignmentDeciderIT method testEnableAssignmentAfterRestart.

/**
 * Test that the {@link EnableAssignmentDecider#CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING} setting correctly
 * prevents persistent tasks to be assigned after a cluster restart.
 */
public void testEnableAssignmentAfterRestart() throws Exception {
    final int numberOfTasks = randomIntBetween(1, 10);
    logger.trace("creating {} persistent tasks", numberOfTasks);
    final CountDownLatch latch = new CountDownLatch(numberOfTasks);
    for (int i = 0; i < numberOfTasks; i++) {
        PersistentTasksService service = internalCluster().getInstance(PersistentTasksService.class);
        service.sendStartRequest("task_" + i, TestPersistentTasksExecutor.NAME, new TestParams(randomAlphaOfLength(10)), ActionListener.wrap(latch::countDown));
    }
    latch.await();
    ClusterService clusterService = internalCluster().clusterService(internalCluster().getMasterName());
    PersistentTasksCustomMetadata tasks = clusterService.state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    assertEquals(numberOfTasks, tasks.tasks().stream().filter(t -> TestPersistentTasksExecutor.NAME.equals(t.getTaskName())).count());
    logger.trace("waiting for the tasks to be running");
    assertBusy(() -> {
        ListTasksResponse listTasks = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get();
        assertThat(listTasks.getTasks().size(), equalTo(numberOfTasks));
    });
    try {
        logger.trace("disable persistent tasks assignment");
        disablePersistentTasksAssignment();
        logger.trace("restart the cluster");
        internalCluster().fullRestart();
        ensureYellow();
        logger.trace("persistent tasks assignment is still disabled");
        assertEnableAssignmentSetting(Allocation.NONE);
        logger.trace("persistent tasks are not assigned");
        tasks = internalCluster().clusterService().state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
        assertEquals(numberOfTasks, tasks.tasks().stream().filter(t -> TestPersistentTasksExecutor.NAME.equals(t.getTaskName())).filter(t -> t.isAssigned() == false).count());
        ListTasksResponse runningTasks = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get();
        assertThat(runningTasks.getTasks().size(), equalTo(0));
        logger.trace("enable persistent tasks assignment");
        if (randomBoolean()) {
            enablePersistentTasksAssignment();
        } else {
            resetPersistentTasksAssignment();
        }
        assertBusy(() -> {
            ListTasksResponse listTasks = client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]").get();
            assertThat(listTasks.getTasks().size(), equalTo(numberOfTasks));
        });
    } finally {
        resetPersistentTasksAssignment();
    }
}
Also used : OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) TestPersistentTasksPlugin(org.opensearch.persistent.TestPersistentTasksPlugin) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) PersistentTasksService(org.opensearch.persistent.PersistentTasksService) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) Collection(java.util.Collection) PersistentTasksCustomMetadata(org.opensearch.persistent.PersistentTasksCustomMetadata) Settings(org.opensearch.common.settings.Settings) Plugin(org.opensearch.plugins.Plugin) Collections.singletonList(java.util.Collections.singletonList) TestPersistentTasksExecutor(org.opensearch.persistent.TestPersistentTasksPlugin.TestPersistentTasksExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ClusterService(org.opensearch.cluster.service.ClusterService) Allocation(org.opensearch.persistent.decider.EnableAssignmentDecider.Allocation) ActionListener(org.opensearch.action.ActionListener) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING(org.opensearch.persistent.decider.EnableAssignmentDecider.CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING) ClusterService(org.opensearch.cluster.service.ClusterService) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) PersistentTasksService(org.opensearch.persistent.PersistentTasksService) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentTasksCustomMetadata(org.opensearch.persistent.PersistentTasksCustomMetadata) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)

Aggregations

TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)21 PersistentTask (org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)12 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)8 ClusterState (org.opensearch.cluster.ClusterState)8 TaskInfo (org.opensearch.tasks.TaskInfo)7 Mockito.anyString (org.mockito.Mockito.anyString)6 Metadata (org.opensearch.cluster.metadata.Metadata)6 ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)5 Assignment (org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment)5 ActionListener (org.opensearch.action.ActionListener)4 Settings (org.opensearch.common.settings.Settings)4 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)3 TaskManager (org.opensearch.tasks.TaskManager)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2