diff --git a/server/src/internalClusterTest/java/org/elasticsearch/index/shard/IndexShardIT.java b/server/src/internalClusterTest/java/org/elasticsearch/index/shard/IndexShardIT.java index 058d56b9d5a29..f51e7209314ba 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/index/shard/IndexShardIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/index/shard/IndexShardIT.java @@ -305,6 +305,9 @@ public void testHeapUsageEstimateIsPresent() { } public void testNodeWriteLoadsArePresent() { + // Disable write load decider to begin with + setWriteLoadDeciderEnablement(WriteLoadConstraintSettings.WriteLoadDeciderStatus.DISABLED); + InternalClusterInfoService clusterInfoService = (InternalClusterInfoService) getInstanceFromNode(ClusterInfoService.class); ClusterInfoServiceUtils.refresh(clusterInfoService); Map nodeThreadPoolStats = clusterInfoService.getClusterInfo() @@ -315,15 +318,10 @@ public void testNodeWriteLoadsArePresent() { assertTrue(nodeThreadPoolStats.isEmpty()); // Enable collection for node write loads. - updateClusterSettings( - Settings.builder() - .put( - WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey(), - randomBoolean() - ? WriteLoadConstraintSettings.WriteLoadDeciderStatus.ENABLED - : WriteLoadConstraintSettings.WriteLoadDeciderStatus.LOW_THRESHOLD_ONLY - ) - .build() + setWriteLoadDeciderEnablement( + randomBoolean() + ? WriteLoadConstraintSettings.WriteLoadDeciderStatus.ENABLED + : WriteLoadConstraintSettings.WriteLoadDeciderStatus.LOW_THRESHOLD_ONLY ); try { // Force a ClusterInfo refresh to run collection of the node thread pool usage stats. @@ -346,9 +344,7 @@ public void testNodeWriteLoadsArePresent() { assertThat(writeThreadPoolStats.maxThreadPoolQueueLatencyMillis(), greaterThanOrEqualTo(0L)); } } finally { - updateClusterSettings( - Settings.builder().putNull(WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey()).build() - ); + clearWriteLoadDeciderEnablementSetting(); } } @@ -365,27 +361,25 @@ public void testShardWriteLoadsArePresent() { final InternalClusterInfoService clusterInfoService = (InternalClusterInfoService) getInstanceFromNode(ClusterInfoService.class); - // Not collecting stats yet because allocation write load stats collection is disabled by default. - { - ClusterInfoServiceUtils.refresh(clusterInfoService); - final Map shardWriteLoads = clusterInfoService.getClusterInfo().getShardWriteLoads(); - assertNotNull(shardWriteLoads); - assertTrue(shardWriteLoads.isEmpty()); - } - - // Turn on collection of write-load stats. - updateClusterSettings( - Settings.builder() - .put( - WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey(), - randomBoolean() - ? WriteLoadConstraintSettings.WriteLoadDeciderStatus.ENABLED - : WriteLoadConstraintSettings.WriteLoadDeciderStatus.LOW_THRESHOLD_ONLY - ) - .build() - ); + // Explicitly disable write load decider + setWriteLoadDeciderEnablement(WriteLoadConstraintSettings.WriteLoadDeciderStatus.DISABLED); try { + // Stats should not be collected when the decider is disabled + { + ClusterInfoServiceUtils.refresh(clusterInfoService); + final Map shardWriteLoads = clusterInfoService.getClusterInfo().getShardWriteLoads(); + assertNotNull(shardWriteLoads); + assertTrue(shardWriteLoads.isEmpty()); + } + + // Turn on collection of write-load stats. + setWriteLoadDeciderEnablement( + randomBoolean() + ? WriteLoadConstraintSettings.WriteLoadDeciderStatus.ENABLED + : WriteLoadConstraintSettings.WriteLoadDeciderStatus.LOW_THRESHOLD_ONLY + ); + // Force a ClusterInfo refresh to run collection of the write-load stats. ClusterInfoServiceUtils.refresh(clusterInfoService); final Map shardWriteLoads = clusterInfoService.getClusterInfo().getShardWriteLoads(); @@ -404,12 +398,14 @@ public void testShardWriteLoadsArePresent() { assertThat(maximumLoadRecorded, greaterThan(0.0)); } } finally { - updateClusterSettings( - Settings.builder().putNull(WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey()).build() - ); + clearWriteLoadDeciderEnablementSetting(); } } + private void clearWriteLoadDeciderEnablementSetting() { + updateClusterSettings(Settings.builder().putNull(WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey()).build()); + } + public void testMaxHeapPerNodeIsPresent() { InternalClusterInfoService clusterInfoService = (InternalClusterInfoService) getInstanceFromNode(ClusterInfoService.class); ClusterInfoServiceUtils.refresh(clusterInfoService); @@ -764,6 +760,12 @@ public void postDelete(ShardId shardId, Engine.Delete delete, Engine.DeleteResul } } + private void setWriteLoadDeciderEnablement(WriteLoadConstraintSettings.WriteLoadDeciderStatus status) { + updateClusterSettings( + Settings.builder().put(WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey(), status).build() + ); + } + public static final IndexShard recoverShard(IndexShard newShard) throws IOException { DiscoveryNode localNode = DiscoveryNodeUtils.builder("foo").roles(emptySet()).build(); newShard.markAsRecovering("store", new RecoveryState(newShard.routingEntry(), localNode, null)); diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/WriteLoadConstraintSettings.java b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/WriteLoadConstraintSettings.java index 21c1a7ba04f0f..156f1d30d66c0 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/WriteLoadConstraintSettings.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/WriteLoadConstraintSettings.java @@ -15,6 +15,7 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.unit.RatioValue; +import org.elasticsearch.common.util.FeatureFlag; import org.elasticsearch.core.TimeValue; /** @@ -23,6 +24,7 @@ public class WriteLoadConstraintSettings { private static final String SETTING_PREFIX = "cluster.routing.allocation.write_load_decider."; + private static final FeatureFlag WRITE_LOAD_DECIDER_FEATURE_FLAG = new FeatureFlag("write_load_decider"); public enum WriteLoadDeciderStatus { /** @@ -59,7 +61,7 @@ public boolean disabled() { public static final Setting WRITE_LOAD_DECIDER_ENABLED_SETTING = Setting.enumSetting( WriteLoadDeciderStatus.class, SETTING_PREFIX + "enabled", - WriteLoadDeciderStatus.DISABLED, + WRITE_LOAD_DECIDER_FEATURE_FLAG.isEnabled() ? WriteLoadDeciderStatus.ENABLED : WriteLoadDeciderStatus.DISABLED, Setting.Property.Dynamic, Setting.Property.NodeScope ); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/WriteLoadConstraintDeciderTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/WriteLoadConstraintDeciderTests.java index 5b844cc1da635..08b4a706e4785 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/WriteLoadConstraintDeciderTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/WriteLoadConstraintDeciderTests.java @@ -43,9 +43,14 @@ public void testWriteLoadDeciderDisabled() { String indexName = "test-index"; var testHarness = createClusterStateAndRoutingAllocation(indexName); - // The write load decider is disabled by default. - - var writeLoadDecider = createWriteLoadConstraintDecider(Settings.builder().build()); + var writeLoadDecider = createWriteLoadConstraintDecider( + Settings.builder() + .put( + WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey(), + WriteLoadConstraintSettings.WriteLoadDeciderStatus.DISABLED + ) + .build() + ); assertEquals( Decision.Type.YES,