Search in sources :

Example 1 with Assignment

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

the class PersistentTasksClusterService method shouldReassignPersistentTasks.

/**
 * Returns true if the cluster state change(s) require to reassign some persistent tasks. It can happen in the following
 * situations: a node left or is added, the routing table changed, the master node changed, the metadata changed or the
 * persistent tasks changed.
 */
boolean shouldReassignPersistentTasks(final ClusterChangedEvent event) {
    final PersistentTasksCustomMetadata tasks = event.state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    if (tasks == null) {
        return false;
    }
    boolean masterChanged = event.previousState().nodes().isLocalNodeElectedMaster() == false;
    if (persistentTasksChanged(event) || event.nodesChanged() || event.routingTableChanged() || event.metadataChanged() || masterChanged) {
        for (PersistentTask<?> task : tasks.tasks()) {
            if (needsReassignment(task.getAssignment(), event.state().nodes())) {
                Assignment assignment = createAssignment(task.getTaskName(), task.getParams(), event.state());
                if (Objects.equals(assignment, task.getAssignment()) == false) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment)

Example 2 with Assignment

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

the class PersistentTasksClusterServiceTests method testNeedsReassignment.

public void testNeedsReassignment() {
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(new DiscoveryNode("_node_1", buildNewFakeTransportAddress(), Version.CURRENT)).add(new DiscoveryNode("_node_2", buildNewFakeTransportAddress(), Version.CURRENT)).build();
    assertTrue(needsReassignment(new Assignment(null, "unassigned"), nodes));
    assertTrue(needsReassignment(new Assignment("_node_left", "assigned to a node that left"), nodes));
    assertFalse(needsReassignment(new Assignment("_node_1", "assigned"), nodes));
}
Also used : Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 3 with Assignment

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

the class PersistentTasksClusterServiceTests method significantChange.

private ClusterState significantChange(ClusterState clusterState) {
    ClusterState.Builder builder = ClusterState.builder(clusterState);
    PersistentTasksCustomMetadata tasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    if (tasks != null) {
        if (randomBoolean()) {
            for (PersistentTask<?> task : tasks.tasks()) {
                if (task.isAssigned() && clusterState.nodes().nodeExists(task.getExecutorNode())) {
                    logger.info("removed node {}", task.getExecutorNode());
                    builder.nodes(DiscoveryNodes.builder(clusterState.nodes()).remove(task.getExecutorNode()));
                    return builder.build();
                }
            }
        }
    }
    boolean tasksOrNodesChanged = false;
    // add a new unassigned task
    if (hasAssignableTasks(tasks, clusterState.nodes()) == false) {
        // we don't have any unassigned tasks - add some
        if (randomBoolean()) {
            logger.info("added random task");
            addRandomTask(builder, Metadata.builder(clusterState.metadata()), PersistentTasksCustomMetadata.builder(tasks), null);
            tasksOrNodesChanged = true;
        } else {
            logger.info("added unassignable task with custom assignment message");
            addRandomTask(builder, Metadata.builder(clusterState.metadata()), PersistentTasksCustomMetadata.builder(tasks), new Assignment(null, "change me"), "never_assign");
            tasksOrNodesChanged = true;
        }
    }
    // add a node if there are unassigned tasks
    if (clusterState.nodes().getNodes().isEmpty()) {
        logger.info("added random node");
        builder.nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode(randomAlphaOfLength(10))));
        tasksOrNodesChanged = true;
    }
    if (tasksOrNodesChanged == false) {
        // change routing table to simulate a change
        logger.info("changed routing table");
        Metadata.Builder metadata = Metadata.builder(clusterState.metadata());
        RoutingTable.Builder routingTable = RoutingTable.builder(clusterState.routingTable());
        changeRoutingTable(metadata, routingTable);
        builder.metadata(metadata).routingTable(routingTable.build());
    }
    return builder.build();
}
Also used : Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 4 with Assignment

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

the class PersistentTasksClusterServiceTests method randomNodeAssignment.

private Assignment randomNodeAssignment(DiscoveryNodes nodes) {
    if (nodes.getNodes().isEmpty()) {
        return NO_NODE_FOUND;
    }
    List<String> nodeList = new ArrayList<>();
    for (ObjectCursor<String> node : nodes.getNodes().keys()) {
        nodeList.add(node.value);
    }
    String node = randomFrom(nodeList);
    if (node != null) {
        return new Assignment(node, "test assignment");
    } else {
        return NO_NODE_FOUND;
    }
}
Also used : Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString)

Example 5 with Assignment

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

the class PersistentTasksClusterServiceTests method testPersistentTasksChangedTaskAdded.

public void testPersistentTasksChangedTaskAdded() {
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(new DiscoveryNode("_node_1", buildNewFakeTransportAddress(), Version.CURRENT)).build();
    ClusterState previous = ClusterState.builder(new ClusterName("_name")).nodes(nodes).build();
    PersistentTasksCustomMetadata tasks = PersistentTasksCustomMetadata.builder().addTask("_task_1", "test", null, new Assignment(null, "_reason")).build();
    ClusterState current = ClusterState.builder(new ClusterName("_name")).nodes(nodes).metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasks)).build();
    assertTrue("persistent tasks changed (task added)", persistentTasksChanged(new ClusterChangedEvent("test", current, previous)));
}
Also used : Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterName(org.opensearch.cluster.ClusterName) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Aggregations

Assignment (org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment)19 ClusterState (org.opensearch.cluster.ClusterState)14 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)11 ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)10 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)10 ClusterName (org.opensearch.cluster.ClusterName)9 Mockito.anyString (org.mockito.Mockito.anyString)8 Metadata (org.opensearch.cluster.metadata.Metadata)8 TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)7 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)5 ArrayList (java.util.ArrayList)4 ActionListener (org.opensearch.action.ActionListener)4 ObjectCursor (com.carrotsearch.hppc.cursors.ObjectCursor)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Collections.emptyMap (java.util.Collections.emptyMap)3 Collections.singleton (java.util.Collections.singleton)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3