-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[WIP] ES-11463 Components are notified to updates to per-project settings #131637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexey-ivanov-es
wants to merge
6
commits into
elastic:main
Choose a base branch
from
alexey-ivanov-es:ES-11463
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+841
−331
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
959d7e8
ES-11463 Components are notified to updates to per-project settings
alexey-ivanov-es 15bd092
[CI] Auto commit changes from spotless
876612e
Fix build
alexey-ivanov-es 88b707d
Fix build
alexey-ivanov-es db5dbb2
Merge branch 'main' into ES-11463
alexey-ivanov-es b5143e3
Javadocs
alexey-ivanov-es File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
...er/src/main/java/org/elasticsearch/common/settings/AbstractContextlessScopedSettings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.settings; | ||
|
|
||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.common.TriConsumer; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.function.Consumer; | ||
|
|
||
| public abstract class AbstractContextlessScopedSettings extends AbstractScopedSettings<Void> { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this name, but didn't manage to find anything better
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add Javadoc then? Since the name isn't especially informative. |
||
|
|
||
| protected final Settings settings; | ||
| private Settings lastSettingsApplied; | ||
|
|
||
| public AbstractContextlessScopedSettings( | ||
| Settings nodeSettings, | ||
| Settings scopeSettings, | ||
| AbstractContextlessScopedSettings other, | ||
| Logger logger | ||
| ) { | ||
| super(other, logger); | ||
|
|
||
| this.settings = nodeSettings; | ||
| this.lastSettingsApplied = scopeSettings; | ||
| } | ||
|
|
||
| public AbstractContextlessScopedSettings(Settings settings, Set<Setting<?>> settingsSet, Setting.Property scope) { | ||
| super(settingsSet, scope); | ||
|
|
||
| this.settings = settings; | ||
| this.lastSettingsApplied = Settings.EMPTY; | ||
| } | ||
|
|
||
| /** | ||
| * Validates the given settings by running it through all update listeners without applying it. This | ||
| * method will not change any settings but will fail if any of the settings can't be applied. | ||
| */ | ||
| public synchronized Settings validateUpdate(Settings settings) { | ||
| final Settings current = Settings.builder().put(this.settings).put(settings).build(); | ||
| final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build(); | ||
| validateUpdate(current, previous); | ||
|
|
||
| return current; | ||
| } | ||
|
|
||
| /** | ||
| * Applies the given settings to all the settings consumers or to none of them. The settings | ||
| * will be merged with the node settings before they are applied while given settings override existing node | ||
| * settings. | ||
| * @param newSettings the settings to apply | ||
| * @return the unmerged applied settings | ||
| */ | ||
| public synchronized Settings applySettings(Settings newSettings) { | ||
| if (lastSettingsApplied != null && newSettings.equals(lastSettingsApplied)) { | ||
| // nothing changed in the settings, ignore | ||
| return newSettings; | ||
| } | ||
| final Settings current = Settings.builder().put(this.settings).put(newSettings).build(); | ||
| final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build(); | ||
| executeSettingsUpdaters(null, current, previous); | ||
|
|
||
| return lastSettingsApplied = newSettings; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value for the given setting. | ||
| */ | ||
| public <T> T get(Setting<T> setting) { | ||
| if (setting.getProperties().contains(scope) == false) { | ||
| throw new IllegalArgumentException( | ||
| "settings scope doesn't match the setting scope [" + this.scope + "] not in [" + setting.getProperties() + "]" | ||
| ); | ||
| } | ||
| if (get(setting.getKey()) == null) { | ||
| throw new IllegalArgumentException("setting " + setting.getKey() + " has not been registered"); | ||
| } | ||
| return setting.get(this.lastSettingsApplied, settings); | ||
| } | ||
|
|
||
| private static <T, V> TriConsumer<Void, T, V> wrapIgnoringContext(BiConsumer<T, V> consumer) { | ||
| return (ctx, t, v) -> consumer.accept(t, v); | ||
| } | ||
|
|
||
| private static <V> BiConsumer<Void, V> wrapIgnoringContext(Consumer<V> consumer) { | ||
| return (ctx, v) -> consumer.accept(v); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a settings consumer with a predicate that is only evaluated at update time. | ||
| * <p> | ||
| * Note: Only settings registered in {@link SettingsModule} can be changed dynamically. | ||
| * </p> | ||
| * @param <T> The type of the setting's value. | ||
| * @param setting The setting for which the updates are to be handled. | ||
| * @param consumer A {@link BiConsumer} that will be executed with the updated setting value. | ||
| * @param validator an additional validator that is only applied to updates of this setting. | ||
| * This is useful to add additional validation to settings at runtime compared to at startup time. | ||
| */ | ||
| public synchronized <T> void addSettingsUpdateConsumer(Setting<T> setting, Consumer<T> consumer, Consumer<T> validator) { | ||
| super.addSettingsUpdateConsumer(setting, wrapIgnoringContext(consumer), validator); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a settings consumer. | ||
| * <p> | ||
| * Note: Only settings registered in {@link org.elasticsearch.cluster.ClusterModule} can be changed dynamically. | ||
| * </p> | ||
| */ | ||
| public synchronized <T> void addSettingsUpdateConsumer(Setting<T> setting, Consumer<T> consumer) { | ||
| super.addSettingsUpdateConsumer(setting, wrapIgnoringContext(consumer)); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a settings consumer that is only executed if any setting in the supplied list of settings is changed. In that case all the | ||
| * settings are specified in the argument are returned. | ||
| * | ||
| * Also automatically adds empty consumers for all settings in order to activate logging | ||
| */ | ||
| public synchronized void addSettingsUpdateConsumer(Consumer<Settings> consumer, List<? extends Setting<?>> settings) { | ||
| super.addSettingsUpdateConsumer(wrapIgnoringContext(consumer), settings); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a settings consumer that is only executed if any setting in the supplied list of settings is changed. In that case all the | ||
| * settings are specified in the argument are returned. The validator is run across all specified settings before the settings are | ||
| * applied. | ||
| * | ||
| * Also automatically adds empty consumers for all settings in order to activate logging | ||
| */ | ||
| public synchronized void addSettingsUpdateConsumer( | ||
| Consumer<Settings> consumer, | ||
| List<? extends Setting<?>> settings, | ||
| Consumer<Settings> validator | ||
| ) { | ||
| super.addSettingsUpdateConsumer(wrapIgnoringContext(consumer), settings, validator); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a settings consumer for affix settings. Affix settings have a namespace associated to it that needs to be available to the | ||
| * consumer in order to be processed correctly. | ||
| */ | ||
| public synchronized <T> void addAffixUpdateConsumer( | ||
| Setting.AffixSetting<T> setting, | ||
| BiConsumer<String, T> consumer, | ||
| BiConsumer<String, T> validator | ||
| ) { | ||
| super.addAffixUpdateConsumer(setting, wrapIgnoringContext(consumer), validator); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a affix settings consumer that accepts the settings for a group of settings. The consumer is only | ||
| * notified if at least one of the settings change. | ||
| * <p> | ||
| * Note: Only settings registered in {@link SettingsModule} can be changed dynamically. | ||
| * </p> | ||
| */ | ||
| public synchronized void addAffixGroupUpdateConsumer(List<Setting.AffixSetting<?>> settings, BiConsumer<String, Settings> consumer) { | ||
| super.addAffixGroupUpdateConsumer(settings, wrapIgnoringContext(consumer)); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a settings consumer for affix settings. Affix settings have a namespace associated to it that needs to be available to the | ||
| * consumer in order to be processed correctly. This consumer will get a namespace to value map instead of each individual namespace | ||
| * and value as in {@link #addAffixUpdateConsumer(Setting.AffixSetting, BiConsumer, BiConsumer)} | ||
| */ | ||
| public synchronized <T> void addAffixMapUpdateConsumer( | ||
| Setting.AffixSetting<T> setting, | ||
| Consumer<Map<String, T>> consumer, | ||
| BiConsumer<String, T> validator | ||
| ) { | ||
| super.addAffixMapUpdateConsumer(setting, wrapIgnoringContext(consumer), validator); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a settings consumer that accepts the values for two settings. The consumer is only notified if one or both settings change | ||
| * and if the provided validator succeeded. | ||
| * <p> | ||
| * Note: Only settings registered in {@link SettingsModule} can be changed dynamically. | ||
| * </p> | ||
| * This method registers a compound updater that is useful if two settings are depending on each other. | ||
| * The consumer is always provided with both values even if only one of the two changes. | ||
| */ | ||
| public synchronized <A, B> void addSettingsUpdateConsumer( | ||
| Setting<A> a, | ||
| Setting<B> b, | ||
| BiConsumer<A, B> consumer, | ||
| BiConsumer<A, B> validator | ||
| ) { | ||
| super.addSettingsUpdateConsumer(a, b, wrapIgnoringContext(consumer), validator); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a settings consumer that accepts the values for two settings. | ||
| * See {@link #addSettingsUpdateConsumer(Setting, Setting, BiConsumer, BiConsumer)} for details. | ||
| */ | ||
| public synchronized <A, B> void addSettingsUpdateConsumer(Setting<A> a, Setting<B> b, BiConsumer<A, B> consumer) { | ||
| super.addSettingsUpdateConsumer(a, b, wrapIgnoringContext(consumer)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have project id here?