summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/testutils/Waiter.java
blob: 15587d6a3fb759f61508786ccd0236ecac84ab1d (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
125
126
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.testutils;

import java.util.logging.Level;
import com.yahoo.vdslib.state.ClusterState;
import com.yahoo.vdslib.state.Node;
import com.yahoo.vespa.clustercontroller.core.DummyVdsNode;
import com.yahoo.vespa.clustercontroller.core.FleetController;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

public interface Waiter {

    interface DataRetriever {
        Object getMonitor();
        FleetController getFleetController();
        List<DummyVdsNode> getDummyNodes();
        int getTimeoutMS();
    }

    ClusterState waitForState(String state) throws Exception;
    ClusterState waitForStateInSpace(String space, String state) throws Exception;
    ClusterState waitForStateInAllSpaces(String state) throws Exception;
    ClusterState waitForState(String state, int timeoutMS) throws Exception;
    ClusterState waitForStableSystem() throws Exception;
    ClusterState waitForStableSystem(int nodeCount) throws Exception;
    ClusterState waitForInitProgressPassed(Node n, double progress);
    ClusterState waitForClusterStateIncludingNodesWithMinUsedBits(int bitcount, int nodecount);
    void wait(WaitCondition c, WaitTask wt, int timeoutMS);

    class Impl implements Waiter {

        private static final Logger log = Logger.getLogger(Impl.class.getName());
        private final DataRetriever data;

        public Impl(DataRetriever data) {
            this.data = data;
        }

        // TODO refactor
        private ClusterState waitForState(String state, int timeoutMS, boolean checkAllSpaces, Set<String> checkSpaces) {
            LinkedList<DummyVdsNode> nodesToCheck = new LinkedList<>();
            for(DummyVdsNode node : data.getDummyNodes()) {
                if (node.isConnected()) nodesToCheck.add(node);
            }
            WaitCondition.StateWait swc = new WaitCondition.RegexStateMatcher(
                    state, data.getFleetController(), data.getMonitor())
                    .includeNotifyingNodes(nodesToCheck)
                    .checkAllSpaces(checkAllSpaces)
                    .checkSpaceSubset(checkSpaces);
            wait(swc, new WaitTask.StateResender(data.getFleetController()), timeoutMS);
            return swc.getCurrentState();
        }

        public ClusterState waitForState(String state) {
            return waitForState(state, data.getTimeoutMS());
        }
        public ClusterState waitForStateInAllSpaces(String state) {
            return waitForState(state, data.getTimeoutMS(), true, Collections.emptySet());
        }
        public ClusterState waitForStateInSpace(String space, String state) {
            return waitForState(state, data.getTimeoutMS(), false, Collections.singleton(space));
        }
        public ClusterState waitForState(String state, int timeoutMS) {
            return waitForState(state, timeoutMS, false, Collections.emptySet());
        }
        public ClusterState waitForStableSystem() {
            return waitForStableSystem(data.getDummyNodes().size() / 2);
        }
        public ClusterState waitForStableSystem(int nodeCount) {
            WaitCondition.StateWait swc = new WaitCondition.RegexStateMatcher("version:\\d+ distributor:"+nodeCount+" storage:"+nodeCount, data.getFleetController(), data.getMonitor()).includeNotifyingNodes(data.getDummyNodes());
            wait(swc, new WaitTask.StateResender(data.getFleetController()), data.getTimeoutMS());
            return swc.getCurrentState();
        }
        public ClusterState waitForInitProgressPassed(Node n, double progress) {
            WaitCondition.StateWait swc = new WaitCondition.InitProgressPassedMatcher(n, progress, data.getFleetController(), data.getMonitor());
            wait(swc, new WaitTask.StateResender(data.getFleetController()), data.getTimeoutMS());
            return swc.getCurrentState();
        }
        public ClusterState waitForClusterStateIncludingNodesWithMinUsedBits(int bitcount, int nodecount) {
            WaitCondition.StateWait swc = new WaitCondition.MinUsedBitsMatcher(bitcount, nodecount, data.getFleetController(), data.getMonitor());
            wait(swc, new WaitTask.StateResender(data.getFleetController()), data.getTimeoutMS());
            return swc.getCurrentState();
        }

        public final void wait(WaitCondition c, WaitTask wt, int timeoutMS) {
            log.log(LogLevel.INFO, "Waiting for " + c + (wt == null ? "" : " with wait task " + wt));
            final long startTime = System.currentTimeMillis();
            final long endTime = startTime + timeoutMS;
            String lastReason = null;
            while (true) {
                synchronized (data.getMonitor()) {
                    String reason = c.isConditionMet();
                    if (reason == null) {
                        log.log(LogLevel.INFO, "Condition met. Returning");
                        return;
                    }
                    if (lastReason == null || !lastReason.equals(reason)) {
                        log.log(LogLevel.INFO, "Wait condition not met: " + reason);
                        lastReason = reason;
                    }
                    try {
                        boolean allowWait = true;
                        if (wt != null) {
                            if (wt.performWaitTask()) {
                                data.getMonitor().notifyAll();
                                allowWait = false;
                            }
                        }
                        final long timeLeft = endTime - System.currentTimeMillis();
                        if (timeLeft <= 0) {
                            throw new IllegalStateException("Timed out waiting max " + timeoutMS + " ms for " + c + (wt == null ? "" : "\n  with wait task " + wt) + ",\n  reason: " + reason);
                        }
                        if (allowWait) data.getMonitor().wait(wt == null ? WaitTask.defaultTaskFrequencyMillis : Math.min(wt.getWaitTaskFrequencyInMillis(), timeLeft));
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
    }

}