@@ -140,9 +140,8 @@ public static long sizeOfRelocatingShards(
140140
141141 // Where reserved space is unavailable (e.g. stats are out-of-sync) compute a conservative estimate for initialising shards
142142 final List <ShardRouting > initializingShards = node .shardsWithState (ShardRoutingState .INITIALIZING );
143- initializingShards .removeIf (shardRouting -> reservedSpace .containsShardId (shardRouting .shardId ()));
144143 for (ShardRouting routing : initializingShards ) {
145- if (routing .relocatingNodeId () == null ) {
144+ if (routing .relocatingNodeId () == null || reservedSpace . containsShardId ( routing . shardId ()) ) {
146145 // in practice the only initializing-but-not-relocating shards with a nonzero expected shard size will be ones created
147146 // by a resize (shrink/split/clone) operation which we expect to happen using hard links, so they shouldn't be taking
148147 // any additional space and can be ignored here
@@ -230,7 +229,14 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
230229
231230 // subtractLeavingShards is passed as false here, because they still use disk space, and therefore we should be extra careful
232231 // and take the size into account
233- final DiskUsageWithRelocations usage = getDiskUsage (node , allocation , usages , false );
232+ final DiskUsageWithRelocations usage = getDiskUsage (
233+ node ,
234+ allocation ,
235+ usages ,
236+ clusterInfo .getAvgFreeByte (),
237+ clusterInfo .getAvgTotalBytes (),
238+ false
239+ );
234240 // First, check that the node currently over the low watermark
235241 double freeDiskPercentage = usage .getFreeDiskAsPercentage ();
236242 // Cache the used disk percentage for displaying disk percentages consistent with documentation
@@ -492,7 +498,14 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
492498
493499 // subtractLeavingShards is passed as true here, since this is only for shards remaining, we will *eventually* have enough disk
494500 // since shards are moving away. No new shards will be incoming since in canAllocate we pass false for this check.
495- final DiskUsageWithRelocations usage = getDiskUsage (node , allocation , usages , true );
501+ final DiskUsageWithRelocations usage = getDiskUsage (
502+ node ,
503+ allocation ,
504+ usages ,
505+ clusterInfo .getAvgFreeByte (),
506+ clusterInfo .getAvgTotalBytes (),
507+ true
508+ );
496509 final String dataPath = clusterInfo .getDataPath (shardRouting );
497510 // If this node is already above the high threshold, the shard cannot remain (get it off!)
498511 final double freeDiskPercentage = usage .getFreeDiskAsPercentage ();
@@ -581,13 +594,15 @@ private DiskUsageWithRelocations getDiskUsage(
581594 RoutingNode node ,
582595 RoutingAllocation allocation ,
583596 final Map <String , DiskUsage > usages ,
597+ final long avgFreeBytes ,
598+ final long avgTotalBytes ,
584599 boolean subtractLeavingShards
585600 ) {
586601 DiskUsage usage = usages .get (node .nodeId ());
587602 if (usage == null ) {
588603 // If there is no usage, and we have other nodes in the cluster,
589604 // use the average usage for all nodes as the usage for this node
590- usage = averageUsage (node , usages );
605+ usage = new DiskUsage (node . nodeId (), node . node (). getName (), "_na_" , avgTotalBytes , avgFreeBytes );
591606 if (logger .isDebugEnabled ()) {
592607 logger .debug (
593608 "unable to determine disk usage for {}, defaulting to average across nodes [{} total] [{} free] [{}% free]" ,
@@ -619,26 +634,6 @@ private DiskUsageWithRelocations getDiskUsage(
619634 return diskUsageWithRelocations ;
620635 }
621636
622- /**
623- * Returns a {@link DiskUsage} for the {@link RoutingNode} using the
624- * average usage of other nodes in the disk usage map.
625- * @param node Node to return an averaged DiskUsage object for
626- * @param usages Map of nodeId to DiskUsage for all known nodes
627- * @return DiskUsage representing given node using the average disk usage
628- */
629- DiskUsage averageUsage (RoutingNode node , final Map <String , DiskUsage > usages ) {
630- if (usages .size () == 0 ) {
631- return new DiskUsage (node .nodeId (), node .node ().getName (), "_na_" , 0 , 0 );
632- }
633- long totalBytes = 0 ;
634- long freeBytes = 0 ;
635- for (DiskUsage du : usages .values ()) {
636- totalBytes += du .getTotalBytes ();
637- freeBytes += du .getFreeBytes ();
638- }
639- return new DiskUsage (node .nodeId (), node .node ().getName (), "_na_" , totalBytes / usages .size (), freeBytes / usages .size ());
640- }
641-
642637 /**
643638 * Given the DiskUsage for a node and the size of the shard, return the
644639 * percentage of free disk if the shard were to be allocated to the node.
0 commit comments