aboutsummaryrefslogtreecommitdiffstats
path: root/orchestrator/src/test/java/com/yahoo/vespa/orchestrator/model/ApplicationApiImplTest.java
blob: c0d6e01104aa5c912886d3c6301f8cac7190329f (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.model;

import com.yahoo.vespa.applicationmodel.ApplicationInstance;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.applicationmodel.ServiceStatus;
import com.yahoo.vespa.applicationmodel.ServiceType;
import com.yahoo.vespa.orchestrator.status.HostStatus;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

public class ApplicationApiImplTest {
    final ModelTestUtils modelUtils = new ModelTestUtils();

    @Test
    public void testApplicationId() {
        try (var api = modelUtils.createScopedApplicationApi(modelUtils.createApplicationInstance(new ArrayList<>()))) {
            assertEquals("tenant:application-name:default", api.applicationApi().applicationId().serializedForm());
        }
    }

    @Test
    public void testGetClustersThatAreOnAtLeastOneNodeInGroup() {
        HostName hostName1 = new HostName("host1");
        HostName hostName2 = new HostName("host2");
        HostName hostName3 = new HostName("host3");
        HostName hostName4 = new HostName("host4");

        ApplicationInstance applicationInstance =
                modelUtils.createApplicationInstance(Arrays.asList(
                        modelUtils.createServiceCluster(
                                "cluster-3",
                                new ServiceType("service-type-3"),
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-1", hostName1, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-2", hostName2, ServiceStatus.UP)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-1",
                                new ServiceType("service-type-1"),
                                Arrays.asList(
                                    modelUtils.createServiceInstance("config-id-3", hostName1, ServiceStatus.UP),
                                    modelUtils.createServiceInstance("config-id-4", hostName3, ServiceStatus.UP)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-2",
                                new ServiceType("service-type-2"),
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-5", hostName1, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-6", hostName2, ServiceStatus.UP)
                                )
                        )
                ));

        verifyClustersInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName1), 1, 2, 3);
        verifyClustersInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName2), 2, 3);
        verifyClustersInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName3), 1);
        verifyClustersInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName4));
    }

    private void verifyClustersInOrder(ScopedApplicationApi scopedApi,
                                       Integer... expectedClusterNumbers) {
        try (scopedApi) {
            // Note: we require the clusters to be in order.
            List<ClusterApi> clusterApis = scopedApi.applicationApi().getClusters();
            String clusterInfos = clusterApis.stream().map(clusterApi -> clusterApi.clusterInfo()).collect(Collectors.joining(","));

            String expectedClusterInfos = Arrays.stream(expectedClusterNumbers)
                    .map(number -> "{ clusterId=cluster-" + number + ", serviceType=service-type-" + number + " }")
                    .collect(Collectors.joining(","));

            assertEquals(expectedClusterInfos, clusterInfos);
        }
    }

    @Test
    public void testGetUpStorageNodesInGroupInClusterOrder() {
        HostName hostName1 = new HostName("host1");
        HostName hostName2 = new HostName("host2");
        HostName hostName3 = new HostName("host3");
        HostName hostName4 = new HostName("host4");
        HostName hostName5 = new HostName("host5");
        HostName hostName6 = new HostName("host6");
        HostName hostName7 = new HostName("host7");

        ApplicationInstance applicationInstance =
                modelUtils.createApplicationInstance(Arrays.asList(
                        modelUtils.createServiceCluster(
                                "cluster-3",
                                ServiceType.STORAGE,
                                Arrays.asList(
                                    modelUtils.createServiceInstance("config-id-30", hostName1, ServiceStatus.UP),
                                    modelUtils.createServiceInstance("config-id-31", hostName2, ServiceStatus.UP)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-1",
                                ServiceType.STORAGE,
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-10", hostName3, ServiceStatus.DOWN),
                                        modelUtils.createServiceInstance("config-id-11", hostName4, ServiceStatus.UP)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-4",
                                new ServiceType("service-type-4"),
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-40", hostName1, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-41", hostName2, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-42", hostName3, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-43", hostName5, ServiceStatus.UP)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-2",
                                ServiceType.STORAGE,
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-20", hostName6, ServiceStatus.DOWN),
                                        modelUtils.createServiceInstance("config-id-21", hostName7, ServiceStatus.UP)
                                )
                        )
                ));

        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName1), hostName1);
        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName2), hostName2);
        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName3), hostName3); // host3 is DOWN
        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName4), hostName4);
        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName5)); // not a storage cluster

        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName1, hostName3), hostName3, hostName1);

        // For the node group (host1, host4), they both have an up storage node (service instance)
        // with clusters (cluster-3, cluster-1) respectively, and so the order of the hosts are reversed
        // (host4, host1) when sorted by the clusters.
        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(applicationInstance, hostName1, hostName4), hostName4, hostName1);

        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(
                applicationInstance, hostName1, hostName4, hostName5), hostName4, hostName1);
        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(
                applicationInstance, hostName1, hostName4, hostName5, hostName6), hostName4, hostName6, hostName1);
        verifyNoRemarksStorageNodesInOrder(modelUtils.createScopedApplicationApi(
                applicationInstance, hostName1, hostName4, hostName5, hostName7), hostName4, hostName7, hostName1);
    }

    private void verifyNoRemarksStorageNodesInOrder(ScopedApplicationApi scopedApi,
                                                    HostName... expectedHostNames) {
        try (scopedApi) {
            List<HostName> upStorageNodes = scopedApi.applicationApi().getNoRemarksStorageNodesInGroupInClusterOrder().stream()
                    .map(storageNode -> storageNode.hostName())
                    .toList();
            assertEquals(Arrays.asList(expectedHostNames), upStorageNodes);
        }
    }

    @Test
    public void testUpConditionOfStorageNode() {
        verifyNoRemarksConditionWith(HostStatus.NO_REMARKS, ServiceStatus.UP, true);
        verifyNoRemarksConditionWith(HostStatus.NO_REMARKS, ServiceStatus.NOT_CHECKED, true);
        verifyNoRemarksConditionWith(HostStatus.NO_REMARKS, ServiceStatus.DOWN, true);
        verifyNoRemarksConditionWith(HostStatus.NO_REMARKS, ServiceStatus.UNKNOWN, true);
        verifyNoRemarksConditionWith(HostStatus.ALLOWED_TO_BE_DOWN, ServiceStatus.UP, false);
        verifyNoRemarksConditionWith(HostStatus.ALLOWED_TO_BE_DOWN, ServiceStatus.NOT_CHECKED, false);
        verifyNoRemarksConditionWith(HostStatus.ALLOWED_TO_BE_DOWN, ServiceStatus.DOWN, false);
        verifyNoRemarksConditionWith(HostStatus.ALLOWED_TO_BE_DOWN, ServiceStatus.UNKNOWN, false);
    }

    private void verifyNoRemarksConditionWith(HostStatus hostStatus, ServiceStatus serviceStatus, boolean expectUp) {
        HostName hostName1 = new HostName("host1");
        ApplicationInstance applicationInstance =
                modelUtils.createApplicationInstance(List.of(
                        modelUtils.createServiceCluster(
                                "cluster-1",
                                ServiceType.STORAGE,
                                List.of(modelUtils.createServiceInstance("config-id-1", hostName1, serviceStatus))
                        )
                ));

        modelUtils.createNode("host1", hostStatus);

        try (var scopedApi = modelUtils.createScopedApplicationApi(applicationInstance, hostName1)) {
            List<HostName> upStorageNodes = expectUp ? List.of(hostName1) : new ArrayList<>();

            List<HostName> actualStorageNodes = scopedApi.applicationApi()
                    .getNoRemarksStorageNodesInGroupInClusterOrder()
                    .stream()
                    .map(storageNode -> storageNode.hostName())
                    .toList();
            assertEquals(upStorageNodes, actualStorageNodes);
        }
    }

    @Test
    public void testGetNodesInGroupWithStatus() {
        HostName hostName1 = new HostName("host1");
        HostName hostName2 = new HostName("host2");
        HostName hostName3 = new HostName("host3");

        ApplicationInstance applicationInstance =
                modelUtils.createApplicationInstance(Arrays.asList(
                        modelUtils.createServiceCluster(
                                "cluster-1",
                                new ServiceType("service-type-1"),
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-10", hostName1, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-11", hostName2, ServiceStatus.UP)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-2",
                                new ServiceType("service-type-2"),
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-20", hostName1, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-21", hostName3, ServiceStatus.UP)
                                )
                        )
                ));

        modelUtils.createNode(hostName1, HostStatus.NO_REMARKS);
        modelUtils.createNode(hostName2, HostStatus.NO_REMARKS);
        modelUtils.createNode(hostName3, HostStatus.ALLOWED_TO_BE_DOWN);

        verifyNodesInGroupWithoutRemarks(
                modelUtils.createScopedApplicationApi(applicationInstance, hostName1),
                List.of(hostName1),
                List.of());
        verifyNodesInGroupWithoutRemarks(
                modelUtils.createScopedApplicationApi(applicationInstance, hostName1, hostName2),
                Arrays.asList(hostName1, hostName2),
                List.of());
        verifyNodesInGroupWithoutRemarks(
                modelUtils.createScopedApplicationApi(applicationInstance, hostName1, hostName2, hostName3),
                Arrays.asList(hostName1, hostName2),
                List.of(hostName3));
        verifyNodesInGroupWithoutRemarks(
                modelUtils.createScopedApplicationApi(applicationInstance, hostName3),
                List.of(),
                List.of(hostName3));
    }

    private void verifyNodesInGroupWithoutRemarks(ScopedApplicationApi scopedApi,
                                                  List<HostName> noRemarksHostNames,
                                                  List<HostName> allowedToBeDownHostNames) {
        try (scopedApi) {
            List<HostName> actualNoRemarksHosts = scopedApi.applicationApi().getNodesInGroupWithStatus(HostStatus.NO_REMARKS);
            assertEquals(noRemarksHostNames, actualNoRemarksHosts);
            List<HostName> actualAllowedToBeDownHosts = scopedApi.applicationApi()
                    .getNodesInGroupWithStatus(HostStatus.ALLOWED_TO_BE_DOWN);
            assertEquals(allowedToBeDownHostNames, actualAllowedToBeDownHosts);
        }
    }

    @Test
    public void testGetStorageNodesAllowedToBeDownInGroupInReverseClusterOrder() {
        HostName allowedToBeDownHost1 = new HostName("host1");
        HostName noRemarksHost2 = new HostName("host2");
        HostName allowedToBeDownHost3 = new HostName("host3");
        HostName allowedToBeDownHost4 = new HostName("host4");
        HostName noRemarksHost5 = new HostName("host5");
        HostName noRemarksHost6 = new HostName("host6");
        HostName allowedToBeDownHost7 = new HostName("host7");

        ApplicationInstance applicationInstance =
                modelUtils.createApplicationInstance(Arrays.asList(
                        modelUtils.createServiceCluster(
                                "cluster-4",
                                ServiceType.STORAGE,
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-40", allowedToBeDownHost1, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-41", noRemarksHost2, ServiceStatus.DOWN)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-1",
                                new ServiceType("service-type-1"),
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-10", allowedToBeDownHost1, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-11", allowedToBeDownHost3, ServiceStatus.UP)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-3",
                                ServiceType.STORAGE,
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-30", allowedToBeDownHost4, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-31", noRemarksHost5, ServiceStatus.UP)
                                )
                        ),
                        modelUtils.createServiceCluster(
                                "cluster-2",
                                ServiceType.STORAGE,
                                Arrays.asList(
                                        modelUtils.createServiceInstance("config-id-20", noRemarksHost6, ServiceStatus.UP),
                                        modelUtils.createServiceInstance("config-id-21", allowedToBeDownHost7, ServiceStatus.UP)
                                )
                        )
                ));

        modelUtils.createNode(allowedToBeDownHost1, HostStatus.ALLOWED_TO_BE_DOWN);
        modelUtils.createNode(noRemarksHost2, HostStatus.NO_REMARKS);
        modelUtils.createNode(allowedToBeDownHost3, HostStatus.ALLOWED_TO_BE_DOWN);
        modelUtils.createNode(allowedToBeDownHost4, HostStatus.ALLOWED_TO_BE_DOWN);
        modelUtils.createNode(noRemarksHost5, HostStatus.ALLOWED_TO_BE_DOWN); // Really?
        modelUtils.createNode(noRemarksHost6, HostStatus.NO_REMARKS);
        modelUtils.createNode(allowedToBeDownHost7, HostStatus.ALLOWED_TO_BE_DOWN);

        verifyStorageNodesAllowedToBeDown(
                modelUtils.createScopedApplicationApi(applicationInstance, allowedToBeDownHost1), allowedToBeDownHost1);
        verifyStorageNodesAllowedToBeDown(
                modelUtils.createScopedApplicationApi(applicationInstance, noRemarksHost2));
        verifyStorageNodesAllowedToBeDown(
                modelUtils.createScopedApplicationApi(applicationInstance, allowedToBeDownHost3));

        verifyStorageNodesAllowedToBeDown(
                modelUtils.createScopedApplicationApi(applicationInstance, allowedToBeDownHost1, noRemarksHost6), allowedToBeDownHost1);

        // allowedToBeDownHost4 is in cluster-3, while allowedToBeDownHost1 is in cluster-4, so allowedToBeDownHost4 should be ordered
        // before allowedToBeDownHost1.
        verifyStorageNodesAllowedToBeDown(
                modelUtils.createScopedApplicationApi(applicationInstance, allowedToBeDownHost1, noRemarksHost6, allowedToBeDownHost4),
                allowedToBeDownHost4, allowedToBeDownHost1);

        verifyStorageNodesAllowedToBeDown(
                modelUtils.createScopedApplicationApi(applicationInstance, allowedToBeDownHost1, allowedToBeDownHost4, allowedToBeDownHost7),
                allowedToBeDownHost7, allowedToBeDownHost4, allowedToBeDownHost1);

        verifyStorageNodesAllowedToBeDown(
                modelUtils.createScopedApplicationApi(applicationInstance, allowedToBeDownHost4, allowedToBeDownHost1, allowedToBeDownHost7),
                allowedToBeDownHost7, allowedToBeDownHost4, allowedToBeDownHost1);
    }

    private void verifyStorageNodesAllowedToBeDown(
            ScopedApplicationApi scopedApi, HostName... hostNames) {
        try (scopedApi) {
            List<HostName> actualStorageNodes = scopedApi
                    .applicationApi()
                    .getSuspendedStorageNodesInGroupInReverseClusterOrder()
                    .stream()
                    .map(storageNode -> storageNode.hostName())
                    .toList();
            assertEquals(Arrays.asList(hostNames), actualStorageNodes);
        }
    }
}