Skip to content

Commit 20ff6c1

Browse files
authored
Use switch expressions in EnableAllocationDecider and NodeShutdownAllocationDecider (#83641) (#83673)
* Reverse condition for replicas
1 parent 3ffe086 commit 20ff6c1

File tree

2 files changed

+50
-104
lines changed

2 files changed

+50
-104
lines changed

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

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -124,38 +124,23 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingAllocation allocat
124124
enable = this.enableAllocation;
125125
usedIndexSetting = false;
126126
}
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(
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(
139135
Decision.NO,
140136
NAME,
141137
"non-new primary allocations are forbidden due to %s",
142138
setting(enable, usedIndexSetting)
143139
);
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-
}
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+
};
159144
}
160145

161146
@Override
@@ -193,36 +178,16 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca
193178
enable = this.enableRebalance;
194179
usedIndexSetting = false;
195180
}
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-
}
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+
};
226191
}
227192

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

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

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

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

11-
import org.apache.logging.log4j.LogManager;
12-
import org.apache.logging.log4j.Logger;
1311
import org.elasticsearch.cluster.metadata.IndexMetadata;
1412
import org.elasticsearch.cluster.metadata.Metadata;
1513
import org.elasticsearch.cluster.metadata.NodesShutdownMetadata;
@@ -29,7 +27,6 @@
2927
* on, a node which is shutting down for restart.
3028
*/
3129
public class NodeShutdownAllocationDecider extends AllocationDecider {
32-
private static final Logger logger = LogManager.getLogger(NodeShutdownAllocationDecider.class);
3330

3431
private static final String NAME = "node_shutdown";
3532

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

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-
}
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+
};
7159
}
7260

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

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-
}
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+
};
11596
}
11697

11798
@Nullable

0 commit comments

Comments
 (0)