diff --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.java index de83db8e967..b6f417764a3 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 @@ -403,10 +403,12 @@ protected void verifyDeleteReplicas( public abstract static class WeightedNode implements Comparable { private final Node node; private final Map>> replicas; + private final Set allReplicas; //a flattened list of all replicas, computing from the map could be costly public WeightedNode(Node node) { this.node = node; this.replicas = new HashMap<>(); + this.allReplicas = new HashSet<>(); } public Node getNode() { @@ -414,10 +416,11 @@ public Node getNode() { } public Set getAllReplicasOnNode() { - return replicas.values().stream() - .flatMap(shard -> shard.values().stream()) - .flatMap(Collection::stream) - .collect(Collectors.toSet()); + return new HashSet<>(allReplicas); + } + + public int getAllReplicaCount() { + return allReplicas.size(); } public Set getCollectionsOnNode() { @@ -454,6 +457,7 @@ public boolean canAddReplica(Replica replica) { } private boolean addReplicaToInternalState(Replica replica) { + allReplicas.add(replica); return replicas .computeIfAbsent(replica.getShard().getCollection().getName(), k -> new HashMap<>()) .computeIfAbsent(replica.getShard().getShardName(), k -> CollectionUtil.newHashSet(1)) @@ -515,6 +519,7 @@ public final void removeReplica(Replica replica) { }); if (hasReplica.get()) { removeProjectedReplicaWeights(replica); + allReplicas.remove(replica); } } diff --git a/solr/core/src/java/org/apache/solr/cluster/placement/plugins/SimplePlacementFactory.java b/solr/core/src/java/org/apache/solr/cluster/placement/plugins/SimplePlacementFactory.java index 52f30c367b0..e21ac7d03bd 100644 --- a/solr/core/src/java/org/apache/solr/cluster/placement/plugins/SimplePlacementFactory.java +++ b/solr/core/src/java/org/apache/solr/cluster/placement/plugins/SimplePlacementFactory.java @@ -129,7 +129,7 @@ public int calcRelevantWeightWithReplica(Replica replica) { int colReplicaCount = collectionReplicas.getOrDefault(replica.getShard().getCollection().getName(), 0); int shardReplicaCount = getReplicasForShardOnNode(replica.getShard()).size(); - return getAllReplicasOnNode().size() + return getAllReplicaCount() + 1 + colReplicaCount * SAME_COL_MULT + shardReplicaCount * SAME_SHARD_MULT;