aboutsummaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java
blob: 48b4e9d91bcbe75a63c29bf2a783085a89ff33e8 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

import java.util.Optional;

/**
 * A capacity request.
 *
 * @author Ulf Lilleengen
 * @author bratseth
 */
public final class Capacity {

    /** Resources should stay between these values, inclusive */
    private final ClusterResources min, max;

    private final boolean required;

    private final boolean canFail;

    private final NodeType type;

    private Capacity(ClusterResources min, ClusterResources max, boolean required, boolean canFail, NodeType type) {
        if (max.smallerThan(min))
            throw new IllegalArgumentException("The max capacity must be larger than the min capacity, but got min " +
                                               min + " and max " + max);
        this.min = min;
        this.max = max;
        this.required = required;
        this.canFail = canFail;
        this.type = type;
    }

    /** Returns the number of nodes requested */
    @Deprecated // TODO: Remove after April 2020
    public int nodeCount() { return min.nodes(); }

    /** Returns the number of nodes requested (across all groups), or 0 if not specified */
    @Deprecated // TODO: Remove after April 2020
    public int nodes() { return min.nodes(); }

    /** Returns the number of groups requested, or 0 if not specified */
    @Deprecated // TODO: Remove after April 2020
    public int groups() { return min.groups(); }

    /**
     * The node flavor requested, or empty if no legacy flavor name has been used.
     * This may be satisfied by the requested flavor or a suitable replacement.
     *
     * @deprecated use nodeResources instead
     */
    @Deprecated // TODO: Remove after March 2020
    public Optional<String> flavor() {
        if (nodeResources().isEmpty()) return Optional.empty();
        return Optional.of(min.nodeResources().toString());
    }

    /** Returns the resources requested for each node, or empty to leave this decision to provisioning */
    @Deprecated // TODO: Remove after March 2020
    public Optional<NodeResources> nodeResources() {
        if (min.nodeResources() == NodeResources.unspecified) return Optional.empty();
        return Optional.of(min.nodeResources());
    }

    public ClusterResources minResources() { return min; }
    public ClusterResources maxResources() { return max; }

    /** Returns whether the requested number of nodes must be met exactly for a request for this to succeed */
    public boolean isRequired() { return required; }

    /**
     * Returns true if an exception should be thrown if the specified capacity can not be satisfied
     * (to whatever policies are applied and taking required true/false into account).
     * Returns false if it is preferable to still succeed with partially satisfied capacity.
     */
    public boolean canFail() { return canFail; }

    /**
     * Returns the node type (role) requested. This is tenant nodes by default.
     * If some other type is requested the node count and flavor may be ignored
     * and all nodes of the requested type returned instead.
     */
    public NodeType type() { return type; }

    public Capacity withGroups(int groups) {
        return new Capacity(min.withGroups(groups), max.withGroups(groups), required, canFail, type);
    }

    @Override
    public String toString() {
        return (required ? "required " : "") +
               (min.equals(max) ? min : "between " + min + " and " + max);
    }

    /** Create a non-required, failable capacity request */
    public static Capacity from(ClusterResources resources) {
        return from(resources, false, true);
    }

    public static Capacity from(ClusterResources resources, boolean required, boolean canFail) {
        return from(resources, required, canFail, NodeType.tenant);
    }

    public static Capacity from(ClusterResources min, ClusterResources max, boolean required, boolean canFail) {
        return new Capacity(min, max, required, canFail, NodeType.tenant);
    }

    /** Create a non-required, failable capacity request */
    @Deprecated // TODO: Remove after April 2020
    public static Capacity fromCount(int nodes, NodeResources resources) {
        return fromCount(nodes, resources, false, true);
    }

    @Deprecated // TODO: Remove after April 2020
    public static Capacity fromCount(int nodes, NodeResources resources, boolean required, boolean canFail) {
        return fromCount(nodes, Optional.of(resources), required, canFail);
    }

    @Deprecated // TODO: Remove after April 2020
    public static Capacity fromCount(int nodes, Optional<NodeResources> resources, boolean required, boolean canFail) {
        return from(new ClusterResources(nodes, 0, resources.orElse(NodeResources.unspecified)),
                    required, canFail, NodeType.tenant);
    }

    /** Creates this from a node type */
    public static Capacity fromRequiredNodeType(NodeType type) {
        return from(new ClusterResources(0, 0, NodeResources.unspecified), true, false, type);
    }

    private static Capacity from(ClusterResources resources, boolean required, boolean canFail, NodeType type) {
        return new Capacity(resources, resources, required, canFail, type);
    }

}