add ability to reset index settings to it's default
This commit is contained in:
parent
79f4697f3e
commit
3567daa9c8
|
@ -52,11 +52,11 @@ final class SettingsUpdater {
|
||||||
boolean changed = false;
|
boolean changed = false;
|
||||||
Settings.Builder transientSettings = Settings.settingsBuilder();
|
Settings.Builder transientSettings = Settings.settingsBuilder();
|
||||||
transientSettings.put(currentState.metaData().transientSettings());
|
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();
|
Settings.Builder persistentSettings = Settings.settingsBuilder();
|
||||||
persistentSettings.put(currentState.metaData().persistentSettings());
|
persistentSettings.put(currentState.metaData().persistentSettings());
|
||||||
changed |= clusterSettings.applyDynamicSettings(persistentToApply, persistentSettings, persistentUpdates, "persistent");
|
changed |= clusterSettings.updateSettings(persistentToApply, persistentSettings, persistentUpdates, "persistent", false);
|
||||||
|
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
return currentState;
|
return currentState;
|
||||||
|
|
|
@ -42,6 +42,7 @@ import org.elasticsearch.common.settings.IndexScopeSettings;
|
||||||
import org.elasticsearch.common.settings.Setting;
|
import org.elasticsearch.common.settings.Setting;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.unit.TimeValue;
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
|
import org.elasticsearch.index.IndexNotFoundException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -256,13 +257,31 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!openIndices.isEmpty()) {
|
if (!openIndices.isEmpty()) {
|
||||||
String[] indices = openIndices.toArray(new String[openIndices.size()]);
|
for (String index : openIndices) {
|
||||||
metaDataBuilder.updateSettings(openSettings, indices);
|
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()) {
|
if (!closeIndices.isEmpty()) {
|
||||||
String[] indices = closeIndices.toArray(new String[closeIndices.size()]);
|
for (String index : closeIndices) {
|
||||||
metaDataBuilder.updateSettings(closedSettings, indices);
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -296,14 +296,23 @@ public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
return setting.get(this.lastSettingsApplied);
|
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;
|
boolean changed = false;
|
||||||
final Set<String> toRemove = new HashSet<>();
|
final Set<String> toRemove = new HashSet<>();
|
||||||
Settings.Builder settingsBuilder = Settings.settingsBuilder();
|
Settings.Builder settingsBuilder = Settings.settingsBuilder();
|
||||||
for (Map.Entry<String, String> entry : toApply.getAsMap().entrySet()) {
|
for (Map.Entry<String, String> entry : toApply.getAsMap().entrySet()) {
|
||||||
if (entry.getValue() == null) {
|
if (entry.getValue() == null) {
|
||||||
toRemove.add(entry.getKey());
|
toRemove.add(entry.getKey());
|
||||||
} else if (hasDynamicSetting(entry.getKey())) {
|
} else if ((all && get(entry.getKey()) != null) || hasDynamicSetting(entry.getKey())) {
|
||||||
validate(entry.getKey(), entry.getValue());
|
validate(entry.getKey(), entry.getValue());
|
||||||
settingsBuilder.put(entry.getKey(), entry.getValue());
|
settingsBuilder.put(entry.getKey(), entry.getValue());
|
||||||
updates.put(entry.getKey(), entry.getValue());
|
updates.put(entry.getKey(), entry.getValue());
|
||||||
|
|
|
@ -30,11 +30,13 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
|
||||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||||
import org.elasticsearch.common.Priority;
|
import org.elasticsearch.common.Priority;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
import org.elasticsearch.index.IndexService;
|
||||||
import org.elasticsearch.index.engine.VersionConflictEngineException;
|
import org.elasticsearch.index.engine.VersionConflictEngineException;
|
||||||
import org.elasticsearch.index.MergePolicyConfig;
|
import org.elasticsearch.index.MergePolicyConfig;
|
||||||
import org.elasticsearch.index.MergeSchedulerConfig;
|
import org.elasticsearch.index.MergeSchedulerConfig;
|
||||||
import org.elasticsearch.index.store.IndexStore;
|
import org.elasticsearch.index.store.IndexStore;
|
||||||
import org.elasticsearch.index.store.Store;
|
import org.elasticsearch.index.store.Store;
|
||||||
|
import org.elasticsearch.indices.IndicesService;
|
||||||
import org.elasticsearch.test.ESIntegTestCase;
|
import org.elasticsearch.test.ESIntegTestCase;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -50,6 +52,40 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
|
||||||
public class UpdateSettingsIT extends ESIntegTestCase {
|
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 {
|
public void testOpenCloseUpdateSettings() throws Exception {
|
||||||
createIndex("test");
|
createIndex("test");
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue