summaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/provisioning/VirtualNodeProvisioningTest.java
blob: 64c91e66c26d7a8149df8915033313e670f45896 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.provisioning;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.OutOfCapacityException;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.hosted.provision.Node;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * Tests provisioning of virtual nodes
 *
 * @author hmusum
 * @author mpolden
 */
// Note: Some of the tests here should be moved to DockerProvisioningTest if we stop using VMs and want
// to remove these tests
public class VirtualNodeProvisioningTest {

    private static final NodeResources flavor = new NodeResources(4, 8, 100, 1);
    private static final ClusterSpec contentClusterSpec = ClusterSpec.request(ClusterSpec.Type.content, ClusterSpec.Id.from("myContent"),
                                                                              Version.fromString("6.42"), false, Optional.empty(), Optional.empty());
    private static final ClusterSpec containerClusterSpec = ClusterSpec.request(ClusterSpec.Type.container, ClusterSpec.Id.from("myContainer"),
                                                                                Version.fromString("6.42"), false, Optional.empty(), Optional.empty());

    private ProvisioningTester tester = new ProvisioningTester.Builder().build();
    private ApplicationId applicationId = tester.makeApplicationId();

    @Test
    public void distinct_parent_host_for_each_node_in_a_cluster() {
        tester.makeReadyVirtualDockerNodes(2, flavor, "parentHost1");
        tester.makeReadyVirtualDockerNodes(2, flavor, "parentHost2");
        tester.makeReadyVirtualDockerNodes(2, flavor, "parentHost3");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost4");

        final int containerNodeCount = 4;
        final int contentNodeCount = 3;
        final int groups = 1;
        List<HostSpec> containerHosts = prepare(containerClusterSpec, containerNodeCount, groups);
        List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
        activate(containerHosts, contentHosts);

        final List<Node> nodes = getNodes(applicationId);
        assertEquals(contentNodeCount + containerNodeCount, nodes.size());
        assertDistinctParentHosts(nodes, ClusterSpec.Type.container, containerNodeCount);
        assertDistinctParentHosts(nodes, ClusterSpec.Type.content, contentNodeCount);

        // Go down to 3 nodes in container cluster
        List<HostSpec> containerHosts2 = prepare(containerClusterSpec, containerNodeCount - 1, groups);
        activate(containerHosts2);
        List<Node> nodes2 = getNodes(applicationId);
        assertDistinctParentHosts(nodes2, ClusterSpec.Type.container, containerNodeCount - 1);

        // Go up to 4 nodes again in container cluster
        List<HostSpec> containerHosts3 = prepare(containerClusterSpec, containerNodeCount, groups);
        activate(containerHosts3);
        List<Node> nodes3 = getNodes(applicationId);
        assertDistinctParentHosts(nodes3, ClusterSpec.Type.container, containerNodeCount);
    }

    @Test
    public void allow_same_parent_host_for_nodes_in_a_cluster_in_cd_and_non_prod() {
        final int containerNodeCount = 2;
        final int contentNodeCount = 2;
        final int groups = 1;

        // Allowed to use same parent host for several nodes in same cluster in dev
        {
            NodeResources flavor = new NodeResources(1, 4, 10, 1);
            tester = new ProvisioningTester.Builder().zone(new Zone(Environment.dev, RegionName.from("us-east"))).build();
            tester.makeReadyNodes(4, flavor, NodeType.host, 1);
            tester.prepareAndActivateInfraApplication(tester.makeApplicationId(), NodeType.host);

            List<HostSpec> containerHosts = prepare(containerClusterSpec, containerNodeCount, groups, flavor);
            List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups, flavor);
            activate(containerHosts, contentHosts);

            // downscaled to 1 node per cluster in dev, so 2 in total
            assertEquals(2, getNodes(applicationId).size());
        }

