Search in sources :

Example 11 with Assignment

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

the class PersistentTasksClusterService method reassignTasks.

/**
 * Evaluates the cluster state and tries to assign tasks to nodes.
 *
 * @param currentState the cluster state to analyze
 * @return an updated version of the cluster state
 */
ClusterState reassignTasks(final ClusterState currentState) {
    ClusterState clusterState = currentState;
    final PersistentTasksCustomMetadata tasks = currentState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE);
    if (tasks != null) {
        logger.trace("reassigning {} persistent tasks", tasks.tasks().size());
        final DiscoveryNodes nodes = currentState.nodes();
        // We need to check if removed nodes were running any of the tasks and reassign them
        for (PersistentTask<?> task : tasks.tasks()) {
            if (needsReassignment(task.getAssignment(), nodes)) {
                Assignment assignment = createAssignment(task.getTaskName(), task.getParams(), clusterState);
                if (Objects.equals(assignment, task.getAssignment()) == false) {
                    logger.trace("reassigning task {} from node {} to node {}", task.getId(), task.getAssignment().getExecutorNode(), assignment.getExecutorNode());
                    clusterState = update(clusterState, builder(clusterState).reassignTask(task.getId(), assignment));
                } else {
                    logger.trace("ignoring task {} because assignment is the same {}", task.getId(), assignment);
                }
            } else {
                logger.trace("ignoring task {} because it is still running", task.getId());
            }
        }
    }
    return clusterState;
}
Also used : Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 12 with Assignment

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

the class PersistentTasksClusterServiceTests method testPersistentTasksAssigned.

public void testPersistentTasksAssigned() {
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(new DiscoveryNode("_node_1", buildNewFakeTransportAddress(), Version.CURRENT)).add(new DiscoveryNode("_node_2", buildNewFakeTransportAddress(), Version.CURRENT)).build();
    PersistentTasksCustomMetadata previousTasks = PersistentTasksCustomMetadata.builder().addTask("_task_1", "test", null, new Assignment("_node_1", "")).addTask("_task_2", "test", null, new Assignment(null, "unassigned")).build();
    ClusterState previous = ClusterState.builder(new ClusterName("_name")).nodes(nodes).metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, previousTasks)).build();
    PersistentTasksCustomMetadata currentTasks = PersistentTasksCustomMetadata.builder().addTask("_task_1", "test", null, new Assignment("_node_1", "")).addTask("_task_2", "test", null, new Assignment("_node_2", "")).build();
    ClusterState current = ClusterState.builder(new ClusterName("_name")).nodes(nodes).metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, currentTasks)).build();
    assertTrue("persistent tasks changed (task assigned)", 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)

Example 13 with Assignment

use of org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment 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 14 with Assignment

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

the class PersistentTasksClusterServiceTests method testReassignmentRequiredOnMetadataChanges.

public void testReassignmentRequiredOnMetadataChanges() {
    EnableAssignmentDecider.Allocation allocation = randomFrom(EnableAssignmentDecider.Allocation.values());
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(new DiscoveryNode("_node", buildNewFakeTransportAddress(), Version.CURRENT)).localNodeId("_node").masterNodeId("_node").build();
    boolean unassigned = randomBoolean();
    PersistentTasksCustomMetadata tasks = PersistentTasksCustomMetadata.builder().addTask("_task_1", TestPersistentTasksExecutor.NAME, null, new Assignment(unassigned ? null : "_node", "_reason")).build();
    Metadata metadata = Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasks).persistentSettings(Settings.builder().put(EnableAssignmentDecider.CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING.getKey(), allocation.toString()).build()).build();
    ClusterState previous = ClusterState.builder(new ClusterName("_name")).nodes(nodes).metadata(metadata).build();
    ClusterState current;
    final boolean changed = randomBoolean();
    if (changed) {
        allocation = randomValueOtherThan(allocation, () -> randomFrom(EnableAssignmentDecider.Allocation.values()));
        current = ClusterState.builder(previous).metadata(Metadata.builder(previous.metadata()).persistentSettings(Settings.builder().put(EnableAssignmentDecider.CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING.getKey(), allocation.toString()).build()).build()).build();
    } else {
        current = ClusterState.builder(previous).build();
    }
    final ClusterChangedEvent event = new ClusterChangedEvent("test", current, previous);
    final PersistentTasksClusterService service = createService((params, clusterState) -> randomNodeAssignment(clusterState.nodes()));
    assertThat(dumpEvent(event), service.shouldReassignPersistentTasks(event), equalTo(changed && unassigned));
}
Also used : Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) EnableAssignmentDecider(org.opensearch.persistent.decider.EnableAssignmentDecider) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ClusterName(org.opensearch.cluster.ClusterName) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 15 with Assignment

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

the class PersistentTasksClusterServiceTests method testUnassignTask.

public void testUnassignTask() {
    ClusterState clusterState = initialState();
    ClusterState.Builder builder = ClusterState.builder(clusterState);
    PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder(clusterState.metadata().custom(PersistentTasksCustomMetadata.TYPE));
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder().add(new DiscoveryNode("_node_1", buildNewFakeTransportAddress(), Version.CURRENT)).localNodeId("_node_1").masterNodeId("_node_1").add(new DiscoveryNode("_node_2", buildNewFakeTransportAddress(), Version.CURRENT));
    String unassignedId = addTask(tasks, "unassign", "_node_2");
    Metadata.Builder metadata = Metadata.builder(clusterState.metadata()).putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build());
    clusterState = builder.metadata(metadata).nodes(nodes).build();
    setState(clusterService, clusterState);
    PersistentTasksClusterService service = createService((params, currentState) -> new Assignment("_node_2", "test"));
    service.unassignPersistentTask(unassignedId, tasks.getLastAllocationId(), "unassignment test", ActionListener.wrap(task -> {
        assertThat(task.getAssignment().getExecutorNode(), is(nullValue()));
        assertThat(task.getId(), equalTo(unassignedId));
        assertThat(task.getAssignment().getExplanation(), equalTo("unassignment test"));
    }, e -> fail()));
}
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) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Mockito.anyString(org.mockito.Mockito.anyString) Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) 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