Skip to content

Commit d1307b4

Browse files
committed
Offline calculation of total shard per node and caching it for weight calculation inside LocalShardBalancer
Signed-off-by: RS146BIJAY <[email protected]>
1 parent f9512db commit d1307b4

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class LocalShardsBalancer extends ShardsBalancer {
7070
private final float avgPrimaryShardsPerNode;
7171
private final BalancedShardsAllocator.NodeSorter sorter;
7272
private final Set<RoutingNode> inEligibleTargetNode;
73+
private int totalShardCount = 0;
7374

7475
public LocalShardsBalancer(
7576
Logger logger,
@@ -127,8 +128,7 @@ public float avgPrimaryShardsPerNode() {
127128
*/
128129
@Override
129130
public float avgShardsPerNode() {
130-
float totalShards = nodes.values().stream().map(BalancedShardsAllocator.ModelNode::numShards).reduce(0, Integer::sum);
131-
return totalShards / nodes.size();
131+
return totalShardCount / nodes.size();
132132
}
133133

134134
/**
@@ -600,13 +600,15 @@ void moveShards() {
600600
final BalancedShardsAllocator.ModelNode sourceNode = nodes.get(shardRouting.currentNodeId());
601601
final BalancedShardsAllocator.ModelNode targetNode = nodes.get(moveDecision.getTargetNode().getId());
602602
sourceNode.removeShard(shardRouting);
603+
--totalShardCount;
603604
Tuple<ShardRouting, ShardRouting> relocatingShards = routingNodes.relocateShard(
604605
shardRouting,
605606
targetNode.getNodeId(),
606607
allocation.clusterInfo().getShardSize(shardRouting, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE),
607608
allocation.changes()
608609
);
609610
targetNode.addShard(relocatingShards.v2());
611+
++totalShardCount;
610612
if (logger.isTraceEnabled()) {
611613
logger.trace("Moved shard [{}] to node [{}]", shardRouting, targetNode.getRoutingNode());
612614
}
@@ -726,6 +728,7 @@ private Map<String, BalancedShardsAllocator.ModelNode> buildModelFromAssigned()
726728
/* we skip relocating shards here since we expect an initializing shard with the same id coming in */
727729
if (RoutingPool.LOCAL_ONLY.equals(RoutingPool.getShardPool(shard, allocation)) && shard.state() != RELOCATING) {
728730
node.addShard(shard);
731+
++totalShardCount;
729732
if (logger.isTraceEnabled()) {
730733
logger.trace("Assigned shard [{}] to node [{}]", shard, node.getNodeId());
731734
}
@@ -816,6 +819,7 @@ void allocateUnassigned() {
816819
);
817820
shard = routingNodes.initializeShard(shard, minNode.getNodeId(), null, shardSize, allocation.changes());
818821
minNode.addShard(shard);
822+
++totalShardCount;
819823
if (!shard.primary()) {
820824
// copy over the same replica shards to the secondary array so they will get allocated
821825
// in a subsequent iteration, allowing replicas of other shards to be allocated first
@@ -845,6 +849,7 @@ void allocateUnassigned() {
845849
allocation.routingTable()
846850
);
847851
minNode.addShard(shard.initialize(minNode.getNodeId(), null, shardSize));
852+
++totalShardCount;
848853
} else {
849854
if (logger.isTraceEnabled()) {
850855
logger.trace("No Node found to assign shard [{}]", shard);
@@ -1012,18 +1017,21 @@ private boolean tryRelocateShard(BalancedShardsAllocator.ModelNode minNode, Bala
10121017
}
10131018
final Decision decision = new Decision.Multi().add(allocationDecision).add(rebalanceDecision);
10141019
maxNode.removeShard(shard);
1020+
--totalShardCount;
10151021
long shardSize = allocation.clusterInfo().getShardSize(shard, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE);
10161022

10171023
if (decision.type() == Decision.Type.YES) {
10181024
/* only allocate on the cluster if we are not throttled */
10191025
logger.debug("Relocate [{}] from [{}] to [{}]", shard, maxNode.getNodeId(), minNode.getNodeId());
10201026
minNode.addShard(routingNodes.relocateShard(shard, minNode.getNodeId(), shardSize, allocation.changes()).v1());
1027+
++totalShardCount;
10211028
return true;
10221029
} else {
10231030
/* allocate on the model even if throttled */
10241031
logger.debug("Simulate relocation of [{}] from [{}] to [{}]", shard, maxNode.getNodeId(), minNode.getNodeId());
10251032
assert decision.type() == Decision.Type.THROTTLE;
10261033
minNode.addShard(shard.relocate(minNode.getNodeId(), shardSize));
1034+
++totalShardCount;
10271035
return false;
10281036
}
10291037
}

0 commit comments

Comments
 (0)