diff options
author | Martin Polden <mpolden@mpolden.no> | 2021-01-08 10:59:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-08 10:59:04 +0100 |
commit | acde14de1286c4ef663ee5b26be768c46dbdcbbf (patch) | |
tree | ac51923810812a1237d103a376d278280c93a020 /vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java | |
parent | 55599006c8693209cfb7109329cbaacc56f2fbf1 (diff) |
Revert "Revert "Count lock timeout as unsuccessful run""
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java | 42 |
1 files changed, 16 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 9fb5172ab0a..daad1f8fb4b 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java @@ -1,7 +1,6 @@ // Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.concurrent.maintenance; -import com.google.common.util.concurrent.UncheckedTimeoutException; import com.yahoo.net.HostName; import java.time.Duration; @@ -33,15 +32,15 @@ public abstract class Maintainer implements Runnable { private final ScheduledExecutorService service; private final AtomicBoolean shutDown = new AtomicBoolean(); - public Maintainer(String name, Duration interval, Instant startedAt, JobControl jobControl, JobMetrics jobMetrics, List<String> clusterHostnames) { - this(name, interval, staggeredDelay(interval, startedAt, HostName.getLocalhost(), clusterHostnames), jobControl, jobMetrics); - } - - public Maintainer(String name, Duration interval, Duration initialDelay, JobControl jobControl, JobMetrics jobMetrics) { + public Maintainer(String name, Duration interval, Instant startedAt, JobControl jobControl, + JobMetrics jobMetrics, List<String> clusterHostnames) { this.name = name; this.interval = requireInterval(interval); this.jobControl = Objects.requireNonNull(jobControl); this.jobMetrics = Objects.requireNonNull(jobMetrics); + Objects.requireNonNull(startedAt); + Objects.requireNonNull(clusterHostnames); + Duration initialDelay = staggeredDelay(interval, startedAt, HostName.getLocalhost(), clusterHostnames); service = new ScheduledThreadPoolExecutor(1, r -> new Thread(r, name() + "-worker")); service.scheduleAtFixedRate(this, initialDelay.toMillis(), interval.toMillis(), TimeUnit.MILLISECONDS); jobControl.started(name(), this); @@ -49,17 +48,7 @@ public abstract class Maintainer implements Runnable { @Override public void run() { - log.log(Level.FINE, () -> "Running " + this.getClass().getSimpleName()); - try { - if (jobControl.isActive(name())) { - lockAndMaintain(); - } - } catch (UncheckedTimeoutException ignored) { - // Another actor is running this job - } catch (Throwable e) { - log.log(Level.WARNING, this + " failed. Will retry in " + interval.toMinutes() + " minutes", e); - } - log.log(Level.FINE, () -> "Finished " + this.getClass().getSimpleName()); + lockAndMaintain(false); } /** Starts shutdown of this, typically by shutting down executors. {@link #awaitShutdown()} waits for shutdown to complete. */ @@ -92,17 +81,18 @@ public abstract class Maintainer implements Runnable { protected Duration interval() { return interval; } /** Run this while holding the job lock */ - @SuppressWarnings("unused") - public final void lockAndMaintain() { + public final void lockAndMaintain(boolean force) { + if (!force && !jobControl.isActive(name())) return; + log.log(Level.FINE, () -> "Running " + this.getClass().getSimpleName()); + jobMetrics.recordRunOf(name()); try (var lock = jobControl.lockJob(name())) { - try { - jobMetrics.recordRunOf(name()); - if (maintain()) jobMetrics.recordSuccessOf(name()); - } finally { - // Always forward metrics - jobMetrics.forward(name()); - } + if (maintain()) jobMetrics.recordSuccessOf(name()); + } catch (Throwable e) { + log.log(Level.WARNING, this + " failed. Will retry in " + interval.toMinutes() + " minutes", e); + } finally { + jobMetrics.forward(name()); } + log.log(Level.FINE, () -> "Finished " + this.getClass().getSimpleName()); } /** Returns the simple name of this job */ |