summaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/maintenance/InactiveAndFailedExpirerTest.java
blob: d409927701c81d139f247a8f67e0efb310f13653 (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
// 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.maintenance;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.node.Agent;
import com.yahoo.vespa.hosted.provision.node.History;
import com.yahoo.vespa.hosted.provision.provisioning.ProvisioningTester;
import com.yahoo.vespa.hosted.provision.testutils.MockDeployer;
import com.yahoo.vespa.orchestrator.OrchestrationException;
import com.yahoo.vespa.orchestrator.Orchestrator;
import org.junit.Test;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

/**
 * @author bratseth
 * @author mpolden
 */
public class InactiveAndFailedExpirerTest {

    private final NodeResources nodeResources = new NodeResources(2, 8, 50, 1);

    private final ApplicationId applicationId = ApplicationId.from(TenantName.from("foo"),
                                                                   ApplicationName.from("bar"),
                                                                   InstanceName.from("fuz"));

    @Test
    public void inactive_and_failed_times_out() {
        ProvisioningTester tester = new ProvisioningTester.Builder().zone(new Zone(Environment.prod, RegionName.from("us-east"))).build();
        List<Node> nodes = tester.makeReadyNodes(2, nodeResources);

        // Allocate then deallocate 2 nodes
        ClusterSpec cluster = ClusterSpec.request(ClusterSpec.Type.content, ClusterSpec.Id.from("test"), Version.fromString("6.42"), false);
        List<HostSpec> preparedNodes = tester.prepare(applicationId, cluster, Capacity.fromCount(2, nodeResources), 1);
        tester.activate(applicationId, new HashSet<>(preparedNodes));
        assertEquals(2, tester.getNodes(applicationId, Node.State.active).size());
        tester.deactivate(applicationId);
        List<Node> inactiveNodes = tester.getNodes(applicationId, Node.State.inactive).asList();
        assertEquals(2, inactiveNodes.size());

        // Inactive times out
        tester.advanceTime(Duration.ofMinutes(14));
        new InactiveExpirer(tester.nodeRepository(), tester.clock(), Duration.ofMinutes(10)).run();
        assertEquals(0, tester.nodeRepository().getNodes(Node.State.inactive).size());
        List<Node> dirty = tester.nodeRepository().getNodes(Node.State.dirty);
        assertEquals(2, dirty.size());
        assertFalse(dirty.get(0).allocation().isPresent());
        assertFalse(dirty.get(1).allocation().isPresent());

        // One node is set back to ready
        Node ready = tester.nodeRepository().setReady(Collections.singletonList(dirty.get(0)), Agent.system, getClass().getSimpleName()).get(0);
        assertEquals("Allocated history is removed on readying",
                Arrays.asList(History.Event.Type.provisioned, History.Event.Type.readied),
                ready.history().events().stream().map(History.Event::type).collect(Collectors.toList()));

        // Dirty times out for the other one
        tester.advanceTime(Duration.ofMinutes(14));
        new DirtyExpirer(tester.nodeRepository(), tester.clock(), Duration.ofMinutes(10)).run();
        assertEquals(0, tester.nodeRepository().getNodes(NodeType.tenant, Node.State.dirty).size());
        List<Node> failed = tester.nodeRepository().getNodes(NodeType.tenant, Node.State.failed);
        assertEquals(1, failed.size());
        assertEquals(1, failed.get(0).status().failCount());
    }

    @Test
    public void reboot_generation_is_increased_when_node_moves_to_dirty() {
        ProvisioningTester tester = new ProvisioningTester.Builder().zone(new Zone(Environment.prod, RegionName.from("us-east"))).build();
        List<Node> nodes = tester.makeReadyNodes(2, nodeResources);

        // Allocate and deallocate a single node
        ClusterSpec cluster = ClusterSpec.request(ClusterSpec.Type.content,
                                                  ClusterSpec.Id.from("test"),
                                                  Version.fromString("6.42"),
                                                  false);
        List<HostSpec> preparedNodes = tester.prepare(applicationId, cluster, Capacity.fromCount(2, nodeResources), 1);
        tester.activate(applicationId, new HashSet<>(preparedNodes));
        assertEquals(2, tester.getNodes(applicationId, Node.State.active).size());
        tester.deactivate(applicationId);
        List<Node> inactiveNodes = tester.getNodes(applicationId, Node.State.inactive).asList();
        assertEquals(2, inactiveNodes.size());

        // Check reboot generation before node is moved. New nodes transition from provisioned to dirty, so their
        // wanted reboot generation will always be 1.
        long wantedRebootGeneration = inactiveNodes.get(0).status().reboot().wanted();
        assertEquals(1, wantedRebootGeneration);

        // Inactive times out and node is moved to dirty
        tester.advanceTime(Duration.ofMinutes(14));
        new InactiveExpirer(tester.nodeRepository(), tester.clock(), Duration.ofMinutes(10)).run();
        List<Node> dirty = tester.nodeRepository().getNodes(Node.State.dirty);
        assertEquals(2, dirty.size());

        // Reboot generation is increased
        assertEquals(wantedRebootGeneration + 1, dirty.get(0).status().reboot().wanted());
    }

    @Test
    public void node_that_wants_to_retire_is_moved_to_parked() throws OrchestrationException {
        ProvisioningTester tester = new ProvisioningTester.Builder().zone(new Zone(Environment.prod, RegionName.from("us-east"))).build();
        ClusterSpec cluster = ClusterSpec.request(ClusterSpec.Type.content, ClusterSpec.Id.from("test"),
                                                  Version.fromString("6.42"), false);
        tester.makeReadyNodes(5, nodeResources);

        // Allocate two nodes
        {
            List<HostSpec> hostSpecs = tester.prepare(applicationId,
                                                      cluster,
                                                      Capacity.fromCount(2, nodeResources),
                                                      1);
            tester.activate(applicationId, new HashSet<>(hostSpecs));
            assertEquals(2, tester.getNodes(applicationId, Node.State.active).size());
        }

        // Flag one node for retirement and redeploy
        {
            Node toRetire = tester.getNodes(applicationId, Node.State.active).asList().get(0);
            tester.patchNode(toRetire.withWantToRetire(true, Agent.operator, tester.clock().instant()));
            List<HostSpec> hostSpecs = tester.prepare(applicationId, cluster, Capacity.fromCount(2, nodeResources), 1);
            tester.activate(applicationId, new HashSet<>(hostSpecs));
        }

        // Retire times out and one node is moved to inactive
        tester.advanceTime(Duration.ofMinutes(11)); // Trigger RetiredExpirer
        MockDeployer deployer = new MockDeployer(
                tester.provisioner(),
                tester.clock(),
                Collections.singletonMap(
                        applicationId,
                        new MockDeployer.ApplicationContext(applicationId, cluster,
                                                            Capacity.fromCount(2,
                                                                               nodeResources,
                                                                               false, true),
                                                            1)
                )
        );
        Orchestrator orchestrator = mock(Orchestrator.class);
        doThrow(new RuntimeException()).when(orchestrator).acquirePermissionToRemove(any());
        new RetiredExpirer(tester.nodeRepository(), tester.orchestrator(), deployer, tester.clock(), Duration.ofDays(30),
                           Duration.ofMinutes(10)).run();
        assertEquals(1, tester.nodeRepository().getNodes(Node.State.inactive).size());

        // Inactive times out and one node is moved to parked
        tester.advanceTime(Duration.ofMinutes(11)); // Trigger InactiveExpirer
        new InactiveExpirer(tester.nodeRepository(), tester.clock(), Duration.ofMinutes(10)).run();
        assertEquals(1, tester.nodeRepository().getNodes(Node.State.parked).size());
    }

    @Test
    public void testersExpireImmediately() {
        ApplicationId testerId = ApplicationId.from(applicationId.tenant().value(),
                                                    applicationId.application().value(),
                                                    applicationId.instance().value() + "-t");

        // Allocate then deallocate a node
        ProvisioningTester tester = new ProvisioningTester.Builder().zone(new Zone(Environment.prod, RegionName.from("us-east"))).build();
        tester.makeReadyNodes(1, nodeResources);
        ClusterSpec cluster = ClusterSpec.request(ClusterSpec.Type.content, ClusterSpec.Id.from("test"), Version.fromString("6.42"), false);
        List<HostSpec> preparedNodes = tester.prepare(testerId, cluster, Capacity.fromCount(2, nodeResources), 1);
        tester.activate(testerId, new HashSet<>(preparedNodes));
        assertEquals(1, tester.getNodes(testerId, Node.State.active).size());
        tester.deactivate(testerId);
        List<Node> inactiveNodes = tester.getNodes(testerId, Node.State.inactive).asList();
        assertEquals(1, inactiveNodes.size());

        // See that nodes are moved to dirty immediately.
        new InactiveExpirer(tester.nodeRepository(), tester.clock(), Duration.ofMinutes(10)).run();
        assertEquals(0, tester.nodeRepository().getNodes(Node.State.inactive).size());
        List<Node> dirty = tester.nodeRepository().getNodes(Node.State.dirty);
        assertEquals(1, dirty.size());
        assertFalse(dirty.get(0).allocation().isPresent());

    }

}