Apache Solr open-source search software
SOLR-17331: More optimal placements with OrderedNodePlacementPlugin (#2515)
Houston Putmanfc0d84afaa8b
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex bd113b9974c..03876f1a414 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -129,6 +129,8 @@ Improvements* SOLR-17109: Give security manager explicit read access to sharedLib (Tomás Fernández Löbbe via Eric Pugh)+* SOLR-17331: OrderedNodePlacementPlugin will give an even more optimal replica placements during ReplicaMigration commands (Houston Putman, Yohann Callea)+Optimizations---------------------* SOLR-17257: Both Minimize Cores and the Affinity replica placement strategies would over-gatherdiff --git a/solr/core/src/java/org/apache/solr/cluster/placement/plugins/OrderedNodePlacementPlugin.java b/solr/core/src/java/org/apache/solr/cluster/placement/plugins/OrderedNodePlacementPlugin.javaindex 5c8d195954e..86ca79526b3 100644--- a/solr/core/src/java/org/apache/solr/cluster/placement/plugins/OrderedNodePlacementPlugin.java+++ b/solr/core/src/java/org/apache/solr/cluster/placement/plugins/OrderedNodePlacementPlugin.java@@ -69,23 +69,26 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {Set<Node> allNodes = new HashSet<>();Set<SolrCollection> allCollections = new HashSet<>();- Deque<PendingPlacementRequest> pendingRequests = new ArrayDeque<>(requests.size());+ // This helps us keep track if we have made a full lap of the outstanding requests+ // without doing a placement or not.+ int placementCount = 0;+ Deque<OutstandingPlacementRequest> outstandingRequests = new ArrayDeque<>(requests.size());for (PlacementRequest request : requests) {- PendingPlacementRequest pending = new PendingPlacementRequest(request);- pendingRequests.add(pending);+ OutstandingPlacementRequest outstanding = new OutstandingPlacementRequest(request);+ outstandingRequests.add(outstanding);placementPlans.add(placementContext.getPlacementPlanFactory()- .createPlacementPlan(request, pending.getComputedPlacementSet()));+ .createPlacementPlan(request, outstanding.getComputedPlacementSet()));allNodes.addAll(request.getTargetNodes());allCollections.add(request.getCollection());}Collection<WeightedNode> weightedNodes =getWeightedNodes(placementContext, allNodes, allCollections, true).values();- while (!pendingRequests.isEmpty()) {- PendingPlacementRequest request = pendingRequests.poll();- if (!request.isPending()) {+ while (!outstandingRequests.isEmpty()) {+ OutstandingPlacementRequest request = outstandingRequests.poll();+ if (request.isComplete()) {continue;}@@ -94,9 +97,9 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {SolrCollection solrCollection = request.getCollection();// Now place all replicas of all shards on available nodes- for (String shardName : request.getPendingShards()) {- for (Replica.ReplicaType replicaType : request.getPendingReplicaTypes(shardName)) {- int replicaCount = request.getPendingReplicas(shardName, replicaType);+ for (String shardName : request.getOutstandingShards()) {+ for (Replica.ReplicaType replicaType : request.getOutstandingReplicaTypes(shardName)) {+ int replicaCount = request.getOutstandingReplicas(shardName, replicaType);if (log.isDebugEnabled()) {log.debug("Placing {} replicas for Collection: {}, Shard: {}, ReplicaType: {}",@@ -131,12 +134,13 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {// options than we have replicas to place, that's ok, because the replicas will likely// be put on all the tie options.//- // Only skip the request if it can be requeued, and there are other pending requests to- // compute.+ // Only skip the request if it can be requeued, and there are other outstanding requests+ // to+ // compute. If the next outstanding request cannot be requeued,int numWeightTies = nodesForReplicaType.peekTies();- if (!pendingRequests.isEmpty()- && request.canBeRequeued()- && numWeightTies > (replicaCount - replicasPlaced)) {+ if (numWeightTies > (replicaCount - replicasPlaced)+ && !outstandingRequests.isEmpty()+ && request.canBeRequeued(placementCount)) {log.debug("There is a tie for best weight. There are more options ({}) than replicas to place ({}), so try this placement request later: {}",numWeightTies,@@ -151,6 +155,7 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {node.addReplica(createProjectedReplica(solrCollection, shardName, replicaType, node.getNode()));replicasPlaced += 1;+ placementCount += 1;request.addPlacement(placementContext.getPlacementPlanFactory()@@ -185,9 +190,9 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {}}}- if (request.isPending()) {- request.requeue();- pendingRequests.add(request);+ if (!request.isComplete()) {+ request.requeue(placementCount);+ outstandingRequests.add(request);}}return placementPlans;@@ -808,8 +813,8 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {}/** Context for a placement request still has replicas that need to be placed. */- static class PendingPlacementRequest {- boolean hasBeenRequeued;+ static class OutstandingPlacementRequest {+ int requeuedAtPlacementCount;final SolrCollection collection;@@ -821,8 +826,8 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {// A live view on how many replicas still need to be placed for each shard & replica typefinal Map<String, Map<Replica.ReplicaType, Integer>> replicasToPlaceForShards;- public PendingPlacementRequest(PlacementRequest request) {- hasBeenRequeued = false;+ public OutstandingPlacementRequest(PlacementRequest request) {+ requeuedAtPlacementCount = -1;collection = request.getCollection();targetNodes = request.getTargetNodes();Set<String> shards = request.getShardNames();@@ -843,13 +848,13 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {}/**- * Determine if this request is not yet complete, and there are requested replicas that have not+ * Determine if this request is complete, and there are no requested replicas that have not yet* had placements computed.*- * @return if there are still replica placements that need to be computed+ * @return if all replica placements have been computed*/- public boolean isPending() {- return !replicasToPlaceForShards.isEmpty();+ public boolean isComplete() {+ return replicasToPlaceForShards.isEmpty();}public SolrCollection getCollection() {@@ -864,7 +869,7 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {* The set of ReplicaPlacements computed for this request.** <p>The list that is returned is the same list used internally, so it will be augmented until- * {@link #isPending()} returns false.+ * {@link #isComplete()} returns true.** @return The live set of replicaPlacements for this request.*/@@ -879,7 +884,7 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {** @return list of unfinished shards*/- public Collection<String> getPendingShards() {+ public Collection<String> getOutstandingShards() {return new ArrayList<>(replicasToPlaceForShards.keySet());}@@ -890,7 +895,7 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {* @param shard name of the shard to check for uncomputed placements* @return the set of unfinished replica types*/- public Collection<Replica.ReplicaType> getPendingReplicaTypes(String shard) {+ public Collection<Replica.ReplicaType> getOutstandingReplicaTypes(String shard) {return Optional.ofNullable(replicasToPlaceForShards.get(shard)).map(Map::keySet)// Use a sorted TreeSet to make sure that tests are repeatable@@ -906,30 +911,36 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {* @param type type of replica to be placed* @return the number of replicas that have not yet had placements computed*/- public int getPendingReplicas(String shard, Replica.ReplicaType type) {+ public int getOutstandingReplicas(String shard, Replica.ReplicaType type) {return Optional.ofNullable(replicasToPlaceForShards.get(shard)).map(m -> m.get(type)).orElse(0);}/**- * Currently, only of requeue is allowed per pending request.+ * The request can be requeued as long as there have been placements since the last time it was+ * requeued. If no placements have been made since, we will not allow a requeue which will force+ * placements to be determined. This removes the possibility of a never-ending loop.** @return true if the request has not been requeued already*/- public boolean canBeRequeued() {- return !hasBeenRequeued;+ public boolean canBeRequeued(int placementCount) {+ return requeuedAtPlacementCount < placementCount;}- /** Let the pending request know that it has been requeued */- public void requeue() {- hasBeenRequeued = true;+ /**+ * Let the request know that it has been requeued+ *+ * @param placementCount the number of placements made at the time of the requeue+ */+ public void requeue(int placementCount) {+ requeuedAtPlacementCount = placementCount;}/**- * Track the given replica placement for this pending request.+ * Track the given replica placement for this request.*- * @param replica placement that has been made for the pending request+ * @param replica placement that has been made for the request*/public void addPlacement(ReplicaPlacement replica) {computedPlacements.add(replica);diff --git a/solr/core/src/test/org/apache/solr/cloud/MigrateReplicasTest.java b/solr/core/src/test/org/apache/solr/cloud/MigrateReplicasTest.javaindex f96b621c023..39ccf43fb53 100644--- a/solr/core/src/test/org/apache/solr/cloud/MigrateReplicasTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/MigrateReplicasTest.java@@ -237,7 +237,7 @@ public class MigrateReplicasTest extends SolrCloudTestCase {}@Test- public void testGoodSpreadDuringAssignWithNoTarget() throws Exception {+ public void testWithNoTarget() throws Exception {configureCluster(5).addConfig("conf1", TEST_PATH().resolve("configsets").resolve("cloud-dynamic").resolve("conf"))@@ -300,9 +300,9 @@ public class MigrateReplicasTest extends SolrCloudTestCase {}for (String node : eventualTargetNodes) {- assertEquals(- "The non-source node '" + node + "' has the wrong number of replicas after the migration",- 2,+ assertNotEquals(+ "The non-source node '" + node + "' should not receive all replicas from the migration",+ 4,collection.getReplicas(node).size());}}diff --git a/solr/core/src/test/org/apache/solr/cluster/placement/plugins/MinimizeCoresPlacementFactoryTest.java b/solr/core/src/test/org/apache/solr/cluster/placement/plugins/MinimizeCoresPlacementFactoryTest.javaindex d85d30c8a13..bb058a734bb 100644--- a/solr/core/src/test/org/apache/solr/cluster/placement/plugins/MinimizeCoresPlacementFactoryTest.java+++ b/solr/core/src/test/org/apache/solr/cluster/placement/plugins/MinimizeCoresPlacementFactoryTest.java@@ -39,7 +39,7 @@ import org.junit.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;-/** Unit test for {@link AffinityPlacementFactory} */+/** Unit test for {@link MinimizeCoresPlacementFactoryTest} */public class MinimizeCoresPlacementFactoryTest extends AbstractPlacementFactoryTest {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());diff --git a/solr/core/src/test/org/apache/solr/cluster/placement/plugins/SimplePlacementFactoryTest.java b/solr/core/src/test/org/apache/solr/cluster/placement/plugins/SimplePlacementFactoryTest.javanew file mode 100644index 00000000000..2f4d81f8658--- /dev/null+++ b/solr/core/src/test/org/apache/solr/cluster/placement/plugins/SimplePlacementFactoryTest.java@@ -0,0 +1,370 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.cluster.placement.plugins;++import java.lang.invoke.MethodHandles;+import java.util.Collections;+import java.util.HashSet;+import java.util.Iterator;+import java.util.List;+import java.util.Locale;+import java.util.Map;+import java.util.Set;+import java.util.stream.Collectors;+import org.apache.solr.cluster.Node;+import org.apache.solr.cluster.Shard;+import org.apache.solr.cluster.SolrCollection;+import org.apache.solr.cluster.placement.BalancePlan;+import org.apache.solr.cluster.placement.Builders;+import org.apache.solr.cluster.placement.PlacementContext;+import org.apache.solr.cluster.placement.PlacementPlan;+import org.apache.solr.cluster.placement.PlacementPlugin;+import org.apache.solr.cluster.placement.PlacementRequest;+import org.apache.solr.cluster.placement.ReplicaPlacement;+import org.apache.solr.cluster.placement.impl.BalanceRequestImpl;+import org.apache.solr.cluster.placement.impl.PlacementRequestImpl;+import org.apache.solr.common.cloud.ReplicaCount;+import org.junit.Before;+import org.junit.Test;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++/** Unit test for {@link SimplePlacementFactoryTest} */+public class SimplePlacementFactoryTest extends AbstractPlacementFactoryTest {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ private PlacementPlugin plugin;++ @Before+ public void setupPlugin() {+ configurePlugin();+ }++ private void configurePlugin() {+ SimplePlacementFactory factory = new SimplePlacementFactory();+ plugin = factory.createPluginInstance();+ }++ @Test+ public void testBasicPlacementNewCollection() throws Exception {+ testBasicPlacementInternal(false);+ }++ @Test+ public void testBasicPlacementExistingCollection() throws Exception {+ testBasicPlacementInternal(true);+ }++ /**+ * When this test places a replica for a new collection, it should pick the node with fewer cores.+ *+ * <p>+ *+ * <p>When it places a replica for an existing collection, it should pick the node with fewer+ * cores that doesn't already have a replica for the shard.+ */+ private void testBasicPlacementInternal(boolean hasExistingCollection) throws Exception {+ String collectionName = "basicCollection";++ Builders.ClusterBuilder clusterBuilder = Builders.newClusterBuilder().initializeLiveNodes(2);+ List<Builders.NodeBuilder> nodeBuilders = clusterBuilder.getLiveNodeBuilders();++ Builders.CollectionBuilder collectionBuilder = Builders.newCollectionBuilder(collectionName);++ if (hasExistingCollection) {+ // Existing collection has replicas for its shards and is visible in the cluster state+ collectionBuilder.initializeShardsReplicas(1, 1, 0, 0, nodeBuilders);+ clusterBuilder.addCollection(collectionBuilder);+ } else {+ // New collection to create has the shards defined but no replicas and is not present in+ // cluster state+ collectionBuilder.initializeShardsReplicas(1, 0, 0, 0, List.of());+ }++ PlacementContext placementContext = clusterBuilder.buildPlacementContext();++ SolrCollection solrCollection = collectionBuilder.build();+ List<Node> liveNodes = clusterBuilder.buildLiveNodes();++ // Place a new replica for the (only) existing shard of the collection+ PlacementRequestImpl placementRequest =+ new PlacementRequestImpl(+ solrCollection,+ Set.of(solrCollection.shards().iterator().next().getShardName()),+ new HashSet<>(liveNodes),+ ReplicaCount.of(1, 0, 0));++ PlacementPlan pp = plugin.computePlacement(placementRequest, placementContext);++ assertEquals(1, pp.getReplicaPlacements().size());+ ReplicaPlacement rp = pp.getReplicaPlacements().iterator().next();+ assertEquals(hasExistingCollection ? liveNodes.get(1) : liveNodes.get(0), rp.getNode());+ }++ /**+ * Tests that existing collection replicas are taken into account when preventing more than one+ * replica per shard to be placed on any node.+ */+ @Test+ public void testMultiplePlacementsWithExistingReplicas() throws Exception {+ String collectionName = "existingCollection";++ // Cluster nodes and their attributes+ Builders.ClusterBuilder clusterBuilder = Builders.newClusterBuilder().initializeLiveNodes(3);++ // The collection already exists with shards and replicas+ Builders.CollectionBuilder collectionBuilder = Builders.newCollectionBuilder(collectionName);+ // Note that the collection as defined below is in a state that would NOT be returned by the+ // placement plugin: shard 1 has two replicas on node 0. The plugin should still be able to+ // place additional replicas as long as they don't break the rules.+ List<List<String>> shardsReplicas =+ List.of(+ List.of("NRT 0"), // shard 1+ List.of("NRT 1"), // shard 2+ List.of("NRT 2")); // shard 3+ collectionBuilder.customCollectionSetup(shardsReplicas, clusterBuilder.getLiveNodeBuilders());+ clusterBuilder.addCollection(collectionBuilder);+ SolrCollection solrCollection = collectionBuilder.build();++ List<Node> liveNodes = clusterBuilder.buildLiveNodes();++ // Place an additional NRT and an additional TLOG replica for each shard+ PlacementRequestImpl placementRequest =+ new PlacementRequestImpl(+ solrCollection,+ solrCollection.getShardNames(),+ new HashSet<>(liveNodes),+ ReplicaCount.of(1, 0, 0));++ List<PlacementRequest> placementRequests =+ solrCollection.getShardNames().stream()+ .map(+ shard ->+ new PlacementRequestImpl(+ solrCollection,+ Collections.singleton(shard),+ new HashSet<>(liveNodes),+ ReplicaCount.of(1, 0, 0)))+ .collect(Collectors.toList());+ // Randomize the order of the requests+ Collections.shuffle(placementRequests, random());++ // The replicas must be placed on the most appropriate nodes, i.e. those that do not already+ // have a replica for the shard and then on the node with the lowest number of cores. NRT are+ // placed first and given the cluster state here the placement is deterministic (easier to test,+ // only one good placement).+ List<PlacementPlan> pps =+ plugin.computePlacements(placementRequests, clusterBuilder.buildPlacementContext());++ List<ReplicaPlacement> replicaPlacements =+ pps.stream().flatMap(pp -> pp.getReplicaPlacements().stream()).collect(Collectors.toList());+ replicaPlacements.forEach(+ rp ->+ assertNotEquals(+ "Two replicas of the same shard on the same node",+ solrCollection.getShard(rp.getShardName()).iterator().next().getNode(),+ rp.getNode()));+ Map<Node, List<ReplicaPlacement>> placementsByNode =+ replicaPlacements.stream()+ .collect(Collectors.groupingBy(ReplicaPlacement::getNode, Collectors.toList()));+ for (Map.Entry<Node, List<ReplicaPlacement>> entry : placementsByNode.entrySet()) {+ assertEquals(+ String.format(+ Locale.ROOT,+ "Node %s has multiple replicas placed on it, when each node should have 1 placement: [%s]",+ entry.getKey().getName(),+ entry.getValue().stream()+ .map(ReplicaPlacement::getShardName)+ .collect(Collectors.joining(", "))),+ 1,+ entry.getValue().size());+ }+ }++ /**+ * Tests that existing collection replicas are taken into account when preventing more than one+ * replica per shard to be placed on any node.+ */+ @Test+ public void testPlacementWithExistingReplicas() throws Exception {+ String collectionName = "existingCollection";++ // Cluster nodes and their attributes+ Builders.ClusterBuilder clusterBuilder = Builders.newClusterBuilder().initializeLiveNodes(5);++ // The collection already exists with shards and replicas+ Builders.CollectionBuilder collectionBuilder = Builders.newCollectionBuilder(collectionName);+ // Note that the collection as defined below is in a state that would NOT be returned by the+ // placement plugin: shard 1 has two replicas on node 0. The plugin should still be able to+ // place additional replicas as long as they don't break the rules.+ List<List<String>> shardsReplicas =+ List.of(+ List.of("NRT 0", "TLOG 0", "NRT 3"), // shard 1+ List.of("NRT 1", "NRT 3", "TLOG 2")); // shard 2+ collectionBuilder.customCollectionSetup(shardsReplicas, clusterBuilder.getLiveNodeBuilders());+ clusterBuilder.addCollection(collectionBuilder);+ SolrCollection solrCollection = collectionBuilder.build();++ List<Node> liveNodes = clusterBuilder.buildLiveNodes();++ // Place an additional NRT and an additional TLOG replica for each shard+ PlacementRequestImpl placementRequest =+ new PlacementRequestImpl(+ solrCollection,+ solrCollection.getShardNames(),+ new HashSet<>(liveNodes),+ ReplicaCount.of(1, 1, 0));++ // The replicas must be placed on the most appropriate nodes, i.e. those that do not already+ // have a replica for the shard and then on the node with the lowest number of cores. NRT are+ // placed first and given the cluster state here the placement is deterministic (easier to test,+ // only one good placement).+ PlacementPlan pp =+ plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());++ // Each expected placement is represented as a string "shard replica-type node"+ Set<String> expectedPlacements = Set.of("1 NRT 1", "1 TLOG 2", "2 TLOG 0", "2 NRT 4");+ verifyPlacements(expectedPlacements, pp, collectionBuilder.getShardBuilders(), liveNodes);+ }++ /**+ * Tests that if a collection has replicas on nodes not currently live, placement for new replicas+ * works ok.+ */+ @Test+ public void testCollectionOnDeadNodes() throws Exception {+ String collectionName = "walkingDead";++ // Cluster nodes and their attributes+ Builders.ClusterBuilder clusterBuilder = Builders.newClusterBuilder().initializeLiveNodes(3);++ // The collection already exists with shards and replicas+ Builders.CollectionBuilder collectionBuilder = Builders.newCollectionBuilder(collectionName);+ // The collection below has shard 1 having replicas only on dead nodes and shard 2 no replicas+ // at all... (which is likely a challenging condition to recover from, but the placement+ // computations should still execute happily).+ List<List<String>> shardsReplicas =+ List.of(+ List.of("NRT 10", "TLOG 11"), // shard 1+ List.of()); // shard 2+ collectionBuilder.customCollectionSetup(shardsReplicas, clusterBuilder.getLiveNodeBuilders());+ clusterBuilder.addCollection(collectionBuilder);+ SolrCollection solrCollection = collectionBuilder.build();++ List<Node> liveNodes = clusterBuilder.buildLiveNodes();++ // Place an additional PULL replica for shard 1+ PlacementRequestImpl placementRequest =+ new PlacementRequestImpl(+ solrCollection,+ Set.of(solrCollection.iterator().next().getShardName()),+ new HashSet<>(liveNodes),+ ReplicaCount.of(0, 0, 1));++ PlacementPlan pp =+ plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());++ // Each expected placement is represented as a string "shard replica-type node"+ // Node 0 has fewer cores than node 1 (0 vs 1) so the placement should go there.+ Set<String> expectedPlacements = Set.of("1 PULL 0");+ verifyPlacements(expectedPlacements, pp, collectionBuilder.getShardBuilders(), liveNodes);++ // If we placed instead a replica for shard 2 (starting with the same initial cluster state, not+ // including the first placement above), it should go too to node 0 since it has fewer cores...+ Iterator<Shard> it = solrCollection.iterator();+ it.next(); // skip first shard to do placement for the second one...+ placementRequest =+ new PlacementRequestImpl(+ solrCollection,+ Set.of(it.next().getShardName()),+ new HashSet<>(liveNodes),+ ReplicaCount.of(0, 0, 1));+ pp = plugin.computePlacement(placementRequest, clusterBuilder.buildPlacementContext());+ expectedPlacements = Set.of("2 PULL 0");+ verifyPlacements(expectedPlacements, pp, collectionBuilder.getShardBuilders(), liveNodes);+ }++ /** Tests replica balancing across all nodes in a cluster */+ @Test+ public void testBalancing() throws Exception {+ // Cluster nodes and their attributes+ Builders.ClusterBuilder clusterBuilder = Builders.newClusterBuilder().initializeLiveNodes(5);+ List<Builders.NodeBuilder> nodeBuilders = clusterBuilder.getLiveNodeBuilders();++ // The collection already exists with shards and replicas+ Builders.CollectionBuilder collectionBuilder = Builders.newCollectionBuilder("a");+ // Note that the collection as defined below is in a state that would NOT be returned by the+ // placement plugin: shard 1 has two replicas on node 0. The plugin should still be able to+ // place additional replicas as long as they don't break the rules.+ List<List<String>> shardsReplicas =+ List.of(+ List.of("NRT 0", "TLOG 0", "NRT 3"), // shard 1+ List.of("NRT 1", "NRT 3", "TLOG 2")); // shard 2+ collectionBuilder.customCollectionSetup(shardsReplicas, nodeBuilders);+ clusterBuilder.addCollection(collectionBuilder);++ BalanceRequestImpl balanceRequest =+ new BalanceRequestImpl(new HashSet<>(clusterBuilder.buildLiveNodes()));+ BalancePlan balancePlan =+ plugin.computeBalancing(balanceRequest, clusterBuilder.buildPlacementContext());++ // Each expected placement is represented as a string "col shard replica-type fromNode ->+ // toNode"+ Set<String> expectedPlacements = Set.of("a 1 NRT 0 -> 4");+ verifyBalancing(+ expectedPlacements,+ balancePlan,+ collectionBuilder.getShardBuilders(),+ clusterBuilder.buildLiveNodes());+ }++ /** Tests that balancing works across a subset of nodes */+ @Test+ public void testBalancingWithNodeSubset() throws Exception {+ // Cluster nodes and their attributes+ Builders.ClusterBuilder clusterBuilder = Builders.newClusterBuilder().initializeLiveNodes(5);+ // The collection already exists with shards and replicas+ Builders.CollectionBuilder collectionBuilder = Builders.newCollectionBuilder("a");+ // Note that the collection as defined below is in a state that would NOT be returned by the+ // placement plugin: shard 1 has two replicas on node 0. The plugin should still be able to+ // place additional replicas as long as they don't break the rules.+ List<List<String>> shardsReplicas =+ List.of(+ List.of("NRT 0", "TLOG 0", "NRT 3"), // shard 1+ List.of("NRT 1", "NRT 3", "TLOG 2")); // shard 2+ collectionBuilder.customCollectionSetup(shardsReplicas, clusterBuilder.getLiveNodeBuilders());+ clusterBuilder.addCollection(collectionBuilder);++ // Only balance over node 1-4+ List<Node> overNodes = clusterBuilder.buildLiveNodes();+ overNodes.remove(0);++ BalanceRequestImpl balanceRequest = new BalanceRequestImpl(new HashSet<>(overNodes));+ BalancePlan balancePlan =+ plugin.computeBalancing(balanceRequest, clusterBuilder.buildPlacementContext());++ // Each expected placement is represented as a string "col shard replica-type fromNode ->+ // toNode"+ Set<String> expectedPlacements = Set.of("a 1 NRT 3 -> 4");+ verifyBalancing(+ expectedPlacements,+ balancePlan,+ collectionBuilder.getShardBuilders(),+ clusterBuilder.buildLiveNodes());+ }+}