diff options
Diffstat (limited to 'node-admin')
3 files changed, 116 insertions, 0 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 new file mode 100644 index 00000000000..245652ed927 --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManager.java @@ -0,0 +1,77 @@ +// Copyright 2019 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.node.admin.nodeagent; + +import java.util.Objects; + +/** + * @author freva + */ +public class NodeAgentContextManager implements NodeAgentContextSupplier, NodeAgentScheduler { + private final Object monitor = new Object(); + private NodeAgentContext currentContext; + private NodeAgentContext nextContext; + private boolean wantFrozen = false; + private boolean isFrozen = true; + private boolean pendingInterrupt = false; + + public NodeAgentContextManager(NodeAgentContext context) { + currentContext = context; + } + + @Override + public void scheduleTickWith(NodeAgentContext context) { + synchronized (monitor) { + nextContext = Objects.requireNonNull(context); + monitor.notifyAll(); + } + } + + @Override + public boolean setFrozen(boolean frozen) { + synchronized (monitor) { + if (wantFrozen != frozen) { + wantFrozen = frozen; + monitor.notifyAll(); + } + + return isFrozen == frozen; + } + } + + @Override + public NodeAgentContext nextContext() throws InterruptedException { + synchronized (monitor) { + isFrozen = true; + while (nextContext == null) { + if (pendingInterrupt) { + pendingInterrupt = false; + throw new InterruptedException("interrupt() was called before next context was scheduled"); + } + + try { + monitor.wait(); + } catch (InterruptedException ignored) { } + } + isFrozen = false; + + currentContext = nextContext; + nextContext = null; + return currentContext; + } + } + + @Override + public NodeAgentContext currentContext() { + synchronized (monitor) { + return currentContext; + } + } + + @Override + public void interrupt() { + synchronized (monitor) { + pendingInterrupt = true; + monitor.notifyAll(); + } + } +}
\ No newline at end of file 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 new file mode 100644 index 00000000000..1fc730a3cb0 --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextSupplier.java @@ -0,0 +1,21 @@ +// Copyright 2019 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.node.admin.nodeagent; + +/** + * @author freva + */ +public interface NodeAgentContextSupplier { + + /** + * Blocks until the next context is ready + * @return context + * @throws InterruptedException if {@link #interrupt()} was called before this method returned + */ + NodeAgentContext nextContext() throws InterruptedException; + + /** @return the last context returned by {@link #nextContext()} or a default value */ + NodeAgentContext currentContext(); + + /** Interrupts the thread(s) currently waiting in {@link #nextContext()} */ + void interrupt(); +} diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentScheduler.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentScheduler.java new file mode 100644 index 00000000000..3806c3d6bf3 --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentScheduler.java @@ -0,0 +1,18 @@ +// Copyright 2019 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.node.admin.nodeagent; + +/** + * @author freva + */ +public interface NodeAgentScheduler { + + /** Schedule a tick for NodeAgent to run with the given NodeAgentContext */ + void scheduleTickWith(NodeAgentContext context); + + /** + * Will eventually freeze/unfreeze the node agent + * @param frozen whether node agent should be frozen + * @return True if node agent has converged to the desired state + */ + boolean setFrozen(boolean frozen); +} |