aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/DummyCommunicator.java
blob: 682e36254c91bc7ad3743562c6796bb6fb1ea5c6 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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 com.yahoo.collections.Pair;
import com.yahoo.vdslib.state.ClusterState;
import com.yahoo.vdslib.state.Node;
import com.yahoo.vdslib.state.NodeState;
import com.yahoo.vdslib.state.State;
import com.yahoo.vespa.clustercontroller.core.listeners.SlobrokListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class DummyCommunicator implements Communicator, NodeLookup {

    List<Node> newNodes;
    private final Timer timer;
    private boolean shouldDeferDistributorClusterStateAcks = false;
    private final List<Pair<Waiter<SetClusterStateRequest>, DummySetClusterStateRequest>> deferredClusterStateAcks = new ArrayList<>();

    void setShouldDeferDistributorClusterStateAcks(boolean shouldDeferDistributorClusterStateAcks) {
        this.shouldDeferDistributorClusterStateAcks = shouldDeferDistributorClusterStateAcks;
    }

    static class DummyGetNodeStateRequest extends GetNodeStateRequest {
        final Waiter<GetNodeStateRequest> waiter;

        DummyGetNodeStateRequest(NodeInfo nodeInfo, Waiter<GetNodeStateRequest> waiter) {
            super(nodeInfo);
            this.waiter = waiter;
        }

        @Override
        public void abort() {}
    }

    public static class DummySetClusterStateRequest extends SetClusterStateRequest {

        DummySetClusterStateRequest(NodeInfo nodeInfo, ClusterState state) {
            super(nodeInfo, state.getVersion());
        }

    }

    public static class DummyActivateClusterStateVersionRequest extends ActivateClusterStateVersionRequest {

        DummyActivateClusterStateVersionRequest(NodeInfo nodeInfo, int stateVersion) {
            super(nodeInfo, stateVersion);
        }

    }

    private final Map<Node, DummyGetNodeStateRequest> getNodeStateRequests = new TreeMap<>();

    DummyCommunicator(List<Node> nodeList, Timer timer) {
        this.newNodes = nodeList;
        this.timer = timer;
    }

    @Override
    public synchronized void getNodeState(NodeInfo node, Waiter<GetNodeStateRequest> waiter) {
        DummyGetNodeStateRequest req = new DummyGetNodeStateRequest(node, waiter);
        getNodeStateRequests.put(node.getNode(), req);
        node.setCurrentNodeStateRequest(req, timer.getCurrentTimeInMillis());
        notifyAll();
    }

    public void propagateOptions(final FleetControllerOptions options) {

    }

    public boolean setNodeState(Node node, State state, String description) {
        return setNodeState(node, new NodeState(node.getType(), state).setDescription(description), "");
    }

    public boolean setNodeState(Node node, NodeState state, String hostInfo) {
        DummyGetNodeStateRequest req = getNodeStateRequests.remove(node);

        if (req == null) {
            throw new IllegalStateException("Premature set node state - wait for fleet controller to request first: " + node);
        }

        GetNodeStateRequest.Reply reply = new GetNodeStateRequest.Reply(state.serialize(), hostInfo);
        req.setReply(reply);

        req.waiter.done(req);

        return true;
    }

    @Override
    public void setSystemState(ClusterStateBundle stateBundle, NodeInfo node, Waiter<SetClusterStateRequest> waiter) {
        ClusterState baselineState = stateBundle.getBaselineClusterState();
        DummySetClusterStateRequest req = new DummySetClusterStateRequest(node, baselineState);
        node.setClusterStateVersionBundleSent(stateBundle);
        req.setReply(new SetClusterStateRequest.Reply());
        if (node.isStorage() || !shouldDeferDistributorClusterStateAcks) {
            waiter.done(req);
        } else {
            deferredClusterStateAcks.add(new Pair<>(waiter, req));
        }
    }

    @Override
    public void activateClusterStateVersion(int clusterStateVersion, NodeInfo node, Waiter<ActivateClusterStateVersionRequest> waiter) {
        var req = new DummyActivateClusterStateVersionRequest(node, clusterStateVersion);
        req.setReply(ActivateClusterStateVersionRequest.Reply.withActualVersion(clusterStateVersion));
        waiter.done(req);
    }

    void sendAllDeferredDistributorClusterStateAcks() {
        deferredClusterStateAcks.forEach(reqAndWaiter -> reqAndWaiter.getFirst().done(reqAndWaiter.getSecond()));
        deferredClusterStateAcks.clear();
    }

    void sendPartialDeferredDistributorClusterStateAcks() {
        if (deferredClusterStateAcks.isEmpty()) {
            throw new IllegalStateException("Tried to send partial ACKs with no ACKs deferred");
        }
        List<Pair<Waiter<SetClusterStateRequest>, DummySetClusterStateRequest>> toAck =
                deferredClusterStateAcks.subList(0, deferredClusterStateAcks.size() - 1);
        toAck.forEach(reqAndWaiter -> reqAndWaiter.getFirst().done(reqAndWaiter.getSecond()));
        deferredClusterStateAcks.removeAll(toAck);
    }

    @Override
    public void shutdown() {
    }

    @Override
    public boolean updateCluster(ContentCluster cluster, SlobrokListener listener) {
        if (newNodes != null) {
            List<Node> tmp = newNodes;

            for (Node node : tmp)
                cluster.clusterInfo().setRpcAddress(node, "foo");

            for (NodeInfo info : cluster.getNodeInfos()) {
                if (!tmp.contains(info.getNode())) {
                    info.markRpcAddressOutdated(timer);
                    listener.handleMissingNode(info);
                }
            }

            newNodes = null;
            return true;
        }

        return false;
    }

}