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