Apache Solr open-source search software
SOLR-17076: Optimize `OrderedNodePlacementPlugin#getAllReplicasOnNode` (#2076)
patsonluk26c286aba566
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.javaindex de83db8e967..5c8d195954e 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@@ -404,9 +404,13 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {private final Node node;private final Map<String, Map<String, Set<Replica>>> replicas;+ // a flattened list of all replicas, as computing from the map could be costly+ private final Set<Replica> allReplicas;+public WeightedNode(Node node) {this.node = node;this.replicas = new HashMap<>();+ this.allReplicas = new HashSet<>();}public Node getNode() {@@ -414,10 +418,11 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {}public Set<Replica> 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<String> getCollectionsOnNode() {@@ -454,6 +459,7 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {}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))@@ -508,6 +514,7 @@ public abstract class OrderedNodePlacementPlugin implements PlacementPlugin {(shard, reps) -> {if (reps.remove(replica)) {hasReplica.set(true);+ allReplicas.remove(replica);}return reps.isEmpty() ? null : reps;});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.javaindex 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 class SimplePlacementFactoryint 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;