From 6afba62fab82afffd43a12b975d0ed46ca9a9cff Mon Sep 17 00:00:00 2001 From: dblock Date: Thu, 6 May 2021 17:23:15 +0000 Subject: [PATCH 1/4] Standardize int, long, double and float Setting constructors. Signed-off-by: dblock --- .../opensearch/common/settings/Setting.java | 299 ++++++++++++------ .../common/settings/SettingTests.java | 122 ++++++- 2 files changed, 326 insertions(+), 95 deletions(-) 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 73732a338b598..289917e386d96 100644 --- a/server/src/main/java/org/opensearch/common/settings/Setting.java +++ b/server/src/main/java/org/opensearch/common/settings/Setting.java @@ -1160,62 +1160,236 @@ public static Setting versionSetting(final String key, final Version de return new Setting<>(key, s -> Integer.toString(defaultValue.id), s -> Version.fromId(Integer.parseInt(s)), properties); } + private static boolean isFiltered(Property[] properties) { + return properties != null && Arrays.asList(properties).contains(Property.Filtered); + } + + // Float + + private static float parseFloat(String s, float minValue, float maxValue, String key, boolean isFiltered) { + float value = Float.parseFloat(s); + if (value < minValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; + throw new IllegalArgumentException(err); + } + if (value > maxValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; + throw new IllegalArgumentException(err); + } + return value; + } + + // Setting with defaultValue + public static Setting floatSetting(String key, float defaultValue, Property... properties) { - return new Setting<>(key, (s) -> Float.toString(defaultValue), Float::parseFloat, properties); + return floatSetting(key, defaultValue, Float.MIN_VALUE, Float.MAX_VALUE, properties); } public static Setting floatSetting(String key, float defaultValue, float minValue, Property... properties) { - return new Setting<>(key, (s) -> Float.toString(defaultValue), (s) -> { - float value = Float.parseFloat(s); - if (value < minValue) { - String err = "Failed to parse value" + - (isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; - throw new IllegalArgumentException(err); - } - return value; - }, properties); + return floatSetting(key, defaultValue, minValue, Float.MAX_VALUE, properties); + } + + public static Setting floatSetting(String key, float defaultValue, float minValue, float maxValue, Property... properties) { + return floatSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); } - private static boolean isFiltered(Property[] properties) { - return properties != null && Arrays.asList(properties).contains(Property.Filtered); + public static Setting floatSetting(String key, float defaultValue, float minValue, float maxValue, Validator validator, Property... properties) { + return new Setting<>(key, Float.toString(defaultValue), (s) -> parseFloat(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); } - public static Setting 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, isFiltered(properties)), properties); + // Setting with fallback + + public static Setting floatSetting(String key, Setting fallbackSetting, Property... properties) { + return floatSetting(key, fallbackSetting, Float.MIN_VALUE, Float.MAX_VALUE, properties); + } + + public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, Property... properties) { + return floatSetting(key, fallbackSetting, minValue, Float.MAX_VALUE, properties); + } + + public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, float maxValue, Property... properties) { + return floatSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); + } + + public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, float maxValue, Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseFloat(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + } + + // Integer + + public static int parseInt(String s, int minValue, String key) { + return parseInt(s, minValue, Integer.MAX_VALUE, key); + } + + public static int parseInt(String s, int minValue, int maxValue, String key) { + return parseInt(s, minValue, maxValue, key, false); + } + + public static int parseInt(String s, int minValue, int maxValue, String key, boolean isFiltered) { + int value = Integer.parseInt(s); + if (value < minValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; + throw new IllegalArgumentException(err); + } + if (value > maxValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; + throw new IllegalArgumentException(err); + } + return value; + } + + // Setting with defaultValue + + public static Setting intSetting(String key, int defaultValue, Property... properties) { + return intSetting(key, defaultValue, Integer.MIN_VALUE, Integer.MAX_VALUE, properties); } public static Setting intSetting(String key, int defaultValue, int minValue, Property... properties) { - return new Setting<>(key, (s) -> Integer.toString(defaultValue), (s) -> parseInt(s, minValue, key, isFiltered(properties)), - properties); + return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, properties); + } + + public static Setting intSetting(String key, int defaultValue, int minValue, int maxValue, Property... properties) { + return intSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); + } + + public static Setting intSetting(String key, int defaultValue, int minValue, Validator validator, Property... properties) { + return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, validator, properties); } - public static Setting intSetting(String key, int defaultValue, int minValue, Validator validator, - Property... properties) { - return new Setting<>(key, Integer.toString(defaultValue), (s) -> parseInt(s, minValue, key, isFiltered(properties)), validator, - properties); + public static Setting intSetting(String key, int defaultValue, int minValue, int maxValue, Validator validator, Property... properties) { + return new Setting<>(key, Integer.toString(defaultValue), (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + } + + // Setting with fallback + + public static Setting intSetting(String key, Setting fallbackSetting, Property... properties) { + return intSetting(key, fallbackSetting, Integer.MIN_VALUE, Integer.MAX_VALUE, properties); } public static Setting intSetting(String key, Setting fallbackSetting, int minValue, Property... properties) { - return new Setting<>(key, fallbackSetting, (s) -> parseInt(s, minValue, key, isFiltered(properties)), properties); + return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, properties); } - public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, - Property... properties) { - return new Setting<>(key, fallbackSetting, (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), properties); + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, Property... properties) { + return intSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); } - public static Setting intSetting(String key, Setting fallbackSetting, int minValue, Validator validator, - Property... properties) { - return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, - (s) -> parseInt(s, minValue, key, isFiltered(properties)),validator, properties); + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, Validator validator, Property... properties) { + return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, validator, properties); + } + + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + } + + // Long + + private static long parseLong(String s, long minValue, long maxValue, String key, boolean isFiltered) { + long value = Long.parseLong(s); + if (value < minValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; + throw new IllegalArgumentException(err); + } + if (value > maxValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; + throw new IllegalArgumentException(err); + } + return value; + } + + // Setting with defaultValue + + public static Setting longSetting(String key, long defaultValue, Property... properties) { + return longSetting(key, defaultValue, Long.MIN_VALUE, Long.MAX_VALUE, properties); } public static Setting longSetting(String key, long defaultValue, long minValue, Property... properties) { - return new Setting<>(key, (s) -> Long.toString(defaultValue), (s) -> parseLong(s, minValue, key, isFiltered(properties)), - properties); + return longSetting(key, defaultValue, minValue, Long.MAX_VALUE, properties); + } + + public static Setting longSetting(String key, long defaultValue, long minValue, long maxValue, Property... properties) { + return longSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); + } + + public static Setting longSetting(String key, long defaultValue, long minValue, long maxValue, Validator validator, Property... properties) { + return new Setting<>(key, Long.toString(defaultValue), (s) -> parseLong(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); } + // Setting with fallback + + public static Setting longSetting(String key, Setting fallbackSetting, Property... properties) { + return longSetting(key, fallbackSetting, Long.MIN_VALUE, Long.MAX_VALUE, properties); + } + + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, Property... properties) { + return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, properties); + } + + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, Property... properties) { + return longSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); + } + + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, Validator validator, Property... properties) { + return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, validator, properties); + } + + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseLong(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + } + + // Double + + private static double parseDouble(String s, double minValue, double maxValue, String key, boolean isFiltered) { + double value = Double.parseDouble(s); + if (value < minValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; + throw new IllegalArgumentException(err); + } + if (value > maxValue) { + String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; + throw new IllegalArgumentException(err); + } + return value; + } + + // Setting with defaultValue + + public static Setting doubleSetting(String key, double defaultValue, Property... properties) { + return doubleSetting(key, defaultValue, Double.MIN_VALUE, Double.MAX_VALUE, properties); + } + + public static Setting doubleSetting(String key, double defaultValue, double minValue, Property... properties) { + return doubleSetting(key, defaultValue, minValue, Double.MAX_VALUE, properties); + } + + public static Setting doubleSetting(String key, double defaultValue, double minValue, double maxValue, Property... properties) { + return doubleSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); + } + + public static Setting doubleSetting(String key, double defaultValue, double minValue, double maxValue, Validator validator, Property... properties) { + return new Setting<>(key, Double.toString(defaultValue), (s) -> parseDouble(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + } + + // Setting with fallback + + public static Setting doubleSetting(String key, Setting fallbackSetting, Property... properties) { + return doubleSetting(key, fallbackSetting, Double.MIN_VALUE, Double.MAX_VALUE, properties); + } + + public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, Property... properties) { + return doubleSetting(key, fallbackSetting, minValue, Double.MAX_VALUE, properties); + } + + public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, double maxValue, Property... properties) { + return doubleSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); + } + + public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, double maxValue, Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseDouble(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + } + + /// simpleString + public static Setting simpleString(String key, Property... properties) { return new Setting<>(key, s -> "", Function.identity(), properties); } @@ -1257,44 +1431,6 @@ public static Setting simpleString(String key, String defaultValue, Prop return new Setting<>(key, s -> defaultValue, Function.identity(), properties); } - public static int parseInt(String s, int minValue, String 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); - } - - public static int parseInt(String s, int minValue, int maxValue, String key, boolean isFiltered) { - int value = Integer.parseInt(s); - if (value < minValue) { - String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; - throw new IllegalArgumentException(err); - } - if (value > 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) { - String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; - throw new IllegalArgumentException(err); - } - return value; - } - public static TimeValue parseTimeValue(String s, TimeValue minValue, String key) { TimeValue timeValue = TimeValue.parseTimeValue(s, null, key); if (timeValue.millis() < minValue.millis()) { @@ -1303,10 +1439,6 @@ public static TimeValue parseTimeValue(String s, TimeValue minValue, String key) return timeValue; } - public static Setting intSetting(String key, int defaultValue, Property... properties) { - return intSetting(key, defaultValue, Integer.MIN_VALUE, properties); - } - public static Setting boolSetting(String key, boolean defaultValue, Property... properties) { return new Setting<>(key, (s) -> Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)), properties); } @@ -1708,27 +1840,6 @@ public static Setting positiveTimeSetting( return timeSetting(key, fallbackSetting, minValue, properties); } - public static Setting doubleSetting(String key, double defaultValue, double minValue, Property... properties) { - return doubleSetting(key, defaultValue, minValue, Double.POSITIVE_INFINITY, properties); - } - - public static Setting doubleSetting(String key, double defaultValue, double minValue, double maxValue, Property... properties) { - return new Setting<>(key, (s) -> Double.toString(defaultValue), (s) -> { - final double d = Double.parseDouble(s); - if (d < minValue) { - String err = "Failed to parse value" + (isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key + - "] must be >= " + minValue; - throw new IllegalArgumentException(err); - } - if (d > maxValue) { - String err = "Failed to parse value" + (isFiltered(properties) ? "" : " [" + s + "]") + " for setting [" + key + - "] must be <= " + maxValue; - throw new IllegalArgumentException(err); - } - return d; - }, properties); - } - @Override public boolean equals(Object o) { if (this == o) return true; 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 d104c3d5ef374..a2dc6a5162147 100644 --- a/server/src/test/java/org/opensearch/common/settings/SettingTests.java +++ b/server/src/test/java/org/opensearch/common/settings/SettingTests.java @@ -896,7 +896,22 @@ public Iterator> settings() { assertEquals(fix1.get(Settings.builder().put("abc.qrx", 30).build()), Integer.valueOf(30)); } - public void testMinMaxInt() { + // Integer + + public void testIntWithDefaultValue() { + Setting integerSetting = Setting.intSetting("foo.bar", 42); + assertEquals(integerSetting.get(Settings.EMPTY), Integer.valueOf(42)); + } + + public void testIntWithFallbackValue() { + Setting fallbackSetting = Setting.intSetting("foo.baz", 2); + Setting integerSetting = Setting.intSetting("foo.bar", fallbackSetting); + assertEquals(integerSetting.get(Settings.EMPTY), Integer.valueOf(2)); + assertEquals(integerSetting.get(Settings.builder().put("foo.bar", 3).build()), Integer.valueOf(3)); + assertEquals(integerSetting.get(Settings.builder().put("foo.baz", 3).build()), Integer.valueOf(3)); + } + + public void testIntWithMinMax() { Setting integerSetting = Setting.intSetting("foo.bar", 1, 0, 10, Property.NodeScope); try { integerSetting.get(Settings.builder().put("foo.bar", 11).build()); @@ -916,6 +931,111 @@ public void testMinMaxInt() { assertEquals(1, integerSetting.get(Settings.EMPTY).intValue()); } + // Long + + public void testLongWithDefaultValue() { + Setting longSetting = Setting.longSetting("foo.bar", 42); + assertEquals(longSetting.get(Settings.EMPTY), Long.valueOf(42)); + } + + public void testLongWithFallbackValue() { + Setting fallbackSetting = Setting.longSetting("foo.baz", 2); + Setting longSetting = Setting.longSetting("foo.bar", fallbackSetting); + assertEquals(longSetting.get(Settings.EMPTY), Long.valueOf(2)); + assertEquals(longSetting.get(Settings.builder().put("foo.bar", 3).build()), Long.valueOf(3)); + assertEquals(longSetting.get(Settings.builder().put("foo.baz", 3).build()), Long.valueOf(3)); + } + + public void testLongWithMinMax() { + Setting longSetting = Setting.longSetting("foo.bar", 1, 0, 10, Property.NodeScope); + try { + longSetting.get(Settings.builder().put("foo.bar", 11).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [11] for setting [foo.bar] must be <= 10", ex.getMessage()); + } + + try { + longSetting.get(Settings.builder().put("foo.bar", -1).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [-1] for setting [foo.bar] must be >= 0", ex.getMessage()); + } + + assertEquals(5, longSetting.get(Settings.builder().put("foo.bar", 5).build()).longValue()); + assertEquals(1, longSetting.get(Settings.EMPTY).longValue()); + } + + // Float + + public void testFloatWithDefaultValue() { + Setting floatSetting = Setting.floatSetting("foo.bar", (float) 42.1); + assertEquals(floatSetting.get(Settings.EMPTY), Float.valueOf((float) 42.1)); + } + + public void testFloatWithFallbackValue() { + Setting fallbackSetting = Setting.floatSetting("foo.baz", (float) 2.1); + Setting floatSetting = Setting.floatSetting("foo.bar", fallbackSetting); + assertEquals(floatSetting.get(Settings.EMPTY), Float.valueOf((float) 2.1)); + assertEquals(floatSetting.get(Settings.builder().put("foo.bar", 3.2).build()), Float.valueOf((float) 3.2)); + assertEquals(floatSetting.get(Settings.builder().put("foo.baz", 3.2).build()), Float.valueOf((float) 3.2)); + } + + public void testFloatWithMinMax() { + Setting floatSetting = Setting.floatSetting("foo.bar", (float) 1.2, 0, 10, Property.NodeScope); + try { + floatSetting.get(Settings.builder().put("foo.bar", (float) 11.3).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [11.3] for setting [foo.bar] must be <= 10.0", ex.getMessage()); + } + + try { + floatSetting.get(Settings.builder().put("foo.bar", (float) -1.4).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [-1.4] for setting [foo.bar] must be >= 0.0", ex.getMessage()); + } + + assertEquals(5.6, floatSetting.get(Settings.builder().put("foo.bar", (float) 5.6).build()).floatValue(), 0.01); + assertEquals(1.2, floatSetting.get(Settings.EMPTY).floatValue(), 0.01); + } + + // Double + + public void testDoubleWithDefaultValue() { + Setting doubleSetting = Setting.doubleSetting("foo.bar", 42.1); + assertEquals(doubleSetting.get(Settings.EMPTY), Double.valueOf(42.1)); + } + + public void testDoubleWithFallbackValue() { + Setting fallbackSetting = Setting.doubleSetting("foo.baz", 2.1); + Setting doubleSetting = Setting.doubleSetting("foo.bar", fallbackSetting); + assertEquals(doubleSetting.get(Settings.EMPTY), Double.valueOf(2.1)); + assertEquals(doubleSetting.get(Settings.builder().put("foo.bar", 3.2).build()), Double.valueOf(3.2)); + assertEquals(doubleSetting.get(Settings.builder().put("foo.baz", 3.2).build()), Double.valueOf(3.2)); + } + + public void testDoubleWithMinMax() { + Setting doubleSetting = Setting.doubleSetting("foo.bar", 1.2, 0, 10, Property.NodeScope); + try { + doubleSetting.get(Settings.builder().put("foo.bar", 11.3).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [11.3] for setting [foo.bar] must be <= 10.0", ex.getMessage()); + } + + try { + doubleSetting.get(Settings.builder().put("foo.bar", -1.4).build()); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Failed to parse value [-1.4] for setting [foo.bar] must be >= 0.0", ex.getMessage()); + } + + assertEquals(5.6, doubleSetting.get(Settings.builder().put("foo.bar", 5.6).build()).doubleValue(), 0.01); + assertEquals(1.2, doubleSetting.get(Settings.EMPTY).doubleValue(), 0.01); + } + /** * Only one single scope can be added to any setting */ From 50d320f756a6c5e95de2bee6fc122d6855714bda Mon Sep 17 00:00:00 2001 From: dblock Date: Thu, 6 May 2021 18:29:35 +0000 Subject: [PATCH 2/4] Fix: positiveTimeSetting confuses min and default with fallback. Signed-off-by: dblock --- .../org/opensearch/common/settings/Setting.java | 14 ++++---------- .../java/org/opensearch/script/ScriptService.java | 3 +-- .../opensearch/transport/RemoteClusterService.java | 1 - 3 files changed, 5 insertions(+), 13 deletions(-) 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 289917e386d96..aae5b3a4cb5f2 100644 --- a/server/src/main/java/org/opensearch/common/settings/Setting.java +++ b/server/src/main/java/org/opensearch/common/settings/Setting.java @@ -1822,22 +1822,16 @@ public static Setting timeSetting(String key, Setting fall return new Setting<>(key, fallbackSetting, (s) -> TimeValue.parseTimeValue(s, key), properties); } - public static Setting timeSetting(String key, Setting fallBackSetting, Validator validator, - Property... properties) { - return new Setting<>(new SimpleKey(key), fallBackSetting, fallBackSetting::getRaw, (s) -> TimeValue.parseTimeValue(s, key), - validator, properties); + public static Setting timeSetting(String key, Setting fallBackSetting, Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallBackSetting, fallBackSetting::getRaw, (s) -> TimeValue.parseTimeValue(s, key), validator, properties); } public static Setting positiveTimeSetting(String key, TimeValue defaultValue, Property... properties) { return timeSetting(key, defaultValue, TimeValue.timeValueMillis(0), properties); } - public static Setting positiveTimeSetting( - final String key, - final Setting fallbackSetting, - final TimeValue minValue, - final Property... properties) { - return timeSetting(key, fallbackSetting, minValue, properties); + public static Setting positiveTimeSetting(final String key, final Setting fallbackSetting, final Property... properties) { + return timeSetting(key, fallbackSetting, TimeValue.timeValueMillis(0), properties); } @Override diff --git a/server/src/main/java/org/opensearch/script/ScriptService.java b/server/src/main/java/org/opensearch/script/ScriptService.java index 622c23ed76ba4..af4dae88ed786 100644 --- a/server/src/main/java/org/opensearch/script/ScriptService.java +++ b/server/src/main/java/org/opensearch/script/ScriptService.java @@ -106,8 +106,7 @@ public class ScriptService implements Closeable, ClusterStateApplier { public static final Setting.AffixSetting SCRIPT_CACHE_EXPIRE_SETTING = Setting.affixKeySetting(CONTEXT_PREFIX, "cache_expire", - key -> Setting.positiveTimeSetting(key, SCRIPT_GENERAL_CACHE_EXPIRE_SETTING, TimeValue.timeValueMillis(0), - Property.NodeScope, Property.Dynamic)); + key -> Setting.positiveTimeSetting(key, SCRIPT_GENERAL_CACHE_EXPIRE_SETTING, Property.NodeScope, Property.Dynamic)); // Unlimited compilation rate for context-specific script caches static final String UNLIMITED_COMPILATION_RATE_KEY = "unlimited"; diff --git a/server/src/main/java/org/opensearch/transport/RemoteClusterService.java b/server/src/main/java/org/opensearch/transport/RemoteClusterService.java index e19a8eab3915e..1b792df8ec4e3 100644 --- a/server/src/main/java/org/opensearch/transport/RemoteClusterService.java +++ b/server/src/main/java/org/opensearch/transport/RemoteClusterService.java @@ -100,7 +100,6 @@ public final class RemoteClusterService extends RemoteClusterAware implements Cl Setting.positiveTimeSetting( "cluster.remote.initial_connect_timeout", SEARCH_REMOTE_INITIAL_CONNECTION_TIMEOUT_SETTING, // the default needs to be thirty seconds when fallback is removed - TimeValue.timeValueSeconds(30), Setting.Property.NodeScope); public static final Setting SEARCH_REMOTE_NODE_ATTRIBUTE = From 44a7f57c9b25a2e33a8440bfbcbe9c3e12920d53 Mon Sep 17 00:00:00 2001 From: dblock Date: Thu, 6 May 2021 18:58:32 +0000 Subject: [PATCH 3/4] Make checkstyle happy. Signed-off-by: dblock --- .../opensearch/common/settings/Setting.java | 141 ++++++++++++------ 1 file changed, 94 insertions(+), 47 deletions(-) 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 aae5b3a4cb5f2..70535eb2decba 100644 --- a/server/src/main/java/org/opensearch/common/settings/Setting.java +++ b/server/src/main/java/org/opensearch/common/settings/Setting.java @@ -225,8 +225,10 @@ public Setting(Key key, Function defaultValue, Function defaultValue, Function parser, Validator validator, Property... properties) { - this(key, null, defaultValue, parser, validator, properties); + Key key, Function defaultValue, Function parser, + Validator validator, Property... properties) { + this(key, null, defaultValue, parser, + validator, properties); } /** @@ -249,8 +251,10 @@ public Setting(String key, String defaultValue, Function parser, Prop * @param validator a {@link Validator} for validating this setting * @param properties properties for this setting */ - public Setting(String key, String defaultValue, Function parser, Validator validator, Property... properties) { - this(new SimpleKey(key), s -> defaultValue, parser, validator, properties); + public Setting(String key, String defaultValue, Function parser, + Validator validator, Property... properties) { + this(new SimpleKey(key), s -> defaultValue, parser, + validator, properties); } /** @@ -1193,8 +1197,10 @@ public static Setting floatSetting(String key, float defaultValue, float return floatSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); } - public static Setting floatSetting(String key, float defaultValue, float minValue, float maxValue, Validator validator, Property... properties) { - return new Setting<>(key, Float.toString(defaultValue), (s) -> parseFloat(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + public static Setting floatSetting(String key, float defaultValue, float minValue, float maxValue, + Validator validator, Property... properties) { + return new Setting<>(key, Float.toString(defaultValue), (s) -> parseFloat(s, minValue, maxValue, key, isFiltered(properties)), + validator, properties); } // Setting with fallback @@ -1207,12 +1213,15 @@ public static Setting floatSetting(String key, Setting fallbackSet return floatSetting(key, fallbackSetting, minValue, Float.MAX_VALUE, properties); } - public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, float maxValue, Property... properties) { + public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, float maxValue, + Property... properties) { return floatSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); } - public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, float maxValue, Validator validator, Property... properties) { - return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseFloat(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + public static Setting floatSetting(String key, Setting fallbackSetting, float minValue, float maxValue, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseFloat(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); } // Integer @@ -1252,12 +1261,16 @@ public static Setting intSetting(String key, int defaultValue, int minV return intSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); } - public static Setting intSetting(String key, int defaultValue, int minValue, Validator validator, Property... properties) { - return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, validator, properties); + public static Setting intSetting(String key, int defaultValue, int minValue, + Validator validator, Property... properties) { + return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, + validator, properties); } - public static Setting intSetting(String key, int defaultValue, int minValue, int maxValue, Validator validator, Property... properties) { - return new Setting<>(key, Integer.toString(defaultValue), (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + public static Setting intSetting(String key, int defaultValue, int minValue, int maxValue, + Validator validator, Property... properties) { + return new Setting<>(key, Integer.toString(defaultValue), (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), + validator, properties); } // Setting with fallback @@ -1270,16 +1283,21 @@ public static Setting intSetting(String key, Setting fallbackS return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, properties); } - public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, Property... properties) { + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, + Property... properties) { return intSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); } - public static Setting intSetting(String key, Setting fallbackSetting, int minValue, Validator validator, Property... properties) { - return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, validator, properties); + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, + Validator validator, Property... properties) { + return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, + validator, properties); } - public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, Validator validator, Property... properties) { - return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseInt(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseInt(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); } // Long @@ -1311,8 +1329,10 @@ public static Setting longSetting(String key, long defaultValue, long minV return longSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); } - public static Setting longSetting(String key, long defaultValue, long minValue, long maxValue, Validator validator, Property... properties) { - return new Setting<>(key, Long.toString(defaultValue), (s) -> parseLong(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + public static Setting longSetting(String key, long defaultValue, long minValue, long maxValue, + Validator validator, Property... properties) { + return new Setting<>(key, Long.toString(defaultValue), (s) -> parseLong(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); } // Setting with fallback @@ -1325,16 +1345,21 @@ public static Setting longSetting(String key, Setting fallbackSettin return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, properties); } - public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, Property... properties) { + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, + Property... properties) { return longSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); } - public static Setting longSetting(String key, Setting fallbackSetting, long minValue, Validator validator, Property... properties) { - return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, validator, properties); + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, + Validator validator, Property... properties) { + return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, + validator, properties); } - public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, Validator validator, Property... properties) { - return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseLong(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseLong(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); } // Double @@ -1366,8 +1391,10 @@ public static Setting doubleSetting(String key, double defaultValue, dou return doubleSetting(key, defaultValue, minValue, maxValue, v -> {}, properties); } - public static Setting doubleSetting(String key, double defaultValue, double minValue, double maxValue, Validator validator, Property... properties) { - return new Setting<>(key, Double.toString(defaultValue), (s) -> parseDouble(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + public static Setting doubleSetting(String key, double defaultValue, double minValue, double maxValue, + Validator validator, Property... properties) { + return new Setting<>(key, Double.toString(defaultValue), (s) -> parseDouble(s, minValue, maxValue, key, isFiltered(properties)), + validator, properties); } // Setting with fallback @@ -1380,12 +1407,15 @@ public static Setting doubleSetting(String key, Setting fallback return doubleSetting(key, fallbackSetting, minValue, Double.MAX_VALUE, properties); } - public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, double maxValue, Property... properties) { + public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, double maxValue, + Property... properties) { return doubleSetting(key, fallbackSetting, minValue, maxValue, v -> {}, properties); } - public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, double maxValue, Validator validator, Property... properties) { - return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseDouble(s, minValue, maxValue, key, isFiltered(properties)), validator, properties); + public static Setting doubleSetting(String key, Setting fallbackSetting, double minValue, double maxValue, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, (s) -> parseDouble(s, minValue, maxValue, key, + isFiltered(properties)), validator, properties); } /// simpleString @@ -1394,17 +1424,23 @@ public static Setting simpleString(String key, Property... properties) { return new Setting<>(key, s -> "", Function.identity(), properties); } - public static Setting simpleString(String key, Validator validator, Property... properties) { - return new Setting<>(new SimpleKey(key), null, s -> "", Function.identity(), validator, properties); + public static Setting simpleString(String key, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), null, s -> "", Function.identity(), + validator, properties); } - public static Setting simpleString(String key, Validator validator, Setting fallback, Property... properties) { - return new Setting<>(new SimpleKey(key), fallback, fallback::getRaw, Function.identity(), validator, properties); + public static Setting simpleString(String key, + Validator validator, Setting fallback, Property... properties) { + return new Setting<>(new SimpleKey(key), fallback, fallback::getRaw, Function.identity(), + validator, properties); } - public static Setting simpleString(String key, String defaultValue, Validator validator, Property... properties) { + public static Setting simpleString(String key, String defaultValue, + Validator validator, Property... properties) { validator.validate(defaultValue); - return new Setting<>(new SimpleKey(key), null, s -> defaultValue, Function.identity(), validator, properties); + return new Setting<>(new SimpleKey(key), null, s -> defaultValue, Function.identity(), + validator, properties); } public static Setting simpleString(String key, Setting fallback, Property... properties) { @@ -1447,14 +1483,18 @@ public static Setting boolSetting(String key, Setting fallback return new Setting<>(key, fallbackSetting, b -> parseBoolean(b, key, isFiltered(properties)), properties); } - public static Setting boolSetting(String key, Setting fallbackSetting, Validator validator, + public static Setting boolSetting(String key, Setting fallbackSetting, + Validator validator, Property... properties) { return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, b -> parseBoolean(b, key, - isFiltered(properties)), validator, properties); + isFiltered(properties)), + validator, properties); } - public static Setting boolSetting(String key, boolean defaultValue, Validator validator, Property... properties) { - return new Setting<>(key, Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)), validator, properties); + public static Setting boolSetting(String key, boolean defaultValue, + Validator validator, Property... properties) { + return new Setting<>(key, Boolean.toString(defaultValue), b -> parseBoolean(b, key, isFiltered(properties)), + validator, properties); } public static Setting boolSetting(String key, Function defaultValueFn, Property... properties) { @@ -1578,7 +1618,8 @@ public static Setting> listSetting( final Function singleValueParser, final Validator> validator, final Property... properties) { - return listSetting(key, null, singleValueParser, (s) -> defaultStringValue, validator, properties); + return listSetting(key, null, singleValueParser, (s) -> defaultStringValue, + validator, properties); } // TODO this one's two argument get is still broken @@ -1604,7 +1645,8 @@ public static Setting> listSetting( final Function> defaultStringValue, final Validator> validator, final Property... properties) { - return listSetting(key, null, singleValueParser, defaultStringValue, validator, properties); + return listSetting(key, null, singleValueParser, defaultStringValue, + validator, properties); } public static Setting> listSetting( @@ -1629,7 +1671,8 @@ public static Setting> listSetting( Function> parser = (s) -> parseableStringToList(s).stream().map(singleValueParser).collect(Collectors.toList()); - return new ListSetting<>(key, fallbackSetting, defaultStringValue, parser, validator, properties); + return new ListSetting<>(key, fallbackSetting, defaultStringValue, parser, + validator, properties); } private static List parseableStringToList(String parsableString) { @@ -1727,7 +1770,8 @@ public static Setting groupSetting(String key, Property... properties) } public static Setting groupSetting(String key, Consumer validator, Property... properties) { - return new GroupSetting(key, validator, properties); + return new GroupSetting(key, + validator, properties); } public static Setting timeSetting( @@ -1822,15 +1866,18 @@ public static Setting timeSetting(String key, Setting fall return new Setting<>(key, fallbackSetting, (s) -> TimeValue.parseTimeValue(s, key), properties); } - public static Setting timeSetting(String key, Setting fallBackSetting, Validator validator, Property... properties) { - return new Setting<>(new SimpleKey(key), fallBackSetting, fallBackSetting::getRaw, (s) -> TimeValue.parseTimeValue(s, key), validator, properties); + public static Setting timeSetting(String key, Setting fallBackSetting, + Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), fallBackSetting, fallBackSetting::getRaw, (s) -> TimeValue.parseTimeValue(s, key), + validator, properties); } public static Setting positiveTimeSetting(String key, TimeValue defaultValue, Property... properties) { return timeSetting(key, defaultValue, TimeValue.timeValueMillis(0), properties); } - public static Setting positiveTimeSetting(final String key, final Setting fallbackSetting, final Property... properties) { + public static Setting positiveTimeSetting(final String key, final Setting fallbackSetting, + final Property... properties) { return timeSetting(key, fallbackSetting, TimeValue.timeValueMillis(0), properties); } From 870127e8e9bfbb88b08f08415125f0b9c7f15684 Mon Sep 17 00:00:00 2001 From: dblock Date: Thu, 6 May 2021 20:19:37 +0000 Subject: [PATCH 4/4] Fixup unnecessary CR/LFs. Signed-off-by: dblock --- .../opensearch/common/settings/Setting.java | 54 +++++++------------ 1 file changed, 18 insertions(+), 36 deletions(-) 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 70535eb2decba..ef10f9a42c5af 100644 --- a/server/src/main/java/org/opensearch/common/settings/Setting.java +++ b/server/src/main/java/org/opensearch/common/settings/Setting.java @@ -225,10 +225,8 @@ public Setting(Key key, Function defaultValue, Function defaultValue, Function parser, - Validator validator, Property... properties) { - this(key, null, defaultValue, parser, - validator, properties); + Key key, Function defaultValue, Function parser, Validator validator, Property... properties) { + this(key, null, defaultValue, parser, validator, properties); } /** @@ -251,10 +249,8 @@ public Setting(String key, String defaultValue, Function parser, Prop * @param validator a {@link Validator} for validating this setting * @param properties properties for this setting */ - public Setting(String key, String defaultValue, Function parser, - Validator validator, Property... properties) { - this(new SimpleKey(key), s -> defaultValue, parser, - validator, properties); + public Setting(String key, String defaultValue, Function parser, Validator validator, Property... properties) { + this(new SimpleKey(key), s -> defaultValue, parser, validator, properties); } /** @@ -1263,8 +1259,7 @@ public static Setting intSetting(String key, int defaultValue, int minV public static Setting intSetting(String key, int defaultValue, int minValue, Validator validator, Property... properties) { - return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, - validator, properties); + return intSetting(key, defaultValue, minValue, Integer.MAX_VALUE, validator, properties); } public static Setting intSetting(String key, int defaultValue, int minValue, int maxValue, @@ -1290,8 +1285,7 @@ public static Setting intSetting(String key, Setting fallbackS public static Setting intSetting(String key, Setting fallbackSetting, int minValue, Validator validator, Property... properties) { - return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, - validator, properties); + return intSetting(key, fallbackSetting, minValue, Integer.MAX_VALUE, validator, properties); } public static Setting intSetting(String key, Setting fallbackSetting, int minValue, int maxValue, @@ -1352,8 +1346,7 @@ public static Setting longSetting(String key, Setting fallbackSettin public static Setting longSetting(String key, Setting fallbackSetting, long minValue, Validator validator, Property... properties) { - return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, - validator, properties); + return longSetting(key, fallbackSetting, minValue, Long.MAX_VALUE, validator, properties); } public static Setting longSetting(String key, Setting fallbackSetting, long minValue, long maxValue, @@ -1424,23 +1417,17 @@ public static Setting simpleString(String key, Property... properties) { return new Setting<>(key, s -> "", Function.identity(), properties); } - public static Setting simpleString(String key, - Validator validator, Property... properties) { - return new Setting<>(new SimpleKey(key), null, s -> "", Function.identity(), - validator, properties); + public static Setting simpleString(String key, Validator validator, Property... properties) { + return new Setting<>(new SimpleKey(key), null, s -> "", Function.identity(), validator, properties); } - public static Setting simpleString(String key, - Validator validator, Setting fallback, Property... properties) { - return new Setting<>(new SimpleKey(key), fallback, fallback::getRaw, Function.identity(), - validator, properties); + public static Setting simpleString(String key, Validator validator, Setting fallback, Property... properties) { + return new Setting<>(new SimpleKey(key), fallback, fallback::getRaw, Function.identity(), validator, properties); } - public static Setting simpleString(String key, String defaultValue, - Validator validator, Property... properties) { + public static Setting simpleString(String key, String defaultValue, Validator validator, Property... properties) { validator.validate(defaultValue); - return new Setting<>(new SimpleKey(key), null, s -> defaultValue, Function.identity(), - validator, properties); + return new Setting<>(new SimpleKey(key), null, s -> defaultValue, Function.identity(), validator, properties); } public static Setting simpleString(String key, Setting fallback, Property... properties) { @@ -1484,8 +1471,7 @@ public static Setting boolSetting(String key, Setting fallback } public static Setting boolSetting(String key, Setting fallbackSetting, - Validator validator, - Property... properties) { + Validator validator, Property... properties) { return new Setting<>(new SimpleKey(key), fallbackSetting, fallbackSetting::getRaw, b -> parseBoolean(b, key, isFiltered(properties)), validator, properties); @@ -1618,8 +1604,7 @@ public static Setting> listSetting( final Function singleValueParser, final Validator> validator, final Property... properties) { - return listSetting(key, null, singleValueParser, (s) -> defaultStringValue, - validator, properties); + return listSetting(key, null, singleValueParser, (s) -> defaultStringValue, validator, properties); } // TODO this one's two argument get is still broken @@ -1645,8 +1630,7 @@ public static Setting> listSetting( final Function> defaultStringValue, final Validator> validator, final Property... properties) { - return listSetting(key, null, singleValueParser, defaultStringValue, - validator, properties); + return listSetting(key, null, singleValueParser, defaultStringValue, validator, properties); } public static Setting> listSetting( @@ -1671,8 +1655,7 @@ public static Setting> listSetting( Function> parser = (s) -> parseableStringToList(s).stream().map(singleValueParser).collect(Collectors.toList()); - return new ListSetting<>(key, fallbackSetting, defaultStringValue, parser, - validator, properties); + return new ListSetting<>(key, fallbackSetting, defaultStringValue, parser, validator, properties); } private static List parseableStringToList(String parsableString) { @@ -1770,8 +1753,7 @@ public static Setting groupSetting(String key, Property... properties) } public static Setting groupSetting(String key, Consumer validator, Property... properties) { - return new GroupSetting(key, - validator, properties); + return new GroupSetting(key, validator, properties); } public static Setting timeSetting(