diff options
author | Valerij Fredriksen <valerijf@yahooinc.com> | 2022-06-10 12:58:40 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@yahooinc.com> | 2022-06-10 12:58:40 +0200 |
commit | 5b3738bfd02fbf0c24ba2920d4651258f55864f4 (patch) | |
tree | a89a72d87e54b1157296622fe3d847d92f6e1ce1 /node-admin | |
parent | 3d42f3aeac08fa79db2eb0adc2d707b4635d2b4a (diff) |
Throw custom exception and minor fixes
Diffstat (limited to 'node-admin')
4 files changed, 19 insertions, 15 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManager.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManager.java index b76c48c3839..00fe7198667 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManager.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManager.java @@ -21,7 +21,7 @@ public class NodeAgentContextManager implements NodeAgentContextSupplier, NodeAg private Instant nextContextAt; private boolean wantFrozen = false; private boolean isFrozen = true; - private boolean pendingInterrupt = false; + private boolean interrupted = false; private boolean isWaitingForNextContext = false; public NodeAgentContextManager(Clock clock, NodeAgentContext context) { @@ -60,19 +60,19 @@ public class NodeAgentContextManager implements NodeAgentContextSupplier, NodeAg } @Override - public NodeAgentContext nextContext() throws InterruptedException { + public NodeAgentContext nextContext() throws ContextSupplierInterruptedException { synchronized (monitor) { nextContext = null; // Reset any previous context and wait for the next one isWaitingForNextContext = true; - monitor.notify(); + monitor.notifyAll(); Duration untilNextContext = Duration.ZERO; - while (setAndGetIsFrozen(wantFrozen) || - nextContext == null || - (untilNextContext = Duration.between(Instant.now(), nextContextAt)).toMillis() > 0) { - if (pendingInterrupt) { - pendingInterrupt = false; - throw new InterruptedException("interrupt() was called before next context was scheduled"); - } + while (true) { + if (interrupted) throw new ContextSupplierInterruptedException(); + + if (!setAndGetIsFrozen(wantFrozen) && + nextContext != null && + (untilNextContext = Duration.between(Instant.now(), nextContextAt)).toMillis() <= 0) + break; try { monitor.wait(Math.max(untilNextContext.toMillis(), 0L)); // Wait until scheduler provides a new context @@ -95,7 +95,7 @@ public class NodeAgentContextManager implements NodeAgentContextSupplier, NodeAg @Override public void interrupt() { synchronized (monitor) { - pendingInterrupt = true; + interrupted = true; monitor.notifyAll(); } } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextSupplier.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextSupplier.java index a0b690b7ad4..c3ae5c7cc39 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextSupplier.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextSupplier.java @@ -9,10 +9,12 @@ public interface NodeAgentContextSupplier { /** * Blocks until the next context is ready * @return context - * @throws InterruptedException if {@link #interrupt()} was called before this method returned + * @throws ContextSupplierInterruptedException if {@link #interrupt()} was called before this method returned */ - NodeAgentContext nextContext() throws InterruptedException; + NodeAgentContext nextContext() throws ContextSupplierInterruptedException; /** Interrupts the thread(s) currently waiting in {@link #nextContext()} */ void interrupt(); + + class ContextSupplierInterruptedException extends RuntimeException { } } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentImpl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentImpl.java index dbab7270f08..09bc58bdaa2 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentImpl.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentImpl.java @@ -40,6 +40,7 @@ import java.util.function.Function; import java.util.logging.Level; import java.util.logging.Logger; +import static com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContextSupplier.ContextSupplierInterruptedException; import static com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentImpl.ContainerState.ABSENT; import static com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentImpl.ContainerState.STARTING; import static com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentImpl.ContainerState.UNKNOWN; @@ -141,7 +142,7 @@ public class NodeAgentImpl implements NodeAgent { while (!terminated.get()) { try { converge(contextSupplier.nextContext()); - } catch (InterruptedException ignored) { } + } catch (ContextSupplierInterruptedException ignored) { } } }); loopThread.setName("tick-" + initialContext.hostname()); diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java index 197b1c81b57..7c879a96e48 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java @@ -11,6 +11,7 @@ import java.util.Optional; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; +import static com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContextSupplier.ContextSupplierInterruptedException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; @@ -82,7 +83,7 @@ public class NodeAgentContextManagerTest { manager.interrupt(); async.awaitResult(); - assertEquals(Optional.of(InterruptedException.class), async.exception.map(Exception::getClass)); + assertEquals(Optional.of(ContextSupplierInterruptedException.class), async.exception.map(Exception::getClass)); assertFalse(async.response.isPresent()); } |