Search in sources :

Example 1 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class MetaDataUpdateSettingsService method updateSettings.

public void updateSettings(final UpdateSettingsClusterStateUpdateRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
    final Settings normalizedSettings = Settings.builder().put(request.settings()).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX).build();
    Settings.Builder settingsForClosedIndices = Settings.builder();
    Settings.Builder settingsForOpenIndices = Settings.builder();
    Settings.Builder skipppedSettings = Settings.builder();
    indexScopedSettings.validate(normalizedSettings);
    // never allow to change the number of shards
    for (Map.Entry<String, String> entry : normalizedSettings.getAsMap().entrySet()) {
        if (entry.getKey().equals(IndexMetaData.SETTING_NUMBER_OF_SHARDS)) {
            listener.onFailure(new IllegalArgumentException("can't change the number of shards for an index"));
            return;
        }
        Setting setting = indexScopedSettings.get(entry.getKey());
        // we already validated the normalized settings
        assert setting != null;
        settingsForClosedIndices.put(entry.getKey(), entry.getValue());
        if (setting.isDynamic()) {
            settingsForOpenIndices.put(entry.getKey(), entry.getValue());
        } else {
            skipppedSettings.put(entry.getKey(), entry.getValue());
        }
    }
    final Settings skippedSettigns = skipppedSettings.build();
    final Settings closedSettings = settingsForClosedIndices.build();
    final Settings openSettings = settingsForOpenIndices.build();
    final boolean preserveExisting = request.isPreserveExisting();
    clusterService.submitStateUpdateTask("update-settings", new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.URGENT, request, wrapPreservingContext(listener)) {

        @Override
        protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
            return new ClusterStateUpdateResponse(acknowledged);
        }

        @Override
        public ClusterState execute(ClusterState currentState) {
            RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable());
            MetaData.Builder metaDataBuilder = MetaData.builder(currentState.metaData());
            // allow to change any settings to a close index, and only allow dynamic settings to be changed
            // on an open index
            Set<Index> openIndices = new HashSet<>();
            Set<Index> closeIndices = new HashSet<>();
            final String[] actualIndices = new String[request.indices().length];
            for (int i = 0; i < request.indices().length; i++) {
                Index index = request.indices()[i];
                actualIndices[i] = index.getName();
                final IndexMetaData metaData = currentState.metaData().getIndexSafe(index);
                if (metaData.getState() == IndexMetaData.State.OPEN) {
                    openIndices.add(index);
                } else {
                    closeIndices.add(index);
                }
            }
            if (!skippedSettigns.isEmpty() && !openIndices.isEmpty()) {
                throw new IllegalArgumentException(String.format(Locale.ROOT, "Can't update non dynamic settings [%s] for open indices %s", skippedSettigns.getAsMap().keySet(), openIndices));
            }
            int updatedNumberOfReplicas = openSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, -1);
            if (updatedNumberOfReplicas != -1 && preserveExisting == false) {
                // we do *not* update the in sync allocation ids as they will be removed upon the first index
                // operation which make these copies stale
                // TODO: update the list once the data is deleted by the node?
                routingTableBuilder.updateNumberOfReplicas(updatedNumberOfReplicas, actualIndices);
                metaDataBuilder.updateNumberOfReplicas(updatedNumberOfReplicas, actualIndices);
                logger.info("updating number_of_replicas to [{}] for indices {}", updatedNumberOfReplicas, actualIndices);
            }
            ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetaData.INDEX_READ_ONLY_BLOCK, IndexMetaData.INDEX_READ_ONLY_SETTING, openSettings);
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetaData.INDEX_METADATA_BLOCK, IndexMetaData.INDEX_BLOCKS_METADATA_SETTING, openSettings);
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetaData.INDEX_WRITE_BLOCK, IndexMetaData.INDEX_BLOCKS_WRITE_SETTING, openSettings);
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetaData.INDEX_READ_BLOCK, IndexMetaData.INDEX_BLOCKS_READ_SETTING, openSettings);
            if (!openIndices.isEmpty()) {
                for (Index index : openIndices) {
                    IndexMetaData indexMetaData = metaDataBuilder.getSafe(index);
                    Settings.Builder updates = Settings.builder();
                    Settings.Builder indexSettings = Settings.builder().put(indexMetaData.getSettings());
                    if (indexScopedSettings.updateDynamicSettings(openSettings, indexSettings, updates, index.getName())) {
                        if (preserveExisting) {
                            indexSettings.put(indexMetaData.getSettings());
                        }
                        metaDataBuilder.put(IndexMetaData.builder(indexMetaData).settings(indexSettings));
                    }
                }
            }
            if (!closeIndices.isEmpty()) {
                for (Index index : closeIndices) {
                    IndexMetaData indexMetaData = metaDataBuilder.getSafe(index);
                    Settings.Builder updates = Settings.builder();
                    Settings.Builder indexSettings = Settings.builder().put(indexMetaData.getSettings());
                    if (indexScopedSettings.updateSettings(closedSettings, indexSettings, updates, index.getName())) {
                        if (preserveExisting) {
                            indexSettings.put(indexMetaData.getSettings());
                        }
                        metaDataBuilder.put(IndexMetaData.builder(indexMetaData).settings(indexSettings));
                    }
                }
            }
            ClusterState updatedState = ClusterState.builder(currentState).metaData(metaDataBuilder).routingTable(routingTableBuilder.build()).blocks(blocks).build();
            // now, reroute in case things change that require it (like number of replicas)
            updatedState = allocationService.reroute(updatedState, "settings update");
            try {
                for (Index index : openIndices) {
                    final IndexMetaData currentMetaData = currentState.getMetaData().getIndexSafe(index);
                    final IndexMetaData updatedMetaData = updatedState.metaData().getIndexSafe(index);
                    indicesService.verifyIndexMetadata(currentMetaData, updatedMetaData);
                }
                for (Index index : closeIndices) {
                    final IndexMetaData currentMetaData = currentState.getMetaData().getIndexSafe(index);
                    final IndexMetaData updatedMetaData = updatedState.metaData().getIndexSafe(index);
                    indicesService.verifyIndexMetadata(currentMetaData, updatedMetaData);
                }
            } catch (IOException ex) {
                throw ExceptionsHelper.convertToElastic(ex);
            }
            return updatedState;
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) HashSet(java.util.HashSet) Set(java.util.Set) Setting(org.elasticsearch.common.settings.Setting) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings)

