diff options
author | jonmv <venstad@gmail.com> | 2023-08-29 08:18:21 +0200 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2023-08-29 08:18:21 +0200 |
commit | 63c2710a5705d361d198908be67841a4d899a83a (patch) | |
tree | c6045c68fe3d62722f2d9e9ceb6b33637bae8130 /vespajlib | |
parent | a8e2f22f3b722543c1116f1a29845ef1093dcfe8 (diff) |
Improve readability
Diffstat (limited to 'vespajlib')
3 files changed, 31 insertions, 26 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java b/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java index fdb73e405c0..7fa591a88ba 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java @@ -48,9 +48,7 @@ public abstract class Maintainer implements Runnable { this.ignoreCollision = ignoreCollision; this.clock = clock; this.successFactorBaseline = successFactorBaseline; - var startedAt = clock.instant(); - Objects.requireNonNull(clusterHostnames); - Duration initialDelay = staggeredDelay(interval, startedAt, HostName.getLocalhost(), clusterHostnames) + Duration initialDelay = staggeredDelay(interval, HostName.getLocalhost(), clusterHostnames) .plus(Duration.ofSeconds(30)); // Let the system stabilize before maintenance service = new ScheduledThreadPoolExecutor(1, r -> new Thread(r, name() + "-worker")); service.scheduleAtFixedRate(this, initialDelay.toMillis(), interval.toMillis(), TimeUnit.MILLISECONDS); @@ -149,11 +147,12 @@ public abstract class Maintainer implements Runnable { } /** Returns the initial delay of this calculated from cluster index of the hostname of this node, and the maintainer name. */ - Duration staggeredDelay(Duration interval, Instant now, String hostname, List<String> clusterHostnames) { + Duration staggeredDelay(Duration interval, String hostname, List<String> clusterHostnames) { Objects.requireNonNull(clusterHostnames); if ( ! clusterHostnames.contains(hostname)) return interval; + Instant now = clock.instant(); long nodeOffset = clusterHostnames.indexOf(hostname) * interval.toMillis() / clusterHostnames.size(); long maintainerOffset = getClass().getName().hashCode() % interval.toMillis(); long totalOffset = nodeOffset + maintainerOffset; diff --git a/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/MaintainerTest.java b/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/MaintainerTest.java index 01e23c81971..a65af1e264e 100644 --- a/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/MaintainerTest.java +++ b/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/MaintainerTest.java @@ -2,6 +2,7 @@ package com.yahoo.concurrent.maintenance; import com.yahoo.concurrent.UncheckedTimeoutException; +import com.yahoo.test.ManualClock; import org.junit.Test; import java.time.Duration; @@ -23,29 +24,30 @@ public class MaintainerTest { public void staggering() { List<String> cluster = List.of("cfg1", "cfg2", "cfg3"); Duration interval = Duration.ofMillis(300); - Instant now = Instant.ofEpochMilli(1000); + ManualClock clock = new ManualClock(Instant.ofEpochMilli(1000)); + // ∠( ᐛ 」∠)_ - class MaintainerWithBestHashE extends TestMaintainer { MaintainerWithBestHashE() { super(null, jobControl, new TestJobMetrics()); } } - class MaintainerWithBestHashF extends TestMaintainer { MaintainerWithBestHashF() { super(null, jobControl, new TestJobMetrics()); } } - class MaintainerWithBestHashG extends TestMaintainer { MaintainerWithBestHashG() { super(null, jobControl, new TestJobMetrics()); } } - Maintainer maintainer = new MaintainerWithBestHashF(); - assertEquals(200, maintainer.staggeredDelay(interval, now, "cfg1", cluster).toMillis()); - assertEquals(299, new MaintainerWithBestHashE().staggeredDelay(interval, now, "cfg2", cluster).toMillis()); - assertEquals(0, maintainer.staggeredDelay(interval, now, "cfg2", cluster).toMillis()); - assertEquals(1, new MaintainerWithBestHashG().staggeredDelay(interval, now, "cfg2", cluster).toMillis()); - assertEquals(100, maintainer.staggeredDelay(interval, now, "cfg3", cluster).toMillis()); - - now = Instant.ofEpochMilli(1001); - assertEquals(199, maintainer.staggeredDelay(interval, now, "cfg1", cluster).toMillis()); - assertEquals(299, maintainer.staggeredDelay(interval, now, "cfg2", cluster).toMillis()); - assertEquals(99, maintainer.staggeredDelay(interval, now, "cfg3", cluster).toMillis()); - - now = Instant.ofEpochMilli(1101); - assertEquals(99, maintainer.staggeredDelay(interval, now, "cfg1", cluster).toMillis()); - assertEquals(199, maintainer.staggeredDelay(interval, now, "cfg2", cluster).toMillis()); - assertEquals(299, maintainer.staggeredDelay(interval, now, "cfg3", cluster).toMillis()); - - assertEquals(300, maintainer.staggeredDelay(interval, now, "cfg0", cluster).toMillis()); + class MaintainerWithBestHashE extends TestMaintainer { MaintainerWithBestHashE() { super(jobControl, new TestJobMetrics(), clock); } } + class MaintainerWithBestHashF extends TestMaintainer { MaintainerWithBestHashF() { super(jobControl, new TestJobMetrics(), clock); } } + class MaintainerWithBestHashG extends TestMaintainer { MaintainerWithBestHashG() { super(jobControl, new TestJobMetrics(), clock); } } + + assertEquals(200, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg1", cluster).toMillis()); + assertEquals(299, new MaintainerWithBestHashE().staggeredDelay(interval, "cfg2", cluster).toMillis()); + assertEquals( 0, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg2", cluster).toMillis()); + assertEquals( 1, new MaintainerWithBestHashG().staggeredDelay(interval, "cfg2", cluster).toMillis()); + assertEquals(100, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg3", cluster).toMillis()); + + clock.advance(Duration.ofMillis(1)); + assertEquals(199, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg1", cluster).toMillis()); + assertEquals(299, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg2", cluster).toMillis()); + assertEquals( 99, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg3", cluster).toMillis()); + + clock.advance(Duration.ofMillis(100)); + assertEquals( 99, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg1", cluster).toMillis()); + assertEquals(199, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg2", cluster).toMillis()); + assertEquals(299, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg3", cluster).toMillis()); + + assertEquals(300, new MaintainerWithBestHashF().staggeredDelay(interval, "cfg0", cluster).toMillis()); } @Test diff --git a/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/TestMaintainer.java b/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/TestMaintainer.java index 9e994312738..8c7ca1e18db 100644 --- a/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/TestMaintainer.java +++ b/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/TestMaintainer.java @@ -15,6 +15,10 @@ class TestMaintainer extends Maintainer { private double success = 1.0; private RuntimeException exceptionToThrow = null; + TestMaintainer(JobControl jobControl, JobMetrics jobMetrics, Clock clock) { + super(null, Duration.ofDays(1), clock, jobControl, jobMetrics, List.of(), false); + } + TestMaintainer(String name, JobControl jobControl, JobMetrics jobMetrics) { super(name, Duration.ofDays(1), Clock.systemUTC(), jobControl, jobMetrics, List.of(), false); } |