Skip to content

Commit 4889650

Browse files
Pass IndexMetadata to AllocationDecider.can_remain (#88453)
We need the metadata in a number of allocation deciders and pass it to other allocation methods. Passing it here avoids redundant lookups across deciders.
1 parent ecc9605 commit 4889650

File tree

21 files changed

+139
-71
lines changed

21 files changed

+139
-71
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
4242
* Returns a {@link Decision} whether the given shard routing can be remain
4343
* on the given node. The default is {@link Decision#ALWAYS}.
4444
*/
45-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
45+
public Decision canRemain(IndexMetadata indexMetadata, ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
4646
return Decision.ALWAYS;
4747
}
4848

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
8686
}
8787
return Decision.NO;
8888
}
89+
final IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index());
8990
if (allocation.debugDecision()) {
9091
Decision.Multi ret = new Decision.Multi();
9192
for (AllocationDecider allocationDecider : allocations) {
92-
Decision decision = allocationDecider.canRemain(shardRouting, node, allocation);
93+
Decision decision = allocationDecider.canRemain(indexMetadata, shardRouting, node, allocation);
9394
// short track if a NO is returned.
9495
if (decision.type() == Decision.Type.NO) {
9596
maybeTraceLogNoDecision(shardRouting, node, allocationDecider);
@@ -103,7 +104,7 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
103104
// tighter loop if debug information is not collected: don't collect yes decisions + break out right away on NO
104105
Decision ret = Decision.YES;
105106
for (AllocationDecider allocationDecider : allocations) {
106-
switch (allocationDecider.canRemain(shardRouting, node, allocation).type()) {
107+
switch (allocationDecider.canRemain(indexMetadata, shardRouting, node, allocation).type()) {
107108
case NO -> {
108109
maybeTraceLogNoDecision(shardRouting, node, allocationDecider);
109110
return Decision.NO;

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void setAwarenessAttributes(List<String> awarenessAttributes) {
123123

124124
@Override
125125
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
126-
return underCapacity(shardRouting, node, allocation, true);
126+
return underCapacity(allocation.metadata().getIndexSafe(shardRouting.index()), shardRouting, node, allocation, true);
127127
}
128128

129129
@Override
@@ -135,8 +135,8 @@ public Decision canForceAllocateDuringReplace(ShardRouting shardRouting, Routing
135135
}
136136

137137
@Override
138-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
139-
return underCapacity(shardRouting, node, allocation, false);
138+
public Decision canRemain(IndexMetadata indexMetadata, ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
139+
return underCapacity(indexMetadata, shardRouting, node, allocation, false);
140140
}
141141

142142
private static final Decision YES_NOT_ENABLED = Decision.single(
@@ -155,13 +155,18 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
155155

156156
private static final Decision YES_ALL_MET = Decision.single(Decision.Type.YES, NAME, "node meets all awareness attribute requirements");
157157

158-
private Decision underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) {
158+
private Decision underCapacity(
159+
IndexMetadata indexMetadata,
160+
ShardRouting shardRouting,
161+
RoutingNode node,
162+
RoutingAllocation allocation,
163+
boolean moveToNode
164+
) {
159165
if (awarenessAttributes.isEmpty()) {
160166
return YES_NOT_ENABLED;
161167
}
162168

163169
final boolean debug = allocation.debugDecision();
164-
final IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index());
165170

166171
if (indexMetadata.getAutoExpandReplicas().expandToAllNodes()) {
167172
return YES_AUTO_EXPAND_ALL;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public Decision canForceAllocateDuringReplace(ShardRouting shardRouting, Routing
461461
);
462462

463463
@Override
464-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
464+
public Decision canRemain(IndexMetadata indexMetadata, ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
465465
if (shardRouting.currentNodeId().equals(node.nodeId()) == false) {
466466
throw new IllegalArgumentException("Shard [" + shardRouting + "] is not allocated on node: [" + node.nodeId() + "]");
467467
}
@@ -472,7 +472,7 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
472472
return decision;
473473
}
474474

475-
if (allocation.metadata().index(shardRouting.index()).ignoreDiskWatermarks()) {
475+
if (indexMetadata.ignoreDiskWatermarks()) {
476476
return YES_DISK_WATERMARKS_IGNORED;
477477
}
478478

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ public FilterAllocationDecider(Settings settings, ClusterSettings clusterSetting
9090

9191
@Override
9292
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
93+
IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index());
9394
if (shardRouting.unassigned() && shardRouting.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
9495
// only for unassigned - we filter allocation right after the index creation (for shard shrinking) to ensure
9596
// that once it has been allocated post API the replicas can be allocated elsewhere without user interaction
9697
// this is a setting that can only be set within the system!
97-
IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index());
9898
DiscoveryNodeFilters initialRecoveryFilters = DiscoveryNodeFilters.trimTier(indexMetadata.getInitialRecoveryFilters());
9999
if (initialRecoveryFilters != null && initialRecoveryFilters.match(node.node()) == false) {
100100
String explanation =
101101
"initial allocation of the shrunken index is only allowed on nodes [%s] that hold a copy of every shard in the index";
102102
return allocation.decision(Decision.NO, NAME, explanation, initialRecoveryFilters);
103103
}
104104
}
105-
return shouldFilter(shardRouting, node.node(), allocation);
105+
return shouldFilter(indexMetadata, node.node(), allocation);
106106
}
107107

108108
@Override
@@ -111,8 +111,8 @@ public Decision canAllocate(IndexMetadata indexMetadata, RoutingNode node, Routi
111111
}
112112

113113
@Override
114-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
115-
return shouldFilter(shardRouting, node.node(), allocation);
114+
public Decision canRemain(IndexMetadata indexMetadata, ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
115+
return shouldFilter(indexMetadata, node.node(), allocation);
116116
}
117117

118118
@Override
@@ -126,16 +126,6 @@ public Decision shouldAutoExpandToNode(IndexMetadata indexMetadata, DiscoveryNod
126126
return allocation.decision(Decision.YES, NAME, "node passes include/exclude/require filters");
127127
}
128128

129-
private Decision shouldFilter(ShardRouting shardRouting, DiscoveryNode node, RoutingAllocation allocation) {
130-
Decision decision = shouldClusterFilter(node, allocation);
131-
if (decision != null) return decision;
132-
133-
decision = shouldIndexFilter(allocation.metadata().getIndexSafe(shardRouting.index()), node, allocation);
134-
if (decision != null) return decision;
135-
136-
return allocation.decision(Decision.YES, NAME, "node passes include/exclude/require filters");
137-
}
138-
139129
private Decision shouldFilter(IndexMetadata indexMd, DiscoveryNode node, RoutingAllocation allocation) {
140130
Decision decision = shouldClusterFilter(node, allocation);
141131
if (decision != null) return decision;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
7373
}
7474

7575
@Override
76-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
76+
public Decision canRemain(IndexMetadata indexMetadata, ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
7777
if (replacementOngoing(allocation) == false) {
7878
return NO_REPLACEMENTS;
7979
} else if (isReplacementSource(allocation, node.nodeId())) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
6363
* determine if shards can remain on their current node.
6464
*/
6565
@Override
66-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
66+
public Decision canRemain(IndexMetadata indexMetadata, ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
6767
return this.canAllocate(shardRouting, node, allocation);
6868
}
6969

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,28 @@ private void setClusterShardLimit(int clusterShardLimit) {
8181

8282
@Override
8383
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
84-
return doDecide(shardRouting, node, allocation, (count, limit) -> count >= limit);
84+
return doDecide(
85+
allocation.metadata().getIndexSafe(shardRouting.index()),
86+
shardRouting,
87+
node,
88+
allocation,
89+
(count, limit) -> count >= limit
90+
);
8591
}
8692

8793
@Override
88-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
89-
return doDecide(shardRouting, node, allocation, (count, limit) -> count > limit);
94+
public Decision canRemain(IndexMetadata indexMetadata, ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
95+
return doDecide(indexMetadata, shardRouting, node, allocation, (count, limit) -> count > limit);
9096

9197
}
9298

9399
private Decision doDecide(
100+
IndexMetadata indexMd,
94101
ShardRouting shardRouting,
95102
RoutingNode node,
96103
RoutingAllocation allocation,
97104
BiPredicate<Integer, Integer> decider
98105
) {
99-
IndexMetadata indexMd = allocation.metadata().getIndexSafe(shardRouting.index());
100106
final int indexShardLimit = indexMd.getShardsPerNodeLimit();
101107
// Capture the limit here in case it changes during this method's
102108
// execution

server/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
224224
}
225225

226226
@Override
227-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
227+
public Decision canRemain(IndexMetadata indexMetadata, ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
228228
return getRandomDecision();
229229
}
230230

server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDecidersTests.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.cluster.ClusterName;
1313
import org.elasticsearch.cluster.ClusterState;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
15+
import org.elasticsearch.cluster.metadata.Metadata;
1516
import org.elasticsearch.cluster.node.DiscoveryNode;
1617
import org.elasticsearch.cluster.routing.RecoverySource;
1718
import org.elasticsearch.cluster.routing.RoutingNode;
@@ -54,7 +55,12 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca
5455
}
5556

5657
@Override
57-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
58+
public Decision canRemain(
59+
IndexMetadata indexMetadata,
60+
ShardRouting shardRouting,
61+
RoutingNode node,
62+
RoutingAllocation allocation
63+
) {
5864
return Decision.YES;
5965
}
6066

@@ -79,18 +85,25 @@ public Decision canRebalance(RoutingAllocation allocation) {
7985
}
8086
}));
8187

82-
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).build();
88+
IndexMetadata idx = IndexMetadata.builder("idx").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).build();
89+
IndexMetadata testIdx = IndexMetadata.builder("test")
90+
.settings(settings(Version.CURRENT))
91+
.numberOfShards(1)
92+
.numberOfReplicas(0)
93+
.build();
94+
ClusterState clusterState = ClusterState.builder(new ClusterName("test"))
95+
.metadata(Metadata.builder().put(idx, false).put(testIdx, false).build())
96+
.build();
8397
final RoutingAllocation allocation = new RoutingAllocation(deciders, clusterState, null, null, 0L);
8498

8599
allocation.setDebugMode(mode);
86100
final UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "_message");
87101
final ShardRouting shardRouting = ShardRouting.newUnassigned(
88-
new ShardId("test", "testUUID", 0),
102+
new ShardId(testIdx.getIndex(), 0),
89103
true,
90104
RecoverySource.ExistingStoreRecoverySource.INSTANCE,
91105
unassignedInfo
92106
);
93-
IndexMetadata idx = IndexMetadata.builder("idx").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).build();
94107

95108
RoutingNode routingNode = RoutingNodesHelper.routingNode("testNode", null);
96109
verify(deciders.canAllocate(shardRouting, routingNode, allocation), matcher);
@@ -130,7 +143,12 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca
130143
}
131144

