|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.common.settings; |
| 11 | + |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.elasticsearch.common.TriConsumer; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.function.BiConsumer; |
| 19 | +import java.util.function.Consumer; |
| 20 | + |
| 21 | +public abstract class AbstractContextlessScopedSettings extends AbstractScopedSettings<Void> { |
| 22 | + |
| 23 | + protected final Settings settings; |
| 24 | + private Settings lastSettingsApplied; |
| 25 | + |
| 26 | + public AbstractContextlessScopedSettings(Settings nodeSettings, Settings scopeSettings, AbstractContextlessScopedSettings other, Logger logger) { |
| 27 | + super(other, logger); |
| 28 | + |
| 29 | + this.settings = nodeSettings; |
| 30 | + this.lastSettingsApplied = scopeSettings; |
| 31 | + } |
| 32 | + |
| 33 | + public AbstractContextlessScopedSettings(Settings settings, Set<Setting<?>> settingsSet, Setting.Property scope) { |
| 34 | + super(settingsSet, scope); |
| 35 | + |
| 36 | + this.settings = settings; |
| 37 | + this.lastSettingsApplied = Settings.EMPTY; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Validates the given settings by running it through all update listeners without applying it. This |
| 42 | + * method will not change any settings but will fail if any of the settings can't be applied. |
| 43 | + */ |
| 44 | + public synchronized Settings validateUpdate(Settings settings) { |
| 45 | + final Settings current = Settings.builder().put(this.settings).put(settings).build(); |
| 46 | + final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build(); |
| 47 | + validateUpdate(current, previous); |
| 48 | + |
| 49 | + return current; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Applies the given settings to all the settings consumers or to none of them. The settings |
| 54 | + * will be merged with the node settings before they are applied while given settings override existing node |
| 55 | + * settings. |
| 56 | + * @param newSettings the settings to apply |
| 57 | + * @return the unmerged applied settings |
| 58 | + */ |
| 59 | + public synchronized Settings applySettings(Settings newSettings) { |
| 60 | + if (lastSettingsApplied != null && newSettings.equals(lastSettingsApplied)) { |
| 61 | + // nothing changed in the settings, ignore |
| 62 | + return newSettings; |
| 63 | + } |
| 64 | + final Settings current = Settings.builder().put(this.settings).put(newSettings).build(); |
| 65 | + final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build(); |
| 66 | + executeSettingsUpdaters(null, current, previous); |
| 67 | + |
| 68 | + return lastSettingsApplied = newSettings; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the value for the given setting. |
| 73 | + */ |
| 74 | + public <T> T get(Setting<T> setting) { |
| 75 | + if (setting.getProperties().contains(scope) == false) { |
| 76 | + throw new IllegalArgumentException( |
| 77 | + "settings scope doesn't match the setting scope [" + this.scope + "] not in [" + setting.getProperties() + "]" |
| 78 | + ); |
| 79 | + } |
| 80 | + if (get(setting.getKey()) == null) { |
| 81 | + throw new IllegalArgumentException("setting " + setting.getKey() + " has not been registered"); |
| 82 | + } |
| 83 | + return setting.get(this.lastSettingsApplied, settings); |
| 84 | + } |
| 85 | + |
| 86 | + private static <T, V> TriConsumer<Void, T, V> wrapIgnoringContext(BiConsumer<T, V> consumer) { |
| 87 | + return (ctx, t, v) -> consumer.accept(t, v); |
| 88 | + } |
| 89 | + |
| 90 | + private static <V> BiConsumer<Void, V> wrapIgnoringContext(Consumer<V> consumer) { |
| 91 | + return (ctx, v) -> consumer.accept(v); |
| 92 | + } |
| 93 | + |
| 94 | + public synchronized <T> void addSettingsUpdateConsumer(Setting<T> setting, Consumer<T> consumer, Consumer<T> validator) { |
| 95 | + super.addSettingsUpdateConsumer(setting, wrapIgnoringContext(consumer), validator); |
| 96 | + } |
| 97 | + |
| 98 | + public synchronized <T> void addSettingsUpdateConsumer(Setting<T> setting, Consumer<T> consumer) { |
| 99 | + super.addSettingsUpdateConsumer(setting, wrapIgnoringContext(consumer)); |
| 100 | + } |
| 101 | + |
| 102 | + public synchronized void addSettingsUpdateConsumer(Consumer<Settings> consumer, List<? extends Setting<?>> settings) { |
| 103 | + super.addSettingsUpdateConsumer(wrapIgnoringContext(consumer), settings); |
| 104 | + } |
| 105 | + |
| 106 | + public synchronized void addSettingsUpdateConsumer( |
| 107 | + Consumer<Settings> consumer, |
| 108 | + List<? extends Setting<?>> settings, |
| 109 | + Consumer<Settings> validator |
| 110 | + ) { |
| 111 | + super.addSettingsUpdateConsumer(wrapIgnoringContext(consumer), settings, validator); |
| 112 | + } |
| 113 | + |
| 114 | + public synchronized <T> void addAffixUpdateConsumer( |
| 115 | + Setting.AffixSetting<T> setting, |
| 116 | + BiConsumer<String, T> consumer, |
| 117 | + BiConsumer<String, T> validator |
| 118 | + ) { |
| 119 | + super.addAffixUpdateConsumer(setting, wrapIgnoringContext(consumer), validator); |
| 120 | + } |
| 121 | + |
| 122 | + public synchronized void addAffixGroupUpdateConsumer(List<Setting.AffixSetting<?>> settings, BiConsumer<String, Settings> consumer) { |
| 123 | + super.addAffixGroupUpdateConsumer(settings, wrapIgnoringContext(consumer)); |
| 124 | + } |
| 125 | + |
| 126 | + public synchronized <T> void addAffixMapUpdateConsumer( |
| 127 | + Setting.AffixSetting<T> setting, |
| 128 | + Consumer<Map<String, T>> consumer, |
| 129 | + BiConsumer<String, T> validator |
| 130 | + ) { |
| 131 | + super.addAffixMapUpdateConsumer(setting, wrapIgnoringContext(consumer), validator); |
| 132 | + } |
| 133 | + |
| 134 | + public synchronized <A, B> void addSettingsUpdateConsumer( |
| 135 | + Setting<A> a, |
| 136 | + Setting<B> b, |
| 137 | + BiConsumer<A, B> consumer, |
| 138 | + BiConsumer<A, B> validator |
| 139 | + ) { |
| 140 | + super.addSettingsUpdateConsumer(a, b, wrapIgnoringContext(consumer), validator); |
| 141 | + } |
| 142 | + |
| 143 | + public synchronized <A, B> void addSettingsUpdateConsumer( |
| 144 | + Setting<A> a, |
| 145 | + Setting<B> b, |
| 146 | + BiConsumer<A, B> consumer |
| 147 | + ) { |
| 148 | + super.addSettingsUpdateConsumer(a, b, wrapIgnoringContext(consumer)); |
| 149 | + } |
| 150 | +} |
0 commit comments