aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManager.java
blob: 54d59aac3c8760805924c0aa58e916835ca52055 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright Yahoo. 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 com.yahoo.jdisc.Timer;

import java.time.Duration;
import java.time.Instant;
import java.util.Objects;

/**
 * This class should be used by exactly 2 threads, 1 for each interface it implements.
 *
 * @author freva
 */
public class NodeAgentContextManager implements NodeAgentContextSupplier, NodeAgentScheduler {

    private final Object monitor = new Object();
    private final Timer timer;

    private NodeAgentContext currentContext;
    private NodeAgentContext nextContext;
    private Instant nextContextAt;
    private boolean wantFrozen = false;
    private boolean isFrozen = true;
    private boolean interrupted = false;
    private boolean isWaitingForNextContext = false;

    public NodeAgentContextManager(Timer timer, NodeAgentContext context) {
        this.timer = timer;
        this.currentContext = context;
    }

    @Override
    public void scheduleTickWith(NodeAgentContext context, Instant at) {
        synchronized (monitor) {
            nextContext = Objects.requireNonNull(context);
            nextContextAt = Objects.requireNonNull(at);
            monitor.notifyAll(); // Notify of new context
        }
    }

    @Override
    public boolean setFrozen(boolean frozen, Duration timeout) {
        synchronized (monitor) {
            if (wantFrozen != frozen) {
                wantFrozen = frozen;
                monitor.notifyAll(); // Notify the supplier of the wantFrozen change
            }

            boolean successful;
            long remainder;
            long end = timer.currentTime().plus(timeout).toEpochMilli();
            while (!(successful = isFrozen == frozen) && (remainder = end - timer.currentTimeMillis()) > 0) {
                try {
                    monitor.wait(remainder); // Wait with timeout until the supplier is has reached wanted frozen state
                } catch (InterruptedException ignored) { }
            }

            return successful;
        }
    }

    @Override
    public NodeAgentContext nextContext() throws ContextSupplierInterruptedException {
        synchronized (monitor) {
            nextContext = null; // Reset any previous context and wait for the next one
            isWaitingForNextContext = true;
            monitor.notifyAll();
            Duration untilNextContext = Duration.ZERO;
            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
                } catch (InterruptedException ignored) { }
            }

            isWaitingForNextContext = false;
            currentContext = nextContext;
            return currentContext;
        }
    }

    @Override
    public NodeAgentContext currentContext() {
        synchronized (monitor) {
            return currentContext;
        }
    }

    @Override
    public void interrupt() {
        synchronized (monitor) {
            interrupted = true;
            monitor.notifyAll();
        }
    }

    private boolean setAndGetIsFrozen(boolean isFrozen) {
        synchronized (monitor) {
            if (this.isFrozen != isFrozen) {
                this.isFrozen = isFrozen;
                monitor.notifyAll(); // Notify the scheduler of the isFrozen change
            }
            return this.isFrozen;
        }
    }

    /** FOR TESTING ONLY */
    void waitUntilWaitingForNextContext() {
        synchronized (monitor) {
            while (!isWaitingForNextContext) {
                try {
                    monitor.wait();
                } catch (InterruptedException ignored) { }
            }
        }
    }
}