You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by js...@apache.org on 2023/11/21 15:42:44 UTC

(solr) branch branch_9x updated: SOLR-17076: Optimize `OrderedNodePlacementPlugin#getAllReplicasOnNode` (#2076)

This is an automated email from the ASF dual-hosted git repository.

jsweeney pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 8277c131eab SOLR-17076: Optimize `OrderedNodePlacementPlugin#getAllReplicasOnNode` (#2076)
8277c131eab is described below

commit 8277c131eab3d54d0c8ae3f18b8caa71176bb37d
Author: patsonluk <pa...@users.noreply.github.com>
AuthorDate: Tue Nov 21 06:57:34 2023 -0800

    SOLR-17076: Optimize `OrderedNodePlacementPlugin#getAllReplicasOnNode` (#2076)
    
    * 1. Changed getAllReplicasOnNode to just return a copy of `allReplicas` field, which keeps track of all replicas. Instead of computing a new list every time.
    2. Added a getAllReplicaCount method to avoid creating new list of replicas if only the count is required
    
    * ./gradlew tidy
    
    * minor refactoring
---
 .../placement/plugins/OrderedNodePlacementPlugin.java     | 15 +++++++++++----
 .../cluster/placement/plugins/SimplePlacementFactory.java |  2 +-
 2 files changed, 12 insertions(+), 5 deletions(-)

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..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.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 class SimplePlacementFactory
       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;