Skip to content

Commit 45b2cf5

Browse files
authored
Revert "Use switch expressions in EnableAllocationDecider and NodeShutdownAllocationDecider (#83641) (#83673)" (#83677)
This reverts commit 20ff6c1.
1 parent e1935d7 commit 45b2cf5

File tree

2 files changed

+104
-50
lines changed

2 files changed

+104
-50
lines changed

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

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,38 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingAllocation allocat
124124
enable = this.enableAllocation;
125125
usedIndexSetting = false;
126126
}
127-
return switch (enable) {
128-
case ALL -> allocation.decision(Decision.YES, NAME, "all allocations are allowed");
129-
case NONE -> allocation.decision(Decision.NO, NAME, "no allocations are allowed due to %s", setting(enable, usedIndexSetting));
130-
case NEW_PRIMARIES -> (shardRouting.primary()
131-
&& shardRouting.active() == false
132-
&& shardRouting.recoverySource().getType() != RecoverySource.Type.EXISTING_STORE)
133-
? allocation.decision(Decision.YES, NAME, "new primary allocations are allowed")
134-
: allocation.decision(
127+
switch (enable) {
128+
case ALL:
129+
return allocation.decision(Decision.YES, NAME, "all allocations are allowed");
130+
case NONE:
131+
return allocation.decision(Decision.NO, NAME, "no allocations are allowed due to %s", setting(enable, usedIndexSetting));
132+
case NEW_PRIMARIES:
133+
if (shardRouting.primary()
134+
&& shardRouting.active() == false
135+
&& shardRouting.recoverySource().getType() != RecoverySource.Type.EXISTING_STORE) {
136+
return allocation.decision(Decision.YES, NAME, "new primary allocations are allowed");
137+
} else {
138+
return allocation.decision(
135139
Decision.NO,
136140
NAME,
137141
"non-new primary allocations are forbidden due to %s",
138142
setting(enable, usedIndexSetting)
139143
);
140-
case PRIMARIES -> shardRouting.primary()
141-
? allocation.decision(Decision.YES, NAME, "primary allocations are allowed")
142-
: allocation.decision(Decision.NO, NAME, "replica allocations are forbidden due to %s", setting(enable, usedIndexSetting));
143-
};
144+
}
145+
case PRIMARIES:
146+
if (shardRouting.primary()) {
147+
return allocation.decision(Decision.YES, NAME, "primary allocations are allowed");
148+
} else {
149+
return allocation.decision(
150+
Decision.NO,
151+
NAME,
152+
"replica allocations are forbidden due to %s",
153+
setting(enable, usedIndexSetting)
154+
);
155+
}
156+
default:
157+
throw new IllegalStateException("Unknown allocation option");
158+
}
144159
}
145160

146161
@Override
@@ -178,16 +193,36 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca
178193
enable = this.enableRebalance;
179194
usedIndexSetting = false;
180195
}
181-
return switch (enable) {
182-
case ALL -> allocation.decision(Decision.YES, NAME, "all rebalancing is allowed");
183-
case NONE -> allocation.decision(Decision.NO, NAME, "no rebalancing is allowed due to %s", setting(enable, usedIndexSetting));
184-
case PRIMARIES -> shardRouting.primary()
185-
? allocation.decision(Decision.YES, NAME, "primary rebalancing is allowed")
186-
: allocation.decision(Decision.NO, NAME, "replica rebalancing is forbidden due to %s", setting(enable, usedIndexSetting));
187-
case REPLICAS -> shardRouting.primary()
188-
? allocation.decision(Decision.NO, NAME, "primary rebalancing is forbidden due to %s", setting(enable, usedIndexSetting))
189-
: allocation.decision(Decision.YES, NAME, "replica rebalancing is allowed");
190-
};
196+
switch (enable) {
197+
case ALL:
198+
return allocation.decision(Decision.YES, NAME, "all rebalancing is allowed");
199+
case NONE:
200+
return allocation.decision(Decision.NO, NAME, "no rebalancing is allowed due to %s", setting(enable, usedIndexSetting));
201+
case PRIMARIES:
202+
if (shardRouting.primary()) {
203+
return allocation.decision(Decision.YES, NAME, "primary rebalancing is allowed");
204+
} else {
205+
return allocation.decision(
206+
Decision.NO,
207+
NAME,
208+
"replica rebalancing is forbidden due to %s",
209+
setting(enable, usedIndexSetting)
210+
);
211+
}
212+
case REPLICAS:
213+
if (shardRouting.primary() == false) {
214+
return allocation.decision(Decision.YES, NAME, "replica rebalancing is allowed");
215+
} else {
216+
return allocation.decision(
217+
Decision.NO,
218+
NAME,
219+
"primary rebalancing is forbidden due to %s",
220+
setting(enable, usedIndexSetting)
221+
);
222+
}
223+
default:
224+
throw new IllegalStateException("Unknown rebalance option");
225+
}
191226
}
192227

