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

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

    /**
     * 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) {
        NodeList nodes = fixture.nodes();
        float oneExtraNodeFactor = (float)(nodes.size() - 1.0) / (nodes.size());
        Instant initialTime = fixture.tester().clock().instant();
        for (int i = 0; i < count; i++) {
            fixture.tester().clock().advance(Duration.ofSeconds(150));
            for (Node node : nodes) {
                Load load = new Load(value,
                                     ClusterModel.idealMemoryLoad,
                                     ClusterModel.idealContentDiskLoad).multiply(oneExtraNodeFactor);
                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(Duration.ofMinutes(5));
        }
        return Duration.between(initialTime, fixture.tester().clock().instant());
    }

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

    public void applyMemLoad(double memLoad, int measurements) {
        Duration samplingInterval = Duration.ofSeconds(150L); // in addMemMeasurements
        addMemMeasurements(memLoad, measurements);
        fixture.tester().clock().advance(samplingInterval.negated().multipliedBy(measurements));
        addQueryRateMeasurements(measurements, samplingInterval, 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) {
        NodeList nodes = fixture.nodes();
        float oneExtraNodeFactor = (float)(nodes.size() - 1.0) / (nodes.size());
        for (int i = 0; i < count; i++) {
            fixture.tester().clock().advance(Duration.ofMinutes(1));
            for (Node node : nodes) {
                Load load = new Load(0.2,
                                     value,
                                     ClusterModel.idealContentDiskLoad).multiply(oneExtraNodeFactor);
                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(double cpu, double memory, double disk, int count)  {
        return addMeasurements(cpu, memory, disk, 0, true, true, count);
    }

    public Duration addMeasurements(double cpu, double memory, double disk, 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(Duration.ofMinutes(1));
            for (Node node : fixture.nodes()) {
                fixture.tester().nodeMetricsDb().addNodeMetrics(List.of(new Pair<>(node.hostname(),
                                                                        new NodeMetricSnapshot(fixture.tester().clock().instant(),
                                                                                               new Load(cpu, memory, disk),
                                                                                               generation,
                                                                                               inService,
                                                                                               stable,
                                                                                               0.0))));
            }
        }
        return Duration.between(initialTime, fixture.tester().clock().instant());
    }

    public void applyLoad(double cpuLoad, double memoryLoad, double diskLoad, int measurements) {
        Duration samplingInterval = Duration.ofSeconds(150L); // in addCpuMeasurements
        addMeasurements(cpuLoad, memoryLoad, diskLoad, measurements);
        fixture.tester().clock().advance(samplingInterval.negated().multipliedBy(measurements));
        addQueryRateMeasurements(measurements, samplingInterval, t -> t == 0 ? 20.0 : 10.0); // Query traffic only
    }

    public void applyLoad(double cpuLoad, double memoryLoad, double diskLoad, int generation, boolean inService, boolean stable, int measurements) {
        Duration samplingInterval = Duration.ofSeconds(150L); // in addCpuMeasurements
        addMeasurements(cpuLoad, memoryLoad, diskLoad, generation, inService, stable, measurements);
        fixture.tester().clock().advance(samplingInterval.negated().multipliedBy(measurements));
        addQueryRateMeasurements(measurements, samplingInterval, t -> t == 0 ? 20.0 : 10.0); // Query traffic only
    }

    public Duration addQueryRateMeasurements(int measurements, Duration samplingInterval, 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());
    }

}