aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ResourceUsageStats.java
blob: 442f5bba0494c510be5713e9c25edd381ffc8818 (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
// Copyright Vespa.ai. 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 final double diskLimit;
    private final double memoryLimit;

    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;
        this.diskLimit = 0.0;
        this.memoryLimit = 0.0;
    }

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

    public double getMaxDiskUtilization() {
        return maxDiskUtilization;
    }

    public double getMaxMemoryUtilization() {
        return maxMemoryUtilization;
    }

    public int getNodesAboveLimit() {
        return nodesAboveLimit;
    }

    public double getDiskLimit() {
        return diskLimit;
    }

    public double getMemoryLimit() {
        return memoryLimit;
    }

    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));
            }
        }
        double diskLimit = limitOf(diskResource, feedBlockLimits);
        double memoryLimit = limitOf(memoryResource, feedBlockLimits);
        return new ResourceUsageStats(maxDiskUsage / diskLimit,
                maxMemoryUsage / memoryLimit,
                calculateNodesAboveLimit(feedBlock),
                diskLimit, memoryLimit);
    }

    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.isEmpty()) 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;
    }
}