Search in sources :

Example 16 with TestParams

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

the class PersistentTasksClusterServiceTests method assignOnlyOneTaskAtATime.

private Assignment assignOnlyOneTaskAtATime(ClusterState clusterState) {
    DiscoveryNodes nodes = clusterState.nodes();
    PersistentTasksCustomMetadata tasksInProgress = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    if (tasksInProgress.findTasks(TestPersistentTasksExecutor.NAME, task -> "assign_one".equals(((TestParams) task.getParams()).getTestParam()) && nodes.nodeExists(task.getExecutorNode())).isEmpty()) {
        return randomNodeAssignment(clusterState.nodes());
    } else {
        return new Assignment(null, "only one task can be assigned at a time");
    }
}
Also used : Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) BiFunction(java.util.function.BiFunction) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) ClusterServiceUtils.setState(org.opensearch.test.ClusterServiceUtils.setState) Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) TestPersistentTasksExecutor(org.opensearch.persistent.TestPersistentTasksPlugin.TestPersistentTasksExecutor) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Collections.singleton(java.util.Collections.singleton) After(org.junit.After) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Is.is(org.hamcrest.core.Is.is) ActionListener(org.opensearch.action.ActionListener) AfterClass(org.junit.AfterClass) TimeValue(org.opensearch.common.unit.TimeValue) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) EnableAssignmentDecider(org.opensearch.persistent.decider.EnableAssignmentDecider) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) PersistentTasksClusterService.needsReassignment(org.opensearch.persistent.PersistentTasksClusterService.needsReassignment) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Mockito.any(org.mockito.Mockito.any) NO_NODE_FOUND(org.opensearch.persistent.PersistentTasksExecutor.NO_NODE_FOUND) Mockito.mock(org.mockito.Mockito.mock) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) BeforeClass(org.junit.BeforeClass) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) VersionUtils(org.opensearch.test.VersionUtils) UUIDs(org.opensearch.common.UUIDs) Mockito.anyString(org.mockito.Mockito.anyString) PersistentTasksClusterService.persistentTasksChanged(org.opensearch.persistent.PersistentTasksClusterService.persistentTasksChanged) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) Mockito.when(org.mockito.Mockito.when) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Collections(java.util.Collections) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 17 with TestParams

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

the class PersistentTasksClusterServiceTests method insignificantChange.

private ClusterState insignificantChange(ClusterState clusterState) {
    ClusterState.Builder builder = ClusterState.builder(clusterState);
    PersistentTasksCustomMetadata tasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    tasks = removeTasksWithChangingAssignment(tasks);
    PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(tasks);
    if (randomBoolean()) {
        if (hasAssignableTasks(tasks, clusterState.nodes()) == false) {
            // we don't have any unassigned tasks - adding a node or changing a routing table shouldn't affect anything
            if (randomBoolean()) {
                logger.info("added random node");
                builder.nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode(randomAlphaOfLength(10))));
            }
            if (randomBoolean()) {
                logger.info("added random unassignable task");
                addRandomTask(builder, Metadata.builder(clusterState.metadata()), tasksBuilder, NO_NODE_FOUND, "never_assign");
                return builder.build();
            }
            logger.info("changed routing table");
            Metadata.Builder metadata = Metadata.builder(clusterState.metadata());
            metadata.putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build());
            RoutingTable.Builder routingTable = RoutingTable.builder(clusterState.routingTable());
            changeRoutingTable(metadata, routingTable);
            builder.metadata(metadata).routingTable(routingTable.build());
            return builder.build();
        }
    }
    if (randomBoolean()) {
        // remove a node that doesn't have any tasks assigned to it and it's not the master node
        for (DiscoveryNode node : clusterState.nodes()) {
            if (hasTasksAssignedTo(tasks, node.getId()) == false && "this_node".equals(node.getId()) == false) {
                logger.info("removed unassigned node {}", node.getId());
                return builder.nodes(DiscoveryNodes.builder(clusterState.nodes()).remove(node.getId())).build();
            }
        }
    }
    if (randomBoolean()) {
        // clear the task
        if (randomBoolean()) {
            logger.info("removed all tasks");
            Metadata.Builder metadata = Metadata.builder(clusterState.metadata()).putCustom(PersistentTasksCustomMetadata.TYPE, PersistentTasksCustomMetadata.builder().build());
            return builder.metadata(metadata).build();
        } else {
            logger.info("set task custom to null");
            Metadata.Builder metadata = Metadata.builder(clusterState.metadata()).removeCustom(PersistentTasksCustomMetadata.TYPE);
            return builder.metadata(metadata).build();
        }
    }
    logger.info("removed all unassigned tasks and changed routing table");
    if (tasks != null) {
        for (PersistentTask<?> task : tasks.tasks()) {
            if (task.getExecutorNode() == null || "never_assign".equals(((TestParams) task.getParams()).getTestParam())) {
                tasksBuilder.removeTask(task.getId());
            }
        }
    }
    // Just add a random index - that shouldn't change anything
    IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(Settings.builder().put("index.version.created", VersionUtils.randomVersion(random()))).numberOfShards(1).numberOfReplicas(1).build();
    Metadata.Builder metadata = Metadata.builder(clusterState.metadata()).put(indexMetadata, false).putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build());
    return builder.metadata(metadata).build();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 18 with TestParams

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

the class PersistentTasksClusterServiceTests method addTask.

private String addTask(PersistentTasksCustomMetadata.Builder tasks, String param, String node) {
    String id = UUIDs.base64UUID();
    tasks.addTask(id, TestPersistentTasksExecutor.NAME, new TestParams(param), new Assignment(node, "explanation: " + param));
    return id;
}
Also used : Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) Mockito.anyString(org.mockito.Mockito.anyString)

Example 19 with TestParams

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

Example 20 with TestParams

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

the class PersistentTasksCustomMetadataTests method addRandomTask.

private String addRandomTask(Builder builder) {
    String taskId = UUIDs.base64UUID();
    builder.addTask(taskId, TestPersistentTasksExecutor.NAME, new TestParams(randomAlphaOfLength(10)), randomAssignment());
    return taskId;
}
Also used : TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)

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