aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/DeploymentQuotaCalculator.java
blob: cde971b490a544d676929ec7fe4b1f58560a03ff (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.application;

import com.yahoo.config.application.api.DeploymentSpec;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterResources;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.api.integration.billing.Quota;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * Calculates the quota to allocate to a deployment.
 *
 * @author ogronnesby
 * @author andreer
 */
public class DeploymentQuotaCalculator {

    public static Quota calculate(Quota tenantQuota,
                                  List<Application> tenantApps,
                                  ApplicationId deployingApp, ZoneId deployingZone,
                                  DeploymentSpec deploymentSpec)
    {
        if (tenantQuota.budget().isEmpty()) return tenantQuota; // Shortcut if there is no budget limit to care about.
        if (deployingZone.environment().isTest()) return tenantQuota;
        if (deployingZone.environment().isProduction()) return probablyEnoughForAll(tenantQuota, tenantApps, deployingApp, deploymentSpec);
        return getMaximumAllowedQuota(tenantQuota, tenantApps, deployingApp, deployingZone);
    }

    public static QuotaUsage calculateQuotaUsage(com.yahoo.vespa.hosted.controller.api.integration.configserver.Application application) {
        var quotaUsageRate = application.clusters().values().stream()
                .filter(cluster -> ! cluster.type().equals(ClusterSpec.Type.admin))
                .map(cluster -> largestQuotaUsage(cluster.current(), cluster.max()))
                .mapToDouble(resources -> resources.nodes() * resources.nodeResources().cost())
                .sum();
        return QuotaUsage.create(quotaUsageRate);
    }

    private static ClusterResources largestQuotaUsage(ClusterResources a, ClusterResources b) {
        return a.cost() > b.cost() ? a : b;
    }

    /** Just get the maximum quota we are allowed to use. */
    private static Quota getMaximumAllowedQuota(Quota tenantQuota, List<Application> applications,
                                                ApplicationId application, ZoneId zone) {
        var usageOutsideDeployment = applications.stream()
                .map(app -> app.quotaUsage(application, zone))
                .reduce(QuotaUsage::add).orElse(QuotaUsage.none);
        return tenantQuota.subtractUsage(usageOutsideDeployment.rate());
    }

    /**
     * We want to avoid applying a resource change to an instance in production when it seems likely
     * that there will not be enough quota to apply this change to _all_ production instances.
     * <p>
     * To achieve this, we must make the assumption that all production instances will use
     * the same amount of resources, and so equally divide the quota among them.
     */
    private static Quota probablyEnoughForAll(Quota tenantQuota, List<Application> tenantApps,
                                              ApplicationId application, DeploymentSpec deploymentSpec) {

        TenantAndApplicationId deployingAppId = TenantAndApplicationId.from(application);

        var usageOutsideApplication = tenantApps.stream()
                .filter(app -> !app.id().equals(deployingAppId))
                .map(Application::quotaUsage).reduce(QuotaUsage::add).orElse(QuotaUsage.none);

        QuotaUsage manualQuotaUsage = tenantApps.stream()
                .filter(app -> app.id().equals(deployingAppId)).findFirst()
                .map(Application::manualQuotaUsage).orElse(QuotaUsage.none);

        long productionDeployments = Math.max(1, deploymentSpec.instances().stream()
                .flatMap(instance -> instance.zones().stream())
                .filter(zone -> zone.environment().isProduction())
                .count());

        return tenantQuota.withBudget(
                tenantQuota.subtractUsage(usageOutsideApplication.rate() + manualQuotaUsage.rate())
                        .budget().get().divide(BigDecimal.valueOf(productionDeployments),
                        5, RoundingMode.HALF_UP)); // 1/1000th of a cent should be accurate enough
    }
}