Skip to content
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

[7.5] Do not reference values for filtered settings #48466

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 100 additions & 33 deletions server/src/main/java/org/elasticsearch/common/settings/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,13 @@ private T get(Settings settings, boolean validate) {
} catch (ElasticsearchParseException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException("Failed to parse value [" + value + "] for setting [" + getKey() + "]", ex);
String err = "Failed to parse value" + (isFiltered() ? "" : " [" + value + "]") + " for setting [" + getKey() + "]";
throw new IllegalArgumentException(err, ex);
} catch (IllegalArgumentException ex) {
throw ex;
} catch (Exception t) {
throw new IllegalArgumentException("Failed to parse value [" + value + "] for setting [" + getKey() + "]", t);
String err = "Failed to parse value" + (isFiltered() ? "" : " [" + value + "]") + " for setting [" + getKey() + "]";
throw new IllegalArgumentException(err, t);
}
}

Expand Down Expand Up @@ -957,8 +959,9 @@ public Settings getValue(Settings current, Settings previous) {
try {
validator.accept(currentSettings);
} catch (Exception | AssertionError e) {
throw new IllegalArgumentException("illegal value can't update [" + key + "] from ["
+ previousSettings + "] to [" + currentSettings+ "]", e);
String err = "illegal value can't update [" + key + "]" +
(isFiltered() ? "" : " from [" + previousSettings + "] to [" + currentSettings+ "]");
throw new IllegalArgumentException(err, e);
}
return currentSettings;
}
Expand Down Expand Up @@ -1012,8 +1015,12 @@ public T getValue(Settings current, Settings previous) {
accept.accept(inst);
return inst;
} catch (Exception | AssertionError e) {
throw new IllegalArgumentException("illegal value can't update [" + key + "] from [" + value + "] to [" + newValue + "]",
e);
if (isFiltered()) {
throw new IllegalArgumentException("illegal value can't update [" + key + "]");
} else {
throw new IllegalArgumentException("illegal value can't update [" + key + "] from [" + value + "] to [" +
newValue + "]", e);
}
}
}

Expand All @@ -1036,32 +1043,41 @@ public static Setting<Float> floatSetting(String key, float defaultValue, float
return new Setting<>(key, (s) -> Float.toString(defaultValue), (s) -> {
float value = Float.parseFloat(s);
if (value < minValue) {
throw new IllegalArgumentException("Failed to parse value [" + s + "] for setting [" + key + "] must be >= " + minValue);
String err = "Failed to parse value" +
(isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue;
throw new IllegalArgumentException(err);
}
return value;
}, properties);
}

private static boolean isFiltered(Property[] properties) {
return properties != null && Arrays.asList(properties).contains(Property.Filtered);
}

public static Setting<Integer> intSetting(String key, int defaultValue, int minValue, int maxValue, Property... properties) {
return new Setting<>(key, (s) -> Integer.toString(defaultValue), (s) -> parseInt(s, minValue, maxValue, key), properties);
return new Setting<>(key, (s) -> Integer.toString(defaultValue),
(s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), properties);
}

public static Setting<Integer> intSetting(String key, int defaultValue, int minValue, Property... properties) {
return new Setting<>(key, (s) -> Integer.toString(defaultValue), (s) -> parseInt(s, minValue, key), properties);
return new Setting<>(key, (s) -> Integer.toString(defaultValue), (s) -> parseInt(s, minValue, key, isFiltered(properties)),
properties);
}

public static Setting<Integer> intSetting(String key, Setting<Integer> fallbackSetting, int minValue, Property... properties) {
return new Setting<>(key, fallbackSetting, (s) -> parseInt(s, minValue, key), properties);
return new Setting<>(key, fallbackSetting, (s) -> parseInt(s, minValue, key, isFiltered(properties)), properties);
}

public static Setting<Integer> intSetting(String key, Setting<Integer> fallbackSetting, int minValue, Validator<Integer> validator,
Property... properties) {
return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseInt(s, minValue, key),validator,
properties);
return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw,
(s) -> parseInt(s, minValue, key, isFiltered(properties)),validator, properties);
}

