Skip to content

Commit

Permalink
Caching number of primary shards per node for evaluating constraints …
Browse files Browse the repository at this point in the history
…on avg primary shards across all indices per node (#14992) (#15045)

(cherry picked from commit 4707885)

Signed-off-by: RS146BIJAY <rishavsagar4b1@gmail.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent ee74e56 commit cc86e27
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ void updateRebalanceConstraint(String constraint, boolean add) {
public static class ModelNode implements Iterable<ModelIndex> {
private final Map<String, ModelIndex> indices = new HashMap<>();
private int numShards = 0;
private int numPrimaryShards = 0;
private final RoutingNode routingNode;

ModelNode(RoutingNode routingNode) {
Expand Down Expand Up @@ -509,7 +510,7 @@ public int numPrimaryShards(String idx) {
}

public int numPrimaryShards() {
return indices.values().stream().mapToInt(index -> index.numPrimaryShards()).sum();
return numPrimaryShards;
}

public int highestPrimary(String index) {
Expand All @@ -527,6 +528,10 @@ public void addShard(ShardRouting shard) {
indices.put(index.getIndexId(), index);
}
index.addShard(shard);
if (shard.primary()) {
numPrimaryShards++;
}

numShards++;
}

Expand All @@ -538,6 +543,11 @@ public void removeShard(ShardRouting shard) {
indices.remove(shard.getIndexName());
}
}

if (shard.primary()) {
numPrimaryShards--;
}

numShards--;
}

Expand Down

0 comments on commit cc86e27

Please sign in to comment.