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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.autoscale;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.ClusterResources;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.applications.Cluster;
import com.yahoo.vespa.hosted.provision.provisioning.CapacityPolicies;
import java.util.Objects;
/**
* Optional allocation limits
*
* @author bratseth
*/
public class Limits {
private static final Limits empty = new Limits(null, null);
private final ClusterResources min, max;
private Limits(ClusterResources min, ClusterResources max) {
this.min = min;
this.max = max;
}
public static Limits empty() { return empty; }
public boolean isEmpty() { return this == empty; }
public ClusterResources min() {
if (isEmpty()) throw new IllegalStateException("Empty: No min");
return min;
}
public ClusterResources max() {
if (isEmpty()) throw new IllegalStateException("Empty: No max");
return max;
}
public Limits withMin(ClusterResources min) {
return new Limits(min, max);
}
public Limits withMax(ClusterResources max) {
return new Limits(min, max);
}
/** Caps the given resources at the limits of this. If it is empty the node resources are returned as-is */
public NodeResources cap(NodeResources resources) {
if (isEmpty()) return resources;
if (min.nodeResources().isUnspecified()) return resources; // means max is also unspecified
resources = resources.withVcpu(between(min.nodeResources().vcpu(), max.nodeResources().vcpu(), resources.vcpu()));
resources = resources.withMemoryGb(between(min.nodeResources().memoryGb(), max.nodeResources().memoryGb(), resources.memoryGb()));
resources = resources.withDiskGb(between(min.nodeResources().diskGb(), max.nodeResources().diskGb(), resources.diskGb()));
return resources;
}
public Limits fullySpecified(ClusterSpec clusterSpec, NodeRepository nodeRepository, ApplicationId applicationId) {
if (this.isEmpty()) throw new IllegalStateException("Unspecified limits can not be made fully specified");
var defaultResources = new CapacityPolicies(nodeRepository).defaultNodeResources(clusterSpec, applicationId, clusterSpec.isExclusive());
var specifiedMin = min.nodeResources().isUnspecified() ? min.with(defaultResources) : min;
var specifiedMax = max.nodeResources().isUnspecified() ? max.with(defaultResources) : max;
return new Limits(specifiedMin, specifiedMax);
}
private double between(double min, double max, double value) {
value = Math.max(min, value);
value = Math.min(max, value);
return value;
}
public static Limits of(Cluster cluster) {
return new Limits(cluster.minResources(), cluster.maxResources());
}
public static Limits of(Capacity capacity) {
return new Limits(capacity.minResources(), capacity.maxResources());
}
public static Limits of(ClusterResources min, ClusterResources max) {
return new Limits(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"));
}
@Override
public String toString() {
if (isEmpty()) return "no limits";
return "limits: from " + min + " to " + max;
}
}
|