aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/NodeMetricsDbMaintainer.java
blob: 4c5ea45f3ecaf734c12eb42d1171a3ad3a70a554 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.maintenance;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.jdisc.Metric;
import com.yahoo.lang.MutableInteger;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.autoscale.MetricsFetcher;
import com.yahoo.vespa.hosted.provision.autoscale.MetricsResponse;
import com.yahoo.yolean.Exceptions;

import java.time.Duration;
import java.util.Set;
import java.util.logging.Level;

/**
 * Maintainer which keeps the node metric db up to date by periodically fetching metrics from all active nodes.
 *
 * @author bratseth
 */
public class NodeMetricsDbMaintainer extends NodeRepositoryMaintainer {

    private static final int maxWarningsPerInvocation = 2;

    private final MetricsFetcher metricsFetcher;

    public NodeMetricsDbMaintainer(NodeRepository nodeRepository,
                                   MetricsFetcher metricsFetcher,
                                   Duration interval,
                                   Metric metric) {
        super(nodeRepository, interval, metric);
        this.metricsFetcher = metricsFetcher;
    }

    @Override
    protected double maintain() {
        int attempts = 0;
        var failures = new MutableInteger(0);
        try {
            Set<ApplicationId> applications = activeNodesByApplication().keySet();
            if (applications.isEmpty()) return 1.0;

            long pauseMs = interval().toMillis() / Math.max(4, applications.size()); // spread requests over interval
            int done = 0;
            for (ApplicationId application : applications) {
                if (shuttingDown()) return asSuccessFactorDeviation(attempts, failures.get());

                attempts++;
                metricsFetcher.fetchMetrics(application)
                              .whenComplete((metricsResponse, exception) -> handleResponse(metricsResponse,
                                                                                           exception,
                                                                                           failures,
                                                                                           application));
                if (++done < applications.size())
                    Thread.sleep(pauseMs);
            }

            nodeRepository().metricsDb().gc();

            return asSuccessFactorDeviation(attempts, failures.get());
        }
        catch (InterruptedException e) {
            return asSuccessFactorDeviation(attempts, failures.get());
        }
    }

    @Override
    public void shutdown() {
        super.shutdown();
        metricsFetcher.deconstruct();
    }

    private void handleResponse(MetricsResponse response,
                                Throwable exception,
                                MutableInteger failures,
                                ApplicationId application) {
        if (exception != null) {
            if (failures.get() < maxWarningsPerInvocation)
                log.log(Level.WARNING, "Could not update metrics for " + application + ": " +
                                       Exceptions.toMessageString(exception));
            failures.add(1);
        }
        else if (response != null) {
            nodeRepository().metricsDb().addNodeMetrics(response.nodeMetrics());
            nodeRepository().metricsDb().addClusterMetrics(application, response.clusterMetrics());
        }
    }

}