        // Allowed to use same parent host for several nodes in same cluster in CD (even if prod env)
        {
            tester = new ProvisioningTester.Builder().zone(new Zone(SystemName.cd, Environment.prod, RegionName.from("us-east"))).build();
            tester.makeReadyNodes(4, flavor, NodeType.host, 1);
            tester.prepareAndActivateInfraApplication(tester.makeApplicationId(), NodeType.host);

            List<HostSpec> containerHosts = prepare(containerClusterSpec, containerNodeCount, groups);
            List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
            activate(containerHosts, contentHosts);

            assertEquals(4, getNodes(applicationId).size());
        }
    }

    @Test
    public void will_retire_clashing_active() {
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost1");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost2");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost3");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost4");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost5");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost6");

        int containerNodeCount = 2;
        int contentNodeCount = 2;
        int groups = 1;
        List<HostSpec> containerHosts = prepare(containerClusterSpec, containerNodeCount, groups);
        List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
        activate(containerHosts, contentHosts);

        List<Node> nodes = getNodes(applicationId);
        assertEquals(4, nodes.size());
        assertDistinctParentHosts(nodes, ClusterSpec.Type.container, containerNodeCount);
        assertDistinctParentHosts(nodes, ClusterSpec.Type.content, contentNodeCount);

        for (Node n : nodes) {
          tester.patchNode(n.withParentHostname("clashing"));
        }
        containerHosts = prepare(containerClusterSpec, containerNodeCount, groups);
        contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
        activate(containerHosts, contentHosts);

        nodes = getNodes(applicationId);
        assertEquals(6, nodes.size());
        assertEquals(2, nodes.stream().filter(n -> n.allocation().get().membership().retired()).count());
    }

    @Test
    public void fail_when_all_hosts_become_clashing() {
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost1");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost2");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost3");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost4");

        int containerNodeCount = 2;
        int contentNodeCount = 2;
        int groups = 1;
        List<HostSpec> containerHosts = prepare(containerClusterSpec, containerNodeCount, groups);
        List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
        activate(containerHosts, contentHosts);

        List<Node> nodes = getNodes(applicationId);
        assertEquals(4, nodes.size());
        assertDistinctParentHosts(nodes, ClusterSpec.Type.container, containerNodeCount);
        assertDistinctParentHosts(nodes, ClusterSpec.Type.content, contentNodeCount);

        for (Node n : nodes) {
          tester.patchNode(n.withParentHostname("clashing"));
        }
        OutOfCapacityException expected = null;
        try {
          containerHosts = prepare(containerClusterSpec, containerNodeCount, groups);
        } catch (OutOfCapacityException e) {
          expected = e;
        }
        assertNotNull(expected);
    }

    @Test(expected = OutOfCapacityException.class)
    public void fail_when_too_few_distinct_parent_hosts() {
        tester.makeReadyVirtualDockerNodes(2, flavor, "parentHost1");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost2");

        int contentNodeCount = 3;
        List<HostSpec> hosts = prepare(contentClusterSpec, contentNodeCount, 1);
        activate(hosts);

        List<Node> nodes = getNodes(applicationId);
        assertDistinctParentHosts(nodes, ClusterSpec.Type.content, contentNodeCount);
    }

    @Test
    public void incomplete_parent_hosts_has_distinct_distribution() {
        tester.makeReadyVirtualDockerNode(1, flavor, "parentHost1");
        tester.makeReadyVirtualDockerNode(1, flavor, "parentHost2");
        tester.makeReadyVirtualNodes(1, flavor);

        final int contentNodeCount = 3;
        final int groups = 1;
        final List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
        activate(contentHosts);
        assertEquals(3, getNodes(applicationId).size());

        tester.makeReadyVirtualDockerNode(2, flavor, "parentHost1");
        tester.makeReadyVirtualDockerNode(2, flavor, "parentHost2");

        assertEquals(contentHosts, prepare(contentClusterSpec, contentNodeCount, groups));
    }

    @Test
    public void indistinct_distribution_with_known_ready_nodes() {
        tester.makeReadyVirtualNodes(3, flavor);

        final int contentNodeCount = 3;
        final int groups = 1;
        final List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
        activate(contentHosts);

        List<Node> nodes = getNodes(applicationId);
        assertEquals(3, nodes.size());

        // Set indistinct parents
        tester.patchNode(nodes.get(0).withParentHostname("parentHost1"));
        tester.patchNode(nodes.get(1).withParentHostname("parentHost1"));
        tester.patchNode(nodes.get(2).withParentHostname("parentHost2"));
        nodes = getNodes(applicationId);
        assertEquals(3, nodes.stream().filter(n -> n.parentHostname().isPresent()).count());

        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost1");
        tester.makeReadyVirtualDockerNodes(2, flavor, "parentHost2");

        OutOfCapacityException expectedException = null;
        try {
            prepare(contentClusterSpec, contentNodeCount, groups);
        } catch (OutOfCapacityException e) {
            expectedException = e;
        }
        assertNotNull(expectedException);
    }

    @Test
    public void unknown_distribution_with_known_ready_nodes() {
        tester.makeReadyVirtualNodes(3, flavor);

        final int contentNodeCount = 3;
        final int groups = 1;
        final List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
        activate(contentHosts);
        assertEquals(3, getNodes(applicationId).size());

        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost1");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost2");
        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost3");
        assertEquals(contentHosts, prepare(contentClusterSpec, contentNodeCount, groups));
    }

    @Test
    public void unknown_distribution_with_known_and_unknown_ready_nodes() {
        tester.makeReadyVirtualNodes(3, flavor);

        int contentNodeCount = 3;
        int groups = 1;
        List<HostSpec> contentHosts = prepare(contentClusterSpec, contentNodeCount, groups);
        activate(contentHosts);
        assertEquals(3, getNodes(applicationId).size());

        tester.makeReadyVirtualDockerNodes(1, flavor, "parentHost1");
        tester.makeReadyVirtualNodes(1, flavor);
        assertEquals(contentHosts, prepare(contentClusterSpec, contentNodeCount, groups));
    }

    private void assertDistinctParentHosts(List<Node> nodes, ClusterSpec.Type clusterType, int expectedCount) {
        List<String> parentHosts = getParentHostsFromNodes(nodes, Optional.of(clusterType));

        assertEquals(expectedCount, parentHosts.size());
        assertEquals(expectedCount, getDistinctParentHosts(parentHosts).size());
    }

    private List<String> getParentHostsFromNodes(List<Node> nodes, Optional<ClusterSpec.Type> clusterType) {
        List<String> parentHosts = new ArrayList<>();
        for (Node node : nodes) {
            if (node.parentHostname().isPresent() && (clusterType.isPresent() && clusterType.get() == node.allocation().get().membership().cluster().type())) {
                parentHosts.add(node.parentHostname().get());
            }
        }
        return parentHosts;
    }

    private Set<String> getDistinctParentHosts(List<String> hostnames) {
        return hostnames.stream()
                .distinct()
                .collect(Collectors.<String>toSet());
    }

    private List<Node> getNodes(ApplicationId applicationId) {
        return tester.getNodes(applicationId, Node.State.active).asList();
    }

    private List<HostSpec> prepare(ClusterSpec clusterSpec, int nodeCount, int groups) {
        return tester.prepare(applicationId, clusterSpec, nodeCount, groups, flavor);
    }

    private List<HostSpec> prepare(ClusterSpec clusterSpec, int nodeCount, int groups, NodeResources flavor) {
        return tester.prepare(applicationId, clusterSpec, nodeCount, groups, flavor);
    }

    @SafeVarargs
    private final void activate(List<HostSpec>... hostLists) {
        HashSet<HostSpec> hosts = new HashSet<>();
        for (List<HostSpec> h : hostLists) {
           hosts.addAll(h);
        }
        tester.activate(applicationId, hosts);
    }

}