From 3567daa9c82ac83a9d3997e7861ac6014e33e93b Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sat, 16 Jan 2016 15:22:29 +0100 Subject: [PATCH] add ability to reset index settings to it's default --- .../cluster/settings/SettingsUpdater.java | 4 +-- .../MetaDataUpdateSettingsService.java | 27 +++++++++++--- .../settings/AbstractScopedSettings.java | 13 +++++-- .../indices/settings/UpdateSettingsIT.java | 36 +++++++++++++++++++ 4 files changed, 72 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/settings/SettingsUpdater.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/settings/SettingsUpdater.java index b0a3b35c81e..2b222eb1b90 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/settings/SettingsUpdater.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/settings/SettingsUpdater.java @@ -52,11 +52,11 @@ final class SettingsUpdater { boolean changed = false; Settings.Builder transientSettings = Settings.settingsBuilder(); transientSettings.put(currentState.metaData().transientSettings()); - changed |= clusterSettings.applyDynamicSettings(transientToApply, transientSettings, transientUpdates, "transient"); + changed |= clusterSettings.updateSettings(transientToApply, transientSettings, transientUpdates, "transient", false); Settings.Builder persistentSettings = Settings.settingsBuilder(); persistentSettings.put(currentState.metaData().persistentSettings()); - changed |= clusterSettings.applyDynamicSettings(persistentToApply, persistentSettings, persistentUpdates, "persistent"); + changed |= clusterSettings.updateSettings(persistentToApply, persistentSettings, persistentUpdates, "persistent", false); if (!changed) { return currentState; diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java index a76ce36ea37..0943ebdcb8b 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java @@ -42,6 +42,7 @@ import org.elasticsearch.common.settings.IndexScopeSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.index.IndexNotFoundException; import java.util.ArrayList; import java.util.HashMap; @@ -256,13 +257,31 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements } if (!openIndices.isEmpty()) { - String[] indices = openIndices.toArray(new String[openIndices.size()]); - metaDataBuilder.updateSettings(openSettings, indices); + for (String index : openIndices) { + IndexMetaData indexMetaData = metaDataBuilder.get(index); + if (indexMetaData == null) { + throw new IndexNotFoundException(index); + } + Settings.Builder updates = Settings.builder(); + Settings.Builder indexSettings = Settings.builder().put(indexMetaData.getSettings()); + if (indexScopeSettings.updateSettings(openSettings, indexSettings, updates, index, false)) { + metaDataBuilder.put(IndexMetaData.builder(indexMetaData).settings(indexSettings)); + } + } } if (!closeIndices.isEmpty()) { - String[] indices = closeIndices.toArray(new String[closeIndices.size()]); - metaDataBuilder.updateSettings(closedSettings, indices); + for (String index : closeIndices) { + IndexMetaData indexMetaData = metaDataBuilder.get(index); + if (indexMetaData == null) { + throw new IndexNotFoundException(index); + } + Settings.Builder updates = Settings.builder(); + Settings.Builder indexSettings = Settings.builder().put(indexMetaData.getSettings()); + if (indexScopeSettings.updateSettings(closedSettings, indexSettings, updates, index, true)) { + metaDataBuilder.put(IndexMetaData.builder(indexMetaData).settings(indexSettings)); + } + } } diff --git a/core/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java b/core/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java index f35e8d1f3d8..9a4ddb02f14 100644 --- a/core/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java +++ b/core/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java @@ -296,14 +296,23 @@ public abstract class AbstractScopedSettings extends AbstractComponent { return setting.get(this.lastSettingsApplied); } - public boolean applyDynamicSettings(Settings toApply, Settings.Builder target, Settings.Builder updates, String type) { + /** + * Updates a target settings builder with new, updated or deleted settings from a given settings builder. + * @param toApply the new settings to apply + * @param target the target settings builder that the updates are applied to. All keys that have explicit null value in toApply will be removed from this builder + * @param updates a settings builder that holds all updates applied to target + * @param type a free text string to allow better exceptions messages + * @param all if true all settings are updated otherwise only dynamic settings are updated. if set to false and a non-dynamic setting is updated an exception is thrown + * @return true if the target has changed otherwise false + */ + public boolean updateSettings(Settings toApply, Settings.Builder target, Settings.Builder updates, String type, boolean all) { boolean changed = false; final Set toRemove = new HashSet<>(); Settings.Builder settingsBuilder = Settings.settingsBuilder(); for (Map.Entry entry : toApply.getAsMap().entrySet()) { if (entry.getValue() == null) { toRemove.add(entry.getKey()); - } else if (hasDynamicSetting(entry.getKey())) { + } else if ((all && get(entry.getKey()) != null) || hasDynamicSetting(entry.getKey())) { validate(entry.getKey(), entry.getValue()); settingsBuilder.put(entry.getKey(), entry.getValue()); updates.put(entry.getKey(), entry.getValue()); diff --git a/core/src/test/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java b/core/src/test/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java index 2179e153c1e..67fc5acd09a 100644 --- a/core/src/test/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java +++ b/core/src/test/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java @@ -30,11 +30,13 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Priority; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.index.MergePolicyConfig; import org.elasticsearch.index.MergeSchedulerConfig; import org.elasticsearch.index.store.IndexStore; import org.elasticsearch.index.store.Store; +import org.elasticsearch.indices.IndicesService; import org.elasticsearch.test.ESIntegTestCase; import java.util.Arrays; @@ -50,6 +52,40 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; public class UpdateSettingsIT extends ESIntegTestCase { + + public void testResetDefault() { + createIndex("test"); + + client().admin().indices().prepareUpdateSettings("test") + .setSettings(Settings.settingsBuilder() + .put("index.refresh_interval", -1) + .put("index.translog.flush_threshold_size", "1024b") + ) + .execute().actionGet(); + IndexMetaData indexMetaData = client().admin().cluster().prepareState().execute().actionGet().getState().metaData().index("test"); + assertEquals(indexMetaData.getSettings().get("index.refresh_interval"), "-1"); + for (IndicesService service : internalCluster().getInstances(IndicesService.class)) { + IndexService indexService = service.indexService("test"); + if (indexService != null) { + assertEquals(indexService.getIndexSettings().getRefreshInterval().millis(), -1); + assertEquals(indexService.getIndexSettings().getFlushThresholdSize().bytes(), 1024); + } + } + client().admin().indices().prepareUpdateSettings("test") + .setSettings(Settings.settingsBuilder() + .putNull("index.refresh_interval") + ) + .execute().actionGet(); + indexMetaData = client().admin().cluster().prepareState().execute().actionGet().getState().metaData().index("test"); + assertNull(indexMetaData.getSettings().get("index.refresh_interval")); + for (IndicesService service : internalCluster().getInstances(IndicesService.class)) { + IndexService indexService = service.indexService("test"); + if (indexService != null) { + assertEquals(indexService.getIndexSettings().getRefreshInterval().millis(), 1000); + assertEquals(indexService.getIndexSettings().getFlushThresholdSize().bytes(), 1024); + } + } + } public void testOpenCloseUpdateSettings() throws Exception { createIndex("test"); try {