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;
}
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)));
}
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");
}
}
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));
}
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()));
}
Aggregations