2727import org .elasticsearch .cluster .ClusterInfo ;
2828import org .elasticsearch .cluster .DiskUsage ;
2929import org .elasticsearch .cluster .metadata .IndexMetaData ;
30+ import org .elasticsearch .cluster .metadata .MetaData ;
3031import org .elasticsearch .cluster .routing .IndexShardRoutingTable ;
3132import org .elasticsearch .cluster .routing .RecoverySource ;
3233import org .elasticsearch .cluster .routing .RoutingNode ;
34+ import org .elasticsearch .cluster .routing .RoutingTable ;
3335import org .elasticsearch .cluster .routing .ShardRouting ;
3436import org .elasticsearch .cluster .routing .ShardRoutingState ;
3537import org .elasticsearch .cluster .routing .allocation .DiskThresholdSettings ;
@@ -86,10 +88,9 @@ public DiskThresholdDecider(Settings settings, ClusterSettings clusterSettings)
8688 *
8789 * If subtractShardsMovingAway is true then the size of shards moving away is subtracted from the total size of all shards
8890 */
89- static long sizeOfRelocatingShards (RoutingNode node , RoutingAllocation allocation ,
90- boolean subtractShardsMovingAway , String dataPath ) {
91- ClusterInfo clusterInfo = allocation .clusterInfo ();
92- long totalSize = 0 ;
91+ public static long sizeOfRelocatingShards (RoutingNode node , boolean subtractShardsMovingAway , String dataPath , ClusterInfo clusterInfo ,
92+ MetaData metaData , RoutingTable routingTable ) {
93+ long totalSize = 0L ;
9394
9495 for (ShardRouting routing : node .shardsWithState (ShardRoutingState .INITIALIZING )) {
9596 if (routing .relocatingNodeId () == null ) {
@@ -103,7 +104,7 @@ static long sizeOfRelocatingShards(RoutingNode node, RoutingAllocation allocatio
103104 // if we don't yet know the actual path of the incoming shard then conservatively assume it's going to the path with the least
104105 // free space
105106 if (actualPath == null || actualPath .equals (dataPath )) {
106- totalSize += getExpectedShardSize (routing , allocation , 0 );
107+ totalSize += getExpectedShardSize (routing , 0L , clusterInfo , metaData , routingTable );
107108 }
108109 }
109110
@@ -115,7 +116,7 @@ static long sizeOfRelocatingShards(RoutingNode node, RoutingAllocation allocatio
115116 actualPath = clusterInfo .getDataPath (routing .cancelRelocation ());
116117 }
117118 if (dataPath .equals (actualPath )) {
118- totalSize -= getExpectedShardSize (routing , allocation , 0 );
119+ totalSize -= getExpectedShardSize (routing , 0L , clusterInfo , metaData , routingTable );
119120 }
120121 }
121122 }
@@ -239,7 +240,8 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
239240 }
240241
241242 // Secondly, check that allocating the shard to this node doesn't put it above the high watermark
242- final long shardSize = getExpectedShardSize (shardRouting , allocation , 0 );
243+ final long shardSize = getExpectedShardSize (shardRouting , 0L ,
244+ allocation .clusterInfo (), allocation .metaData (), allocation .routingTable ());
243245 double freeSpaceAfterShard = freeDiskPercentageAfterShardAssigned (usage , shardSize );
244246 long freeBytesAfterShard = freeBytes - shardSize ;
245247 if (freeBytesAfterShard < diskThresholdSettings .getFreeBytesThresholdHigh ().getBytes ()) {
@@ -339,7 +341,8 @@ private DiskUsage getDiskUsage(RoutingNode node, RoutingAllocation allocation,
339341 node .nodeId (), usage .getTotalBytes (), usage .getFreeBytes (), usage .getFreeDiskAsPercentage ());
340342 }
341343
342- final long relocatingShardsSize = sizeOfRelocatingShards (node , allocation , subtractLeavingShards , usage .getPath ());
344+ final long relocatingShardsSize = sizeOfRelocatingShards (node , subtractLeavingShards , usage .getPath (),
345+ allocation .clusterInfo (), allocation .metaData (), allocation .routingTable ());
343346 final DiskUsage usageIncludingRelocations = new DiskUsage (node .nodeId (), node .node ().getName (), usage .getPath (),
344347 usage .getTotalBytes (), usage .getFreeBytes () - relocatingShardsSize );
345348 logger .trace ("getDiskUsage: usage [{}] with [{}] bytes relocating yields [{}]" ,
@@ -418,29 +421,28 @@ private Decision earlyTerminate(RoutingAllocation allocation, ImmutableOpenMap<S
418421 * Returns the expected shard size for the given shard or the default value provided if not enough information are available
419422 * to estimate the shards size.
420423 */
421- public static long getExpectedShardSize (ShardRouting shard , RoutingAllocation allocation , long defaultValue ) {
422- final IndexMetaData metaData = allocation . metaData (). getIndexSafe ( shard . index ());
423- final ClusterInfo info = allocation . clusterInfo ( );
424- if (metaData .getResizeSourceIndex () != null && shard .active () == false &&
424+ public static long getExpectedShardSize (ShardRouting shard , long defaultValue , ClusterInfo clusterInfo , MetaData metaData ,
425+ RoutingTable routingTable ) {
426+ final IndexMetaData indexMetaData = metaData . getIndexSafe ( shard . index () );
427+ if (indexMetaData .getResizeSourceIndex () != null && shard .active () == false &&
425428 shard .recoverySource ().getType () == RecoverySource .Type .LOCAL_SHARDS ) {
426429 // in the shrink index case we sum up the source index shards since we basically make a copy of the shard in
427430 // the worst case
428431 long targetShardSize = 0 ;
429- final Index mergeSourceIndex = metaData .getResizeSourceIndex ();
430- final IndexMetaData sourceIndexMeta = allocation . metaData () .index (mergeSourceIndex );
432+ final Index mergeSourceIndex = indexMetaData .getResizeSourceIndex ();
433+ final IndexMetaData sourceIndexMeta = metaData .index (mergeSourceIndex );
431434 if (sourceIndexMeta != null ) {
432435 final Set <ShardId > shardIds = IndexMetaData .selectRecoverFromShards (shard .id (),
433- sourceIndexMeta , metaData .getNumberOfShards ());
434- for (IndexShardRoutingTable shardRoutingTable : allocation . routingTable () .index (mergeSourceIndex .getName ())) {
436+ sourceIndexMeta , indexMetaData .getNumberOfShards ());
437+ for (IndexShardRoutingTable shardRoutingTable : routingTable .index (mergeSourceIndex .getName ())) {
435438 if (shardIds .contains (shardRoutingTable .shardId ())) {
436- targetShardSize += info .getShardSize (shardRoutingTable .primaryShard (), 0 );
439+ targetShardSize += clusterInfo .getShardSize (shardRoutingTable .primaryShard (), 0 );
437440 }
438441 }
439442 }
440443 return targetShardSize == 0 ? defaultValue : targetShardSize ;
441444 } else {
442- return info .getShardSize (shard , defaultValue );
445+ return clusterInfo .getShardSize (shard , defaultValue );
443446 }
444-
445447 }
446448}
0 commit comments