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

import com.yahoo.collections.Pair;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeList;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.function.IntFunction;

/**
 * A helper for applying load to an application represented by a fixture,
 *
 * @author bratseth
 */
public class Loader {

    private final Fixture fixture;
    private final Duration samplingInterval = Duration.ofSeconds(150L);

    public Loader(Fixture fixture) {
        this.fixture = fixture;
    }

    /** Assign measured zero traffic in the same way as the system will. */
    public Duration zeroTraffic(int measurements, int prodRegions) {
        try (var lock = fixture.tester().nodeRepository().applications().lock(fixture.applicationId())) {
            var statusWithZeroLoad = fixture.application().status()
                                            .withCurrentReadShare(0)
                                            // the line below from TrafficShareUpdater
                                            .withMaxReadShare(prodRegions < 2 ? 1.0 : 1.0 / ( prodRegions - 1.0));
            fixture.tester().nodeRepository().applications().put(fixture.application().with(statusWithZeroLoad), lock);
        }
        return addQueryRateMeasurements(measurements, (n) -> 0.0);
    }

    /**
     * Adds measurements with the given resource value and ideal values for the other resources,
     * scaled to take one node redundancy into account.
     * (I.e we adjust to measure a bit lower load than "naively" wanted to offset for the autoscaler
     * wanting to see the ideal load with one node missing.)
     *
     * @param count the number of measurements
     */
    public Duration addCpuMeasurements(double value, int count) {
        var idealLoad = fixture.clusterModel().idealLoad();
        NodeList nodes = fixture.nodes();
        float oneExtraNodeFactor = (float)(nodes.size() - 1.0) / (nodes.size());
        Load load = new Load(value, idealLoad.memory(), idealLoad.disk()).multiply(oneExtraNodeFactor);
        Instant initialTime = fixture.tester().clock().instant();
        for (int i = 0; i < count; i++) {
            fixture.tester().clock().advance(samplingInterval);
            for (Node node : nodes) {
                fixture.tester().nodeMetricsDb().addNodeMetrics(List.of(new Pair<>(node.hostname(),
                                                                         new NodeMetricSnapshot(fixture.tester().clock().instant(),
                                                                                                load,
                                                                                                0,
                                                                                                true,
                                                                                                true,
                                                                                                0.0))));
            }
        }
        return Duration.between(initialTime, fixture.tester().clock().instant());
    }

    /** Creates the given number of measurements, spaced 5 minutes between, using the given function */
    public Duration addLoadMeasurements(int measurements, IntFunction<Double> queryRate, IntFunction<Double> writeRate) {
        Instant initialTime = fixture.tester().clock().instant();
        for (int i = 0; i < measurements; i++) {
            fixture.tester().nodeMetricsDb().addClusterMetrics(fixture.applicationId(),
                                                               Map.of(fixture.clusterId(), new ClusterMetricSnapshot(fixture.tester().clock().instant(),
                                                                                                                     queryRate.apply(i),
                                                                                                                     writeRate.apply(i))));
            fixture.tester().clock().advance(samplingInterval);
        }
        return Duration.between(initialTime, fixture.tester().clock().instant());
    }

    public void applyCpuLoad(double cpuLoad, int measurements) {
        addCpuMeasurements((float)cpuLoad, measurements);
        fixture.tester().clock().advance(samplingInterval.negated().multipliedBy(measurements));
        addQueryRateMeasurements(measurements, t -> t == 0 ? 20.0 : 10.0); // Query traffic only
    }

    public void applyMemLoad(double memLoad, int measurements) {
        addMemMeasurements(memLoad, measurements);
        fixture.tester().clock().advance(samplingInterval.negated().multipliedBy(measurements));
        addQueryRateMeasurements(measurements, t -> t == 0 ? 20.0 : 10.0); // Query traffic only
    }

    /**
     * Adds measurements with the given resource value and ideal values for the other resources,
     * scaled to take one node redundancy into account.
     * (I.e we adjust to measure a bit lower load than "naively" wanted to offset for the autoscaler
     * wanting to see the ideal load with one node missing.)
     */
    public void addMemMeasurements(double value, int count) {
        var idealLoad = fixture.clusterModel().idealLoad();
        NodeList nodes = fixture.nodes();
        float oneExtraNodeFactor = (float)(nodes.size() - 1.0) / (nodes.size());
        Load load = new Load(idealLoad.cpu(), value, idealLoad.disk()).multiply(oneExtraNodeFactor);
        for (int i = 0; i < count; i++) {
            fixture.tester().clock().advance(samplingInterval);
            for (Node node : nodes) {
                fixture.tester().nodeMetricsDb().addNodeMetrics(List.of(new Pair<>(node.hostname(),
                                                                        new NodeMetricSnapshot(fixture.tester().clock().instant(),
                                                                                               load,
                                                                                               0,
                                                                                               true,
                                                                                               true,
                                                                                               0.0))));
            }
        }
    }

    public Duration addMeasurements(Load load, int count)  {
        return addMeasurements(load, 0, true, true, count);
    }

    public Duration addMeasurements(Load load, int generation, boolean inService, boolean stable, int count) {
        Instant initialTime = fixture.tester().clock().instant();
        for (int i = 0; i < count; i++) {
            fixture.tester().clock().advance(samplingInterval);
            for (Node node : fixture.nodes()) {
                fixture.tester().nodeMetricsDb().addNodeMetrics(List.of(new Pair<>(node.hostname(),
                                                                        new NodeMetricSnapshot(fixture.tester().clock().instant(),
                                                                                               load,
                                                                                               generation,
                                                                                               inService,
                                                                                               stable,
                                                                                               0.0))));
            }
        }
        return Duration.between(initialTime, fixture.tester().clock().instant());
    }

    public void applyLoad(Load load, int measurements) {
        addMeasurements(load, measurements);
        fixture.tester().clock().advance(samplingInterval.negated().multipliedBy(measurements));
        addQueryRateMeasurements(measurements, t -> t == 0 ? 20.0 : 10.0); // Query traffic only
    }

    public void applyLoad(Load load, int generation, boolean inService, boolean stable, int measurements) {
        addMeasurements(load, generation, inService, stable, measurements);
        fixture.tester().clock().advance(samplingInterval.negated().multipliedBy(measurements));
        addQueryRateMeasurements(measurements, t -> t == 0 ? 20.0 : 10.0); // Query traffic only
    }

    public Duration addQueryRateMeasurements(int measurements, IntFunction<Double> queryRate) {
        Instant initialTime = fixture.tester().clock().instant();
        for (int i = 0; i < measurements; i++) {
            fixture.tester().nodeMetricsDb().addClusterMetrics(fixture.applicationId(),
                                                     Map.of(fixture.clusterId(), new ClusterMetricSnapshot(fixture.tester().clock().instant(),
                                                                                                   queryRate.apply(i),
                                                                                                   0.0)));
            fixture.tester().clock().advance(samplingInterval);
        }
        return Duration.between(initialTime, fixture.tester().clock().instant());
    }

}