Skip to content

Commit cc86e27

Browse files
Caching number of primary shards per node for evaluating constraints on avg primary shards across all indices per node (opensearch-project#14992) (opensearch-project#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>
1 parent ee74e56 commit cc86e27

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

server/src/main/java/org/opensearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ void updateRebalanceConstraint(String constraint, boolean add) {
476476
public static class ModelNode implements Iterable<ModelIndex> {
477477
private final Map<String, ModelIndex> indices = new HashMap<>();
478478
private int numShards = 0;
479+
private int numPrimaryShards = 0;
479480
private final RoutingNode routingNode;
480481

481482
ModelNode(RoutingNode routingNode) {
@@ -509,7 +510,7 @@ public int numPrimaryShards(String idx) {
509510
}
510511

511512
public int numPrimaryShards() {
512-
return indices.values().stream().mapToInt(index -> index.numPrimaryShards()).sum();
513+
return numPrimaryShards;
513514
}
514515

515516
public int highestPrimary(String index) {
@@ -527,6 +528,10 @@ public void addShard(ShardRouting shard) {
527528
indices.put(index.getIndexId(), index);
528529
}
529530
index.addShard(shard);
531+
if (shard.primary()) {
532+
numPrimaryShards++;
533+
}
534+
530535
numShards++;
531536
}
532537

@@ -538,6 +543,11 @@ public void removeShard(ShardRouting shard) {
538543
indices.remove(shard.getIndexName());
539544
}
540545
}
546+
547+
if (shard.primary()) {
548+
numPrimaryShards--;
549+
}
550+
541551
numShards--;
542552
}
543553

0 commit comments

Comments
 (0)