summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterNodesTimeseries.java
blob: 5ad4ef2e263cf5c4ce563cd7ebe97aafeae968e3 (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
// 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.time.Instant;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

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;

    public ClusterNodesTimeseries(Duration period, Cluster cluster, NodeList clusterNodes, MetricsDb db) {
        this.clusterNodes = clusterNodes;

        // See warmupSeconds*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);
        if (cluster.lastScalingEvent().isPresent()) {
            long currentGeneration = cluster.lastScalingEvent().get().generation();
            timeseries = keepCurrentGenerationAfterWarmup(timeseries, currentGeneration);
        }
        timeseries = keep(timeseries, snapshot -> snapshot.inService() && snapshot.stable());
        timeseries = keep(timeseries, snapshot -> ! snapshot.at().isBefore(db.clock().instant().minus(period)));
        this.timeseries = timeseries;
    }

    private ClusterNodesTimeseries(NodeList clusterNodes, List<NodeTimeseries> timeseries) {
        this.clusterNodes = clusterNodes;
        this.timeseries = timeseries;
    }

    /** Returns the average number of measurements per node */
    public int measurementsPerNode() {
        int measurementCount = timeseries.stream().mapToInt(m -> m.size()).sum();
        return measurementCount / clusterNodes.size();
    }

    /** Returns the number of nodes measured in this */
    public int nodesMeasured() { return timeseries.size(); }

    /** Returns the average load after the given instant */
    public Load averageLoad(Instant start) {
        Load total = Load.zero();
        int count = 0;
        for (var nodeTimeseries : timeseries) {
            for (var snapshot : nodeTimeseries.asList()) {
                if (snapshot.at().isBefore(start)) continue;
                total = total.add(snapshot.load());
                count++;
            }
        }
        return total.divide(count);
    }

    private static List<NodeTimeseries> keep(List<NodeTimeseries> timeseries, Predicate<NodeMetricSnapshot> filter) {
        return timeseries.stream().map(nodeTimeseries -> nodeTimeseries.keep(filter)).collect(Collectors.toList());
    }

    private static List<NodeTimeseries> keepCurrentGenerationAfterWarmup(List<NodeTimeseries> timeseries,
                                                                         long currentGeneration) {
        return timeseries.stream()
                         .map(nodeTimeseries -> nodeTimeseries.keepCurrentGenerationAfterWarmup(currentGeneration))
                         .collect(Collectors.toList());
    }

    public static ClusterNodesTimeseries empty() {
        return new ClusterNodesTimeseries(NodeList.of(), List.of());
    }

}