aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/NodeRepositoryMaintainer.java
blob: c7be505acaabf6b980f2f556b699336f1bcf74b3 (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
// Copyright Vespa.ai. 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.concurrent.maintenance.JobMetrics;
import com.yahoo.concurrent.maintenance.Maintainer;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.NodeType;
import com.yahoo.jdisc.Metric;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeList;
import com.yahoo.vespa.hosted.provision.NodeRepository;

import java.time.Clock;
import java.time.Duration;
import java.util.Map;

/**
 * A maintainer is some job which runs at a fixed rate to perform some maintenance task on the node repo.
 *
 * @author bratseth
 */
public abstract class NodeRepositoryMaintainer extends Maintainer {

    private final NodeRepository nodeRepository;

    public NodeRepositoryMaintainer(NodeRepository nodeRepository, Duration interval, Metric metric) {
        super(null, interval, nodeRepository.clock(), nodeRepository.jobControl(),
              new NodeRepositoryJobMetrics(metric), nodeRepository.database().cluster(), true);
        this.nodeRepository = nodeRepository;
    }

    protected static Duration min(Duration a, Duration b) {
        return a.toMillis() < b.toMillis() ? a : b;
    }

    /** Returns the node repository */
    protected NodeRepository nodeRepository() { return nodeRepository; }

    /** Returns the node repository clock */
    protected Clock clock() { return nodeRepository.clock(); }

    /** A utility to group active tenant nodes by application */
    protected Map<ApplicationId, NodeList> activeNodesByApplication() {
        return nodeRepository().nodes()
                               .list(Node.State.active)
                               .nodeType(NodeType.tenant)
                               .not().tester()
                               .groupingBy(node -> node.allocation().get().owner());
    }

    private static class NodeRepositoryJobMetrics extends JobMetrics {

        private final Metric metric;

        public NodeRepositoryJobMetrics(Metric metric) {
            this.metric = metric;
        }

        @Override
        public void completed(String job, double successFactor, long duration) {
            var context = metric.createContext(Map.of("job", job));
            metric.set("maintenance.successFactorDeviation", successFactor, context);
            metric.set("maintenance.duration", duration, context);
        }

    }

}