public static Setting<Long> longSetting(String key, long defaultValue, long minValue, Property... properties) {
return new Setting<>(key, (s) -> Long.toString(defaultValue), (s) -> parseLong(s, minValue, key), properties);
return new Setting<>(key, (s) -> Long.toString(defaultValue), (s) -> parseLong(s, minValue, key, isFiltered(properties)),
properties);
}

public static Setting<String> simpleString(String key, Property... properties) {
Expand Down Expand Up @@ -1102,24 +1118,39 @@ public static Setting<String> simpleString(String key, String defaultValue, Prop
}

public static int parseInt(String s, int minValue, String key) {
return parseInt(s, minValue, Integer.MAX_VALUE, key);
return parseInt(s, minValue, Integer.MAX_VALUE, key, false);
}

public static int parseInt(String s, int minValue, String key, boolean isFiltered) {
return parseInt(s, minValue, Integer.MAX_VALUE, key, isFiltered);
}

public static int parseInt(String s, int minValue, int maxValue, String key) {
return parseInt(s, minValue, maxValue, key, false);
}

static int parseInt(String s, int minValue, int maxValue, String key, boolean isFiltered) {
int value = Integer.parseInt(s);
if (value < minValue) {
throw new IllegalArgumentException("Failed to parse value [" + s + "] for setting [" + key + "] must be >= " + minValue);
String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue;
throw new IllegalArgumentException(err);
}
if (value > maxValue) {
throw new IllegalArgumentException("Failed to parse value [" + s + "] for setting [" + key + "] must be <= " + maxValue);
String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue;
throw new IllegalArgumentException(err);
}
return value;
}

public static long parseLong(String s, long minValue, String key) {
return parseLong(s, minValue, key, false);
}

static long parseLong(String s, long minValue, String key, boolean isFiltered) {
long value = Long.parseLong(s);
if (value < minValue) {
throw new IllegalArgumentException("Failed to parse value [" + s + "] for setting [" + key + "] must be >= " + minValue);
String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue;
throw new IllegalArgumentException(err);
}
return value;
}
Expand All @@ -1137,15 +1168,27 @@ public static Setting<Integer> intSetting(String key, int defaultValue, Property
}

public static Setting<Boolean> boolSetting(String key, boolean defaultValue, Property... properties) {
return new Setting<>(key, (s) -> Boolean.toString(defaultValue), Booleans::parseBoolean, properties);
return new Setting<>(key, (s) -> Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)), properties);
}

public static Setting<Boolean> boolSetting(String key, Setting<Boolean> fallbackSetting, Property... properties) {
return new Setting<>(key, fallbackSetting, Booleans::parseBoolean, properties);
return new Setting<>(key, fallbackSetting, b -> parseBoolean(b, key, isFiltered(properties)), properties);
}

public static Setting<Boolean> boolSetting(String key, Function<Settings, String> defaultValueFn, Property... properties) {
return new Setting<>(key, defaultValueFn, Booleans::parseBoolean, properties);
return new Setting<>(key, defaultValueFn, b -> parseBoolean(b, key, isFiltered(properties)), properties);
}

static boolean parseBoolean(String b, String key, boolean isFiltered) {
try {
return Booleans.parseBoolean(b);
} catch (IllegalArgumentException ex) {
if (isFiltered) {
throw new IllegalArgumentException("Failed to parse value for setting [" + key + "]");
} else {
throw ex;
}
}
}

public static Setting<ByteSizeValue> byteSizeSetting(String key, ByteSizeValue value, Property... properties) {
Expand Down Expand Up @@ -1406,31 +1449,42 @@ public static Setting<TimeValue> timeSetting(
simpleKey,
fallbackSetting,
fallbackSetting::getRaw,
minTimeValueParser(key, minValue),
minTimeValueParser(key, minValue, isFiltered(properties)),
v -> {},
properties);
}

