aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/restapiv2/StateRestApiTest.java
blob: 1ad5f6828b7ba6006e67b706fe113911132678d8 (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
211
212
213
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.restapiv2;

import com.yahoo.vdslib.distribution.ConfiguredNode;
import com.yahoo.vdslib.distribution.Distribution;
import com.yahoo.vdslib.state.ClusterState;
import com.yahoo.vdslib.state.Node;
import com.yahoo.vdslib.state.NodeState;
import com.yahoo.vdslib.state.NodeType;
import com.yahoo.vdslib.state.State;
import com.yahoo.vespa.clustercontroller.core.AnnotatedClusterState;
import com.yahoo.vespa.clustercontroller.core.ClusterStateBundle;
import com.yahoo.vespa.clustercontroller.core.ContentCluster;
import com.yahoo.vespa.clustercontroller.core.NodeInfo;
import com.yahoo.vespa.clustercontroller.core.RemoteClusterControllerTaskScheduler;
import com.yahoo.vespa.clustercontroller.core.hostinfo.HostInfo;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.StateRestAPI;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.UnitStateRequest;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.server.JsonWriter;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

public abstract class StateRestApiTest {

    ClusterControllerMock books;
    ClusterControllerMock music;
    StateRestAPI restAPI;
    JsonWriter jsonWriter = new JsonWriter();
    Map<Integer, ClusterControllerStateRestAPI.Socket> ccSockets;

    public static class StateRequest implements UnitStateRequest {
        private final List<String> path;
        private final int recursive;

        StateRequest(String req, int recursive) {
            path = req.isEmpty() ? List.of() : List.of(req.split("/"));
            this.recursive = recursive;
        }
        @Override
        public int getRecursiveLevels() { return recursive;
        }
        @Override
        public List<String> getUnitPath() { return path; }
    }

    protected void setUp(boolean dontInitializeNode2) {
        Distribution distribution = new Distribution(getSimpleGroupConfig(2, 10));
        jsonWriter.setDefaultPathPrefix("/cluster/v2");
        {
            Set<ConfiguredNode> nodes = toNodes(0, 1, 2, 3);
            ContentCluster cluster = new ContentCluster("books", nodes, distribution);
            initializeCluster(cluster, nodes);
            AnnotatedClusterState baselineState = AnnotatedClusterState.withoutAnnotations(ClusterState.stateFromString("distributor:4 storage:4"));
            Map<String, AnnotatedClusterState> bucketSpaceStates = new HashMap<>();
            bucketSpaceStates.put("default", AnnotatedClusterState.withoutAnnotations(ClusterState.stateFromString("distributor:4 storage:4 .3.s:m")));
            bucketSpaceStates.put("global", baselineState);
            books = new ClusterControllerMock(cluster, baselineState.getClusterState(),
                    ClusterStateBundle.of(baselineState, bucketSpaceStates), 0, 0);
        }
        {
            Set<ConfiguredNode> nodes = toNodes(1, 2, 3, 5, 7);
            Set<ConfiguredNode> nodesInSlobrok = toNodes(1, 3, 5, 7);

            ContentCluster cluster = new ContentCluster("music", nodes, distribution);
            if (dontInitializeNode2) {
                // TODO: this skips initialization of node 2 to fake that it is not answering
                // which really leaves us in an illegal state
                initializeCluster(cluster, nodesInSlobrok);
            }
            else {
                initializeCluster(cluster, nodes);
            }
            AnnotatedClusterState baselineState = AnnotatedClusterState.withoutAnnotations(ClusterState.stateFromString("distributor:8 .0.s:d .2.s:d .4.s:d .6.s:d "
                                                + "storage:8 .0.s:d .2.s:d .4.s:d .6.s:d"));
            music = new ClusterControllerMock(cluster, baselineState.getClusterState(),
                    ClusterStateBundle.ofBaselineOnly(baselineState), 0, 0);
        }
        ccSockets = new TreeMap<>();
        ccSockets.put(0, new ClusterControllerStateRestAPI.Socket("localhost", 80));
        restAPI = new ClusterControllerStateRestAPI(() -> {
            Map<String, RemoteClusterControllerTaskScheduler> fleetControllers = new LinkedHashMap<>();
            fleetControllers.put(books.context.cluster.getName(), books);
            fleetControllers.put(music.context.cluster.getName(), music);
            return fleetControllers;
        }, ccSockets);
    }

    protected void setUpMusicGroup(int nodeCount, String node1StateString) {
        books = null;
        Distribution distribution = new Distribution(getSimpleGroupConfig(2, nodeCount));
        jsonWriter.setDefaultPathPrefix("/cluster/v2");
        ContentCluster cluster = new ContentCluster("music", distribution.getNodes(), distribution);
        initializeCluster(cluster, distribution.getNodes());
        AnnotatedClusterState baselineState = AnnotatedClusterState
                .withoutAnnotations(ClusterState.stateFromString("distributor:" + nodeCount + " storage:" + nodeCount + node1StateString));
        Map<String, AnnotatedClusterState> bucketSpaceStates = new HashMap<>();
        bucketSpaceStates.put("default", AnnotatedClusterState
                .withoutAnnotations(ClusterState.stateFromString("distributor:" + nodeCount + " storage:" + nodeCount)));
        bucketSpaceStates.put("global", baselineState);
        music = new ClusterControllerMock(cluster, baselineState.getClusterState(),
                ClusterStateBundle.of(baselineState, bucketSpaceStates), 0, 0);
        ccSockets = new TreeMap<>();
        ccSockets.put(0, new ClusterControllerStateRestAPI.Socket("localhost", 80));
        restAPI = new ClusterControllerStateRestAPI(() -> {
            Map<String, RemoteClusterControllerTaskScheduler> fleetControllers = new LinkedHashMap<>();
            fleetControllers.put(music.context.cluster.getName(), music);
            return fleetControllers;
        }, ccSockets);
    }

    private void initializeCluster(ContentCluster cluster, Collection<ConfiguredNode> nodes) {
        for (ConfiguredNode configuredNode : nodes) {
            for (NodeType type : NodeType.getTypes()) {
                NodeState reported = new NodeState(type, State.UP);

                NodeInfo nodeInfo = cluster.clusterInfo().setRpcAddress(new Node(type, configuredNode.index()), "rpc:" + type + "/" + configuredNode);
                nodeInfo.setReportedState(reported, 10);
                nodeInfo.setHostInfo(HostInfo.createHostInfo(getHostInfo(nodes)));
            }
        }
    }

    private String getHostInfo(Collection<ConfiguredNode> nodes) {
        return "{\n" +
                "    \"cluster-state-version\": 0,\n" +
                "    \"metrics\": {\n" +
                "        \"values\": [\n" +
                "            {\n" +
                "                \"name\": \"vds.datastored.alldisks.buckets\",\n" +
                "                \"values\": {\n" +
                "                    \"last\": 1\n" +
                "                }\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\": \"vds.datastored.alldisks.docs\",\n" +
                "                \"values\": {\n" +
                "                    \"last\": 2\n" +
                "                }\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\": \"vds.datastored.alldisks.bytes\",\n" +
                "                \"values\": {\n" +
                "                    \"last\": 3\n" +
                "                }\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"distributor\": {\n" +
                "        \"storage-nodes\": [\n" +

                nodes.stream()
                        .map(configuredNode ->
                "            {\n" +
                "                \"node-index\": " + configuredNode.index() + ",\n" +
                "                \"min-current-replication-factor\": 2\n" +
                "            }")
                        .collect(Collectors.joining(",\n")) + "\n" +

                "        ]\n" +
                "    }\n" +
                "}";
    }

    // TODO: Use config builder instead of creating raw: config
    private static String getSimpleGroupConfig(int redundancy, int nodeCount) {
        StringBuilder sb = new StringBuilder();
        sb.append("raw:redundancy ").append(redundancy).append("\n").append("group[4]\n");

        int group = 0;
        sb.append("group[" + group + "].index \"invalid\"\n")
          .append("group[" + group + "].name \"invalid\"\n")
          .append("group[" + group + "].partitions \"1|*\"\n");

        ++group;
        sb.append("group[" + group + "].index \"0\"\n")
          .append("group[" + group + "].name \"east\"\n")
          .append("group[" + group + "].partitions \"*\"\n");

        ++group;
        sb.append("group[" + group + "].index \"0.0\"\n")
          .append("group[" + group + "].name \"g1\"\n")
          .append("group[" + group + "].partitions \"*\"\n")
          .append("group[" + group + "].nodes[").append((nodeCount + 1) / 2).append("]\n");
        for (int i=0; i<nodeCount; i += 2) {
            sb.append("group[" + group + "].nodes[").append(i / 2).append("].index ").append(i).append("\n");
        }

        ++group;
        sb.append("group[" + group + "].index \"0.1\"\n")
          .append("group[" + group + "].name \"g2\"\n")
          .append("group[" + group + "].partitions \"*\"\n")
          .append("group[" + group + "].nodes[").append(nodeCount / 2).append("]\n");
        for (int i=1; i<nodeCount; i += 2) {
            sb.append("group[" + group + "].nodes[").append(i / 2).append("].index ").append(i).append("\n");
        }
        return sb.toString();
    }

    private static Set<ConfiguredNode> toNodes(Integer ... indexes) {
        return Arrays.stream(indexes)
                .map(i -> new ConfiguredNode(i, false))
                .collect(Collectors.toSet());
    }

}