diff --git a/server/src/main/java/org/opensearch/common/settings/Setting.java b/server/src/main/java/org/opensearch/common/settings/Setting.java index ef10f9a42c5..668f4c2ee7f 100644 --- a/server/src/main/java/org/opensearch/common/settings/Setting.java +++ b/server/src/main/java/org/opensearch/common/settings/Setting.java @@ -158,7 +158,7 @@ public class Setting implements ToXContentObject { private final Key key; protected final Function defaultValue; @Nullable - private final Setting fallbackSetting; + protected final Setting fallbackSetting; private final Function parser; private final Validator validator; private final EnumSet properties; @@ -1016,6 +1016,12 @@ public class Setting implements ToXContentObject { this.validator = validator; } + private GroupSetting(String key, Setting fallback, Consumer validator, Property... properties) { + super(new GroupKey(key), fallback, (s) -> null, properties); + this.key = key; + this.validator = validator; + } + @Override public boolean isGroupSetting() { return true; @@ -1038,6 +1044,9 @@ public class Setting implements ToXContentObject { @Override public Settings get(Settings settings) { Settings byPrefix = settings.getByPrefix(getKey()); + if (byPrefix.size() == 0 && this.fallbackSetting != null) { + byPrefix = fallbackSetting.get(settings); + } validator.accept(byPrefix); return byPrefix; } @@ -1049,7 +1058,11 @@ public class Setting implements ToXContentObject { return true; } } - return false; + if (this.fallbackSetting != null) { + return fallbackSetting.exists(settings); + } else { + return false; + } } @Override @@ -1748,14 +1761,55 @@ public class Setting 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 groupSetting(String key, Property... 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 groupSetting(String key, Consumer validator, Property... 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 groupSetting(String key, final Setting 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 groupSetting(String key, final Setting fallback, + Consumer validator, Property... properties) { + return new GroupSetting(key, fallback, validator, properties); + } + public static Setting timeSetting( final String key, final Setting fallbackSetting, diff --git a/server/src/test/java/org/opensearch/common/settings/SettingTests.java b/server/src/test/java/org/opensearch/common/settings/SettingTests.java index a2dc6a51621..fd307e910dc 100644 --- a/server/src/test/java/org/opensearch/common/settings/SettingTests.java +++ b/server/src/test/java/org/opensearch/common/settings/SettingTests.java @@ -514,6 +514,26 @@ public class SettingTests extends OpenSearchTestCase { } } + public void testGroupSettingFallback() { + Setting fallbackSetting = Setting.groupSetting("old.bar.", Property.Dynamic, Property.NodeScope); + assertFalse(fallbackSetting.exists(Settings.EMPTY)); + + Setting 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 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() { AtomicReference ref = new AtomicReference<>(null); Setting setting = Setting.groupSetting("foo.bar.", Property.Filtered, Property.Dynamic);