Skip to content

Commit

Permalink
1. Changed getAllReplicasOnNode to just return a copy of `allReplicas…
Browse files Browse the repository at this point in the history
…` 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
  • Loading branch information
patsonluk committed Nov 15, 2023
1 parent b98d936 commit d067c9b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,21 +403,24 @@ protected void verifyDeleteReplicas(
public abstract static class WeightedNode implements Comparable<WeightedNode> {
private final Node node;
private final Map<String, Map<String, Set<Replica>>> replicas;
private final Set<Replica> 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() {
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 +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))
Expand Down Expand Up @@ -515,6 +519,7 @@ public final void removeReplica(Replica replica) {
});
if (hasReplica.get()) {
removeProjectedReplicaWeights(replica);
allReplicas.remove(replica);
}
}

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 d067c9b

Please sign in to comment.