Example 2 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class IndexSettingsTests method testMergedSettingsArePassed.

public void testMergedSettingsArePassed() {
    Version version = VersionUtils.getPreviousVersion();
    Settings theSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).put(IndexMetaData.SETTING_INDEX_UUID, "0xdeadbeef").build();
    final AtomicInteger integer = new AtomicInteger(0);
    final StringBuilder builder = new StringBuilder();
    Setting<Integer> integerSetting = Setting.intSetting("index.test.setting.int", -1, Property.Dynamic, Property.IndexScope);
    Setting<String> notUpdated = new Setting<>("index.not.updated", "", Function.identity(), Property.Dynamic, Property.IndexScope);
    IndexSettings settings = newIndexSettings(newIndexMeta("index", theSettings), Settings.EMPTY, integerSetting, notUpdated);
    settings.getScopedSettings().addSettingsUpdateConsumer(integerSetting, integer::set);
    settings.getScopedSettings().addSettingsUpdateConsumer(notUpdated, builder::append);
    assertEquals(0, integer.get());
    assertEquals("", builder.toString());
    IndexMetaData newMetaData = newIndexMeta("index", Settings.builder().put(settings.getIndexMetaData().getSettings()).put("index.test.setting.int", 42).build());
    assertTrue(settings.updateIndexMetaData(newMetaData));
    assertSame(settings.getIndexMetaData(), newMetaData);
    assertEquals(42, integer.get());
    assertEquals("", builder.toString());
    integer.set(0);
    assertTrue(settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(settings.getIndexMetaData().getSettings()).put("index.not.updated", "boom").build())));
    assertEquals("boom", builder.toString());
    assertEquals("not updated - we preserve the old settings", 0, integer.get());
}
Also used : Setting(org.elasticsearch.common.settings.Setting) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Version(org.elasticsearch.Version) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 3 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class IndexSettingsTests method testArchiveBrokenIndexSettings.

