-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Include fallback settings when checking dependencies #33522
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
Changes from all commits
f94c848
cafc63c
3fb9f12
eb9be45
7ae63df
45b63d3
4c07555
d469474
fe724e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,12 +366,25 @@ public T getDefault(Settings settings) { | |
} | ||
|
||
/** | ||
* Returns <code>true</code> iff this setting is present in the given settings object. Otherwise <code>false</code> | ||
* Returns true if and only if this setting is present in the given settings instance. Note that fallback settings are excluded. | ||
* | ||
* @param settings the settings | ||
* @return true if the setting is present in the given settings instance, otherwise false | ||
*/ | ||
public boolean exists(Settings settings) { | ||
public boolean exists(final Settings settings) { | ||
return settings.keySet().contains(getKey()); | ||
} | ||
|
||
/** | ||
* Returns true if and only if this setting including fallback settings is present in the given settings instance. | ||
* | ||
* @param settings the settings | ||
* @return true if the setting including fallback settings is present in the given settings instance, otherwise false | ||
*/ | ||
public boolean existsOrFallbackExists(final Settings settings) { | ||
return settings.keySet().contains(getKey()) || (fallbackSetting != null && fallbackSetting.existsOrFallbackExists(settings)); | ||
} | ||
|
||
/** | ||
* Returns the settings value. If the setting is not present in the given settings object the default value is returned | ||
* instead. | ||
|
@@ -511,7 +524,7 @@ public Setting<T> getConcreteSetting(String key) { | |
* Returns a set of settings that are required at validation time. Unless all of the dependencies are present in the settings | ||
* object validation of setting must fail. | ||
*/ | ||
public Set<String> getSettingsDependencies(String key) { | ||
public Set<Setting<?>> getSettingsDependencies(String key) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
|
@@ -634,12 +647,12 @@ private Stream<String> matchStream(Settings settings) { | |
return settings.keySet().stream().filter(this::match).map(key::getConcreteString); | ||
} | ||
|
||
public Set<String> getSettingsDependencies(String settingsKey) { | ||
public Set<Setting<?>> getSettingsDependencies(String settingsKey) { | ||
if (dependencies.isEmpty()) { | ||
return Collections.emptySet(); | ||
} else { | ||
String namespace = key.getNamespace(settingsKey); | ||
return dependencies.stream().map(s -> s.key.toConcreteKey(namespace).key).collect(Collectors.toSet()); | ||
return dependencies.stream().map(s -> (Setting<?>)s.getConcreteSettingForNamespace(namespace)).collect(Collectors.toSet()); | ||
} | ||
} | ||
|
||
|
@@ -914,40 +927,6 @@ public String toString() { | |
} | ||
} | ||
|
||
private static class ListSetting<T> extends Setting<List<T>> { | ||
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 moved this code so that it's closer to where |
||
private final Function<Settings, List<String>> defaultStringValue; | ||
|
||
private ListSetting(String key, Function<Settings, List<String>> defaultStringValue, Function<String, List<T>> parser, | ||
Property... properties) { | ||
super(new ListKey(key), (s) -> Setting.arrayToParsableString(defaultStringValue.apply(s)), parser, | ||
properties); | ||
this.defaultStringValue = defaultStringValue; | ||
} | ||
|
||
@Override | ||
String innerGetRaw(final Settings settings) { | ||
List<String> array = settings.getAsList(getKey(), null); | ||
return array == null ? defaultValue.apply(settings) : arrayToParsableString(array); | ||
} | ||
|
||
@Override | ||
boolean hasComplexMatcher() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void diff(Settings.Builder builder, Settings source, Settings defaultSettings) { | ||
if (exists(source) == false) { | ||
List<String> asList = defaultSettings.getAsList(getKey(), null); | ||
if (asList == null) { | ||
builder.putList(getKey(), defaultStringValue.apply(defaultSettings)); | ||
} else { | ||
builder.putList(getKey(), asList); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private final class Updater implements AbstractScopedSettings.SettingUpdater<T> { | ||
private final Consumer<T> consumer; | ||
private final Logger logger; | ||
|
@@ -1209,26 +1188,44 @@ public static Setting<ByteSizeValue> memorySizeSetting(String key, String defaul | |
return new Setting<>(key, (s) -> defaultPercentage, (s) -> MemorySizeValue.parseBytesSizeValueOrHeapRatio(s, key), properties); | ||
} | ||
|
||
public static <T> Setting<List<T>> listSetting(String key, List<String> defaultStringValue, Function<String, T> singleValueParser, | ||
Property... properties) { | ||
return listSetting(key, (s) -> defaultStringValue, singleValueParser, properties); | ||
public static <T> Setting<List<T>> listSetting( | ||
final String key, | ||
final List<String> defaultStringValue, | ||
final Function<String, T> singleValueParser, | ||
final Property... properties) { | ||
return listSetting(key, null, singleValueParser, (s) -> defaultStringValue, properties); | ||
} | ||
|
||
// TODO this one's two argument get is still broken | ||
public static <T> Setting<List<T>> listSetting(String key, Setting<List<T>> fallbackSetting, Function<String, T> singleValueParser, | ||
Property... properties) { | ||
return listSetting(key, (s) -> parseableStringToList(fallbackSetting.getRaw(s)), singleValueParser, properties); | ||
public static <T> Setting<List<T>> listSetting( | ||
final String key, | ||
final Setting<List<T>> fallbackSetting, | ||
final Function<String, T> singleValueParser, | ||
final Property... properties) { | ||
return listSetting(key, fallbackSetting, singleValueParser, (s) -> parseableStringToList(fallbackSetting.getRaw(s)), properties); | ||
} | ||
|
||
public static <T> Setting<List<T>> listSetting( | ||
final String key, | ||
final Function<String, T> singleValueParser, | ||
final Function<Settings, List<String>> defaultStringValue, | ||
final Property... properties) { | ||
return listSetting(key, null, singleValueParser, defaultStringValue, properties); | ||
} | ||
|
||
public static <T> Setting<List<T>> listSetting(String key, Function<Settings, List<String>> defaultStringValue, | ||
Function<String, T> singleValueParser, Property... properties) { | ||
public static <T> Setting<List<T>> listSetting( | ||
final String key, | ||
final @Nullable Setting<List<T>> fallbackSetting, | ||
final Function<String, T> singleValueParser, | ||
final Function<Settings, List<String>> defaultStringValue, | ||
final Property... properties) { | ||
if (defaultStringValue.apply(Settings.EMPTY) == null) { | ||
throw new IllegalArgumentException("default value function must not return null"); | ||
} | ||
Function<String, List<T>> parser = (s) -> | ||
parseableStringToList(s).stream().map(singleValueParser).collect(Collectors.toList()); | ||
|
||
return new ListSetting<>(key, defaultStringValue, parser, properties); | ||
return new ListSetting<>(key, fallbackSetting, defaultStringValue, parser, properties); | ||
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. We push the fallback setting down to the concrete setting. |
||
} | ||
|
||
private static List<String> parseableStringToList(String parsableString) { | ||
|
@@ -1266,6 +1263,51 @@ private static String arrayToParsableString(List<String> array) { | |
} | ||
} | ||
|
||
private static class ListSetting<T> extends Setting<List<T>> { | ||
|
||
private final Function<Settings, List<String>> defaultStringValue; | ||
|
||
private ListSetting( | ||
final String key, | ||
final @Nullable Setting<List<T>> fallbackSetting, | ||
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. Fallback settings were not being pushed down here, so |
||
final Function<Settings, List<String>> defaultStringValue, | ||
final Function<String, List<T>> parser, | ||
final Property... properties) { | ||
super( | ||
new ListKey(key), | ||
fallbackSetting, | ||
(s) -> Setting.arrayToParsableString(defaultStringValue.apply(s)), | ||
parser, | ||
(v,s) -> {}, | ||
properties); | ||
this.defaultStringValue = defaultStringValue; | ||
} | ||
|
||
@Override | ||
String innerGetRaw(final Settings settings) { | ||
List<String> array = settings.getAsList(getKey(), null); | ||
return array == null ? defaultValue.apply(settings) : arrayToParsableString(array); | ||
} | ||
|
||
@Override | ||
boolean hasComplexMatcher() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void diff(Settings.Builder builder, Settings source, Settings defaultSettings) { | ||
if (exists(source) == false) { | ||
List<String> asList = defaultSettings.getAsList(getKey(), null); | ||
if (asList == null) { | ||
builder.putList(getKey(), defaultStringValue.apply(defaultSettings)); | ||
} else { | ||
builder.putList(getKey(), asList); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
static void logSettingUpdate(Setting setting, Settings current, Settings previous, Logger logger) { | ||
if (logger.isInfoEnabled()) { | ||
if (setting.isFiltered()) { | ||
|
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.
This change is made so that we can validate dependencies exist using the actual settings object, and can therefore use fallback settings when doing the validation, which would not be possible if using the top-level dependent key only.