aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/NodeRepositoryTest.java
blob: f8aed27dcdfef772238f95feea05db83d789e771 (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
// 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;

import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.hosted.provision.node.Agent;
import com.yahoo.vespa.hosted.provision.node.History;
import com.yahoo.vespa.hosted.provision.node.IP;
import com.yahoo.vespa.hosted.provision.node.Report;
import com.yahoo.vespa.hosted.provision.node.Reports;
import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * tests basic operation of the node repository
 * 
 * @author bratseth
 */
public class NodeRepositoryTest {

    @Test
    public void add_and_remove() {
        NodeRepositoryTester tester = new NodeRepositoryTester();
        assertEquals(0, tester.nodeRepository().getNodes().size());

        tester.addNode("id1", "host1", "default", NodeType.host);
        tester.addNode("id2", "host2", "default", NodeType.host);
        tester.addNode("id3", "host3", "default", NodeType.host);

        assertEquals(3, tester.nodeRepository().getNodes().size());
        
        tester.nodeRepository().park("host2", true, Agent.system, "Parking to unit test");
        tester.nodeRepository().removeRecursively("host2");

        assertEquals(3, tester.nodeRepository().getNodes().size());
        assertEquals(1, tester.nodeRepository().getNodes(Node.State.deprovisioned).size());
    }

    @Test
    public void only_allow_docker_containers_remove_in_ready() {
        NodeRepositoryTester tester = new NodeRepositoryTester();
        tester.addNode("id1", "host1", "docker", NodeType.tenant);

        try {
            tester.nodeRepository().removeRecursively("host1"); // host1 is in state provisioned
            fail("Should not be able to delete docker container node by itself in state provisioned");
        } catch (IllegalArgumentException ignored) {
            // Expected
        }

        tester.nodeRepository().setReady("host1", Agent.system, getClass().getSimpleName());
        tester.nodeRepository().removeRecursively("host1");
    }

    @Test
    public void only_remove_tenant_docker_containers_for_new_allocations() {
        NodeRepositoryTester tester = new NodeRepositoryTester();
        tester.addNode("host1", "host1", "default", NodeType.tenant);
        tester.addNode("host2", "host2", "docker", NodeType.tenant);
        tester.addNode("cfg1", "cfg1", "docker", NodeType.config);

        tester.setNodeState("host1", Node.State.dirty);
        tester.setNodeState("host2", Node.State.dirty);
        tester.setNodeState("cfg1", Node.State.dirty);

        tester.nodeRepository().markNodeAvailableForNewAllocation("host1", Agent.system, getClass().getSimpleName());
        assertEquals(Node.State.ready, tester.nodeRepository().getNode("host1").get().state());

        tester.nodeRepository().markNodeAvailableForNewAllocation("host2", Agent.system, getClass().getSimpleName());
        assertFalse(tester.nodeRepository().getNode("host2").isPresent());

        tester.nodeRepository().markNodeAvailableForNewAllocation("cfg1", Agent.system, getClass().getSimpleName());
        assertEquals(Node.State.ready, tester.nodeRepository().getNode("cfg1").get().state());
    }

    @Test
    public void fail_readying_with_hard_fail() {
        NodeRepositoryTester tester = new NodeRepositoryTester();
        tester.addNode("host1", "host1", "default", NodeType.tenant);
        tester.addNode("host2", "host2", "default", NodeType.tenant);
        tester.setNodeState("host1", Node.State.dirty);
        tester.setNodeState("host2", Node.State.dirty);

        Node node2 = tester.nodeRepository().getNode("host2").orElseThrow();
        var reportsBuilder = new Reports.Builder(node2.reports());
        reportsBuilder.setReport(Report.basicReport("reportId", Report.Type.HARD_FAIL, Instant.EPOCH, "hardware failure"));
        node2 = node2.with(reportsBuilder.build());
        tester.nodeRepository().write(node2, () -> {});

        tester.nodeRepository().markNodeAvailableForNewAllocation("host1", Agent.system, getClass().getSimpleName());
        assertEquals(Node.State.ready, tester.nodeRepository().getNode("host1").get().state());

        try {
            tester.nodeRepository().markNodeAvailableForNewAllocation("host2", Agent.system, getClass().getSimpleName());
            fail();
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), containsString("hardware failure"));
        }
    }

    @Test
    public void delete_host_only_after_all_the_children_have_been_deleted() {
        NodeRepositoryTester tester = new NodeRepositoryTester();

        tester.addNode("id1", "host1", "default", NodeType.host);
        tester.addNode("id2", "host2", "default", NodeType.host);
        tester.addNode("node10", "node10", "host1", "docker", NodeType.tenant);
        tester.addNode("node11", "node11", "host1", "docker", NodeType.tenant);
        tester.addNode("node12", "node12", "host1", "docker", NodeType.tenant);
        tester.addNode("node20", "node20", "host2", "docker", NodeType.tenant);
        tester.setNodeState("node11", Node.State.active);
        assertEquals(6, tester.nodeRepository().getNodes().size());

        try {
            tester.nodeRepository().removeRecursively("host1");
            fail("Should not be able to delete host node, one of the children is in state active");
        } catch (IllegalArgumentException ignored) {
            // Expected
        }
        assertEquals(6, tester.nodeRepository().getNodes().size());

        // Should be OK to delete host2 as both host2 and its only child, node20, are in state provisioned
        tester.nodeRepository().removeRecursively("host2");
        assertEquals(5, tester.nodeRepository().getNodes().size());
        assertEquals(Node.State.deprovisioned, tester.nodeRepository().getNode("host2").get().state());

        // Now node10 is in provisioned, set node11 to failed and node12 to ready, and it should be OK to delete host1
        tester.nodeRepository().fail("node11", Agent.system, getClass().getSimpleName());
        tester.nodeRepository().setReady("node12", Agent.system, getClass().getSimpleName());
        tester.nodeRepository().removeRecursively("node12"); // Remove one of the children first instead
        assertEquals(4, tester.nodeRepository().getNodes().size());
        tester.nodeRepository().removeRecursively("host1");
        assertEquals(Node.State.deprovisioned, tester.nodeRepository().getNode("host1").get().state());
        assertEquals(IP.Config.EMPTY.primary(), tester.nodeRepository().getNode("host1").get().ipConfig().primary());
    }

    @Test
    public void delete_config_host() {
        NodeRepositoryTester tester = new NodeRepositoryTester();

        String cfghost1 = "cfghost1";
        String cfg1 = "cfg1";
        tester.addNode("id1", cfghost1, "default", NodeType.confighost);
        tester.addNode("id2", cfg1, cfghost1, "docker", NodeType.config);
        tester.setNodeState(cfghost1, Node.State.active);
        tester.setNodeState(cfg1, Node.State.active);
        assertEquals(2, tester.nodeRepository().getNodes().size());

        try {
            tester.nodeRepository().removeRecursively(cfghost1);
            fail("Should not be able to delete host node, one of the children is in state active");
        } catch (IllegalArgumentException ignored) { }
        assertEquals(2, tester.nodeRepository().getNodes().size());

        // Fail host and container
        tester.nodeRepository().failRecursively(cfghost1, Agent.system, getClass().getSimpleName());

        // Remove recursively
        tester.nodeRepository().removeRecursively(cfghost1);
        assertEquals(0, tester.nodeRepository().list().not().state(Node.State.deprovisioned).size());
    }

    @Test
    public void relevant_information_from_deprovisioned_hosts_are_merged_into_readded_host() {
        NodeRepositoryTester tester = new NodeRepositoryTester();
        Instant testStart = tester.nodeRepository().clock().instant();

        tester.clock().advance(Duration.ofSeconds(1));
        tester.addNode("id1", "host1", "default", NodeType.host);
        tester.addNode("id2", "host2", "default", NodeType.host);
        assertFalse(tester.nodeRepository().getNode("host1").get().history().hasEventAfter(History.Event.Type.deprovisioned, testStart));

        // Set host 1 properties and deprovision it
        Node host1 = tester.nodeRepository().getNode("host1").get();
        host1 = host1.withWantToRetire(true, true, Agent.system, tester.nodeRepository().clock().instant());
        host1 = host1.withFirmwareVerifiedAt(tester.clock().instant());
        host1 = host1.with(host1.status().withIncreasedFailCount());
        host1 = host1.with(host1.reports().withReport(Report.basicReport("id", Report.Type.HARD_FAIL, tester.clock().instant(), "Test report")));
        tester.nodeRepository().write(host1, tester.nodeRepository().lock(host1));
        tester.nodeRepository().removeRecursively("host1");

        // Host 1 is deprovisioned and unwanted properties are cleared
        host1 = tester.nodeRepository().getNode("host1").get();
        assertEquals(Node.State.deprovisioned, host1.state());
        assertTrue(host1.history().hasEventAfter(History.Event.Type.deprovisioned, testStart));

        // Adding it again preserves some information from the deprovisioned host and removes it
        tester.addNode("id2", "host1", "default", NodeType.host);
        host1 = tester.nodeRepository().getNode("host1").get();
        assertEquals("This is the newly added node", "id2", host1.id());
        assertFalse("The old 'host1' is removed",
                    tester.nodeRepository().getNode("host1", Node.State.deprovisioned).isPresent());
        assertFalse("Not transferred from deprovisioned host", host1.status().wantToRetire());
        assertFalse("Not transferred from deprovisioned host", host1.status().wantToDeprovision());
        assertTrue("Transferred from deprovisioned host", host1.history().hasEventAfter(History.Event.Type.deprovisioned, testStart));
        assertTrue("Transferred from deprovisioned host", host1.status().firmwareVerifiedAt().isPresent());
        assertEquals("Transferred from deprovisioned host", 1, host1.status().failCount());
        assertEquals("Transferred from deprovisioned host", 1, host1.reports().getReports().size());
    }

    @Test
    public void dirty_host_only_if_we_can_dirty_children() {
        NodeRepositoryTester tester = new NodeRepositoryTester();

        tester.addNode("id1", "host1", "default", NodeType.host);
        tester.addNode("id2", "host2", "default", NodeType.host);
        tester.addNode("node10", "node10", "host1", "docker", NodeType.tenant);
        tester.addNode("node11", "node11", "host1", "docker", NodeType.tenant);
        tester.addNode("node12", "node12", "host1", "docker", NodeType.tenant);
        tester.addNode("node20", "node20", "host2", "docker", NodeType.tenant);

        tester.setNodeState("node11", Node.State.ready);
        tester.setNodeState("node12", Node.State.active);
        tester.setNodeState("node20", Node.State.failed);

        assertEquals(6, tester.nodeRepository().getNodes().size());

        // Should be OK to dirty host2 as it is in provisioned and its only child is in failed
        tester.nodeRepository().dirtyRecursively("host2", Agent.system, NodeRepositoryTest.class.getSimpleName());
        assertEquals(asSet("host2", "node20"), filterNodes(tester, node -> node.state() == Node.State.dirty));

        // Cant dirty host1, node11 is ready and node12 is active
        try {
            tester.nodeRepository().dirtyRecursively("host1", Agent.system, NodeRepositoryTest.class.getSimpleName());
            fail("Should not be able to dirty host1");
        } catch (IllegalArgumentException ignored) { } // Expected;

        assertEquals(asSet("host2", "node20"), filterNodes(tester, node -> node.state() == Node.State.dirty));
    }

    private static Set<String> asSet(String... elements) {
        return new HashSet<>(Arrays.asList(elements));
    }

    private static Set<String> filterNodes(NodeRepositoryTester tester, Predicate<Node> filter) {
        return tester.nodeRepository()
                .getNodes().stream()
                .filter(filter)
                .map(Node::hostname)
                .collect(Collectors.toSet());
    }

}