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
|
// 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.vespa.hosted.provision.NodeList;
import com.yahoo.vespa.hosted.provision.applications.Cluster;
import java.time.Duration;
import java.util.List;
import java.util.OptionalDouble;
import java.util.function.Predicate;
import static com.yahoo.vespa.hosted.provision.autoscale.ClusterModel.warmupDuration;
/**
* A series of metric snapshots for the nodes of a cluster used to compute load
*
* @author bratseth
*/
public class ClusterNodesTimeseries {
private final NodeList clusterNodes;
/** The measurements for all nodes in this snapshot */
private final List<NodeTimeseries> timeseries;
private int initialCount, afterWarmupCount, afterStableCount, finalCount;
public ClusterNodesTimeseries(Duration period, Cluster cluster, NodeList clusterNodes, MetricsDb db) {
this.clusterNodes = clusterNodes;
// See warmupDuration*4 into the past to see any generation change in it.
// If none can be detected we assume the node is new/was down.
// If either this is the case, or there is a generation change, we ignore
// the first warmupWindow metrics.
var timeseries = db.getNodeTimeseries(period.plus(warmupDuration.multipliedBy(4)), clusterNodes);
initialCount = totalMeasurementsIn(timeseries);
if (cluster.lastScalingEvent().isPresent()) {
long currentGeneration = cluster.lastScalingEvent().get().generation();
timeseries = keepGenerationAfterWarmup(timeseries, currentGeneration);
afterWarmupCount = totalMeasurementsIn(timeseries);
}
timeseries = keep(timeseries, snapshot -> snapshot.inService() && snapshot.stable());
afterStableCount = totalMeasurementsIn(timeseries);
timeseries = keep(timeseries, snapshot -> ! snapshot.at().isBefore(db.clock().instant().minus(period)));
finalCount = totalMeasurementsIn(timeseries);
this.timeseries = timeseries;
}
private ClusterNodesTimeseries(NodeList clusterNodes, List<NodeTimeseries> timeseries) {
this.clusterNodes = clusterNodes;
this.timeseries = timeseries;
}
public String description() {
return "initial measurements: " + initialCount +
", after warmup: " + afterWarmupCount +
", after stable: " + afterStableCount +
", final measurements: " + finalCount;
}
public boolean isEmpty() {
return measurementsPerNode() == 0;
}
/** Returns the average number of measurements per node */
public double measurementsPerNode() {
if (clusterNodes.size() == 0) return 0;
return (double) totalMeasurementsIn(timeseries) / clusterNodes.size();
}
private int totalMeasurementsIn(List<NodeTimeseries> timeseries) {
return timeseries.stream().mapToInt(m -> m.size()).sum();
}
/** Returns the number of nodes measured in this */
public int nodesMeasured() { return timeseries.size(); }
/**
* Returns the "peak load" in this: Which is for each load dimension,
* the average of the highest reading for that dimension on each node.
*/
public Load peakLoad() {
return new Load(peakLoad(Load.Dimension.cpu), peakLoad(Load.Dimension.memory), peakLoad(Load.Dimension.disk));
}
private double peakLoad(Load.Dimension dimension) {
double total = 0;
int count = 0;
for (var nodeTimeseries : timeseries) {
OptionalDouble value = nodeTimeseries.peak(dimension);
if (value.isEmpty()) continue;
total += value.getAsDouble();
count++;
}
if (count == 0) return 0;
return total / count;
}
private static List<NodeTimeseries> keep(List<NodeTimeseries> timeseries, Predicate<NodeMetricSnapshot> filter) {
return timeseries.stream().map(nodeTimeseries -> nodeTimeseries.keep(filter)).toList();
}
private List<NodeTimeseries> keepGenerationAfterWarmup(List<NodeTimeseries> timeseries, long currentGeneration) {
return timeseries.stream()
.map(nodeTimeseries -> nodeTimeseries.keepGenerationAfterWarmup(currentGeneration,
clusterNodes.node(nodeTimeseries.hostname())))
.toList();
}
public static ClusterNodesTimeseries empty() {
return new ClusterNodesTimeseries(NodeList.of(), List.of());
}
}
|