Skip to content

Commit dfaf3de

Browse files
authored
Allow float settings to be configured with other settings as default (#126751)
Relates ES-11367
1 parent 718315c commit dfaf3de

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

docs/changelog/126751.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 126751
2+
summary: Allow float settings to be configured with other settings as default
3+
area: Infra/Settings
4+
type: enhancement
5+
issues: []

server/src/main/java/org/elasticsearch/common/settings/Setting.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,8 +1367,16 @@ public static Setting<Float> floatSetting(String key, float defaultValue, Proper
13671367
}
13681368

13691369
public static Setting<Float> floatSetting(String key, float defaultValue, float minValue, Property... properties) {
1370+
return new Setting<>(key, Float.toString(defaultValue), floatParser(key, minValue, properties), properties);
1371+
}
1372+
1373+
public static Setting<Float> floatSetting(String key, Setting<Float> fallbackSetting, float minValue, Property... properties) {
1374+
return new Setting<>(key, fallbackSetting, floatParser(key, minValue, properties), properties);
1375+
}
1376+
1377+
private static Function<String, Float> floatParser(String key, float minValue, Property... properties) {
13701378
final boolean isFiltered = isFiltered(properties);
1371-
return new Setting<>(key, Float.toString(defaultValue), (s) -> {
1379+
return (s) -> {
13721380
float value = Float.parseFloat(s);
13731381
if (value < minValue) {
13741382
String err = "Failed to parse value"
@@ -1380,7 +1388,7 @@ public static Setting<Float> floatSetting(String key, float defaultValue, float
13801388
throw new IllegalArgumentException(err);
13811389
}
13821390
return value;
1383-
}, properties);
1391+
};
13841392
}
13851393

13861394
private static boolean isFiltered(Property[] properties) {

server/src/test/java/org/elasticsearch/common/settings/SettingTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,34 @@ public void testFilteredBooleanSetting() {
387387
assertNull(e.getCause());
388388
}
389389

390+
public void testFloatSettingWithOtherSettingAsDefault() {
391+
float defaultFallbackValue = randomFloat();
392+
Setting<Float> fallbackSetting = Setting.floatSetting("fallback_setting", defaultFallbackValue);
393+
Setting<Float> floatSetting = Setting.floatSetting("float_setting", fallbackSetting, Float.MIN_VALUE);
394+
395+
// Neither float_setting nor fallback_setting specified
396+
assertThat(floatSetting.get(Settings.builder().build()), equalTo(defaultFallbackValue));
397+
398+
// Only fallback_setting specified
399+
float explicitFallbackValue = randomValueOtherThan(defaultFallbackValue, ESTestCase::randomFloat);
400+
assertThat(
401+
floatSetting.get(Settings.builder().put("fallback_setting", explicitFallbackValue).build()),
402+
equalTo(explicitFallbackValue)
403+
);
404+
405+
// Both float_setting and fallback_setting specified
406+
float explicitFloatValue = randomValueOtherThanMany(
407+
v -> v != explicitFallbackValue && v != defaultFallbackValue,
408+
ESTestCase::randomFloat
409+
);
410+
assertThat(
411+
floatSetting.get(
412+
Settings.builder().put("fallback_setting", explicitFallbackValue).put("float_setting", explicitFloatValue).build()
413+
),
414+
equalTo(explicitFloatValue)
415+
);
416+
}
417+
390418
private enum TestEnum {
391419
ON,
392420
OFF

0 commit comments

Comments
 (0)