Skip to content

Use switch expressions in EnableAllocationDecider and NodeShutdownAllocationDecider #83641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,38 +124,23 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingAllocation allocat
enable = this.enableAllocation;
usedIndexSetting = false;
}
switch (enable) {
case ALL:
return allocation.decision(Decision.YES, NAME, "all allocations are allowed");
case NONE:
return allocation.decision(Decision.NO, NAME, "no allocations are allowed due to %s", setting(enable, usedIndexSetting));
case NEW_PRIMARIES:
if (shardRouting.primary()
&& shardRouting.active() == false
&& shardRouting.recoverySource().getType() != RecoverySource.Type.EXISTING_STORE) {
return allocation.decision(Decision.YES, NAME, "new primary allocations are allowed");
} else {
return allocation.decision(
return switch (enable) {
case ALL -> allocation.decision(Decision.YES, NAME, "all allocations are allowed");
case NONE -> allocation.decision(Decision.NO, NAME, "no allocations are allowed due to %s", setting(enable, usedIndexSetting));
case NEW_PRIMARIES -> (shardRouting.primary()
&& shardRouting.active() == false
&& shardRouting.recoverySource().getType() != RecoverySource.Type.EXISTING_STORE)
? allocation.decision(Decision.YES, NAME, "new primary allocations are allowed")
: allocation.decision(
Decision.NO,
NAME,
"non-new primary allocations are forbidden due to %s",
setting(enable, usedIndexSetting)
);
}
case PRIMARIES:
if (shardRouting.primary()) {
return allocation.decision(Decision.YES, NAME, "primary allocations are allowed");
} else {
return allocation.decision(
Decision.NO,
NAME,
"replica allocations are forbidden due to %s",
setting(enable, usedIndexSetting)
);
}
default:
throw new IllegalStateException("Unknown allocation option");
}
case PRIMARIES -> shardRouting.primary()
? allocation.decision(Decision.YES, NAME, "primary allocations are allowed")
: allocation.decision(Decision.NO, NAME, "replica allocations are forbidden due to %s", setting(enable, usedIndexSetting));
};
}

@Override
Expand Down Expand Up @@ -193,36 +178,16 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca
enable = this.enableRebalance;
usedIndexSetting = false;
}
switch (enable) {
case ALL:
return allocation.decision(Decision.YES, NAME, "all rebalancing is allowed");
case NONE:
return allocation.decision(Decision.NO, NAME, "no rebalancing is allowed due to %s", setting(enable, usedIndexSetting));
case PRIMARIES:
if (shardRouting.primary()) {
return allocation.decision(Decision.YES, NAME, "primary rebalancing is allowed");
} else {
return allocation.decision(
Decision.NO,
NAME,
"replica rebalancing is forbidden due to %s",
setting(enable, usedIndexSetting)
);
}
case REPLICAS:
if (shardRouting.primary() == false) {
return allocation.decision(Decision.YES, NAME, "replica rebalancing is allowed");
} else {
return allocation.decision(
Decision.NO,
NAME,
"primary rebalancing is forbidden due to %s",
setting(enable, usedIndexSetting)
);
}
default:
throw new IllegalStateException("Unknown rebalance option");
}
return switch (enable) {
case ALL -> allocation.decision(Decision.YES, NAME, "all rebalancing is allowed");
case NONE -> allocation.decision(Decision.NO, NAME, "no rebalancing is allowed due to %s", setting(enable, usedIndexSetting));
case PRIMARIES -> shardRouting.primary()
? allocation.decision(Decision.YES, NAME, "primary rebalancing is allowed")
: allocation.decision(Decision.NO, NAME, "replica rebalancing is forbidden due to %s", setting(enable, usedIndexSetting));
case REPLICAS -> shardRouting.primary()
? allocation.decision(Decision.NO, NAME, "primary rebalancing is forbidden due to %s", setting(enable, usedIndexSetting))
: allocation.decision(Decision.YES, NAME, "replica rebalancing is allowed");
};
}

private static String setting(Allocation allocation, boolean usedIndexSetting) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

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

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

private static final String NAME = "node_shutdown";

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

switch (thisNodeShutdownMetadata.getType()) {
case REPLACE:
case REMOVE:
return allocation.decision(Decision.NO, NAME, "node [%s] is preparing to be removed from the cluster", node.nodeId());
case RESTART:
return allocation.decision(
Decision.YES,
NAME,
"node [%s] is preparing to restart, but will remain in the cluster",
node.nodeId()
);
default:
logger.debug(
"found unrecognized node shutdown type [{}] while deciding allocation for [{}] shard [{}][{}] on node [{}]",
thisNodeShutdownMetadata.getType(),
shardRouting.primary() ? "primary" : "replica",
shardRouting.getIndexName(),
shardRouting.getId(),
node.nodeId()
);
assert false : "node shutdown type not recognized: " + thisNodeShutdownMetadata.getType();
return Decision.YES;
}
return switch (thisNodeShutdownMetadata.getType()) {
case REPLACE, REMOVE -> allocation.decision(
Decision.NO,
NAME,
"node [%s] is preparing to be removed from the cluster",
node.nodeId()
);
case RESTART -> allocation.decision(
Decision.YES,
NAME,
"node [%s] is preparing to restart, but will remain in the cluster",
node.nodeId()
);
};
}

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

switch (thisNodeShutdownMetadata.getType()) {
case RESTART:
return allocation.decision(
Decision.NO,
NAME,
"node [%s] is preparing to restart, auto-expansion waiting until it is complete",
node.getId()
);
case REPLACE:
case REMOVE:
return allocation.decision(Decision.NO, NAME, "node [%s] is preparing for removal from the cluster", node.getId());
default:
logger.debug(
"found unrecognized node shutdown type [{}] while deciding auto-expansion for index [{}] on node [{}]",
thisNodeShutdownMetadata.getType(),
indexMetadata.getIndex().getName(),
node.getId()
);
assert false : "node shutdown type not recognized: " + thisNodeShutdownMetadata.getType();
return Decision.YES;
}
return switch (thisNodeShutdownMetadata.getType()) {
case RESTART -> allocation.decision(
Decision.NO,
NAME,
"node [%s] is preparing to restart, auto-expansion waiting until it is complete",
node.getId()
);
case REPLACE, REMOVE -> allocation.decision(
Decision.NO,
NAME,
"node [%s] is preparing for removal from the cluster",
node.getId()
);
};
}

@Nullable
Expand Down