From b2bfa9ed4f1fc7bde66b9c50a215f22e90d39c2b Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Thu, 7 Jan 2021 11:27:22 -0700 Subject: [PATCH] [7.10] Fix ignoring existed cluster settings in DataTierAllocationDecider (#67137) (4295d489) (#67180) Relates to #67133. Seem to #65037. The main changes of this PR are: Modify the construction method of DataTierAllocationDecider, add a param settings like FilterAllocationDecider. Create DataTierAllocationDecider in the main method of DataTierMigrationRoutedStep and SetSingleNodeAllocateStep, and the DataTierAllocationDecider is constructed using the cluster settings in the cluster metadata, so the cluster level _tier filters can be seen when executing the steps. Add some tests for the change. Co-authored-by: bellengao --- .../allocation/DataTierAllocationDecider.java | 11 ++-- .../elasticsearch/xpack/core/XPackPlugin.java | 2 +- .../core/ilm/DataTierMigrationRoutedStep.java | 14 ++--- .../core/ilm/SetSingleNodeAllocateStep.java | 2 +- .../DataTierAllocationDeciderTests.java | 55 ++++++++++++++++++- 5 files changed, 70 insertions(+), 14 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDecider.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDecider.java index cc555b9c8f8..59ed4140394 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDecider.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDecider.java @@ -70,11 +70,14 @@ public class DataTierAllocationDecider extends AllocationDecider { } } - private volatile String clusterRequire = null; - private volatile String clusterInclude = null; - private volatile String clusterExclude = null; + private volatile String clusterRequire; + private volatile String clusterInclude; + private volatile String clusterExclude; - public DataTierAllocationDecider(ClusterSettings clusterSettings) { + public DataTierAllocationDecider(Settings settings, ClusterSettings clusterSettings) { + clusterRequire = CLUSTER_ROUTING_REQUIRE_SETTING.get(settings); + clusterInclude = CLUSTER_ROUTING_INCLUDE_SETTING.get(settings); + clusterExclude = CLUSTER_ROUTING_EXCLUDE_SETTING.get(settings); clusterSettings.addSettingsUpdateConsumer(CLUSTER_ROUTING_REQUIRE_SETTING, s -> this.clusterRequire = s); clusterSettings.addSettingsUpdateConsumer(CLUSTER_ROUTING_INCLUDE_SETTING, s -> this.clusterInclude = s); clusterSettings.addSettingsUpdateConsumer(CLUSTER_ROUTING_EXCLUDE_SETTING, s -> this.clusterExclude = s); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java index ec6320e43f3..d107110ff04 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java @@ -436,7 +436,7 @@ public class XPackPlugin extends XPackClientPlugin implements ExtensiblePlugin, @Override public Collection createAllocationDeciders(Settings settings, ClusterSettings clusterSettings) { - return Collections.singleton(new DataTierAllocationDecider(clusterSettings)); + return Collections.singleton(new DataTierAllocationDecider(settings, clusterSettings)); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java index 9fa62e22b6c..a8934d73bc1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java @@ -47,12 +47,6 @@ public class DataTierMigrationRoutedStep extends ClusterStateWaitStep { ALL_CLUSTER_SETTINGS = allSettings; } - private static final AllocationDeciders ALLOCATION_DECIDERS = new AllocationDeciders( - org.elasticsearch.common.collect.List.of( - new DataTierAllocationDecider(new ClusterSettings(Settings.EMPTY, ALL_CLUSTER_SETTINGS)) - ) - ); - DataTierMigrationRoutedStep(StepKey key, StepKey nextStepKey) { super(key, nextStepKey); } @@ -64,6 +58,12 @@ public class DataTierMigrationRoutedStep extends ClusterStateWaitStep { @Override public Result isConditionMet(Index index, ClusterState clusterState) { + AllocationDeciders allocationDeciders = new AllocationDeciders( + org.elasticsearch.common.collect.List.of( + new DataTierAllocationDecider(clusterState.getMetadata().settings(), + new ClusterSettings(Settings.EMPTY, ALL_CLUSTER_SETTINGS)) + ) + ); IndexMetadata idxMeta = clusterState.metadata().index(index); if (idxMeta == null) { // Index must have been since deleted, ignore it @@ -98,7 +98,7 @@ public class DataTierMigrationRoutedStep extends ClusterStateWaitStep { return new Result(true, null); } - int allocationPendingAllShards = getPendingAllocations(index, ALLOCATION_DECIDERS, clusterState); + int allocationPendingAllShards = getPendingAllocations(index, allocationDeciders, clusterState); if (allocationPendingAllShards > 0) { String statusMessage = availableDestinationTier.map( diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java index c719478b21b..5b12b8cbfee 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java @@ -75,7 +75,7 @@ public class SetSingleNodeAllocateStep extends AsyncActionStep { AllocationDeciders allocationDeciders = new AllocationDeciders(Arrays.asList( new FilterAllocationDecider(clusterState.getMetadata().settings(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), - new DataTierAllocationDecider(new ClusterSettings(Settings.EMPTY, ALL_CLUSTER_SETTINGS)), + new DataTierAllocationDecider(clusterState.getMetadata().settings(), new ClusterSettings(Settings.EMPTY, ALL_CLUSTER_SETTINGS)), new NodeVersionAllocationDecider() )); final RoutingNodes routingNodes = clusterState.getRoutingNodes(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderTests.java index 8a9487b44db..2f6bbe30de5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderTests.java @@ -53,7 +53,7 @@ public class DataTierAllocationDeciderTests extends ESAllocationTestCase { private static final DiscoveryNode DATA_NODE = newNode("node-data", Collections.singleton(DiscoveryNodeRole.DATA_ROLE)); private final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ALL_SETTINGS); - private final DataTierAllocationDecider decider = new DataTierAllocationDecider(clusterSettings); + private final DataTierAllocationDecider decider = new DataTierAllocationDecider(Settings.EMPTY, clusterSettings); private final AllocationDeciders allocationDeciders = new AllocationDeciders( Arrays.asList(decider, new SameShardAllocationDecider(Settings.EMPTY, clusterSettings), @@ -648,6 +648,59 @@ public class DataTierAllocationDeciderTests extends ESAllocationTestCase { equalTo(Optional.of("data_warm"))); } + public void testExistedClusterFilters() { + Settings existedSettings = Settings.builder() + .put("cluster.routing.allocation.include._tier", "data_hot,data_warm") + .put("cluster.routing.allocation.exclude._tier", "data_cold") + .build(); + ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ALL_SETTINGS); + DataTierAllocationDecider dataTierAllocationDecider = new DataTierAllocationDecider(existedSettings, clusterSettings); + AllocationDeciders allocationDeciders = new AllocationDeciders(Arrays.asList(dataTierAllocationDecider)); + AllocationService service = new AllocationService(allocationDeciders, + new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE, + EmptySnapshotsInfoService.INSTANCE); + + ClusterState clusterState = prepareState(service.reroute(ClusterState.EMPTY_STATE, "initial state")); + + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, clusterState.getRoutingNodes(), clusterState, + null, null, 0); + allocation.debugDecision(true); + Decision d; + RoutingNode node; + + for (DiscoveryNode n : Arrays.asList(HOT_NODE, WARM_NODE)) { + node = new RoutingNode(n.getId(), n, shard); + d = dataTierAllocationDecider.canAllocate(shard, node, allocation); + assertThat(d.type(), equalTo(Decision.Type.YES)); + d = dataTierAllocationDecider.canRemain(shard, node, allocation); + assertThat(d.type(), equalTo(Decision.Type.YES)); + } + + node = new RoutingNode(DATA_NODE.getId(), DATA_NODE, shard); + d = dataTierAllocationDecider.canAllocate(shard, node, allocation); + assertThat(d.type(), equalTo(Decision.Type.NO)); + assertThat(d.getExplanation(), + containsString("node matches any cluster setting [cluster.routing.allocation.exclude._tier] " + + "tier filters [data_cold]")); + d = dataTierAllocationDecider.canRemain(shard, node, allocation); + assertThat(d.type(), equalTo(Decision.Type.NO)); + assertThat(d.getExplanation(), + containsString("node matches any cluster setting [cluster.routing.allocation.exclude._tier] " + + "tier filters [data_cold]")); + + node = new RoutingNode(COLD_NODE.getId(), COLD_NODE, shard); + d = dataTierAllocationDecider.canAllocate(shard, node, allocation); + assertThat(d.type(), equalTo(Decision.Type.NO)); + assertThat(d.getExplanation(), + containsString("node does not match any cluster setting [cluster.routing.allocation.include._tier] " + + "tier filters [data_hot,data_warm]")); + d = dataTierAllocationDecider.canRemain(shard, node, allocation); + assertThat(d.type(), equalTo(Decision.Type.NO)); + assertThat(d.getExplanation(), + containsString("node does not match any cluster setting [cluster.routing.allocation.include._tier] " + + "tier filters [data_hot,data_warm]")); + } + private ClusterState prepareState(ClusterState initialState) { return prepareState(initialState, Settings.EMPTY); }