Create group settings with fallback. (#743) (#745)

* Create group settings with fallback.

Signed-off-by: dblock <dblock@amazon.com>

* Use protected fallbackSetting in Setting.

Signed-off-by: dblock <dblock@amazon.com>
This commit is contained in:
Daniel Doubrovkine (dB.) 2021-05-20 17:06:58 -04:00 committed by GitHub
parent edbe5ae7e8
commit a8c5e71eb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 2 deletions

View File

@ -158,7 +158,7 @@ public class Setting<T> implements ToXContentObject {
private final Key key; private final Key key;
protected final Function<Settings, String> defaultValue; protected final Function<Settings, String> defaultValue;
@Nullable @Nullable
private final Setting<T> fallbackSetting; protected final Setting<T> fallbackSetting;
private final Function<String, T> parser; private final Function<String, T> parser;
private final Validator<T> validator; private final Validator<T> validator;
private final EnumSet<Property> properties; private final EnumSet<Property> properties;
@ -1016,6 +1016,12 @@ public class Setting<T> implements ToXContentObject {
this.validator = validator; this.validator = validator;
} }
private GroupSetting(String key, Setting<Settings> fallback, Consumer<Settings> validator, Property... properties) {
super(new GroupKey(key), fallback, (s) -> null, properties);
this.key = key;
this.validator = validator;
}
@Override @Override
public boolean isGroupSetting() { public boolean isGroupSetting() {
return true; return true;
@ -1038,6 +1044,9 @@ public class Setting<T> implements ToXContentObject {
@Override @Override
public Settings get(Settings settings) { public Settings get(Settings settings) {
Settings byPrefix = settings.getByPrefix(getKey()); Settings byPrefix = settings.getByPrefix(getKey());
if (byPrefix.size() == 0 && this.fallbackSetting != null) {
byPrefix = fallbackSetting.get(settings);
}
validator.accept(byPrefix); validator.accept(byPrefix);
return byPrefix; return byPrefix;
} }
@ -1049,7 +1058,11 @@ public class Setting<T> implements ToXContentObject {
return true; return true;
} }
} }
return false; if (this.fallbackSetting != null) {
return fallbackSetting.exists(settings);
} else {
return false;
}
} }
@Override @Override
@ -1748,14 +1761,55 @@ public class Setting<T> implements ToXContentObject {
} }
} }
/**
* Creates a group of settings prefixed by a key.
*
* @param key the group key for the setting
* @param properties properties properties for this setting like scope, filtering...
* @return the group setting object
*/
public static Setting<Settings> groupSetting(String key, Property... properties) { public static Setting<Settings> groupSetting(String key, Property... properties) {
return groupSetting(key, (s) -> {}, properties); return groupSetting(key, (s) -> {}, properties);
} }
/**
* Creates a group of settings prefixed by a key.
*
* @param key the group key for the setting
* @param validator a {@link Validator} for validating this setting
* @param properties properties properties for this setting like scope, filtering...
* @return the group setting object
*/
public static Setting<Settings> groupSetting(String key, Consumer<Settings> validator, Property... properties) { public static Setting<Settings> groupSetting(String key, Consumer<Settings> validator, Property... properties) {
return new GroupSetting(key, validator, properties); return new GroupSetting(key, validator, properties);
} }
/**
* Creates a group of settings prefixed by a key.
*
* @param key the group key for the setting
* @param fallback a {@link GroupSetting} to use as fallback when no group key values exist
* @param properties properties properties for this setting like scope, filtering...
* @return the group setting object
*/
public static Setting<Settings> groupSetting(String key, final Setting<Settings> fallback, Property... properties) {
return groupSetting(key, fallback, (s) -> {}, properties);
}
/**
* Creates a group of settings prefixed by a key.
*
* @param key the group key for the setting
* @param fallback a {@link GroupSetting} to use as fallback when no group key values exist
* @param validator a {@link Validator} for validating this setting
* @param properties properties properties for this setting like scope, filtering...
* @return the group setting object
*/
public static Setting<Settings> groupSetting(String key, final Setting<Settings> fallback,
Consumer<Settings> validator, Property... properties) {
return new GroupSetting(key, fallback, validator, properties);
}
public static Setting<TimeValue> timeSetting( public static Setting<TimeValue> timeSetting(
final String key, final String key,
final Setting<TimeValue> fallbackSetting, final Setting<TimeValue> fallbackSetting,

View File

@ -514,6 +514,26 @@ public class SettingTests extends OpenSearchTestCase {
} }
} }
public void testGroupSettingFallback() {
Setting<Settings> fallbackSetting = Setting.groupSetting("old.bar.", Property.Dynamic, Property.NodeScope);
assertFalse(fallbackSetting.exists(Settings.EMPTY));
Setting<Settings> groupSetting = Setting.groupSetting("new.bar.", fallbackSetting, Property.Dynamic, Property.NodeScope);
assertFalse(groupSetting.exists(Settings.EMPTY));
Settings values = Settings.builder()
.put("old.bar.1.value", "value 1")
.put("old.bar.2.value", "value 2")
.build();
assertTrue(groupSetting.exists(values));
Map<String, Settings> asMap = groupSetting.get(values).getAsGroups();
assertEquals(2, asMap.size());
assertEquals("value 1", asMap.get("1").get("value"));
assertEquals("value 2", asMap.get("2").get("value"));
}
public void testFilteredGroups() { public void testFilteredGroups() {
AtomicReference<Settings> ref = new AtomicReference<>(null); AtomicReference<Settings> ref = new AtomicReference<>(null);
Setting<Settings> setting = Setting.groupSetting("foo.bar.", Property.Filtered, Property.Dynamic); Setting<Settings> setting = Setting.groupSetting("foo.bar.", Property.Filtered, Property.Dynamic);