public static Setting<TimeValue> timeSetting(
final String key, Function<Settings, TimeValue> defaultValue, final TimeValue minValue, final Property... properties) {
final SimpleKey simpleKey = new SimpleKey(key);
return new Setting<>(simpleKey, s -> defaultValue.apply(s).getStringRep(), minTimeValueParser(key, minValue), properties);
return new Setting<>(simpleKey, s -> defaultValue.apply(s).getStringRep(),
minTimeValueParser(key, minValue, isFiltered(properties)), properties);
}

public static Setting<TimeValue> timeSetting(
final String key, TimeValue defaultValue, final TimeValue minValue, final TimeValue maxValue, final Property... properties) {
final SimpleKey simpleKey = new SimpleKey(key);
return new Setting<>(simpleKey, s -> defaultValue.getStringRep(), minMaxTimeValueParser(key, minValue, maxValue), properties);
return new Setting<>(simpleKey, s -> defaultValue.getStringRep(),
minMaxTimeValueParser(key, minValue, maxValue, isFiltered(properties)), properties);
}

private static Function<String, TimeValue> minTimeValueParser(final String key, final TimeValue minValue) {
private static Function<String, TimeValue> minTimeValueParser(final String key, final TimeValue minValue, boolean isFiltered) {
return s -> {
final TimeValue value = TimeValue.parseTimeValue(s, null, key);
TimeValue value;
try {
value = TimeValue.parseTimeValue(s, null, key);
} catch (RuntimeException ex) {
if (isFiltered) {
throw new IllegalArgumentException("failed to parse value for setting [" + key + "] as a time value");
} else {
throw ex;
}
}
if (value.millis() < minValue.millis()) {
final String message = String.format(
Locale.ROOT,
"failed to parse value [%s] for setting [%s], must be >= [%s]",
s,
"failed to parse value%s for setting [%s], must be >= [%s]",
isFiltered ? "" : " [" + s + "]",
key,
minValue.getStringRep());
throw new IllegalArgumentException(message);
Expand All @@ -1440,14 +1494,23 @@ private static Function<String, TimeValue> minTimeValueParser(final String key,
}

private static Function<String, TimeValue> minMaxTimeValueParser(
final String key, final TimeValue minValue, final TimeValue maxValue) {
final String key, final TimeValue minValue, final TimeValue maxValue, boolean isFiltered) {
return s -> {
final TimeValue value = minTimeValueParser(key, minValue).apply(s);
TimeValue value;
try {
value = minTimeValueParser(key, minValue, isFiltered).apply(s);
} catch (RuntimeException ex) {
if (isFiltered) {
throw new IllegalArgumentException("failed to parse value for setting [" + key + "] as a time value");
} else {
throw ex;
}
}
if (value.millis() > maxValue.millis()) {
final String message = String.format(
Locale.ROOT,
"failed to parse value [%s] for setting [%s], must be <= [%s]",
s,
"failed to parse value%s for setting [%s], must be <= [%s]",
isFiltered ? "" : " [" + s + "]",
key,
maxValue.getStringRep());
throw new IllegalArgumentException(message);
Expand Down Expand Up @@ -1488,10 +1551,14 @@ public static Setting<Double> doubleSetting(String key, double defaultValue, dou
return new Setting<>(key, (s) -> Double.toString(defaultValue), (s) -> {
final double d = Double.parseDouble(s);
if (d < minValue) {
throw new IllegalArgumentException("Failed to parse value [" + s + "] for setting [" + key + "] must be >= " + minValue);
String err = "Failed to parse value" + (isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key +
"] must be >= " + minValue;
throw new IllegalArgumentException(err);
}
if (d > maxValue) {
throw new IllegalArgumentException("Failed to parse value [" + s + "] for setting [" + key + "] must be <= " + maxValue);
String err = "Failed to parse value" + (isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key +
"] must be <= " + maxValue;
throw new IllegalArgumentException(err);
}
return d;
}, properties);
Expand Down
Loading