public void testArchiveBrokenIndexSettings() {
    Settings settings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS.archiveUnknownOrInvalidSettings(Settings.EMPTY, e -> {
        assert false : "should not have been invoked, no unknown settings";
    }, (e, ex) -> {
        assert false : "should not have been invoked, no invalid settings";
    });
    assertSame(settings, Settings.EMPTY);
    settings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS.archiveUnknownOrInvalidSettings(Settings.builder().put("index.refresh_interval", "-200").build(), e -> {
        assert false : "should not have been invoked, no invalid settings";
    }, (e, ex) -> {
        assertThat(e.getKey(), equalTo("index.refresh_interval"));
        assertThat(e.getValue(), equalTo("-200"));
        assertThat(ex, hasToString(containsString("failed to parse setting [index.refresh_interval] with value [-200]")));
    });
    assertEquals("-200", settings.get("archived.index.refresh_interval"));
    assertNull(settings.get("index.refresh_interval"));
    // no double archive
    Settings prevSettings = settings;
    settings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS.archiveUnknownOrInvalidSettings(prevSettings, e -> {
        assert false : "should not have been invoked, no unknown settings";
    }, (e, ex) -> {
        assert false : "should not have been invoked, no invalid settings";
    });
    assertSame(prevSettings, settings);
    settings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS.archiveUnknownOrInvalidSettings(Settings.builder().put("index.version.created", // private setting
    Version.CURRENT.id).put("index.unknown", "foo").put("index.refresh_interval", "2s").build(), e -> {
        assertThat(e.getKey(), equalTo("index.unknown"));
        assertThat(e.getValue(), equalTo("foo"));
    }, (e, ex) -> {
        assert false : "should not have been invoked, no invalid settings";
    });
    assertEquals("foo", settings.get("archived.index.unknown"));
    assertEquals(Integer.toString(Version.CURRENT.id), settings.get("index.version.created"));
    assertEquals("2s", settings.get("index.refresh_interval"));
}
Also used : ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) Arrays(java.util.Arrays) Setting(org.elasticsearch.common.settings.Setting) Property(org.elasticsearch.common.settings.Setting.Property) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Set(java.util.Set) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) Function(java.util.function.Function) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) VersionUtils(org.elasticsearch.test.VersionUtils) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) TimeValue(org.elasticsearch.common.unit.TimeValue) Translog(org.elasticsearch.index.translog.Translog) ESTestCase(org.elasticsearch.test.ESTestCase) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 4 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class IndexSettingsModule method newIndexSettings.

public static IndexSettings newIndexSettings(Index index, Settings indexSetting, Settings nodeSettings, Setting<?>... setting) {
    Settings build = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(indexSetting).build();
    IndexMetaData metaData = IndexMetaData.builder(index.getName()).settings(build).build();
    Set<Setting<?>> settingSet = new HashSet<>(IndexScopedSettings.BUILT_IN_INDEX_SETTINGS);
    if (setting.length > 0) {
        settingSet.addAll(Arrays.asList(setting));
    }
    return new IndexSettings(metaData, nodeSettings, new IndexScopedSettings(Settings.EMPTY, settingSet));
}
Also used : IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) Setting(org.elasticsearch.common.settings.Setting) IndexSettings(org.elasticsearch.index.IndexSettings) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) HashSet(java.util.HashSet)

Example 5 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class ScriptSettingsTests method testSettingsAreProperlyPropogated.

public void testSettingsAreProperlyPropogated() {
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new CustomScriptEngineService()));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    boolean enabled = randomBoolean();
    Settings s = Settings.builder().put("script.inline", enabled).build();
    for (Iterator<Setting<Boolean>> iter = scriptSettings.getScriptLanguageSettings().iterator(); iter.hasNext(); ) {
        Setting<Boolean> setting = iter.next();
        if (setting.getKey().endsWith(".inline")) {
            assertThat("inline settings should have propagated", setting.get(s), equalTo(enabled));
            assertThat(setting.getDefaultRaw(s), equalTo(Boolean.toString(enabled)));
        }
    }
}
Also used : Setting(org.elasticsearch.common.settings.Setting) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

Setting (org.elasticsearch.common.settings.Setting)15 Settings (org.elasticsearch.common.settings.Settings)13 IndexScopedSettings (org.elasticsearch.common.settings.IndexScopedSettings)9 HashSet (java.util.HashSet)8 Set (java.util.Set)5 IOException (java.io.IOException)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Version (org.elasticsearch.Version)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)4 StringContains.containsString (org.hamcrest.core.StringContains.containsString)4 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 ActionListener (org.elasticsearch.action.ActionListener)3 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)3