Skip to content

Commit

Permalink
SOLR-17076: Optimize OrderedNodePlacementPlugin#getAllReplicasOnNode (
Browse files Browse the repository at this point in the history
apache#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
  • Loading branch information
patsonluk committed Nov 24, 2023
1 parent acb887a commit 32326bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,20 +404,25 @@ public abstract static class WeightedNode implements Comparable<WeightedNode> {
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() {
return node;
}

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() {
Expand Down Expand Up @@ -454,6 +459,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))
Expand Down Expand Up @@ -508,6 +514,7 @@ public final void removeReplica(Replica replica) {
(shard, reps) -> {
if (reps.remove(replica)) {
hasReplica.set(true);
allReplicas.remove(replica);
}
return reps.isEmpty() ? null : reps;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 32326bc

Please sign in to comment.