From 2e75546dfbcc8bb9a33101a74b5ee06039f9c8cd Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 15 Aug 2023 13:07:59 +0200 Subject: AllocatableClusterresources -> AllocatableResources --- .../autoscale/AllocatableClusterResources.java | 291 --------------------- .../provision/autoscale/AllocatableResources.java | 291 +++++++++++++++++++++ .../provision/autoscale/AllocationOptimizer.java | 24 +- .../hosted/provision/autoscale/Autoscaler.java | 2 +- .../hosted/provision/autoscale/ClusterModel.java | 8 +- .../hosted/provision/autoscale/ResourceChange.java | 4 +- .../maintenance/AutoscalingMaintainer.java | 4 +- .../provisioning/NodeRepositoryProvisioner.java | 6 +- .../provision/autoscale/ClusterModelTest.java | 2 +- .../vespa/hosted/provision/autoscale/Fixture.java | 12 +- 10 files changed, 319 insertions(+), 325 deletions(-) delete mode 100644 node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocatableClusterResources.java create mode 100644 node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocatableResources.java diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocatableClusterResources.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocatableClusterResources.java deleted file mode 100644 index 69f844e5f5c..00000000000 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocatableClusterResources.java +++ /dev/null @@ -1,291 +0,0 @@ -// 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.ClusterResources; -import com.yahoo.config.provision.ClusterSpec; -import com.yahoo.config.provision.Flavor; -import com.yahoo.config.provision.NodeResources; -import com.yahoo.vespa.hosted.provision.Node; -import com.yahoo.vespa.hosted.provision.NodeList; -import com.yahoo.vespa.hosted.provision.NodeRepository; - -import java.time.Duration; -import java.util.List; -import java.util.Optional; - -/** - * @author bratseth - */ -public class AllocatableClusterResources { - - /** The node count in the cluster */ - private final int nodes; - - /** The number of node groups in the cluster */ - private final int groups; - - private final NodeResources realResources; - private final NodeResources advertisedResources; - - private final ClusterSpec clusterSpec; - - private final double fulfilment; - - /** Fake allocatable resources from requested capacity */ - public AllocatableClusterResources(ClusterResources requested, - ClusterSpec clusterSpec, - NodeRepository nodeRepository) { - this.nodes = requested.nodes(); - this.groups = requested.groups(); - this.realResources = nodeRepository.resourcesCalculator().requestToReal(requested.nodeResources(), nodeRepository.exclusiveAllocation(clusterSpec), false); - this.advertisedResources = requested.nodeResources(); - this.clusterSpec = clusterSpec; - this.fulfilment = 1; - } - - public AllocatableClusterResources(NodeList nodes, NodeRepository nodeRepository) { - this.nodes = nodes.size(); - this.groups = (int)nodes.stream().map(node -> node.allocation().get().membership().cluster().group()).distinct().count(); - this.realResources = averageRealResourcesOf(nodes.asList(), nodeRepository); // Average since we average metrics over nodes - this.advertisedResources = nodes.requestedResources(); - this.clusterSpec = nodes.clusterSpec(); - this.fulfilment = 1; - } - - public AllocatableClusterResources(ClusterResources realResources, - NodeResources advertisedResources, - ClusterResources idealResources, - ClusterSpec clusterSpec) { - this.nodes = realResources.nodes(); - this.groups = realResources.groups(); - this.realResources = realResources.nodeResources(); - this.advertisedResources = advertisedResources; - this.clusterSpec = clusterSpec; - this.fulfilment = fulfilment(realResources, idealResources); - } - - private AllocatableClusterResources(int nodes, - int groups, - NodeResources realResources, - NodeResources advertisedResources, - ClusterSpec clusterSpec, - double fulfilment) { - this.nodes = nodes; - this.groups = groups; - this.realResources = realResources; - this.advertisedResources = advertisedResources; - this.clusterSpec = clusterSpec; - this.fulfilment = fulfilment; - } - - /** Returns this with the redundant node or group removed from counts. */ - public AllocatableClusterResources withoutRedundancy() { - int groupSize = nodes / groups; - int nodesAdjustedForRedundancy = nodes > 1 ? (groups == 1 ? nodes - 1 : nodes - groupSize) : nodes; - int groupsAdjustedForRedundancy = nodes > 1 ? (groups == 1 ? 1 : groups - 1) : groups; - return new AllocatableClusterResources(nodesAdjustedForRedundancy, - groupsAdjustedForRedundancy, - realResources, - advertisedResources, - clusterSpec, - fulfilment); - } - - /** - * Returns the resources which will actually be available per node in this cluster with this allocation. - * These should be used for reasoning about allocation to meet measured demand. - */ - public ClusterResources realResources() { - return new ClusterResources(nodes, groups, realResources); - } - - /** - * Returns the resources advertised by the cloud provider, which are the basis for charging - * and which must be used in resource allocation requests - */ - public ClusterResources advertisedResources() { - return new ClusterResources(nodes, groups, advertisedResources); - } - - public int nodes() { return nodes; } - public int groups() { return groups; } - - public ClusterSpec clusterSpec() { return clusterSpec; } - - /** Returns the standard cost of these resources, in dollars per hour */ - public double cost() { return nodes * advertisedResources.cost(); } - - /** - * Returns the fraction measuring how well the real resources fulfils the ideal: 1 means completely fulfilled, - * 0 means we have zero real resources. - * The real may be short of the ideal due to resource limits imposed by the system or application. - */ - public double fulfilment() { return fulfilment; } - - private static double fulfilment(ClusterResources realResources, ClusterResources idealResources) { - double vcpuFulfilment = Math.min(1, realResources.totalResources().vcpu() / idealResources.totalResources().vcpu()); - double memoryGbFulfilment = Math.min(1, realResources.totalResources().memoryGb() / idealResources.totalResources().memoryGb()); - double diskGbFulfilment = Math.min(1, realResources.totalResources().diskGb() / idealResources.totalResources().diskGb()); - return (vcpuFulfilment + memoryGbFulfilment + diskGbFulfilment) / 3; - } - - public boolean preferableTo(AllocatableClusterResources other, ClusterModel model) { - if (other.fulfilment() < 1 || this.fulfilment() < 1) // always fulfil as much as possible - return this.fulfilment() > other.fulfilment(); - - return this.cost() * toHours(model.allocationDuration()) + this.costChangingFrom(model) - < - other.cost() * toHours(model.allocationDuration()) + other.costChangingFrom(model); - } - - private double toHours(Duration duration) { - return duration.toMillis() / 3600000.0; - } - - /** The estimated cost of changing from the given current resources to this. */ - public double costChangingFrom(ClusterModel model) { - return new ResourceChange(model, this).cost(); - } - - @Override - public String toString() { - return advertisedResources() + - " at cost $" + cost() + - (fulfilment < 1.0 ? " (fulfilment " + fulfilment + ")" : ""); - } - - private static NodeResources averageRealResourcesOf(List nodes, NodeRepository nodeRepository) { - NodeResources sum = new NodeResources(0, 0, 0, 0).justNumbers(); - for (Node node : nodes) { - sum = sum.add(nodeRepository.resourcesCalculator().realResourcesOf(node, nodeRepository).justNumbers()); - } - return nodes.get(0).allocation().get().requestedResources().justNonNumbers() - .withVcpu(sum.vcpu() / nodes.size()) - .withMemoryGb(sum.memoryGb() / nodes.size()) - .withDiskGb(sum.diskGb() / nodes.size()) - .withBandwidthGbps(sum.bandwidthGbps() / nodes.size()); - } - - public static Optional from(ClusterResources wantedResources, - ApplicationId applicationId, - ClusterSpec clusterSpec, - Limits applicationLimits, - List availableRealHostResources, - ClusterModel model, - NodeRepository nodeRepository) { - var systemLimits = nodeRepository.nodeResourceLimits(); - boolean exclusive = nodeRepository.exclusiveAllocation(clusterSpec); - if (! exclusive) { - // We decide resources: Add overhead to what we'll request (advertised) to make sure real becomes (at least) cappedNodeResources - var allocatableResources = calculateAllocatableResources(wantedResources, - nodeRepository, - applicationId, - clusterSpec, - applicationLimits, - exclusive, - true); - - var worstCaseRealResources = nodeRepository.resourcesCalculator().requestToReal(allocatableResources.advertisedResources, - exclusive, - false); - if ( ! systemLimits.isWithinRealLimits(worstCaseRealResources, applicationId, clusterSpec)) { - allocatableResources = calculateAllocatableResources(wantedResources, - nodeRepository, - applicationId, - clusterSpec, - applicationLimits, - exclusive, - false); - } - - if ( ! systemLimits.isWithinRealLimits(allocatableResources.realResources, applicationId, clusterSpec)) - return Optional.empty(); - if ( ! anySatisfies(allocatableResources.realResources, availableRealHostResources)) - return Optional.empty(); - return Optional.of(allocatableResources); - } - else { // Return the cheapest flavor satisfying the requested resources, if any - NodeResources cappedWantedResources = applicationLimits.cap(wantedResources.nodeResources()); - Optional best = Optional.empty(); - Optional bestDisregardingDiskLimit = Optional.empty(); - for (Flavor flavor : nodeRepository.flavors().getFlavors()) { - // Flavor decide resources: Real resources are the worst case real resources we'll get if we ask for these advertised resources - NodeResources advertisedResources = nodeRepository.resourcesCalculator().advertisedResourcesOf(flavor); - NodeResources realResources = nodeRepository.resourcesCalculator().requestToReal(advertisedResources, exclusive, false); - - // Adjust where we don't need exact match to the flavor - if (flavor.resources().storageType() == NodeResources.StorageType.remote) { - double diskGb = systemLimits.enlargeToLegal(cappedWantedResources, applicationId, clusterSpec, exclusive, true).diskGb(); - if (diskGb > applicationLimits.max().nodeResources().diskGb() || diskGb < applicationLimits.min().nodeResources().diskGb()) // TODO: Remove when disk limit is enforced - diskGb = systemLimits.enlargeToLegal(cappedWantedResources, applicationId, clusterSpec, exclusive, false).diskGb(); - advertisedResources = advertisedResources.withDiskGb(diskGb); - realResources = realResources.withDiskGb(diskGb); - } - if (flavor.resources().bandwidthGbps() >= advertisedResources.bandwidthGbps()) { - advertisedResources = advertisedResources.withBandwidthGbps(cappedWantedResources.bandwidthGbps()); - realResources = realResources.withBandwidthGbps(cappedWantedResources.bandwidthGbps()); - } - - if ( ! between(applicationLimits.min().nodeResources(), applicationLimits.max().nodeResources(), advertisedResources)) continue; - if ( ! systemLimits.isWithinRealLimits(realResources, applicationId, clusterSpec)) continue; - - var candidate = new AllocatableClusterResources(wantedResources.with(realResources), - advertisedResources, - wantedResources, - clusterSpec); - - if ( ! systemLimits.isWithinAdvertisedDiskLimits(advertisedResources, clusterSpec)) { // TODO: Remove when disk limit is enforced - if (bestDisregardingDiskLimit.isEmpty() || candidate.preferableTo(bestDisregardingDiskLimit.get(), model)) { - bestDisregardingDiskLimit = Optional.of(candidate); - } - continue; - } - if (best.isEmpty() || candidate.preferableTo(best.get(), model)) { - best = Optional.of(candidate); - } - } - if (best.isEmpty()) - best = bestDisregardingDiskLimit; - return best; - } - } - - private static AllocatableClusterResources calculateAllocatableResources(ClusterResources wantedResources, - NodeRepository nodeRepository, - ApplicationId applicationId, - ClusterSpec clusterSpec, - Limits applicationLimits, - boolean exclusive, - boolean bestCase) { - var systemLimits = nodeRepository.nodeResourceLimits(); - var advertisedResources = nodeRepository.resourcesCalculator().realToRequest(wantedResources.nodeResources(), exclusive, bestCase); - advertisedResources = systemLimits.enlargeToLegal(advertisedResources, applicationId, clusterSpec, exclusive, true); // Ask for something legal - advertisedResources = applicationLimits.cap(advertisedResources); // Overrides other conditions, even if it will then fail - var realResources = nodeRepository.resourcesCalculator().requestToReal(advertisedResources, exclusive, bestCase); // What we'll really get - if ( ! systemLimits.isWithinRealLimits(realResources, applicationId, clusterSpec) - && advertisedResources.storageType() == NodeResources.StorageType.any) { - // Since local disk reserves some of the storage, try to constrain to remote disk - advertisedResources = advertisedResources.with(NodeResources.StorageType.remote); - realResources = nodeRepository.resourcesCalculator().requestToReal(advertisedResources, exclusive, bestCase); - } - return new AllocatableClusterResources(wantedResources.with(realResources), - advertisedResources, - wantedResources, - clusterSpec); - } - - /** Returns true if the given resources could be allocated on any of the given host flavors */ - private static boolean anySatisfies(NodeResources realResources, List availableRealHostResources) { - return availableRealHostResources.stream().anyMatch(realHostResources -> realHostResources.satisfies(realResources)); - } - - private static boolean between(NodeResources min, NodeResources max, NodeResources r) { - if ( ! min.isUnspecified() && ! min.justNonNumbers().compatibleWith(r.justNonNumbers())) return false; - if ( ! max.isUnspecified() && ! max.justNonNumbers().compatibleWith(r.justNonNumbers())) return false; - if ( ! min.isUnspecified() && ! r.justNumbers().satisfies(min.justNumbers())) return false; - if ( ! max.isUnspecified() && ! max.justNumbers().satisfies(r.justNumbers())) return false; - return true; - } - -} diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocatableResources.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocatableResources.java new file mode 100644 index 00000000000..8069c9c089b --- /dev/null +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocatableResources.java @@ -0,0 +1,291 @@ +// 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.ClusterResources; +import com.yahoo.config.provision.ClusterSpec; +import com.yahoo.config.provision.Flavor; +import com.yahoo.config.provision.NodeResources; +import com.yahoo.vespa.hosted.provision.Node; +import com.yahoo.vespa.hosted.provision.NodeList; +import com.yahoo.vespa.hosted.provision.NodeRepository; + +import java.time.Duration; +import java.util.List; +import java.util.Optional; + +/** + * @author bratseth + */ +public class AllocatableResources { + + /** The node count in the cluster */ + private final int nodes; + + /** The number of node groups in the cluster */ + private final int groups; + + private final NodeResources realResources; + private final NodeResources advertisedResources; + + private final ClusterSpec clusterSpec; + + private final double fulfilment; + + /** Fake allocatable resources from requested capacity */ + public AllocatableResources(ClusterResources requested, + ClusterSpec clusterSpec, + NodeRepository nodeRepository) { + this.nodes = requested.nodes(); + this.groups = requested.groups(); + this.realResources = nodeRepository.resourcesCalculator().requestToReal(requested.nodeResources(), nodeRepository.exclusiveAllocation(clusterSpec), false); + this.advertisedResources = requested.nodeResources(); + this.clusterSpec = clusterSpec; + this.fulfilment = 1; + } + + public AllocatableResources(NodeList nodes, NodeRepository nodeRepository) { + this.nodes = nodes.size(); + this.groups = (int)nodes.stream().map(node -> node.allocation().get().membership().cluster().group()).distinct().count(); + this.realResources = averageRealResourcesOf(nodes.asList(), nodeRepository); // Average since we average metrics over nodes + this.advertisedResources = nodes.requestedResources(); + this.clusterSpec = nodes.clusterSpec(); + this.fulfilment = 1; + } + + public AllocatableResources(ClusterResources realResources, + NodeResources advertisedResources, + ClusterResources idealResources, + ClusterSpec clusterSpec) { + this.nodes = realResources.nodes(); + this.groups = realResources.groups(); + this.realResources = realResources.nodeResources(); + this.advertisedResources = advertisedResources; + this.clusterSpec = clusterSpec; + this.fulfilment = fulfilment(realResources, idealResources); + } + + private AllocatableResources(int nodes, + int groups, + NodeResources realResources, + NodeResources advertisedResources, + ClusterSpec clusterSpec, + double fulfilment) { + this.nodes = nodes; + this.groups = groups; + this.realResources = realResources; + this.advertisedResources = advertisedResources; + this.clusterSpec = clusterSpec; + this.fulfilment = fulfilment; + } + + /** Returns this with the redundant node or group removed from counts. */ + public AllocatableResources withoutRedundancy() { + int groupSize = nodes / groups; + int nodesAdjustedForRedundancy = nodes > 1 ? (groups == 1 ? nodes - 1 : nodes - groupSize) : nodes; + int groupsAdjustedForRedundancy = nodes > 1 ? (groups == 1 ? 1 : groups - 1) : groups; + return new AllocatableResources(nodesAdjustedForRedundancy, + groupsAdjustedForRedundancy, + realResources, + advertisedResources, + clusterSpec, + fulfilment); + } + + /** + * Returns the resources which will actually be available per node in this cluster with this allocation. + * These should be used for reasoning about allocation to meet measured demand. + */ + public ClusterResources realResources() { + return new ClusterResources(nodes, groups, realResources); + } + + /** + * Returns the resources advertised by the cloud provider, which are the basis for charging + * and which must be used in resource allocation requests + */ + public ClusterResources advertisedResources() { + return new ClusterResources(nodes, groups, advertisedResources); + } + + public int nodes() { return nodes; } + public int groups() { return groups; } + + public ClusterSpec clusterSpec() { return clusterSpec; } + + /** Returns the standard cost of these resources, in dollars per hour */ + public double cost() { return nodes * advertisedResources.cost(); } + + /** + * Returns the fraction measuring how well the real resources fulfils the ideal: 1 means completely fulfilled, + * 0 means we have zero real resources. + * The real may be short of the ideal due to resource limits imposed by the system or application. + */ + public double fulfilment() { return fulfilment; } + + private static double fulfilment(ClusterResources realResources, ClusterResources idealResources) { + double vcpuFulfilment = Math.min(1, realResources.totalResources().vcpu() / idealResources.totalResources().vcpu()); + double memoryGbFulfilment = Math.min(1, realResources.totalResources().memoryGb() / idealResources.totalResources().memoryGb()); + double diskGbFulfilment = Math.min(1, realResources.totalResources().diskGb() / idealResources.totalResources().diskGb()); + return (vcpuFulfilment + memoryGbFulfilment + diskGbFulfilment) / 3; + } + + public boolean preferableTo(AllocatableResources other, ClusterModel model) { + if (other.fulfilment() < 1 || this.fulfilment() < 1) // always fulfil as much as possible + return this.fulfilment() > other.fulfilment(); + + return this.cost() * toHours(model.allocationDuration()) + this.costChangingFrom(model) + < + other.cost() * toHours(model.allocationDuration()) + other.costChangingFrom(model); + } + + private double toHours(Duration duration) { + return duration.toMillis() / 3600000.0; + } + + /** The estimated cost of changing from the given current resources to this. */ + public double costChangingFrom(ClusterModel model) { + return new ResourceChange(model, this).cost(); + } + + @Override + public String toString() { + return advertisedResources() + + " at cost $" + cost() + + (fulfilment < 1.0 ? " (fulfilment " + fulfilment + ")" : ""); + } + + private static NodeResources averageRealResourcesOf(List nodes, NodeRepository nodeRepository) { + NodeResources sum = new NodeResources(0, 0, 0, 0).justNumbers(); + for (Node node : nodes) { + sum = sum.add(nodeRepository.resourcesCalculator().realResourcesOf(node, nodeRepository).justNumbers()); + } + return nodes.get(0).allocation().get().requestedResources().justNonNumbers() + .withVcpu(sum.vcpu() / nodes.size()) + .withMemoryGb(sum.memoryGb() / nodes.size()) + .withDiskGb(sum.diskGb() / nodes.size()) + .withBandwidthGbps(sum.bandwidthGbps() / nodes.size()); + } + + public static Optional from(ClusterResources wantedResources, + ApplicationId applicationId, + ClusterSpec clusterSpec, + Limits applicationLimits, + List availableRealHostResources, + ClusterModel model, + NodeRepository nodeRepository) { + var systemLimits = nodeRepository.nodeResourceLimits(); + boolean exclusive = nodeRepository.exclusiveAllocation(clusterSpec); + if (! exclusive) { + // We decide resources: Add overhead to what we'll request (advertised) to make sure real becomes (at least) cappedNodeResources + var allocatableResources = calculateAllocatableResources(wantedResources, + nodeRepository, + applicationId, + clusterSpec, + applicationLimits, + exclusive, + true); + + var worstCaseRealResources = nodeRepository.resourcesCalculator().requestToReal(allocatableResources.advertisedResources, + exclusive, + false); + if ( ! systemLimits.isWithinRealLimits(worstCaseRealResources, applicationId, clusterSpec)) { + allocatableResources = calculateAllocatableResources(wantedResources, + nodeRepository, + applicationId, + clusterSpec, + applicationLimits, + exclusive, + false); + } + + if ( ! systemLimits.isWithinRealLimits(allocatableResources.realResources, applicationId, clusterSpec)) + return Optional.empty(); + if ( ! anySatisfies(allocatableResources.realResources, availableRealHostResources)) + return Optional.empty(); + return Optional.of(allocatableResources); + } + else { // Return the cheapest flavor satisfying the requested resources, if any + NodeResources cappedWantedResources = applicationLimits.cap(wantedResources.nodeResources()); + Optional best = Optional.empty(); + Optional bestDisregardingDiskLimit = Optional.empty(); + for (Flavor flavor : nodeRepository.flavors().getFlavors()) { + // Flavor decide resources: Real resources are the worst case real resources we'll get if we ask for these advertised resources + NodeResources advertisedResources = nodeRepository.resourcesCalculator().advertisedResourcesOf(flavor); + NodeResources realResources = nodeRepository.resourcesCalculator().requestToReal(advertisedResources, exclusive, false); + + // Adjust where we don't need exact match to the flavor + if (flavor.resources().storageType() == NodeResources.StorageType.remote) { + double diskGb = systemLimits.enlargeToLegal(cappedWantedResources, applicationId, clusterSpec, exclusive, true).diskGb(); + if (diskGb > applicationLimits.max().nodeResources().diskGb() || diskGb < applicationLimits.min().nodeResources().diskGb()) // TODO: Remove when disk limit is enforced + diskGb = systemLimits.enlargeToLegal(cappedWantedResources, applicationId, clusterSpec, exclusive, false).diskGb(); + advertisedResources = advertisedResources.withDiskGb(diskGb); + realResources = realResources.withDiskGb(diskGb); + } + if (flavor.resources().bandwidthGbps() >= advertisedResources.bandwidthGbps()) { + advertisedResources = advertisedResources.withBandwidthGbps(cappedWantedResources.bandwidthGbps()); + realResources = realResources.withBandwidthGbps(cappedWantedResources.bandwidthGbps()); + } + + if ( ! between(applicationLimits.min().nodeResources(), applicationLimits.max().nodeResources(), advertisedResources)) continue; + if ( ! systemLimits.isWithinRealLimits(realResources, applicationId, clusterSpec)) continue; + + var candidate = new AllocatableResources(wantedResources.with(realResources), + advertisedResources, + wantedResources, + clusterSpec); + + if ( ! systemLimits.isWithinAdvertisedDiskLimits(advertisedResources, clusterSpec)) { // TODO: Remove when disk limit is enforced + if (bestDisregardingDiskLimit.isEmpty() || candidate.preferableTo(bestDisregardingDiskLimit.get(), model)) { + bestDisregardingDiskLimit = Optional.of(candidate); + } + continue; + } + if (best.isEmpty() || candidate.preferableTo(best.get(), model)) { + best = Optional.of(candidate); + } + } + if (best.isEmpty()) + best = bestDisregardingDiskLimit; + return best; + } + } + + private static AllocatableResources calculateAllocatableResources(ClusterResources wantedResources, + NodeRepository nodeRepository, + ApplicationId applicationId, + ClusterSpec clusterSpec, + Limits applicationLimits, + boolean exclusive, + boolean bestCase) { + var systemLimits = nodeRepository.nodeResourceLimits(); + var advertisedResources = nodeRepository.resourcesCalculator().realToRequest(wantedResources.nodeResources(), exclusive, bestCase); + advertisedResources = systemLimits.enlargeToLegal(advertisedResources, applicationId, clusterSpec, exclusive, true); // Ask for something legal + advertisedResources = applicationLimits.cap(advertisedResources); // Overrides other conditions, even if it will then fail + var realResources = nodeRepository.resourcesCalculator().requestToReal(advertisedResources, exclusive, bestCase); // What we'll really get + if ( ! systemLimits.isWithinRealLimits(realResources, applicationId, clusterSpec) + && advertisedResources.storageType() == NodeResources.StorageType.any) { + // Since local disk reserves some of the storage, try to constrain to remote disk + advertisedResources = advertisedResources.with(NodeResources.StorageType.remote); + realResources = nodeRepository.resourcesCalculator().requestToReal(advertisedResources, exclusive, bestCase); + } + return new AllocatableResources(wantedResources.with(realResources), + advertisedResources, + wantedResources, + clusterSpec); + } + + /** Returns true if the given resources could be allocated on any of the given host flavors */ + private static boolean anySatisfies(NodeResources realResources, List availableRealHostResources) { + return availableRealHostResources.stream().anyMatch(realHostResources -> realHostResources.satisfies(realResources)); + } + + private static boolean between(NodeResources min, NodeResources max, NodeResources r) { + if ( ! min.isUnspecified() && ! min.justNonNumbers().compatibleWith(r.justNonNumbers())) return false; + if ( ! max.isUnspecified() && ! max.justNonNumbers().compatibleWith(r.justNonNumbers())) return false; + if ( ! min.isUnspecified() && ! r.justNumbers().satisfies(min.justNumbers())) return false; + if ( ! max.isUnspecified() && ! max.justNumbers().satisfies(r.justNumbers())) return false; + return true; + } + +} diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocationOptimizer.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocationOptimizer.java index 707abd0f4df..f650d8ec269 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocationOptimizer.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocationOptimizer.java @@ -5,9 +5,7 @@ import com.yahoo.config.provision.ClusterResources; import com.yahoo.config.provision.IntRange; import com.yahoo.config.provision.NodeResources; import com.yahoo.vespa.hosted.provision.NodeRepository; -import com.yahoo.vespa.hosted.provision.provisioning.NodeResourceLimits; -import java.time.Duration; import java.util.Optional; import static com.yahoo.vespa.hosted.provision.autoscale.Autoscaler.headroomRequiredToScaleDown; @@ -36,16 +34,16 @@ public class AllocationOptimizer { * @return the best allocation, if there are any possible legal allocations, fulfilling the target * fully or partially, within the limits */ - public Optional findBestAllocation(Load loadAdjustment, - ClusterModel model, - Limits limits) { + public Optional findBestAllocation(Load loadAdjustment, + ClusterModel model, + Limits limits) { if (limits.isEmpty()) limits = Limits.of(new ClusterResources(minimumNodes, 1, NodeResources.unspecified()), new ClusterResources(maximumNodes, maximumNodes, NodeResources.unspecified()), IntRange.empty()); else limits = atLeast(minimumNodes, limits).fullySpecified(model.current().clusterSpec(), nodeRepository, model.application().id()); - Optional bestAllocation = Optional.empty(); + Optional bestAllocation = Optional.empty(); var availableRealHostResources = nodeRepository.zone().cloud().dynamicProvisioning() ? nodeRepository.flavors().getFlavors().stream().map(flavor -> flavor.resources()).toList() : nodeRepository.nodes().list().hosts().stream().map(host -> host.flavor().resources()) @@ -59,13 +57,13 @@ public class AllocationOptimizer { groups, nodeResourcesWith(nodes, groups, limits, loadAdjustment, model)); - var allocatableResources = AllocatableClusterResources.from(resources, - model.application().id(), - model.current().clusterSpec(), - limits, - availableRealHostResources, - model, - nodeRepository); + var allocatableResources = AllocatableResources.from(resources, + model.application().id(), + model.current().clusterSpec(), + limits, + availableRealHostResources, + model, + nodeRepository); if (allocatableResources.isEmpty()) continue; if (bestAllocation.isEmpty() || allocatableResources.get().preferableTo(bestAllocation.get(), model)) bestAllocation = allocatableResources; diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/Autoscaler.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/Autoscaler.java index 091c15dea69..b5f86be68f6 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/Autoscaler.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/Autoscaler.java @@ -59,7 +59,7 @@ public class Autoscaler { clusterNodes.not().retired().clusterSpec(), cluster, clusterNodes, - new AllocatableClusterResources(clusterNodes.not().retired(), nodeRepository), + new AllocatableResources(clusterNodes.not().retired(), nodeRepository), nodeRepository.metricsDb(), nodeRepository.clock()); if (model.isEmpty()) return Autoscaling.empty(); diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterModel.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterModel.java index 27352376be1..3eecf7bdc1b 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterModel.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterModel.java @@ -50,7 +50,7 @@ public class ClusterModel { private final Application application; private final ClusterSpec clusterSpec; private final Cluster cluster; - private final AllocatableClusterResources current; + private final AllocatableResources current; private final CpuModel cpu = new CpuModel(); private final MemoryModel memory = new MemoryModel(); @@ -79,7 +79,7 @@ public class ClusterModel { ClusterSpec clusterSpec, Cluster cluster, NodeList clusterNodes, - AllocatableClusterResources current, + AllocatableResources current, MetricsDb metricsDb, Clock clock) { this.nodeRepository = nodeRepository; @@ -100,7 +100,7 @@ public class ClusterModel { Application application, ClusterSpec clusterSpec, Cluster cluster, - AllocatableClusterResources current, + AllocatableResources current, Clock clock, Duration scalingDuration, Duration allocationDuration, @@ -123,7 +123,7 @@ public class ClusterModel { public Application application() { return application; } public ClusterSpec clusterSpec() { return clusterSpec; } - public AllocatableClusterResources current() { return current; } + public AllocatableResources current() { return current; } private ClusterNodesTimeseries nodeTimeseries() { return nodeTimeseries; } private ClusterTimeseries clusterTimeseries() { return clusterTimeseries; } diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ResourceChange.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ResourceChange.java index cd3a052ee49..c0cbb40d992 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ResourceChange.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/ResourceChange.java @@ -13,10 +13,10 @@ import java.time.Duration; */ public class ResourceChange { - private final AllocatableClusterResources from, to; + private final AllocatableResources from, to; private final ClusterModel model; - public ResourceChange(ClusterModel model, AllocatableClusterResources to) { + public ResourceChange(ClusterModel model, AllocatableResources to) { this.from = model.current(); this.to = to; this.model = model; diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/AutoscalingMaintainer.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/AutoscalingMaintainer.java index 92f86325cf7..6a01a2bcd18 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/AutoscalingMaintainer.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/AutoscalingMaintainer.java @@ -16,7 +16,7 @@ import com.yahoo.vespa.hosted.provision.NodeRepository; import com.yahoo.vespa.hosted.provision.applications.Application; import com.yahoo.vespa.hosted.provision.applications.Applications; import com.yahoo.vespa.hosted.provision.applications.Cluster; -import com.yahoo.vespa.hosted.provision.autoscale.AllocatableClusterResources; +import com.yahoo.vespa.hosted.provision.autoscale.AllocatableResources; import com.yahoo.vespa.hosted.provision.autoscale.Autoscaler; import com.yahoo.vespa.hosted.provision.autoscale.Autoscaling; import com.yahoo.vespa.hosted.provision.autoscale.NodeMetricSnapshot; @@ -87,7 +87,7 @@ public class AutoscalingMaintainer extends NodeRepositoryMaintainer { NodeList clusterNodes = nodeRepository().nodes().list(Node.State.active).owner(applicationId).cluster(clusterId); cluster = updateCompletion(cluster, clusterNodes); - var current = new AllocatableClusterResources(clusterNodes.not().retired(), nodeRepository()).advertisedResources(); + var current = new AllocatableResources(clusterNodes.not().retired(), nodeRepository()).advertisedResources(); // Autoscale unless an autoscaling is already in progress Autoscaling autoscaling = null; diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/provisioning/NodeRepositoryProvisioner.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/provisioning/NodeRepositoryProvisioner.java index 5c8b07c7135..a67a513550a 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/provisioning/NodeRepositoryProvisioner.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/provisioning/NodeRepositoryProvisioner.java @@ -23,7 +23,7 @@ import com.yahoo.vespa.hosted.provision.NodeList; import com.yahoo.vespa.hosted.provision.NodeRepository; import com.yahoo.vespa.hosted.provision.applications.Application; import com.yahoo.vespa.hosted.provision.applications.Cluster; -import com.yahoo.vespa.hosted.provision.autoscale.AllocatableClusterResources; +import com.yahoo.vespa.hosted.provision.autoscale.AllocatableResources; import com.yahoo.vespa.hosted.provision.autoscale.AllocationOptimizer; import com.yahoo.vespa.hosted.provision.autoscale.ClusterModel; import com.yahoo.vespa.hosted.provision.autoscale.Limits; @@ -184,8 +184,8 @@ public class NodeRepositoryProvisioner implements Provisioner { boolean firstDeployment = nodes.isEmpty(); var current = firstDeployment // start at min, preserve current resources otherwise - ? new AllocatableClusterResources(initialResourcesFrom(requested, clusterSpec, application.id()), clusterSpec, nodeRepository) - : new AllocatableClusterResources(nodes, nodeRepository); + ? new AllocatableResources(initialResourcesFrom(requested, clusterSpec, application.id()), clusterSpec, nodeRepository) + : new AllocatableResources(nodes, nodeRepository); var model = new ClusterModel(nodeRepository, application, clusterSpec, cluster, nodes, current, nodeRepository.metricsDb(), nodeRepository.clock()); return within(Limits.of(requested), model, firstDeployment); } diff --git a/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterModelTest.java b/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterModelTest.java index 72b078853aa..f07d52a4a7f 100644 --- a/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterModelTest.java +++ b/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/ClusterModelTest.java @@ -92,7 +92,7 @@ public class ClusterModelTest { return new ClusterModel(nodeRepository, application.with(status), clusterSpec, cluster, - new AllocatableClusterResources(clusterResources(), clusterSpec, nodeRepository), + new AllocatableResources(clusterResources(), clusterSpec, nodeRepository), clock, Duration.ofMinutes(10), Duration.ofMinutes(5), timeseries(cluster,100, queryRate, writeRate, clock), ClusterNodesTimeseries.empty()); diff --git a/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/Fixture.java b/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/Fixture.java index 526fdef9762..f0dfb8eab13 100644 --- a/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/Fixture.java +++ b/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/Fixture.java @@ -5,17 +5,14 @@ import com.yahoo.component.Version; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.Capacity; import com.yahoo.config.provision.Cloud; -import com.yahoo.config.provision.ClusterInfo; import com.yahoo.config.provision.ClusterResources; import com.yahoo.config.provision.ClusterSpec; import com.yahoo.config.provision.Environment; import com.yahoo.config.provision.Flavor; -import com.yahoo.config.provision.NodeFlavors; import com.yahoo.config.provision.NodeResources; import com.yahoo.config.provision.RegionName; import com.yahoo.config.provision.SystemName; import com.yahoo.config.provision.Zone; -import com.yahoo.vespa.curator.mock.MockCurator; import com.yahoo.vespa.flags.InMemoryFlagSource; import com.yahoo.vespa.flags.PermanentFlags; import com.yahoo.vespa.flags.custom.HostResources; @@ -29,7 +26,6 @@ import com.yahoo.vespa.hosted.provision.autoscale.awsnodes.AwsHostResourcesCalcu import com.yahoo.vespa.hosted.provision.autoscale.awsnodes.AwsNodeTypes; import com.yahoo.vespa.hosted.provision.provisioning.DynamicProvisioningTester; import com.yahoo.vespa.hosted.provision.provisioning.HostResourcesCalculator; -import com.yahoo.vespa.hosted.provision.testutils.MockNodeRepository; import java.time.Duration; import java.util.Arrays; @@ -72,9 +68,9 @@ public class Fixture { return tester().nodeRepository().applications().get(applicationId).orElse(Application.empty(applicationId)); } - public AllocatableClusterResources currentResources() { - return new AllocatableClusterResources(tester.nodeRepository().nodes().list(Node.State.active).owner(applicationId).cluster(clusterId()), - tester.nodeRepository()); + public AllocatableResources currentResources() { + return new AllocatableResources(tester.nodeRepository().nodes().list(Node.State.active).owner(applicationId).cluster(clusterId()), + tester.nodeRepository()); } public Cluster cluster() { @@ -89,7 +85,7 @@ public class Fixture { clusterSpec, cluster(), nodes(), - new AllocatableClusterResources(nodes(), tester.nodeRepository()), + new AllocatableResources(nodes(), tester.nodeRepository()), tester.nodeRepository().metricsDb(), tester.nodeRepository().clock()); } -- cgit v1.2.3