Search in sources :

Example 1 with TestParams

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

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

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

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

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

the class PersistentTasksClusterServiceTests method testReassignTasks.

public void testReassignTasks() {
    ClusterState clusterState = initialState();
    ClusterState.Builder builder = ClusterState.builder(clusterState);
    PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder(clusterState.metadata().custom(PersistentTasksCustomMetadata.TYPE));
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(clusterState.nodes());
    addTestNodes(nodes, randomIntBetween(1, 10));
    int numberOfTasks = randomIntBetween(0, 40);
    for (int i = 0; i < numberOfTasks; i++) {
        switch(randomInt(2)) {
            case 0:
                // add an unassigned task that should get assigned because it's assigned to a non-existing node or unassigned
                addTask(tasks, "assign_me", randomBoolean() ? null : "no_longer_exists");
                break;
            case 1:
                // add a task assigned to non-existing node that should not get assigned
                addTask(tasks, "dont_assign_me", randomBoolean() ? null : "no_longer_exists");
                break;
            case 2:
                addTask(tasks, "assign_one", randomBoolean() ? null : "no_longer_exists");
                break;
        }
    }
    Metadata.Builder metadata = Metadata.builder(clusterState.metadata()).putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build());
    clusterState = builder.metadata(metadata).nodes(nodes).build();
    ClusterState newClusterState = reassign(clusterState);
    PersistentTasksCustomMetadata tasksInProgress = newClusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    assertThat(tasksInProgress, notNullValue());
    assertThat("number of tasks shouldn't change as a result or reassignment", numberOfTasks, equalTo(tasksInProgress.tasks().size()));
    int assignOneCount = 0;
    for (PersistentTask<?> task : tasksInProgress.tasks()) {
        // explanation should correspond to the action name
        switch(((TestParams) task.getParams()).getTestParam()) {
            case "assign_me":
                assertThat(task.getExecutorNode(), notNullValue());
                assertThat(task.isAssigned(), equalTo(true));
                if (clusterState.nodes().nodeExists(task.getExecutorNode()) == false) {
                    logger.info(clusterState.metadata().custom(PersistentTasksCustomMetadata.TYPE).toString());
                }
                assertThat("task should be assigned to a node that is in the cluster, was assigned to " + task.getExecutorNode(), clusterState.nodes().nodeExists(task.getExecutorNode()), equalTo(true));
                assertThat(task.getAssignment().getExplanation(), equalTo("test assignment"));
                break;
            case "dont_assign_me":
                assertThat(task.getExecutorNode(), nullValue());
                assertThat(task.isAssigned(), equalTo(false));
                assertThat(task.getAssignment().getExplanation(), equalTo("no appropriate nodes found for the assignment"));
                break;
            case "assign_one":
                if (task.isAssigned()) {
                    assignOneCount++;
                    assertThat("more than one assign_one tasks are assigned", assignOneCount, lessThanOrEqualTo(1));
                    assertThat(task.getAssignment().getExplanation(), equalTo("test assignment"));
                } else {
                    assertThat(task.getAssignment().getExplanation(), equalTo("only one task can be assigned at a time"));
                }
                break;
            default:
                fail("Unknown action " + task.getTaskName());
        }
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

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