summaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/maintenance/RebalancerTest.java
blob: 3e9744a6518a1eaa02dbe2187b93463bcdd780b4 (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
// Copyright 2019 Oath Inc. 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.ClusterSpec;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.Flavor;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.Zone;
import com.yahoo.config.provisioning.FlavorsConfig;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.node.Agent;
import com.yahoo.vespa.hosted.provision.provisioning.FlavorConfigBuilder;
import com.yahoo.vespa.hosted.provision.provisioning.HostResourcesCalculator;
import com.yahoo.vespa.hosted.provision.provisioning.ProvisioningTester;
import org.junit.Test;

import java.time.Duration;
import java.util.HashSet;
import java.util.List;

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

/**
 * @author bratseth
 */
public class RebalancerTest {

    @Test
    public void testRebalancing() {
        ProvisioningTester tester = new ProvisioningTester.Builder().zone(new Zone(Environment.perf, RegionName.from("us-east"))).flavorsConfig(flavorsConfig()).build();
        Rebalancer rebalancer = new Rebalancer(tester.nodeRepository(),
                                               new IdentityHostResourcesCalculator(),
                                               tester.clock(),
                                               Duration.ofMinutes(1));

        NodeResources cpuResources = new NodeResources(8, 4, 1, 0.1);
        NodeResources memResources = new NodeResources(4, 9, 1, 0.1);

        tester.makeReadyNodes(3, "flt", NodeType.host, 8);
        tester.deployZoneApp();

        // Cpu heavy application - causing 1 of these nodes to be skewed
        ApplicationId cpuApp = makeApplicationId("t1", "a1");
        deployApp(cpuApp, clusterSpec("c"), cpuResources, tester, 1);
        String cpuSkewedNodeHostname = tester.nodeRepository().getNodes(cpuApp).get(0).hostname();

        rebalancer.maintain();
        assertFalse("No better place to move the skewed node, so no action is taken",
                    tester.nodeRepository().getNode(cpuSkewedNodeHostname).get().status().wantToRetire());

        tester.makeReadyNodes(1, "cpu", NodeType.host, 8);

        rebalancer.maintain();
        assertTrue("We can now move the node to the cpu skewed host to reduce skew",
                   tester.nodeRepository().getNode(cpuSkewedNodeHostname).get().status().wantToRetire());

        ApplicationId memApp = makeApplicationId("t2", "a2");
        deployApp(memApp, clusterSpec("c"), memResources, tester, 1);
        assertEquals("Assigned to a flat node as that causes least skew", "flt",
                     tester.nodeRepository().list().parentOf(tester.nodeRepository().getNodes(memApp).get(0)).get().flavor().name());
        String memSkewedNodeHostname = tester.nodeRepository().getNodes(memApp).get(0).hostname();

        tester.makeReadyNodes(1, "mem", NodeType.host, 8);
        rebalancer.maintain();
        assertFalse("The mem skewed node is not set want to retire as the cpu skewed node still is",
                    tester.nodeRepository().getNode(memSkewedNodeHostname).get().status().wantToRetire());

        Node cpuSkewedNode = tester.nodeRepository().getNode(cpuSkewedNodeHostname).get();
        tester.nodeRepository().write(cpuSkewedNode.withWantToRetire(false, Agent.system, tester.clock().instant()),
                                      tester.nodeRepository().lock(cpuSkewedNode));
        rebalancer.maintain();
        assertTrue("The mem skewed node is now scheduled for moving",
                    tester.nodeRepository().getNode(memSkewedNodeHostname).get().status().wantToRetire());
        assertFalse("(The cpu skewed node is not because it causes slightly less skew)",
                    tester.nodeRepository().getNode(cpuSkewedNodeHostname).get().status().wantToRetire());
    }

    private ClusterSpec clusterSpec(String clusterId) {
        return ClusterSpec.request(ClusterSpec.Type.content, ClusterSpec.Id.from(clusterId), Version.fromString("6.42"), false);
    }

    private ApplicationId makeApplicationId(String tenant, String appName) {
        return ApplicationId.from(tenant, appName, "default");
    }

    private void deployApp(ApplicationId id, ClusterSpec spec, NodeResources flavor, ProvisioningTester tester, int nodeCount) {
        List<HostSpec> hostSpec = tester.prepare(id, spec, nodeCount, 1, flavor);
        tester.activate(id, new HashSet<>(hostSpec));
    }

    private FlavorsConfig flavorsConfig() {
        FlavorConfigBuilder b = new FlavorConfigBuilder();
        b.addFlavor("flt", 30, 30, 40, 3, Flavor.Type.BARE_METAL);
        b.addFlavor("cpu", 40, 20, 40, 3, Flavor.Type.BARE_METAL);
        b.addFlavor("mem", 20, 40, 40, 3, Flavor.Type.BARE_METAL);
        return b.build();
    }

    private static class IdentityHostResourcesCalculator implements HostResourcesCalculator {

        @Override
        public NodeResources availableCapacityOf(NodeResources hostResources) {
            return hostResources;
        }

    }

}