From 35fa6d93ad402c3cbf8774bbf0f0f4a0b01ee020 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sun, 19 Jun 2011 17:15:05 +0300 Subject: [PATCH] Update Settings: Allow to change non dynamic settings on a closed index, closes #1048. --- .../MetaDataUpdateSettingsService.java | 37 +++++++-- .../indices/settings/UpdateSettingsTests.java | 77 +++++++++++++++++++ 2 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/settings/UpdateSettingsTests.java 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 a7d6301ad15..8beb8853967 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 @@ -122,19 +122,20 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements } } - Set removedSettings = Sets.newHashSet(); + final Settings closeSettings = updatedSettingsBuilder.build(); + + final Set removedSettings = Sets.newHashSet(); for (String key : updatedSettingsBuilder.internalMap().keySet()) { if (!IndexMetaData.dynamicSettings().contains(key)) { removedSettings.add(key); } } if (!removedSettings.isEmpty()) { - logger.warn("{} ignoring non dynamic index level settings: {}", indices, removedSettings); for (String removedSetting : removedSettings) { updatedSettingsBuilder.remove(removedSetting); } } - final Settings settings = updatedSettingsBuilder.build(); + final Settings openSettings = updatedSettingsBuilder.build(); clusterService.submitStateUpdateTask("update-settings", new ProcessedClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { @@ -143,14 +144,38 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements RoutingTable.Builder routingTableBuilder = newRoutingTableBuilder().routingTable(currentState.routingTable()); MetaData.Builder metaDataBuilder = MetaData.newMetaDataBuilder().metaData(currentState.metaData()); - int updatedNumberOfReplicas = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, -1); + int updatedNumberOfReplicas = openSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, -1); if (updatedNumberOfReplicas != -1) { routingTableBuilder.updateNumberOfReplicas(updatedNumberOfReplicas, actualIndices); metaDataBuilder.updateNumberOfReplicas(updatedNumberOfReplicas, actualIndices); - logger.info("Updating number_of_replicas to [{}] for indices {}", updatedNumberOfReplicas, actualIndices); + logger.info("updating number_of_replicas to [{}] for indices {}", updatedNumberOfReplicas, actualIndices); + } + + // allow to change any settings to a close index, and only allow dynamic settings to be changed + // on an open index + Set openIndices = Sets.newHashSet(); + Set closeIndices = Sets.newHashSet(); + for (String index : actualIndices) { + if (currentState.metaData().index(index).state() == IndexMetaData.State.OPEN) { + openIndices.add(index); + } else { + closeIndices.add(index); + } + } + + if (!openIndices.isEmpty()) { + String[] indices = openIndices.toArray(new String[openIndices.size()]); + if (!removedSettings.isEmpty()) { + logger.warn("{} ignoring non dynamic index level settings for open indices: {}", indices, removedSettings); + } + metaDataBuilder.updateSettings(openSettings, indices); + } + + if (!closeIndices.isEmpty()) { + String[] indices = closeIndices.toArray(new String[closeIndices.size()]); + metaDataBuilder.updateSettings(closeSettings, indices); } - metaDataBuilder.updateSettings(settings, actualIndices); return ClusterState.builder().state(currentState).metaData(metaDataBuilder).routingTable(routingTableBuilder).build(); } catch (Exception e) { diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/settings/UpdateSettingsTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/settings/UpdateSettingsTests.java new file mode 100644 index 00000000000..52124ba6f9f --- /dev/null +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/indices/settings/UpdateSettingsTests.java @@ -0,0 +1,77 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.test.integration.indices.settings; + +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.test.integration.AbstractNodesTests; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +public class UpdateSettingsTests extends AbstractNodesTests { + + @BeforeClass public void startNodes() { + startNode("node1"); + } + + @AfterClass public void closeNodes() { + closeAllNodes(); + } + + @Test public void testOpenCloseUpdateSettings() throws Exception { + try { + client("node1").admin().indices().prepareDelete("test").execute().actionGet(); + } catch (Exception e) { + // ignore + } + + client("node1").admin().indices().prepareCreate("test").execute().actionGet(); + + client("node1").admin().indices().prepareUpdateSettings("test") + .setSettings(ImmutableSettings.settingsBuilder() + .put("index.refresh_interval", -1) // this one can change + .put("index.cache.filter.type", "none") // this one can't + ) + .execute().actionGet(); + + IndexMetaData indexMetaData = client("node1").admin().cluster().prepareState().execute().actionGet().getState().metaData().index("test"); + assertThat(indexMetaData.settings().get("index.refresh_interval"), equalTo("-1")); + assertThat(indexMetaData.settings().get("index.cache.filter.type"), nullValue()); + + // now close the index, change the non dynamic setting, and see that it applies + + client("node1").admin().indices().prepareClose("test").execute().actionGet(); + + client("node1").admin().indices().prepareUpdateSettings("test") + .setSettings(ImmutableSettings.settingsBuilder() + .put("index.refresh_interval", "1s") // this one can change + .put("index.cache.filter.type", "none") // this one can't + ) + .execute().actionGet(); + + indexMetaData = client("node1").admin().cluster().prepareState().execute().actionGet().getState().metaData().index("test"); + assertThat(indexMetaData.settings().get("index.refresh_interval"), equalTo("1s")); + assertThat(indexMetaData.settings().get("index.cache.filter.type"), equalTo("none")); + } +} \ No newline at end of file