From 96975ca9cd77470c6e7082a82c0f9638e12294f6 Mon Sep 17 00:00:00 2001 From: Drew Baugher <46505179+dbbaughe@users.noreply.github.com> Date: Mon, 1 Nov 2021 15:00:48 -0700 Subject: [PATCH] Fixes possible negative jitter Signed-off-by: Drew Baugher <46505179+dbbaughe@users.noreply.github.com> --- .../org/opensearch/jobscheduler/scheduler/JobScheduler.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/jobscheduler/scheduler/JobScheduler.java b/src/main/java/org/opensearch/jobscheduler/scheduler/JobScheduler.java index bcf2bf57..8e7bc1fa 100644 --- a/src/main/java/org/opensearch/jobscheduler/scheduler/JobScheduler.java +++ b/src/main/java/org/opensearch/jobscheduler/scheduler/JobScheduler.java @@ -167,7 +167,10 @@ boolean reschedule(ScheduledJobParameter jobParameter, JobSchedulingInfo jobInfo double jitter = jobParameter.getJitter() == null ? 0d : jobParameter.getJitter(); jitter = jitter > jitterLimit ? jitterLimit : jitter; jitter = jitter < 0 ? 0 : jitter; - long jitterMillis = Math.round(Randomness.get().nextLong() % interval.toMillis() * jitter); + long randomLong = Randomness.get().nextLong(); + if (randomLong == Long.MIN_VALUE) randomLong += 17; // to ensure the * -1 below doesn't fail to change to positive + long randomPositiveLong = randomLong < 0 ? randomLong * -1 : randomLong; + long jitterMillis = Math.round(randomPositiveLong % interval.toMillis() * jitter); if (jitter > 0) { log.info("Will delay {} miliseconds for next execution of job {}", jitterMillis, jobParameter.getName()); }