summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterTimeseries.java
blob: 2b4ba3fbbcbe74551756f48709b7fd2a9d7d0c93 (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
// Copyright Verizon Media. 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.config.provision.ClusterSpec;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.applications.Cluster;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * A series of metric snapshots for all nodes in a cluster
 *
 * @author bratseth
 */
public class ClusterTimeseries {

    private final List<Node> clusterNodes;

    final int measurementCount;
    final int measurementCountWithoutStale;
    final int measurementCountWithoutStaleOutOfService;
    final int measurementCountWithoutStaleOutOfServiceUnstable;

    /** The measurements for all hosts in this snapshot */
    private final List<NodeTimeseries> nodeTimeseries;

    public ClusterTimeseries(Cluster cluster, List<Node> clusterNodes, MetricsDb db, NodeRepository nodeRepository) {
        this.clusterNodes = clusterNodes;
        ClusterSpec clusterSpec = clusterNodes.get(0).allocation().get().membership().cluster();
        var timeseries = db.getNodeTimeseries(nodeRepository.clock().instant().minus(Autoscaler.scalingWindow(clusterSpec)),
                                              clusterNodes.stream().map(Node::hostname).collect(Collectors.toSet()));
        Map<String, Instant> startTimePerNode = metricStartTimes(cluster, clusterNodes, timeseries, nodeRepository);

        measurementCount = timeseries.stream().mapToInt(m -> m.size()).sum();

        timeseries = filterStale(timeseries, startTimePerNode);
        measurementCountWithoutStale = timeseries.stream().mapToInt(m -> m.size()).sum();

        timeseries = filter(timeseries, snapshot -> snapshot.inService());
        measurementCountWithoutStaleOutOfService = timeseries.stream().mapToInt(m -> m.size()).sum();

        timeseries = filter(timeseries, snapshot -> snapshot.stable());
        measurementCountWithoutStaleOutOfServiceUnstable = timeseries.stream().mapToInt(m -> m.size()).sum();

        this.nodeTimeseries = timeseries;
    }

    /**
     * Returns the instant of the oldest metric to consider for each node, or an empty map if metrics from the
     * entire (max) window should be considered.
     */
    private Map<String, Instant> metricStartTimes(Cluster cluster,
                                                  List<Node> clusterNodes,
                                                  List<NodeTimeseries> nodeTimeseries,
                                                  NodeRepository nodeRepository) {
        Map<String, Instant> startTimePerHost = new HashMap<>();
        if ( ! cluster.scalingEvents().isEmpty()) {
            var deployment = cluster.scalingEvents().get(cluster.scalingEvents().size() - 1);
            for (Node node : clusterNodes) {
                startTimePerHost.put(node.hostname(), nodeRepository.clock().instant()); // Discard all unless we can prove otherwise
                var nodeGenerationMeasurements =
                        nodeTimeseries.stream().filter(m -> m.hostname().equals(node.hostname())).findAny();
                if (nodeGenerationMeasurements.isPresent()) {
                    var firstMeasurementOfCorrectGeneration =
                            nodeGenerationMeasurements.get().asList().stream()
                                                      .filter(m -> m.generation() >= deployment.generation())
                                                      .findFirst();
                    if (firstMeasurementOfCorrectGeneration.isPresent()) {
                        startTimePerHost.put(node.hostname(), firstMeasurementOfCorrectGeneration.get().at());
                    }
                }
            }
        }
        return startTimePerHost;
    }

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

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

    /** Returns the average load of this resource in this */
    public double averageLoad(Resource resource) {
        int measurementCount = nodeTimeseries.stream().mapToInt(m -> m.size()).sum();
        double measurementSum = nodeTimeseries.stream().flatMap(m -> m.asList().stream()).mapToDouble(m -> value(resource, m)).sum();
        return measurementSum / measurementCount;
    }

    private double value(Resource resource, MetricSnapshot snapshot) {
        switch (resource) {
            case cpu: return snapshot.cpu();
            case memory: return snapshot.memory();
            case disk: return snapshot.disk();
            default: throw new IllegalArgumentException("Got an unknown resource " + resource);
        }
    }

    private List<NodeTimeseries> filterStale(List<NodeTimeseries> timeseries,
                                             Map<String, Instant> startTimePerHost) {
        if (startTimePerHost.isEmpty()) return timeseries; // Map is either empty or complete
        return timeseries.stream().map(m -> m.justAfter(startTimePerHost.get(m.hostname()))).collect(Collectors.toList());
    }

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

}