Skip to content

Commit 7799d84

Browse files
Refactor ThrottleAllocationDecider#canAllocate (elastic#136424)
Return early if simulating for DesiredBalance computation
1 parent c5490f2 commit 7799d84

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,14 @@ private void setPrimariesInitialRecoveries(int primariesInitialRecoveries) {
118118
@Override
119119
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
120120
if (shardRouting.primary() && shardRouting.unassigned()) {
121+
// Primary is unassigned, means we are going to do recovery from store, snapshot or local shards
121122
assert initializingShard(shardRouting, node.nodeId()).recoverySource().getType() != RecoverySource.Type.PEER;
122-
// primary is unassigned, means we are going to do recovery from store, snapshot or local shards
123-
// count *just the primaries* currently doing recovery on the node and check against primariesInitialRecoveries
124123

124+
if (allocation.isSimulating()) {
125+
return allocation.decision(Decision.YES, NAME, "primary allocation is not throttled when simulating");
126+
}
127+
128+
// Count the primaries currently doing recovery on the node, to ensure the primariesInitialRecoveries setting is obeyed.
125129
int primariesInRecovery = 0;
126130
for (ShardRouting shard : node.initializing()) {
127131
// when a primary shard is INITIALIZING, it can be because of *initial recovery* or *relocation from another node*
@@ -130,10 +134,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
130134
primariesInRecovery++;
131135
}
132136
}
133-
if (allocation.isSimulating()) {
134-
return allocation.decision(Decision.YES, NAME, "primary allocation is not throttled when simulating");
135-
} else if (primariesInRecovery >= primariesInitialRecoveries) {
136-
// TODO: Should index creation not be throttled for primary shards?
137+
if (primariesInRecovery >= primariesInitialRecoveries) {
137138
return allocation.decision(
138139
THROTTLE,
139140
NAME,
@@ -142,9 +143,8 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
142143
CLUSTER_ROUTING_ALLOCATION_NODE_INITIAL_PRIMARIES_RECOVERIES_SETTING.getKey(),
143144
primariesInitialRecoveries
144145
);
145-
} else {
146-
return allocation.decision(YES, NAME, "below primary recovery limit of [%d]", primariesInitialRecoveries);
147146
}
147+
return allocation.decision(YES, NAME, "below primary recovery limit of [%d]", primariesInitialRecoveries);
148148
} else {
149149
// Peer recovery
150150
assert initializingShard(shardRouting, node.nodeId()).recoverySource().getType() == RecoverySource.Type.PEER;

0 commit comments

Comments
 (0)