aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ResourceUsageStats.java
blob: aef5b1be468f61919cdbc9502519b52cdb76ef15 (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 Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vespa.clustercontroller.core.hostinfo.ContentNode;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

/**
 * Represents resource usage stats for the cluster that are exposed as metrics.
 */
public class ResourceUsageStats {

    // Max disk utilization (usage / limit) among all content nodes.
    private final double maxDiskUtilization;

    // Max memory utilization (usage / limit) among all content nodes.
    private final double maxMemoryUtilization;

    // The number of content nodes that are above at least one resource limit.
    // When this is above zero feed is blocked in the cluster.
    private final int nodesAboveLimit;

    private static final String diskResource = "disk";
    private static final String memoryResource = "memory";

    public ResourceUsageStats() {
        this.maxDiskUtilization = 0.0;
        this.maxMemoryUtilization = 0.0;
        this.nodesAboveLimit = 0;
    }

    public ResourceUsageStats(double maxDiskUtilization,
                              double maxMemoryUtilization,
                              int nodesAboveLimit) {
        this.maxDiskUtilization = maxDiskUtilization;
        this.maxMemoryUtilization = maxMemoryUtilization;
        this.nodesAboveLimit = nodesAboveLimit;
    }

    public double getMaxDiskUtilization() {
        return maxDiskUtilization;
    }

    public double getMaxMemoryUtilization() {
        return maxMemoryUtilization;
    }

    public int getNodesAboveLimit() {
        return nodesAboveLimit;
    }

    public static ResourceUsageStats calculateFrom(Collection<NodeInfo> nodeInfos,
                                                   Map<String, Double> feedBlockLimits,
                                                   Optional<ClusterStateBundle.FeedBlock> feedBlock) {
        double maxDiskUsage = 0.0;
        double maxMemoryUsage = 0.0;
        for (NodeInfo info : nodeInfos) {
            if (info.isStorage()) {
                var node = info.getHostInfo().getContentNode();
                maxDiskUsage = Double.max(maxDiskUsage, resourceUsageOf(diskResource, node));
                maxMemoryUsage = Double.max(maxMemoryUsage, resourceUsageOf(memoryResource, node));
            }
        }
        return new ResourceUsageStats(maxDiskUsage / limitOf(diskResource, feedBlockLimits),
                maxMemoryUsage / limitOf(memoryResource, feedBlockLimits),
                calculateNodesAboveLimit(feedBlock));
    }

    private static double resourceUsageOf(String type, ContentNode node) {
        var result = node.resourceUsageOf(type);
        return result.isPresent() ? result.get().getUsage() : 0.0;
    }

    private static int calculateNodesAboveLimit(Optional<ClusterStateBundle.FeedBlock> feedBlock) {
        if (!feedBlock.isPresent()) {
            return 0;
        }
        var exhaustions = feedBlock.get().getConcreteExhaustions();
        return (int) exhaustions.stream().map(resource -> resource.node).distinct().count();
    }

    private static double limitOf(String type, Map<String, Double> limits) {
        var result = limits.get(type);
        return (result != null) ? result : 1.0;
    }
}