aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/DummyStateApi.java
blob: a73e20b87553eac42d221396996db849617a76a4 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.staterestapi;

import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.MissingUnitException;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.OperationNotSupportedForUnitException;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.StateRestApiException;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.UnitStateRequest;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DummyStateApi implements StateRestAPI {
    private final DummyBackend backend;
    private Exception induceException;

    public DummyStateApi(DummyBackend backend) {
        this.backend = backend;
    }

    public void induceException(StateRestApiException e) {
        induceException = e;
    }
    public void induceException(RuntimeException e) {
        induceException = e;
    }

    public class SubUnitListImpl implements SubUnitList {
        private final Map<String, String> links = new LinkedHashMap<>();
        private final Map<String, UnitResponse> values = new LinkedHashMap<>();

        @Override
        public Map<String, String> getSubUnitLinks() { return links; }
        @Override
        public Map<String, UnitResponse> getSubUnits() { return values; }

        public void addUnit(DummyBackend.Cluster cluster, int recursive) {
            if (recursive == 0) {
                links.put(cluster.id, cluster.id);
            } else {
                values.put(cluster.id, getClusterState(cluster, recursive - 1));
            }
        }
        public void addUnit(DummyBackend.Node node, int recursive) {
            if (recursive == 0) {
                String link = node.clusterId + '/' + node.id;
                links.put(node.id, link);
            } else {
                values.put(node.id, getNodeState(node));
            }
        }
    }

    private UnitResponse getClusterList(final int recursive) {
        return new UnitResponse() {
            @Override
            public UnitAttributes getAttributes() { return null; }
            @Override
            public CurrentUnitState getCurrentState() { return null; }
            @Override
            public UnitMetrics getMetrics() { return null; }
            @Override
            public Map<String, SubUnitList> getSubUnits() {
                Map<String, SubUnitList> result = new LinkedHashMap<>();
                SubUnitListImpl subUnits = new SubUnitListImpl();
                result.put("cluster", subUnits);
                for (Map.Entry<String, DummyBackend.Cluster> e : backend.getClusters().entrySet()) {
                    subUnits.addUnit(e.getValue(), recursive);
                }
                return result;
            }
            @Override
            public DistributionStates getDistributionStates() {
                return null;
            }
        };
    }
    private UnitResponse getClusterState(final DummyBackend.Cluster cluster, final int recursive) {
        return new UnitResponse() {
            @Override
            public UnitAttributes getAttributes() { return null; }
            @Override
            public CurrentUnitState getCurrentState() { return null; }
            @Override
            public UnitMetrics getMetrics() { return null; }
            @Override
            public Map<String, SubUnitList> getSubUnits() {
                Map<String, SubUnitList> result = new LinkedHashMap<>();
                SubUnitListImpl subUnits = new SubUnitListImpl();
                result.put("node", subUnits);
                for (Map.Entry<String, DummyBackend.Node> e : cluster.nodes.entrySet()) {
                    subUnits.addUnit(e.getValue(), recursive);
                }
                return result;
            }
            @Override
            public DistributionStates getDistributionStates() {
                return null;
            }
        };
    }
    private UnitResponse getNodeState(final DummyBackend.Node node) {
        return new UnitResponse() {
            @Override
            public UnitAttributes getAttributes() {
                return new UnitAttributes() {
                    @Override
                    public Map<String, String> getAttributeValues() {
                        Map<String, String> attrs = new LinkedHashMap<>();
                        attrs.put("group", node.group);
                        return attrs;
                    }
                };
            }
            @Override
            public Map<String, SubUnitList> getSubUnits() { return null; }
            @Override
            public CurrentUnitState getCurrentState() {
                return new CurrentUnitState() {
                    @Override
                    public Map<String, UnitState> getStatePerType() {
                        Map<String, UnitState> m = new LinkedHashMap<>();
                        m.put("current", new UnitState() {
                            @Override
                            public String id() { return node.state; }
                            @Override
                            public String reason() { return node.reason; }
                            @Override
                            public String toString() { return id() +": " + reason(); }
                        });
                        return m;
                    }
                };
            }
            @Override
            public UnitMetrics getMetrics() {
                return new UnitMetrics() {
                    @Override
                    public Map<String, Long> getMetricMap() {
                        Map<String, Long> m = new LinkedHashMap<>();
                        m.put("doc-count", (long) node.docCount);
                        return m;
                    }
                };
            }
            @Override
            public DistributionStates getDistributionStates() {
                return null;
            }
        };

    }

    @Override
    public UnitResponse getState(UnitStateRequest request) throws StateRestApiException {
        checkForInducedException();
        List<String> path = request.getUnitPath();
        if (path.size() == 0) {
            return getClusterList(request.getRecursiveLevels());
        }
        DummyBackend.Cluster c = backend.getClusters().get(path.get(0));
        if (c == null) throw new MissingUnitException(path, 0);
        if (path.size() == 1) {
            return getClusterState(c, request.getRecursiveLevels());
        }
        DummyBackend.Node n = c.nodes.get(path.get(1));
        if (n == null) throw new MissingUnitException(path, 1);
        if (path.size() == 2) {
            return getNodeState(n);
        }
        throw new MissingUnitException(path, 3);
    }

    @Override
    public SetResponse setUnitState(SetUnitStateRequest request) throws StateRestApiException {
        checkForInducedException();
        List<String> path = request.getUnitPath();
        if (path.size() != 2) {
            throw new OperationNotSupportedForUnitException(
                    path, "You can only set states on nodes");
        }
        DummyBackend.Node n = null;
        DummyBackend.Cluster c = backend.getClusters().get(path.get(0));
        if (c != null) {
            n = c.nodes.get(path.get(1));
        }
        if (n == null) throw new MissingUnitException(path, 2);
        Map<String, UnitState> newState = request.getNewState();
        if (newState.size() != 1 || !newState.containsKey("current")) {
            throw new InvalidContentException("Only state of type 'current' is allowed to be set.");
        }
        n.state = newState.get("current").id();
        n.reason = newState.get("current").reason();
        String reason = String.format("DummyStateAPI %s call", request.getResponseWait().getName());
        return new SetResponse(reason, true);
    }

    private void checkForInducedException() throws StateRestApiException {
        if (induceException == null) return;
        Exception e = induceException;
        induceException = null;
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw (StateRestApiException) e;
    }
}