All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.vespa.hosted.provision.provisioning.NodeResourceLimits Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
// Copyright Vespa.ai. 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.CapacityPolicies;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.hosted.provision.NodeRepository;

import java.util.Locale;

/**
 * Defines the resource limits for nodes in various zones
 *
 * @author bratseth
 * @see CapacityPolicies
 */
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) {
        boolean exclusive = nodeRepository.exclusivity().allocation(cluster);
        if (! requested.vcpuIsUnspecified() && requested.vcpu() < minAdvertisedVcpu(cluster, exclusive))
            illegal(type, "vcpu", "", cluster, requested.vcpu(), minAdvertisedVcpu(cluster, exclusive));
        if (! requested.memoryIsUnspecified() && requested.memoryGiB() < minAdvertisedMemoryGb(cluster, exclusive))
            illegal(type, "memoryGb", "Gb", cluster, requested.memoryGiB(), minAdvertisedMemoryGb(cluster, exclusive));
        if (! requested.diskIsUnspecified() && requested.diskGb() < minAdvertisedDiskGb(requested, exclusive))
            illegal(type, "diskGb", "Gb", cluster, requested.diskGb(), minAdvertisedDiskGb(requested, exclusive));
    }

    // TODO: Remove this when we are ready to fail, not just warn on this. */
    public boolean isWithinAdvertisedDiskLimits(NodeResources requested, ClusterSpec cluster) {
        if (requested.diskIsUnspecified() || requested.memoryIsUnspecified()) return true;
        // Override disk limit to be able to use more flavors as content nodes
        return requested.diskGb() >= minAdvertisedDiskGb(requested, cluster, 2.9);
    }

    /** Returns whether the real resources we'll end up with on a given tenant node are within limits */
    public boolean isWithinRealLimits(NodeCandidate candidateNode, ClusterSpec cluster) {
        if (candidateNode.type() != NodeType.tenant) return true; // Resource limits only apply to tenant nodes
        return isWithinRealLimits(nodeRepository.resourcesCalculator().realResourcesOf(candidateNode, nodeRepository),
                                  cluster);
    }

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

        if (realResources.vcpu() < minRealVcpu(cluster)) return false;
        if (realResources.memoryGiB() < minRealMemoryGb(cluster)) return false;
        if (realResources.diskGb() < minRealDiskGb()) return false;
       return true;
    }

    public NodeResources enlargeToLegal(NodeResources requested, ClusterSpec cluster, boolean exclusive, boolean followRecommendations) {
        if (requested.isUnspecified()) return requested;

        if (followRecommendations) // TODO: Do unconditionally when we enforce this limit
            requested = requested.withDiskGb(Math.max(minAdvertisedDiskGb(requested, cluster), requested.diskGb()));

        return requested.withVcpu(Math.max(minAdvertisedVcpu(cluster, exclusive), requested.vcpu()))
                        .withMemoryGiB(Math.max(minAdvertisedMemoryGb(cluster, exclusive), requested.memoryGiB()))
                        .withDiskGb(Math.max(minAdvertisedDiskGb(requested, exclusive), requested.diskGb()));
    }

    private double minAdvertisedVcpu(ClusterSpec cluster, boolean exclusive) {
        if (cluster.type() == ClusterSpec.Type.admin) return 0.1;
        if (zone().environment() == Environment.dev && ! exclusive) return 0.1;
        return 0.5;
    }

    private double minAdvertisedMemoryGb(ClusterSpec cluster, boolean exclusive) {
        if (cluster.type() == ClusterSpec.Type.admin) return 1;
        if (!exclusive) return 4;
        return 8;
    }

    private double minAdvertisedDiskGb(NodeResources requested, boolean exclusive) {
        return minRealDiskGb() + reservedDiskSpaceGb(requested.storageType(), exclusive);
    }

    // TODO: Move this check into the above when we are ready to fail, not just warn on this. */
    private static double minAdvertisedDiskGb(NodeResources requested, ClusterSpec cluster) {
        return minAdvertisedDiskGb(requested, cluster, 3);
    }

    private static double minAdvertisedDiskGb(NodeResources requested, ClusterSpec cluster, double contentNodeDiskToMemoryRatio) {
        return requested.memoryGiB() * switch (cluster.type()) {
            case combined, content -> contentNodeDiskToMemoryRatio;
            case container -> 2;
            default -> 0; // No constraint on other types
        };
    }

    // Note: Assumes node type 'host'
    private long reservedDiskSpaceGb(NodeResources.StorageType storageType, boolean exclusive) {
        if (storageType == NodeResources.StorageType.local && ! zone().cloud().allowHostSharing())
            return nodeRepository.resourcesCalculator().reservedDiskSpaceInBase2Gb(NodeType.host, ! exclusive);
        else
            return 4;
    }

    private double minRealVcpu(ClusterSpec cluster) {
        return minAdvertisedVcpu(cluster, nodeRepository.exclusivity().allocation(cluster));
    }

    private static double minRealMemoryGb(ClusterSpec cluster) {
        if (cluster.type() == ClusterSpec.Type.admin) return 0.95; // TODO: Increase to 1.05 after March 2023
        return 2.3;
    }

    private static double minRealDiskGb() { return 6; }

    private Zone zone() { return nodeRepository.zone(); }

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy