Skip to content

Commit 536e71c

Browse files
committed
[ALLOCATION] Speed-up disk-threshold decider
Instead of iterating all shards of all indices to get all relocating shards for a given node we can just use the RoutingNode#shardsWithState method and fetch all INITIALIZING / RELOCATING shards and check if they are relocating. This operation is much faster and uses pre-build data-structures. Relates to elastic#6372
1 parent 474032f commit 536e71c

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,12 @@ public DiskThresholdDecider(Settings settings, NodeSettingsService nodeSettingsS
221221
* If subtractShardsMovingAway is set then the size of shards moving away is subtracted from the total size
222222
* of all shards
223223
*/
224-
public long sizeOfRelocatingShards(RoutingNode node, RoutingAllocation allocation, Map<String, Long> shardSizes, boolean subtractShardsMovingAway) {
225-
List<ShardRouting> relocatingShards = allocation.routingTable().shardsWithState(ShardRoutingState.RELOCATING);
224+
public long sizeOfRelocatingShards(RoutingNode node, Map<String, Long> shardSizes, boolean subtractShardsMovingAway) {
226225
long totalSize = 0;
227-
for (ShardRouting routing : relocatingShards) {
228-
if (routing.relocatingNodeId().equals(node.nodeId())) {
226+
for (ShardRouting routing : node.shardsWithState(ShardRoutingState.RELOCATING, ShardRoutingState.INITIALIZING)) {
227+
if (routing.initializing() && routing.relocatingNodeId() != null) {
229228
totalSize += getShardSize(routing, shardSizes);
230-
} else if (subtractShardsMovingAway && routing.currentNodeId().equals(node.nodeId())) {
229+
} else if (subtractShardsMovingAway && routing.relocating()) {
231230
totalSize -= getShardSize(routing, shardSizes);
232231
}
233232
}
@@ -285,7 +284,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
285284
}
286285

287286
if (includeRelocations) {
288-
long relocatingShardsSize = sizeOfRelocatingShards(node, allocation, shardSizes, false);
287+
long relocatingShardsSize = sizeOfRelocatingShards(node, shardSizes, false);
289288
DiskUsage usageIncludingRelocations = new DiskUsage(node.nodeId(), node.node().name(),
290289
usage.getTotalBytes(), usage.getFreeBytes() - relocatingShardsSize);
291290
if (logger.isTraceEnabled()) {
@@ -431,7 +430,7 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
431430

432431
if (includeRelocations) {
433432
Map<String, Long> shardSizes = clusterInfo.getShardSizes();
434-
long relocatingShardsSize = sizeOfRelocatingShards(node, allocation, shardSizes, true);
433+
long relocatingShardsSize = sizeOfRelocatingShards(node, shardSizes, true);
435434
DiskUsage usageIncludingRelocations = new DiskUsage(node.nodeId(), node.node().name(),
436435
usage.getTotalBytes(), usage.getFreeBytes() - relocatingShardsSize);
437436
if (logger.isTraceEnabled()) {

0 commit comments

Comments
 (0)