1
0
Эх сурвалжийг харах

Use switch expressions in EnableAllocationDecider and NodeShutdownAllocationDecider (#83641)

* Reverse condition for replicas
Artem Prigoda 3 жил өмнө
parent
commit
4108dab4e3

+ 22 - 57
server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationDecider.java

@@ -124,38 +124,23 @@ public class EnableAllocationDecider extends AllocationDecider {
             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
@@ -193,36 +178,16 @@ public class EnableAllocationDecider extends AllocationDecider {
             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) {

+ 28 - 47
server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/NodeShutdownAllocationDecider.java

@@ -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;
@@ -29,7 +27,6 @@ import org.elasticsearch.core.Nullable;
  * 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";
 
@@ -45,29 +42,20 @@ public class NodeShutdownAllocationDecider extends AllocationDecider {
             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()
+            );
+        };
     }
 
     /**
@@ -91,27 +79,20 @@ public class NodeShutdownAllocationDecider extends AllocationDecider {
             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