summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/StateGatherTest.java
blob: 041f4e4052d8d2cf7d4d7ae766442421b39dee55 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import java.util.logging.Level;
import org.junit.Test;

import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;

import static org.junit.Assert.assertEquals;

public class StateGatherTest extends FleetControllerTest {

    public static Logger log = Logger.getLogger(StateGatherTest.class.getName());

    private String getGetNodeStateReplyCounts(DummyVdsNode node) {
        StringBuilder sb = new StringBuilder();
        sb.append("timedout ").append(node.timedOutStateReplies)
          .append(", outdated ").append(node.outdatedStateReplies)
          .append(", immediate ").append(node.immediateStateReplies)
          .append(", setstate ").append(node.setNodeStateReplies)
          .append(", pending ").append(node.getPendingNodeStateCount());
        return sb.toString();
    }

    @Test
    public void testAlwaysHavePendingGetNodeStateRequestTowardsNodes() throws Exception {
        Logger.getLogger(NodeStateGatherer.class.getName()).setLevel(Level.FINEST);
        startingTest("StateGatherTest::testOverlappingGetNodeStateRequests");
        FleetControllerOptions options = defaultOptions("mycluster");
        options.nodeStateRequestTimeoutMS = 10 * 60 * 1000;
        // Force actual message timeout to be lower than request timeout.
        options.nodeStateRequestTimeoutEarliestPercentage = 80;
        options.nodeStateRequestTimeoutLatestPercentage = 80;
        setUpFleetController(true, options);
        String[] connectionSpecs = new String[1];
        connectionSpecs[0] = "tcp/localhost:" + slobrok.port();
        DummyVdsNodeOptions dummyOptions = new DummyVdsNodeOptions();
        DummyVdsNode dnode = new DummyVdsNode(timer, dummyOptions, connectionSpecs, this.options.clusterName, true, 0);
        DummyVdsNode snode = new DummyVdsNode(timer, dummyOptions, connectionSpecs, this.options.clusterName, false, 0);
        dnode.connect();
        snode.connect();

        waitUntilPendingGetNodeState(dnode, snode);

        assertEquals("timedout 0, outdated 0, immediate 1, setstate 0, pending 1", getGetNodeStateReplyCounts(dnode));
        assertEquals("timedout 0, outdated 0, immediate 1, setstate 0, pending 1", getGetNodeStateReplyCounts(snode));

        waitForCompleteCycle();
        timer.advanceTime(9 * 60 * 1000); // Requests should have timed out on nodes (8 min timeout).

        waitUntilTimedOutGetNodeState(dnode, snode);
        waitForCompleteCycle(); // Send new node state requests.
        waitUntilPendingGetNodeState(dnode, snode);

        assertEquals("timedout 1, outdated 0, immediate 1, setstate 0, pending 1", getGetNodeStateReplyCounts(dnode));
        assertEquals("timedout 1, outdated 0, immediate 1, setstate 0, pending 1", getGetNodeStateReplyCounts(snode));
    }

    private void waitUntilTimedOutGetNodeState(DummyVdsNode dnode, DummyVdsNode snode) throws TimeoutException {
        long timeout = System.currentTimeMillis() + timeoutMS;
        synchronized (timer) {
            while (dnode.timedOutStateReplies != 1 || snode.timedOutStateReplies != 1) {
                if (System.currentTimeMillis() > timeout) {
                    throw new TimeoutException("Did not get to have one timed out within timeout of " + timeoutMS + " ms"
                            + ", " + getGetNodeStateReplyCounts(dnode) + ", " + getGetNodeStateReplyCounts(snode));
                }
                try{ timer.wait(1); } catch (InterruptedException e) { /* ignore */ }
            }
        }
    }

    private void waitUntilPendingGetNodeState(DummyVdsNode dnode, DummyVdsNode snode) throws TimeoutException {
        long timeout = System.currentTimeMillis() + timeoutMS;
        while (dnode.getPendingNodeStateCount() != 1 || snode.getPendingNodeStateCount() != 1) {
            if (System.currentTimeMillis() > timeout) throw new TimeoutException("Did not get to have one pending within timeout of " + timeoutMS + " ms");
            try{ Thread.sleep(1); } catch (InterruptedException e) { /* ignore */ }
        }
    }

}