summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/provisioning/NodeResourceLimits.java
blob: f9eaad072de09d3893a350090966b5a59fa08a27 (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
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.provisioning;

import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;

import java.util.Locale;

/**
 * Defines the resource limits for nodes in various zones
 *
 * @author bratseth
 */
public class NodeResourceLimits {

    private final NodeRepository nodeRepository;

    public NodeResourceLimits(NodeRepository nodeRepository) {
        this.nodeRepository = nodeRepository;
    }

    /** Validates the resources applications ask for (which are in "advertised" resource space) */
    public void ensureWithinAdvertisedLimits(String type, NodeResources requested, ClusterSpec cluster) {
        if (requested.isUnspecified()) return;

        if (requested.memoryGb() < minAdvertisedMemoryGb(cluster.type()))
            illegal(type, "memory", cluster, requested.memoryGb(), minAdvertisedMemoryGb(cluster.type()));
        if (requested.diskGb() < minAdvertisedDiskGb(requested))
            illegal(type, "disk", cluster, requested.diskGb(), minAdvertisedDiskGb(requested));
    }

    /** Returns whether the real resources we'll end up with on a given tenant node are within limits */
    public boolean isWithinRealLimits(Node candidateTenantNode, ClusterSpec cluster) {
        return isWithinRealLimits(nodeRepository.resourcesCalculator().realResourcesOf(candidateTenantNode, nodeRepository),
                                  cluster.type());
    }

    /** Returns whether the real resources we'll end up with on a given tenant node are within limits */
    public boolean isWithinRealLimits(NodeResources realResources, ClusterSpec.Type clusterType) {
        if (realResources.isUnspecified()) return true;

        if (realResources.memoryGb() < minRealMemoryGb(clusterType)) return false;
        if (realResources.diskGb() < minRealDiskGb()) return false;
       return true;
    }

    public NodeResources enlargeToLegal(NodeResources requested, ClusterSpec.Type clusterType) {
        if (requested.isUnspecified()) return requested;

        return requested.withMemoryGb(Math.max(minAdvertisedMemoryGb(clusterType), requested.memoryGb()))
                                  .withDiskGb(Math.max(minAdvertisedDiskGb(requested), requested.diskGb()));
    }

    private double minAdvertisedMemoryGb(ClusterSpec.Type clusterType) {
        if (nodeRepository.zone().system() == SystemName.dev) return 1; // Allow small containers in dev system
        if (clusterType == ClusterSpec.Type.admin) return 2;
        return 4;
    }

    private double minRealMemoryGb(ClusterSpec.Type clusterType) {
        return minAdvertisedMemoryGb(clusterType) - 1.7;
    }

    private double minAdvertisedDiskGb(NodeResources requested) {

        if (requested.storageType() == NodeResources.StorageType.local
            && nodeRepository.zone().getCloud().dynamicProvisioning()) {
            if (nodeRepository.zone().system() == SystemName.Public)
                return 10 + minRealDiskGb();
            else
                return 55 + minRealDiskGb();
        }
        return 4 + minRealDiskGb();
    }

    private double minRealDiskGb() {
        return 6;
    }

    private void illegal(String type, String resource, ClusterSpec cluster, double requested, double minAllowed) {
        String message = String.format(Locale.ENGLISH,
                                       "%s cluster '%s': " + type + " " + resource +
                                       " size is %.2f Gb but must be at least %.2f Gb",
                                       cluster.type().name(), cluster.id().value(), requested, minAllowed);
        throw new IllegalArgumentException(message);
    }

}