From fa19239d4441c8048fca6c714b1afb9b99c6c2f1 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Fri, 12 Aug 2011 02:21:47 +0300 Subject: [PATCH] Updating `index.auto_expand_replicas` might not be applied correctly, closes #1237. --- .../MetaDataUpdateSettingsService.java | 18 +++++++---- .../settings/UpdateNumberOfReplicasTests.java | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java index d10c35d1d84..c73262d0f76 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java @@ -61,13 +61,11 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements if (!event.state().nodes().localNodeMaster()) { return; } - // TODO we only need to do that on first create of an index, or the number of nodes changed + // we need to do this each time in case it was changed by update settings for (final IndexMetaData indexMetaData : event.state().metaData()) { String autoExpandReplicas = indexMetaData.settings().get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS); if (autoExpandReplicas != null && Booleans.parseBoolean(autoExpandReplicas, true)) { // Booleans only work for false values, just as we want it here try { - final int numberOfReplicas = event.state().nodes().dataNodes().size() - 1; - int min; int max; try { @@ -83,20 +81,28 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements continue; } + int numberOfReplicas = event.state().nodes().dataNodes().size() - 1; + if (numberOfReplicas < min) { + numberOfReplicas = min; + } else if (numberOfReplicas > max) { + numberOfReplicas = max; + } + // same value, nothing to do there if (numberOfReplicas == indexMetaData.numberOfReplicas()) { continue; } if (numberOfReplicas >= min && numberOfReplicas <= max) { - Settings settings = ImmutableSettings.settingsBuilder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas).build(); + final int fNumberOfReplicas = numberOfReplicas; + Settings settings = ImmutableSettings.settingsBuilder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, fNumberOfReplicas).build(); updateSettings(settings, new String[]{indexMetaData.index()}, new Listener() { @Override public void onSuccess() { - logger.info("[{}] auto expanded replicas to [{}]", indexMetaData.index(), numberOfReplicas); + logger.info("[{}] auto expanded replicas to [{}]", indexMetaData.index(), fNumberOfReplicas); } @Override public void onFailure(Throwable t) { - logger.warn("[{}] fail to auto expand replicas to [{}]", indexMetaData.index(), numberOfReplicas); + logger.warn("[{}] fail to auto expand replicas to [{}]", indexMetaData.index(), fNumberOfReplicas); } }); } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/settings/UpdateNumberOfReplicasTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/settings/UpdateNumberOfReplicasTests.java index cd5a160a3ee..2cbdd6f589e 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/settings/UpdateNumberOfReplicasTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/settings/UpdateNumberOfReplicasTests.java @@ -241,4 +241,36 @@ public class UpdateNumberOfReplicasTests extends AbstractNodesTests { assertThat(clusterHealth.indices().get("test").numberOfReplicas(), equalTo(1)); assertThat(clusterHealth.indices().get("test").activeShards(), equalTo(2)); } + + @Test public void testAutoExpandNumberReplicas2() { + logger.info("--> add another node"); + startNode("node3"); + logger.info("--> creating index test with auto expand replicas set to 0-2"); + client1.admin().indices().prepareCreate("test").setSettings(settingsBuilder().put("number_of_shards", 2).put("auto_expand_replicas", "0-2")).execute().actionGet(); + + logger.info("--> running cluster health"); + ClusterHealthResponse clusterHealth = client1.admin().cluster().prepareHealth().setWaitForGreenStatus().setWaitForActiveShards(6).execute().actionGet(); + logger.info("--> done cluster health, status " + clusterHealth.status()); + assertThat(clusterHealth.timedOut(), equalTo(false)); + assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN)); + assertThat(clusterHealth.indices().get("test").activePrimaryShards(), equalTo(2)); + assertThat(clusterHealth.indices().get("test").numberOfReplicas(), equalTo(2)); + assertThat(clusterHealth.indices().get("test").activeShards(), equalTo(6)); + + logger.info("--> add two more nodes"); + startNode("node4"); + startNode("node5"); + + logger.info("--> update the auto expand replicas to 0-3"); + client1.admin().indices().prepareUpdateSettings("test").setSettings(settingsBuilder().put("auto_expand_replicas", "0-3")).execute().actionGet(); + + logger.info("--> running cluster health"); + clusterHealth = client1.admin().cluster().prepareHealth().setWaitForGreenStatus().setWaitForActiveShards(8).execute().actionGet(); + logger.info("--> done cluster health, status " + clusterHealth.status()); + assertThat(clusterHealth.timedOut(), equalTo(false)); + assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN)); + assertThat(clusterHealth.indices().get("test").activePrimaryShards(), equalTo(2)); + assertThat(clusterHealth.indices().get("test").numberOfReplicas(), equalTo(3)); + assertThat(clusterHealth.indices().get("test").activeShards(), equalTo(8)); + } }