aboutsummaryrefslogtreecommitdiffstats
path: root/orchestrator/src/test/java/com/yahoo/vespa/orchestrator/model/VespaModelUtilTest.java
blob: 3b1bd7af5253e4570437c0309b623adaea7c797f (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
// 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.ApplicationInstanceId;
import com.yahoo.vespa.applicationmodel.ClusterId;
import com.yahoo.vespa.applicationmodel.ConfigId;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.applicationmodel.ServiceCluster;
import com.yahoo.vespa.applicationmodel.ServiceInstance;
import com.yahoo.vespa.applicationmodel.ServiceStatus;
import com.yahoo.vespa.applicationmodel.ServiceType;
import com.yahoo.vespa.applicationmodel.TenantId;
import com.yahoo.vespa.orchestrator.TestUtil;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author hakonhall
 */
public class VespaModelUtilTest {

    // Cluster Controller Service Cluster

    private static final ClusterId CONTENT_CLUSTER_ID = new ClusterId("content-cluster-0");

    public static final HostName controller0Host = new HostName("controller-0");

    private static final ServiceInstance controller0 = new ServiceInstance(
            TestUtil.clusterControllerConfigId(CONTENT_CLUSTER_ID.toString(), 0),
            controller0Host,
            ServiceStatus.UP);
    private static final ServiceInstance controller1 = new ServiceInstance(
            TestUtil.clusterControllerConfigId(CONTENT_CLUSTER_ID.toString(), 1),
            new HostName("controller-1"),
            ServiceStatus.UP);

    private static final ServiceCluster controllerCluster =
            new ServiceCluster(
                    new ClusterId(CONTENT_CLUSTER_ID.s() + "-controller"),
                    ServiceType.CLUSTER_CONTROLLER,
                    Set.of(controller1, controller0));

    // Distributor Service Cluster

    private static final ServiceInstance distributor0 = new ServiceInstance(
            new ConfigId("distributor-config-id"),
            new HostName("distributor-0"),
            ServiceStatus.UP);


    private static final ServiceCluster distributorCluster =
            new ServiceCluster(
                    CONTENT_CLUSTER_ID,
                    ServiceType.DISTRIBUTOR,
                    Set.of(distributor0));

    // Storage Node Service Cluster

    public static final HostName storage0Host = new HostName("storage-0");
    private static final ServiceInstance storage0 = new ServiceInstance(
            new ConfigId("storage-config-id"),
            storage0Host,
            ServiceStatus.UP);

    private static final ServiceCluster storageCluster =
            new ServiceCluster(
                    CONTENT_CLUSTER_ID,
                    ServiceType.STORAGE,
                    Set.of(storage0));

    // Secondary Distributor Service Cluster

    private static final ServiceInstance secondaryDistributor0 = new ServiceInstance(
            new ConfigId("secondary-distributor-config-id"),
            new HostName("secondary-distributor-0"),
            ServiceStatus.UP);

    private static final ClusterId SECONDARY_CONTENT_CLUSTER_ID = new ClusterId("secondary-content-cluster-0");
    private static final ServiceCluster secondaryDistributorCluster =
            new ServiceCluster(
                    SECONDARY_CONTENT_CLUSTER_ID,
                    ServiceType.DISTRIBUTOR,
                    Set.of(secondaryDistributor0));

    // Secondary Storage Node Service Cluster

    public static final HostName secondaryStorage0Host = new HostName("secondary-storage-0");
    private static final ServiceInstance secondaryStorage0 = new ServiceInstance(
            new ConfigId("secondary-storage-config-id"),
            secondaryStorage0Host,
            ServiceStatus.UP);

    private static final ServiceCluster secondaryStorageCluster =
            new ServiceCluster(
                    SECONDARY_CONTENT_CLUSTER_ID,
                    ServiceType.STORAGE,
                    Set.of(secondaryStorage0));

    // The Application Instance

    public static final ApplicationInstance application =
            new ApplicationInstance(
                    new TenantId("tenant-0"),
                    new ApplicationInstanceId("application-0"),
                    Set.of(
                            controllerCluster,
                            distributorCluster,
                            storageCluster,
                            secondaryDistributorCluster,
                            secondaryStorageCluster));

    private ServiceCluster createServiceCluster(ServiceType serviceType) {
        return new ServiceCluster(
                new ClusterId("cluster-id"),
                serviceType,
                new HashSet<>());
    }

    @Test
    public void verifyControllerClusterIsRecognized() {
        ServiceCluster cluster = createServiceCluster(ServiceType.CLUSTER_CONTROLLER);
        assertTrue(VespaModelUtil.isClusterController(cluster));
    }

    @Test
    public void verifyNonControllerClusterIsNotRecognized() {
        ServiceCluster cluster = createServiceCluster(new ServiceType("foo"));
        assertFalse(VespaModelUtil.isClusterController(cluster));
    }

    @Test
    public void verifyStorageClusterIsRecognized() {
        ServiceCluster cluster = createServiceCluster(ServiceType.STORAGE);
        assertTrue(VespaModelUtil.isStorage(cluster));
        cluster = createServiceCluster(ServiceType.STORAGE);
        assertTrue(VespaModelUtil.isStorage(cluster));
    }

    @Test
    public void verifyNonStorageClusterIsNotRecognized() {
        ServiceCluster cluster = createServiceCluster(new ServiceType("foo"));
        assertFalse(VespaModelUtil.isStorage(cluster));
    }

    @Test
    public void verifyContentClusterIsRecognized() {
        ServiceCluster cluster = createServiceCluster(ServiceType.DISTRIBUTOR);
        assertTrue(VespaModelUtil.isContent(cluster));
        cluster = createServiceCluster(ServiceType.STORAGE);
        assertTrue(VespaModelUtil.isContent(cluster));
        cluster = createServiceCluster(ServiceType.SEARCH);
        assertTrue(VespaModelUtil.isContent(cluster));
    }

    @Test
    public void verifyNonContentClusterIsNotRecognized() {
        ServiceCluster cluster = createServiceCluster(new ServiceType("foo"));
        assertFalse(VespaModelUtil.isContent(cluster));
    }

    @Test
    public void testGettingClusterControllerInstances() {
        List<HostName> controllers = VespaModelUtil.getClusterControllerInstancesInOrder(application, CONTENT_CLUSTER_ID);
        List<HostName> expectedControllers = Arrays.asList(controller0.hostName(), controller1.hostName());

        assertEquals(expectedControllers, controllers);
    }

    @Test
    public void testGetControllerHostName() {
        HostName host = VespaModelUtil.getControllerHostName(application, CONTENT_CLUSTER_ID);
        assertEquals(controller0Host, host);
    }

    @Test
    public void testGetContentClusterName() {
        ClusterId contentClusterName = VespaModelUtil.getContentClusterName(application, distributor0.hostName());
        assertEquals(CONTENT_CLUSTER_ID, contentClusterName);
    }

    @Test
    public void testGetContentClusterNameForSecondaryContentCluster() {
        ClusterId contentClusterName = VespaModelUtil.getContentClusterName(application, secondaryDistributor0.hostName());
        assertEquals(contentClusterName, SECONDARY_CONTENT_CLUSTER_ID);
    }

    @Test
    public void testGetStorageNodeAtHost() {
        Optional<ServiceInstance> service =
                VespaModelUtil.getStorageNodeAtHost(application, storage0Host);
        assertTrue(service.isPresent());
        assertEquals(storage0, service.get());
    }

    @Test
    public void testGetStorageNodeAtHostWithUnknownHost() {
        Optional<ServiceInstance> service =
                VespaModelUtil.getStorageNodeAtHost(application, new HostName("storage-1"));
        assertFalse(service.isPresent());
    }

    @Test
    public void testGetClusterControllerIndex() {
        ConfigId configId = new ConfigId("admin/cluster-controllers/2");
        assertEquals(2, VespaModelUtil.getClusterControllerIndex(configId));
    }

    @Test
    public void testGetClusterControllerIndexWithStandaloneClusterController() {
        ConfigId configId = new ConfigId("fantasy_sports/standalone/fantasy_sports-controllers/1");
        assertEquals(1, VespaModelUtil.getClusterControllerIndex(configId));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBadClusterControllerConfigId() {
        ConfigId configId = new ConfigId("fantasy_sports/storage/9");
        VespaModelUtil.getClusterControllerIndex(configId);
        fail();
    }

    @Test
    public void testGetStorageNodeIndex() {
        ConfigId configId = TestUtil.storageNodeConfigId(CONTENT_CLUSTER_ID.toString(), 3);
        assertEquals(3, VespaModelUtil.getStorageNodeIndex(configId));
    }

}