From 72982d955aa7129527bb1f2bf6165674f7b4d6d7 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 2 May 2013 16:21:16 +0200 Subject: [PATCH] Use current settings as default in BalancedShardsAllocator instead of defaults. Custom settings are not always present in the `Settings` that are passed to `NodeSettingsService.Listener#onRefreshSettings` such that using the defaults will necessarily override the custom settings if set before. Closes #2973 --- .../allocator/BalancedShardsAllocator.java | 44 +++++++++++++++--- .../allocation/BalanceConfigurationTests.java | 45 +++++++++++++++++++ 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java b/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java index b57bda985bf..f59e44c359b 100644 --- a/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java +++ b/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java @@ -66,14 +66,18 @@ public class BalancedShardsAllocator extends AbstractComponent implements Shards public static final String SETTING_INDEX_BALANCE_FACTOR = "cluster.routing.allocation.balance.index"; public static final String SETTING_SHARD_BALANCE_FACTOR = "cluster.routing.allocation.balance.shard"; public static final String SETTING_PRIMARY_BALANCE_FACTOR = "cluster.routing.allocation.balance.primary"; + + private static final float DEFAULT_INDEX_BALANCE_FACTOR = 0.5f; + private static final float DEFAULT_SHARD_BALANCE_FACTOR = 0.45f; + private static final float DEFAULT_PRIMARY_BALANCE_FACTOR = 0.05f; class ApplySettings implements NodeSettingsService.Listener { @Override public void onRefreshSettings(Settings settings) { - float indexBalance = settings.getAsFloat(SETTING_INDEX_BALANCE_FACTOR, 0.5f); - float shardBalance = settings.getAsFloat(SETTING_SHARD_BALANCE_FACTOR, 0.45f); - float primaryBalance = settings.getAsFloat(SETTING_PRIMARY_BALANCE_FACTOR, 0.05f); - float threshold = settings.getAsFloat(SETTING_THRESHOLD, 1.0f); + float indexBalance = settings.getAsFloat(SETTING_INDEX_BALANCE_FACTOR, weightFunction.indexBalance); + float shardBalance = settings.getAsFloat(SETTING_SHARD_BALANCE_FACTOR, weightFunction.shardBalance); + float primaryBalance = settings.getAsFloat(SETTING_PRIMARY_BALANCE_FACTOR, weightFunction.primaryBalance); + float threshold = settings.getAsFloat(SETTING_THRESHOLD, BalancedShardsAllocator.this.threshold); if (threshold <= 0.0f) { throw new ElasticSearchIllegalArgumentException("threshold must be greater than 0.0f but was: " + threshold); } @@ -82,8 +86,8 @@ public class BalancedShardsAllocator extends AbstractComponent implements Shards } } - private volatile WeightFunction weightFunction; - private volatile float threshold; + private volatile WeightFunction weightFunction = new WeightFunction(DEFAULT_INDEX_BALANCE_FACTOR, DEFAULT_SHARD_BALANCE_FACTOR, DEFAULT_PRIMARY_BALANCE_FACTOR); + private volatile float threshold = 1.0f; public BalancedShardsAllocator(Settings settings) { this(settings, new NodeSettingsService(settings)); @@ -119,6 +123,34 @@ public class BalancedShardsAllocator extends AbstractComponent implements Shards final Balancer balancer = new Balancer(logger, allocation, weightFunction, threshold); return balancer.move(shardRouting, node); } + + /** + * Returns the currently configured delta threshold + */ + public float getThreshold() { + return threshold; + } + + /** + * Returns the index related weight factor. + */ + public float getIndexBalance() { + return weightFunction.indexBalance; + } + + /** + * Returns the primary related weight factor. + */ + public float getPrimaryBalance() { + return weightFunction.primaryBalance; + } + + /** + * Returns the shard related weight factor. + */ + public float getShardBalance() { + return weightFunction.shardBalance; + } /** diff --git a/src/test/java/org/elasticsearch/test/unit/cluster/routing/allocation/BalanceConfigurationTests.java b/src/test/java/org/elasticsearch/test/unit/cluster/routing/allocation/BalanceConfigurationTests.java index 20b4ea30e76..87425ba16ad 100644 --- a/src/test/java/org/elasticsearch/test/unit/cluster/routing/allocation/BalanceConfigurationTests.java +++ b/src/test/java/org/elasticsearch/test/unit/cluster/routing/allocation/BalanceConfigurationTests.java @@ -44,6 +44,8 @@ import org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllo import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.node.settings.NodeSettingsService; +import org.elasticsearch.node.settings.NodeSettingsService.Listener; import org.hamcrest.Matchers; import org.testng.annotations.Test; @@ -308,4 +310,47 @@ public class BalanceConfigurationTests { } } + @Test + public void testPersistedSettings() { + ImmutableSettings.Builder settings = settingsBuilder(); + settings.put(BalancedShardsAllocator.SETTING_INDEX_BALANCE_FACTOR, 0.2); + settings.put(BalancedShardsAllocator.SETTING_SHARD_BALANCE_FACTOR, 0.3); + settings.put(BalancedShardsAllocator.SETTING_PRIMARY_BALANCE_FACTOR, 0.5); + settings.put(BalancedShardsAllocator.SETTING_THRESHOLD, 2.0); + final NodeSettingsService.Listener[] listeners = new NodeSettingsService.Listener[1]; + NodeSettingsService service = new NodeSettingsService(settingsBuilder().build()) { + + @Override + public void addListener(Listener listener) { + assert listeners[0] == null; + listeners[0] = listener; + } + + }; + BalancedShardsAllocator allocator = new BalancedShardsAllocator(settings.build(), service); + assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.2f)); + assertThat(allocator.getShardBalance(), Matchers.equalTo(0.3f)); + assertThat(allocator.getPrimaryBalance(), Matchers.equalTo(0.5f)); + assertThat(allocator.getThreshold(), Matchers.equalTo(2.0f)); + + settings = settingsBuilder(); + settings.put("cluster.routing.allocation.allow_rebalance", ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString()); + listeners[0].onRefreshSettings(settings.build()); + assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.2f)); + assertThat(allocator.getShardBalance(), Matchers.equalTo(0.3f)); + assertThat(allocator.getPrimaryBalance(), Matchers.equalTo(0.5f)); + assertThat(allocator.getThreshold(), Matchers.equalTo(2.0f)); + + settings = settingsBuilder(); + settings.put(BalancedShardsAllocator.SETTING_INDEX_BALANCE_FACTOR, 0.5); + settings.put(BalancedShardsAllocator.SETTING_SHARD_BALANCE_FACTOR, 0.1); + settings.put(BalancedShardsAllocator.SETTING_PRIMARY_BALANCE_FACTOR, 0.4); + settings.put(BalancedShardsAllocator.SETTING_THRESHOLD, 3.0); + listeners[0].onRefreshSettings(settings.build()); + assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.5f)); + assertThat(allocator.getShardBalance(), Matchers.equalTo(0.1f)); + assertThat(allocator.getPrimaryBalance(), Matchers.equalTo(0.4f)); + assertThat(allocator.getThreshold(), Matchers.equalTo(3.0f)); + } + } \ No newline at end of file