193228
private static String setting(Allocation allocation, boolean usedIndexSetting) {

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

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package org.elasticsearch.cluster.routing.allocation.decider;
1010

11+
import org.apache.logging.log4j.LogManager;
12+
import org.apache.logging.log4j.Logger;
1113
import org.elasticsearch.cluster.metadata.IndexMetadata;
1214
import org.elasticsearch.cluster.metadata.Metadata;
1315
import org.elasticsearch.cluster.metadata.NodesShutdownMetadata;
@@ -27,6 +29,7 @@
2729
* on, a node which is shutting down for restart.
2830
*/
2931
public class NodeShutdownAllocationDecider extends AllocationDecider {
32+
private static final Logger logger = LogManager.getLogger(NodeShutdownAllocationDecider.class);
3033

3134
private static final String NAME = "node_shutdown";
3235

@@ -42,20 +45,29 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
4245
return allocation.decision(Decision.YES, NAME, "this node is not currently shutting down");
4346
}
4447

45-
return switch (thisNodeShutdownMetadata.getType()) {
46-
case REPLACE, REMOVE -> allocation.decision(
47-
Decision.NO,
48-
NAME,
49-
"node [%s] is preparing to be removed from the cluster",
50-
node.nodeId()
51-
);
52-
case RESTART -> allocation.decision(
53-
Decision.YES,
54-
NAME,
55-
"node [%s] is preparing to restart, but will remain in the cluster",
56-
node.nodeId()
57-
);
58-
};
48+
switch (thisNodeShutdownMetadata.getType()) {
49+
case REPLACE:
50+
case REMOVE:
51+
return allocation.decision(Decision.NO, NAME, "node [%s] is preparing to be removed from the cluster", node.nodeId());
52+
case RESTART:
53+
return allocation.decision(
54+
Decision.YES,
55+
NAME,
56+
"node [%s] is preparing to restart, but will remain in the cluster",
57+
node.nodeId()
58+
);
59+
default:
60+
logger.debug(
61+
"found unrecognized node shutdown type [{}] while deciding allocation for [{}] shard [{}][{}] on node [{}]",
62+
thisNodeShutdownMetadata.getType(),
63+
shardRouting.primary() ? "primary" : "replica",
64+
shardRouting.getIndexName(),
65+
shardRouting.getId(),
66+
node.nodeId()
67+
);
68+
assert false : "node shutdown type not recognized: " + thisNodeShutdownMetadata.getType();
69+
return Decision.YES;
70+
}
5971
}
6072

6173
/**
@@ -79,20 +91,27 @@ public Decision shouldAutoExpandToNode(IndexMetadata indexMetadata, DiscoveryNod
7991
return allocation.decision(Decision.YES, NAME, "node [%s] is not preparing for removal from the cluster");
8092
}
8193

82-
return switch (thisNodeShutdownMetadata.getType()) {
83-
case RESTART -> allocation.decision(
84-
Decision.NO,
85-
NAME,
86-
"node [%s] is preparing to restart, auto-expansion waiting until it is complete",
87-
node.getId()
88-
);
89-
case REPLACE, REMOVE -> allocation.decision(
90-
Decision.NO,
91-
NAME,
92-
"node [%s] is preparing for removal from the cluster",
93-
node.getId()
94-
);
95-
};
94+
switch (thisNodeShutdownMetadata.getType()) {
95+
case RESTART:
96+
return allocation.decision(
97+
Decision.NO,
98+
NAME,
99+
"node [%s] is preparing to restart, auto-expansion waiting until it is complete",
100+
node.getId()
101+
);
102+
case REPLACE:
103+
case REMOVE:
104+
return allocation.decision(Decision.NO, NAME, "node [%s] is preparing for removal from the cluster", node.getId());
105+
default:
106+
logger.debug(
107+
"found unrecognized node shutdown type [{}] while deciding auto-expansion for index [{}] on node [{}]",
108+
thisNodeShutdownMetadata.getType(),
109+
indexMetadata.getIndex().getName(),
110+
node.getId()
111+
);
112+
assert false : "node shutdown type not recognized: " + thisNodeShutdownMetadata.getType();
113+
return Decision.YES;
114+
}
96115
}
97116

98117
@Nullable

0 commit comments

Comments
 (0)