summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/CapacityReportMaintainer.java
blob: 00856e6653cf38b1911a7c083ac8b20bcf7df899 (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
// Copyright 2020 Oath Inc. 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.jdisc.Metric;
import java.util.logging.Level;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;

import java.time.Duration;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import java.util.*;

/**
 * Performs analysis on the node repository to produce metrics that pertain to the capacity of the node repository.
 * These metrics include:
 * Spare host capacity, or how many hosts the repository can stand to lose without ending up in a situation where it's
 * unable to find a new home for orphaned tenants.
 * Overcommitted hosts, which tracks if there are any hosts whose capacity is less than the sum of its children's.
 *
 * @author mgimle
 */
public class CapacityReportMaintainer extends Maintainer {

    private final Metric metric;
    private final NodeRepository nodeRepository;
    private static final Logger log = Logger.getLogger(CapacityReportMaintainer.class.getName());

    CapacityReportMaintainer(NodeRepository nodeRepository,
                             Metric metric,
                             Duration interval) {
        super(nodeRepository, interval);
        this.nodeRepository = nodeRepository;
        this.metric = Objects.requireNonNull(metric);
    }

    @Override
    protected void maintain() {
        if (nodeRepository.zone().cloud().value().equals("aws")) return; // Hosts and nodes are 1-1

        CapacityChecker capacityChecker = new CapacityChecker(this.nodeRepository);
        List<Node> overcommittedHosts = capacityChecker.findOvercommittedHosts();
        if (overcommittedHosts.size() != 0) {
            log.log(LogLevel.WARNING, String.format("%d nodes are overcommitted! [ %s ]", overcommittedHosts.size(),
                                                    overcommittedHosts.stream().map(Node::hostname).collect(Collectors.joining(", "))));
        }
        metric.set("overcommittedHosts", overcommittedHosts.size(), null);

        Optional<CapacityChecker.HostFailurePath> failurePath = capacityChecker.worstCaseHostLossLeadingToFailure();
        if (failurePath.isPresent()) {
            int worstCaseHostLoss = failurePath.get().hostsCausingFailure.size();
            metric.set("spareHostCapacity", worstCaseHostLoss - 1, null);
        }
    }

}