diff options
author | Håkon Hallingstad <hakon@verizonmedia.com> | 2020-10-10 21:31:28 +0200 |
---|---|---|
committer | Håkon Hallingstad <hakon@verizonmedia.com> | 2020-10-10 21:31:28 +0200 |
commit | b722d66b6347bc42254d9014f320413b187f7ae6 (patch) | |
tree | a1d7d6e0d01c8fac92dbc51e2d49bd81aa18f4e3 /container-core/src/main/java/com | |
parent | 805abcff704b2d2f8ae24dcebdad500cc7a658d3 (diff) |
Avoid mock thread factory and simplify construction
Diffstat (limited to 'container-core/src/main/java/com')
-rw-r--r-- | container-core/src/main/java/com/yahoo/container/handler/VipStatus.java | 2 | ||||
-rw-r--r-- | container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java | 61 |
2 files changed, 27 insertions, 36 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/handler/VipStatus.java b/container-core/src/main/java/com/yahoo/container/handler/VipStatus.java index e1b5b769906..876d05b7be9 100644 --- a/container-core/src/main/java/com/yahoo/container/handler/VipStatus.java +++ b/container-core/src/main/java/com/yahoo/container/handler/VipStatus.java @@ -50,7 +50,7 @@ public class VipStatus { /** For testing */ public VipStatus(QrSearchersConfig dispatchers, ClustersStatus clustersStatus) { - this(dispatchers, new VipStatusConfig.Builder().build(), clustersStatus, new StateMonitor()); + this(dispatchers, new VipStatusConfig.Builder().build(), clustersStatus, StateMonitor.createForTesting()); } @Inject diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java index 066118294a0..ccd0864b3ab 100644 --- a/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java @@ -9,6 +9,7 @@ import com.yahoo.jdisc.application.MetricConsumer; import com.yahoo.jdisc.core.SystemTimer; import java.util.Map; +import java.util.Optional; import java.util.TreeSet; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Executors; @@ -32,7 +33,7 @@ public class StateMonitor extends AbstractComponent { public enum Status {up, down, initializing} private final CopyOnWriteArrayList<StateMetricConsumer> consumers = new CopyOnWriteArrayList<>(); - private final ScheduledExecutorService executor; + private final Optional<ScheduledExecutorService> executor; private final Timer timer; private final long snapshotIntervalMs; private volatile long lastSnapshotTimeMs; @@ -40,41 +41,28 @@ public class StateMonitor extends AbstractComponent { private volatile Status status; private final TreeSet<String> valueNames = new TreeSet<>(); - /** For testing */ - public StateMonitor() { - this(new HealthMonitorConfig.Builder().build(), new SystemTimer()); - } - @Inject public StateMonitor(HealthMonitorConfig config, Timer timer) { - this(config, - timer, - runnable -> { - Thread thread = new Thread(runnable, "StateMonitor"); - thread.setDaemon(true); - return thread; - }); + this(config, timer, runnable -> { + Thread thread = new Thread(runnable, "StateMonitor"); + thread.setDaemon(true); + return thread; + }); } - StateMonitor(HealthMonitorConfig config, Timer timer, ThreadFactory threadFactory) { - this((long)(config.snapshot_interval() * TimeUnit.SECONDS.toMillis(1)), - Status.valueOf(config.initialStatus()), - timer, threadFactory, true); + public static StateMonitor createForTesting() { + return new StateMonitor(new HealthMonitorConfig.Builder().build(), new SystemTimer()); } - /* Public for testing only */ - public StateMonitor(long snapshotIntervalMS, Status status, Timer timer, ThreadFactory threadFactory, - boolean start) { + /** Non-private only for unit testing this class. */ + StateMonitor(HealthMonitorConfig config, Timer timer, ThreadFactory threadFactory) { this.timer = timer; - this.snapshotIntervalMs = snapshotIntervalMS; + this.snapshotIntervalMs = (long)(config.snapshot_interval() * TimeUnit.SECONDS.toMillis(1)); this.lastSnapshotTimeMs = timer.currentTimeMillis(); - this.status = status; - this.executor = Executors.newSingleThreadScheduledExecutor(threadFactory); - - if (start) { - executor.scheduleAtFixedRate(this::updateSnapshot, snapshotIntervalMS, - snapshotIntervalMS, TimeUnit.MILLISECONDS); - } + this.status = Status.valueOf(config.initialStatus()); + this.executor = Optional.ofNullable(threadFactory).map(Executors::newSingleThreadScheduledExecutor); + this.executor.ifPresent(exec -> exec.scheduleAtFixedRate(this::updateSnapshot, snapshotIntervalMs, + snapshotIntervalMs, TimeUnit.MILLISECONDS)); } /** Returns a metric consumer for jDisc which will write metrics back to this */ @@ -136,13 +124,16 @@ public class StateMonitor extends AbstractComponent { @Override public void deconstruct() { - executor.shutdown(); - try { - executor.awaitTermination(5, TimeUnit.SECONDS); - } catch (InterruptedException e) { } + executor.ifPresent(exec -> { + exec.shutdown(); + try { + exec.awaitTermination(5, TimeUnit.SECONDS); + } catch (InterruptedException e) { + } - if (!executor.isTerminated()) { - log.warning("StateMonitor failed to terminate within 5 seconds of interrupt signal. Ignoring."); - } + if (!exec.isTerminated()) { + log.warning("StateMonitor failed to terminate within 5 seconds of interrupt signal. Ignoring."); + } + }); } } |