@@ -139,7 +139,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
139
139
140
140
// subtractLeavingShards is passed as false here, because they still use disk space, and therefore should we should be extra careful
141
141
// and take the size into account
142
- DiskUsage usage = getDiskUsage (node , allocation , usages , false );
142
+ DiskUsageWithRelocations usage = getDiskUsage (node , allocation , usages , false );
143
143
// First, check that the node currently over the low watermark
144
144
double freeDiskPercentage = usage .getFreeDiskAsPercentage ();
145
145
// Cache the used disk percentage for displaying disk percentages consistent with documentation
@@ -304,7 +304,7 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
304
304
305
305
// subtractLeavingShards is passed as true here, since this is only for shards remaining, we will *eventually* have enough disk
306
306
// since shards are moving away. No new shards will be incoming since in canAllocate we pass false for this check.
307
- final DiskUsage usage = getDiskUsage (node , allocation , usages , true );
307
+ final DiskUsageWithRelocations usage = getDiskUsage (node , allocation , usages , true );
308
308
final String dataPath = clusterInfo .getDataPath (shardRouting );
309
309
// If this node is already above the high threshold, the shard cannot remain (get it off!)
310
310
final double freeDiskPercentage = usage .getFreeDiskAsPercentage ();
@@ -356,8 +356,8 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
356
356
"there is enough disk on this node for the shard to remain, free: [%s]" , new ByteSizeValue (freeBytes ));
357
357
}
358
358
359
- private DiskUsage getDiskUsage (RoutingNode node , RoutingAllocation allocation ,
360
- ImmutableOpenMap <String , DiskUsage > usages , boolean subtractLeavingShards ) {
359
+ private DiskUsageWithRelocations getDiskUsage (RoutingNode node , RoutingAllocation allocation ,
360
+ ImmutableOpenMap <String , DiskUsage > usages , boolean subtractLeavingShards ) {
361
361
DiskUsage usage = usages .get (node .nodeId ());
362
362
if (usage == null ) {
363
363
// If there is no usage, and we have other nodes in the cluster,
@@ -367,13 +367,11 @@ private DiskUsage getDiskUsage(RoutingNode node, RoutingAllocation allocation,
367
367
node .nodeId (), usage .getTotalBytes (), usage .getFreeBytes (), usage .getFreeDiskAsPercentage ());
368
368
}
369
369
370
- final long relocatingShardsSize = sizeOfRelocatingShards (node , subtractLeavingShards , usage .getPath (),
371
- allocation .clusterInfo (), allocation .metaData (), allocation .routingTable ());
372
- final DiskUsage usageIncludingRelocations = new DiskUsage (node .nodeId (), node .node ().getName (), usage .getPath (),
373
- usage .getTotalBytes (), usage .getFreeBytes () - relocatingShardsSize );
374
- logger .trace ("getDiskUsage: usage [{}] with [{}] bytes relocating yields [{}]" ,
375
- usage , relocatingShardsSize , usageIncludingRelocations );
376
- return usageIncludingRelocations ;
370
+ final DiskUsageWithRelocations diskUsageWithRelocations = new DiskUsageWithRelocations (usage ,
371
+ sizeOfRelocatingShards (node , subtractLeavingShards , usage .getPath (),
372
+ allocation .clusterInfo (), allocation .metaData (), allocation .routingTable ()));
373
+ logger .trace ("getDiskUsage(subtractLeavingShards={}) returning {}" , subtractLeavingShards , diskUsageWithRelocations );
374
+ return diskUsageWithRelocations ;
377
375
}
378
376
379
377
/**
@@ -403,7 +401,7 @@ DiskUsage averageUsage(RoutingNode node, ImmutableOpenMap<String, DiskUsage> usa
403
401
* @param shardSize Size in bytes of the shard
404
402
* @return Percentage of free space after the shard is assigned to the node
405
403
*/
406
- double freeDiskPercentageAfterShardAssigned (DiskUsage usage , Long shardSize ) {
404
+ double freeDiskPercentageAfterShardAssigned (DiskUsageWithRelocations usage , Long shardSize ) {
407
405
shardSize = (shardSize == null ) ? 0 : shardSize ;
408
406
DiskUsage newUsage = new DiskUsage (usage .getNodeId (), usage .getNodeName (), usage .getPath (),
409
407
usage .getTotalBytes (), usage .getFreeBytes () - shardSize );
@@ -471,4 +469,55 @@ public static long getExpectedShardSize(ShardRouting shard, long defaultValue, C
471
469
return clusterInfo .getShardSize (shard , defaultValue );
472
470
}
473
471
}
472
+
473
+ static class DiskUsageWithRelocations {
474
+
475
+ private final DiskUsage diskUsage ;
476
+ private final long relocatingShardSize ;
477
+
478
+ DiskUsageWithRelocations (DiskUsage diskUsage , long relocatingShardSize ) {
479
+ this .diskUsage = diskUsage ;
480
+ this .relocatingShardSize = relocatingShardSize ;
481
+ }
482
+
483
+ @ Override
484
+ public String toString () {
485
+ return "DiskUsageWithRelocations{" +
486
+ "diskUsage=" + diskUsage +
487
+ ", relocatingShardSize=" + relocatingShardSize +
488
+ '}' ;
489
+ }
490
+
491
+ double getFreeDiskAsPercentage () {
492
+ if (getTotalBytes () == 0L ) {
493
+ return 100.0 ;
494
+ }
495
+ return 100.0 * ((double )getFreeBytes () / getTotalBytes ());
496
+ }
497
+
498
+ double getUsedDiskAsPercentage () {
499
+ return 100.0 - getFreeDiskAsPercentage ();
500
+ }
501
+
502
+ long getFreeBytes () {
503
+ return diskUsage .getFreeBytes () - relocatingShardSize ;
504
+ }
505
+
506
+ String getPath () {
507
+ return diskUsage .getPath ();
508
+ }
509
+
510
+ String getNodeId () {
511
+ return diskUsage .getNodeId ();
512
+ }
513
+
514
+ String getNodeName () {
515
+ return diskUsage .getNodeName ();
516
+ }
517
+
518
+ long getTotalBytes () {
519
+ return diskUsage .getTotalBytes ();
520
+ }
521
+ }
522
+
474
523
}
0 commit comments