diff --git a/server/src/main/java/org/elasticsearch/monitor/jvm/JvmGcMonitorService.java b/server/src/main/java/org/elasticsearch/monitor/jvm/JvmGcMonitorService.java index c6a554e6374d7..5d68f0308ba43 100644 --- a/server/src/main/java/org/elasticsearch/monitor/jvm/JvmGcMonitorService.java +++ b/server/src/main/java/org/elasticsearch/monitor/jvm/JvmGcMonitorService.java @@ -177,6 +177,10 @@ private static TimeValue getValidThreshold(Settings settings, String key, String if (threshold == null) { throw new IllegalArgumentException("missing gc_threshold for [" + getThresholdName(key, level) + "]"); + } else if (threshold.nanos() < 0) { + final String settingValue = settings.get(level); + throw new IllegalArgumentException("invalid gc_threshold [" + getThresholdName(key, level) + "] value [" + + settingValue + "]: value cannot be negative"); } return threshold; diff --git a/server/src/test/java/org/elasticsearch/monitor/jvm/JvmGcMonitorServiceSettingsTests.java b/server/src/test/java/org/elasticsearch/monitor/jvm/JvmGcMonitorServiceSettingsTests.java index 4e4ae1cb664c0..8f53888baca9b 100644 --- a/server/src/test/java/org/elasticsearch/monitor/jvm/JvmGcMonitorServiceSettingsTests.java +++ b/server/src/test/java/org/elasticsearch/monitor/jvm/JvmGcMonitorServiceSettingsTests.java @@ -62,7 +62,7 @@ public void testDisabledSetting() throws InterruptedException { public void testNegativeSetting() throws InterruptedException { String collector = randomAlphaOfLength(5); - final String timeValue = "-" + randomTimeValue(); + final String timeValue = "-" + randomTimeValue(2,1000); // -1 is handled separately Settings settings = Settings.builder().put("monitor.jvm.gc.collector." + collector + ".warn", timeValue).build(); execute(settings, (command, interval, name) -> null, e -> { assertThat(e, instanceOf(IllegalArgumentException.class)); @@ -71,6 +71,17 @@ public void testNegativeSetting() throws InterruptedException { }, true, null); } + public void testNegativeOneSetting() throws InterruptedException { + String collector = randomAlphaOfLength(5); + final String timeValue = "-1" + randomFrom("", "d", "h", "m", "s", "ms", "nanos"); + Settings settings = Settings.builder().put("monitor.jvm.gc.collector." + collector + ".warn", timeValue).build(); + execute(settings, (command, interval, name) -> null, e -> { + assertThat(e, instanceOf(IllegalArgumentException.class)); + assertThat(e.getMessage(), equalTo("invalid gc_threshold [monitor.jvm.gc.collector." + collector + ".warn] " + + "value [" + timeValue + "]: value cannot be negative")); + }, true, null); + } + public void testMissingSetting() throws InterruptedException { String collector = randomAlphaOfLength(5); Set> entries = new HashSet<>();