132145
@Override
133-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
146+
public Decision canRemain(
147+
IndexMetadata indexMetadata,
148+
ShardRouting shardRouting,
149+
RoutingNode node,
150+
RoutingAllocation allocation
151+
) {
134152
return decisionOne;
135153
}
136154

@@ -171,7 +189,12 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca
171189
}
172190

173191
@Override
174-
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
192+
public Decision canRemain(
193+
IndexMetadata indexMetadata,
194+
ShardRouting shardRouting,
195+
RoutingNode node,
196+
RoutingAllocation allocation
197+
) {
175198
return decision(allocation);
176199
}
177200

@@ -208,20 +231,28 @@ private Decision decision(RoutingAllocation allocation) {
208231
}
209232
}));
210233

234+
IndexMetadata testIdx = IndexMetadata.builder("test")
235+
.settings(settings(Version.CURRENT))
236+
.numberOfShards(1)
237+
.numberOfReplicas(0)
238+
.build();
239+
211240
// no debug should just short-circuit to no, no matter what kind of no type return the first decider returns
212241
final ShardRouting shardRouting = ShardRouting.newUnassigned(
213-
new ShardId("test", "testUUID", 0),
242+
new ShardId(testIdx.getIndex(), 0),
214243
true,
215244
RecoverySource.ExistingStoreRecoverySource.INSTANCE,
216245
new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "_message")
217246
);
218247
final RoutingNode routingNode = RoutingNodesHelper.routingNode("testNode", null);
219-
final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).build();
220248
final IndexMetadata indexMetadata = IndexMetadata.builder("idx")
221249
.settings(settings(Version.CURRENT))
222250
.numberOfShards(1)
223251
.numberOfReplicas(0)
224252
.build();
253+
final ClusterState clusterState = ClusterState.builder(new ClusterName("test"))
254+
.metadata(Metadata.builder().put(testIdx, false).put(indexMetadata, false).build())
255+
.build();
225256

226257
final RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, clusterState, null, null, 0L);
227258
assertSame(Decision.NO, allocationDeciders.canAllocate(shardRouting, routingNode, allocation));

0 commit comments

Comments
 (0)