aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/NodeResourceExhaustion.java
blob: 963b66158e540ddf0041125b665fb14fc03e18a6 (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
// 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.jrt.Spec;
import com.yahoo.vdslib.state.Node;
import com.yahoo.vespa.clustercontroller.core.hostinfo.ResourceUsage;

import java.util.Locale;
import java.util.Objects;

/**
 * Wrapper that identifies a resource type that has been exhausted on a given node,
 * complete with both actual usage and the limit it exceeded.
 */
public class NodeResourceExhaustion {
    public final Node node;
    public final String resourceType;
    public final ResourceUsage resourceUsage;
    public final double limit;
    public final String rpcAddress;

    public NodeResourceExhaustion(Node node, String resourceType,
                                  ResourceUsage resourceUsage, double limit,
                                  String rpcAddress) {
        this.node = node;
        this.resourceType = resourceType;
        this.resourceUsage = resourceUsage;
        this.limit = limit;
        this.rpcAddress = rpcAddress;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NodeResourceExhaustion that = (NodeResourceExhaustion) o;
        return Double.compare(that.limit, limit) == 0 &&
                Objects.equals(node, that.node) &&
                Objects.equals(resourceType, that.resourceType) &&
                Objects.equals(resourceUsage, that.resourceUsage) &&
                Objects.equals(rpcAddress, that.rpcAddress);
    }

    @Override
    public int hashCode() {
        return Objects.hash(node, resourceType, resourceUsage, limit, rpcAddress);
    }

    public String toExhaustionAddedDescription() {
        return String.format(Locale.US, "%s is %.1f%% full (the configured limit is %.1f%%)",
                makeDescriptionPrefix(), resourceUsage.getUsage() * 100.0, limit * 100.0);
    }

    public String toExhaustionRemovedDescription() {
        return String.format(Locale.US, "%s (<= %.1f%%)", makeDescriptionPrefix(), limit * 100.0);
    }

    public String toShorthandDescription() {
        return String.format(Locale.US, "%s%s %.1f%% > %.1f%%",
                resourceType,
                (resourceUsage.getName() != null ? ":" + resourceUsage.getName() : ""),
                resourceUsage.getUsage() * 100.0, limit * 100.0);
    }

    private String makeDescriptionPrefix() {
        return String.format(Locale.US, "%s%s on node %d [%s]",
                resourceType,
                (resourceUsage.getName() != null ? ":" + resourceUsage.getName() : ""),
                node.getIndex(),
                inferHostnameFromRpcAddress(rpcAddress));
    }

    private static String inferHostnameFromRpcAddress(String rpcAddress) {
        if (rpcAddress == null) {
            return "unknown hostname";
        }
        var spec = new Spec(rpcAddress);
        if (spec.malformed()) {
            return "unknown hostname";
        }
        return spec.host();
    }

}