@@ -68,6 +68,7 @@ public class LocalShardsBalancer extends ShardsBalancer {
68
68
private final float avgPrimaryShardsPerNode ;
69
69
private final BalancedShardsAllocator .NodeSorter sorter ;
70
70
private final Set <RoutingNode > inEligibleTargetNode ;
71
+ private int totalShardCount = 0 ;
71
72
72
73
public LocalShardsBalancer (
73
74
Logger logger ,
@@ -125,8 +126,7 @@ public float avgPrimaryShardsPerNode() {
125
126
*/
126
127
@ Override
127
128
public float avgShardsPerNode () {
128
- float totalShards = nodes .values ().stream ().map (BalancedShardsAllocator .ModelNode ::numShards ).reduce (0 , Integer ::sum );
129
- return totalShards / nodes .size ();
129
+ return totalShardCount / nodes .size ();
130
130
}
131
131
132
132
/**
@@ -598,13 +598,15 @@ void moveShards() {
598
598
final BalancedShardsAllocator .ModelNode sourceNode = nodes .get (shardRouting .currentNodeId ());
599
599
final BalancedShardsAllocator .ModelNode targetNode = nodes .get (moveDecision .getTargetNode ().getId ());
600
600
sourceNode .removeShard (shardRouting );
601
+ --totalShardCount ;
601
602
Tuple <ShardRouting , ShardRouting > relocatingShards = routingNodes .relocateShard (
602
603
shardRouting ,
603
604
targetNode .getNodeId (),
604
605
allocation .clusterInfo ().getShardSize (shardRouting , ShardRouting .UNAVAILABLE_EXPECTED_SHARD_SIZE ),
605
606
allocation .changes ()
606
607
);
607
608
targetNode .addShard (relocatingShards .v2 ());
609
+ ++totalShardCount ;
608
610
if (logger .isTraceEnabled ()) {
609
611
logger .trace ("Moved shard [{}] to node [{}]" , shardRouting , targetNode .getRoutingNode ());
610
612
}
@@ -724,6 +726,7 @@ private Map<String, BalancedShardsAllocator.ModelNode> buildModelFromAssigned()
724
726
/* we skip relocating shards here since we expect an initializing shard with the same id coming in */
725
727
if (RoutingPool .LOCAL_ONLY .equals (RoutingPool .getShardPool (shard , allocation )) && shard .state () != RELOCATING ) {
726
728
node .addShard (shard );
729
+ ++totalShardCount ;
727
730
if (logger .isTraceEnabled ()) {
728
731
logger .trace ("Assigned shard [{}] to node [{}]" , shard , node .getNodeId ());
729
732
}
@@ -815,6 +818,7 @@ void allocateUnassigned() {
815
818
);
816
819
shard = routingNodes .initializeShard (shard , minNode .getNodeId (), null , shardSize , allocation .changes ());
817
820
minNode .addShard (shard );
821
+ ++totalShardCount ;
818
822
if (!shard .primary ()) {
819
823
// copy over the same replica shards to the secondary array so they will get allocated
820
824
// in a subsequent iteration, allowing replicas of other shards to be allocated first
@@ -844,6 +848,7 @@ void allocateUnassigned() {
844
848
allocation .routingTable ()
845
849
);
846
850
minNode .addShard (shard .initialize (minNode .getNodeId (), null , shardSize ));
851
+ ++totalShardCount ;
847
852
} else {
848
853
if (logger .isTraceEnabled ()) {
849
854
logger .trace ("No Node found to assign shard [{}]" , shard );
@@ -1011,18 +1016,21 @@ private boolean tryRelocateShard(BalancedShardsAllocator.ModelNode minNode, Bala
1011
1016
}
1012
1017
final Decision decision = new Decision .Multi ().add (allocationDecision ).add (rebalanceDecision );
1013
1018
maxNode .removeShard (shard );
1019
+ --totalShardCount ;
1014
1020
long shardSize = allocation .clusterInfo ().getShardSize (shard , ShardRouting .UNAVAILABLE_EXPECTED_SHARD_SIZE );
1015
1021
1016
1022
if (decision .type () == Decision .Type .YES ) {
1017
1023
/* only allocate on the cluster if we are not throttled */
1018
1024
logger .debug ("Relocate [{}] from [{}] to [{}]" , shard , maxNode .getNodeId (), minNode .getNodeId ());
1019
1025
minNode .addShard (routingNodes .relocateShard (shard , minNode .getNodeId (), shardSize , allocation .changes ()).v1 ());
1026
+ ++totalShardCount ;
1020
1027
return true ;
1021
1028
} else {
1022
1029
/* allocate on the model even if throttled */
1023
1030
logger .debug ("Simulate relocation of [{}] from [{}] to [{}]" , shard , maxNode .getNodeId (), minNode .getNodeId ());
1024
1031
assert decision .type () == Decision .Type .THROTTLE ;
1025
1032
minNode .addShard (shard .relocate (minNode .getNodeId (), shardSize ));
1033
+ ++totalShardCount ;
1026
1034
return false ;
1027
1035
}
1028
1036
}
0 commit comments