Skip to content

Commit 0c0c756

Browse files
Small Simplifications DiskThresholdDecider (#64703) (#64719)
Some smaller improvements in the direction of #62275 and removal of some dead code and duplication.
1 parent e5dbe4d commit 0c0c756

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDecider.java

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ public static long sizeOfRelocatingShards(RoutingNode node, boolean subtractShar
141141
return totalSize;
142142
}
143143

144+
private static final Decision YES_UNALLOCATED_PRIMARY_BETWEEN_WATERMARKS = Decision.single(Decision.Type.YES, NAME, "the node " +
145+
"is above the low watermark, but less than the high watermark, and this primary shard has never been allocated before");
144146

145147
@Override
146148
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
147-
ClusterInfo clusterInfo = allocation.clusterInfo();
148-
ImmutableOpenMap<String, DiskUsage> usages = clusterInfo.getNodeMostAvailableDiskUsages();
149+
ImmutableOpenMap<String, DiskUsage> usages = allocation.clusterInfo().getNodeMostAvailableDiskUsages();
149150
final Decision decision = earlyTerminate(allocation, usages);
150151
if (decision != null) {
151152
return decision;
@@ -206,9 +207,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
206207
"but allowing allocation because primary has never been allocated",
207208
diskThresholdSettings.getFreeBytesThresholdLow(), freeBytesValue, node.nodeId());
208209
}
209-
return allocation.decision(Decision.YES, NAME,
210-
"the node is above the low watermark, but less than the high watermark, and this primary shard has " +
211-
"never been allocated before");
210+
return YES_UNALLOCATED_PRIMARY_BETWEEN_WATERMARKS;
212211
} else {
213212
// Even though the primary has never been allocated, the node is
214213
// above the high watermark, so don't allow allocating the shard
@@ -249,9 +248,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
249248
Strings.format1Decimals(usedDiskThresholdLow, "%"),
250249
Strings.format1Decimals(usedDiskPercentage, "%"), node.nodeId());
251250
}
252-
return allocation.decision(Decision.YES, NAME,
253-
"the node is above the low watermark, but less than the high watermark, and this primary shard has " +
254-
"never been allocated before");
251+
return YES_UNALLOCATED_PRIMARY_BETWEEN_WATERMARKS;
255252
} else {
256253
// Even though the primary has never been allocated, the node is
257254
// above the high watermark, so don't allow allocating the shard
@@ -307,6 +304,9 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
307304
new ByteSizeValue(freeBytesAfterShard));
308305
}
309306

307+
private static final Decision YES_NOT_MOST_UTILIZED_DISK = Decision.single(Decision.Type.YES, NAME,
308+
"this shard is not allocated on the most utilized disk and can remain");
309+
310310
@Override
311311
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
312312
if (shardRouting.currentNodeId().equals(node.nodeId()) == false) {
@@ -330,8 +330,7 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
330330
logger.trace("node [{}] has {}% free disk ({} bytes)", node.nodeId(), freeDiskPercentage, freeBytes);
331331
}
332332
if (dataPath == null || usage.getPath().equals(dataPath) == false) {
333-
return allocation.decision(Decision.YES, NAME,
334-
"this shard is not allocated on the most utilized disk and can remain");
333+
return YES_NOT_MOST_UTILIZED_DISK;
335334
}
336335
if (freeBytes < 0L) {
337336
final long sizeOfRelocatingShards = sizeOfRelocatingShards(node, true, usage.getPath(),
@@ -430,35 +429,29 @@ DiskUsage averageUsage(RoutingNode node, ImmutableOpenMap<String, DiskUsage> usa
430429
return newUsage.getFreeDiskAsPercentage();
431430
}
432431

432+
private static final Decision YES_DISABLED = Decision.single(Decision.Type.YES, NAME, "the disk threshold decider is disabled");
433+
434+
private static final Decision YES_SINGLE_DATA_NODE =
435+
Decision.single(Decision.Type.YES, NAME, "there is only a single data node present");
436+
437+
private static final Decision YES_USAGES_UNAVAILABLE = Decision.single(Decision.Type.YES, NAME, "disk usages are unavailable");
438+
433439
private Decision earlyTerminate(RoutingAllocation allocation, ImmutableOpenMap<String, DiskUsage> usages) {
434440
// Always allow allocation if the decider is disabled
435441
if (diskThresholdSettings.isEnabled() == false) {
436-
return allocation.decision(Decision.YES, NAME, "the disk threshold decider is disabled");
442+
return YES_DISABLED;
437443
}
438444

439445
// Allow allocation regardless if only a single data node is available
440446
if (enableForSingleDataNode == false && allocation.nodes().getDataNodes().size() <= 1) {
441-
if (logger.isTraceEnabled()) {
442-
logger.trace("only a single data node is present, allowing allocation");
443-
}
444-
return allocation.decision(Decision.YES, NAME, "there is only a single data node present");
445-
}
446-
447-
// Fail open there is no info available
448-
final ClusterInfo clusterInfo = allocation.clusterInfo();
449-
if (clusterInfo == null) {
450-
if (logger.isTraceEnabled()) {
451-
logger.trace("cluster info unavailable for disk threshold decider, allowing allocation.");
452-
}
453-
return allocation.decision(Decision.YES, NAME, "the cluster info is unavailable");
447+
logger.trace("only a single data node is present, allowing allocation");
448+
return YES_SINGLE_DATA_NODE;
454449
}
455450

456451
// Fail open if there are no disk usages available
457452
if (usages.isEmpty()) {
458-
if (logger.isTraceEnabled()) {
459-
logger.trace("unable to determine disk usages for disk-aware allocation, allowing allocation");
460-
}
461-
return allocation.decision(Decision.YES, NAME, "disk usages are unavailable");
453+
logger.trace("unable to determine disk usages for disk-aware allocation, allowing allocation");
454+
return YES_USAGES_UNAVAILABLE;
462455
}
463456
return null;
464457
}

0 commit comments

Comments
 (0)