add ability to reset index settings to it's default

This commit is contained in:
Simon Willnauer 2016-01-16 15:22:29 +01:00
parent 79f4697f3e
commit 3567daa9c8
4 changed files with 72 additions and 8 deletions

View File

@ -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;

View File

@ -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));
}
}
}

View File

@ -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 <code>true</code> all settings are updated otherwise only dynamic settings are updated. if set to <code>false</code> and a non-dynamic setting is updated an exception is thrown
* @return <code>true</code> if the target has changed otherwise <code>false</code>
*/
public boolean updateSettings(Settings toApply, Settings.Builder target, Settings.Builder updates, String type, boolean all) {
boolean changed = false;
final Set<String> toRemove = new HashSet<>();
Settings.Builder settingsBuilder = Settings.settingsBuilder();
for (Map.Entry<String, String> 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());

View File

@ -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 {