aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/autoscale/AllocationOptimizer.java
blob: 45ef2d1d7b53dbec254a770f47d0058f636a5c20 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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.autoscale;

import com.yahoo.config.provision.ClusterResources;
import com.yahoo.config.provision.Flavor;
import com.yahoo.config.provision.IntRange;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.vespa.hosted.provision.NodeRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.yahoo.vespa.hosted.provision.autoscale.Autoscaler.headroomRequiredToScaleDown;

/**
 * A searcher of the space of possible allocation
 *
 * @author bratseth
 */
public class AllocationOptimizer {

    // The min and max nodes to consider when not using application supplied limits
    private static final int minimumNodes = 2; // Since this number includes redundancy it cannot be lower than 2
    private static final int maximumNodes = 150;

    private final NodeRepository nodeRepository;

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

    /**
     * Searches the space of possible allocations given a target relative load
     * and (optionally) cluster limits and returns the best alternative.
     *
     * @return the best allocation, if there are any possible legal allocations, fulfilling the target
     *         fully or partially, within the limits
     */
    public Optional<AllocatableResources> findBestAllocation(Load loadAdjustment,
                                                             ClusterModel model,
                                                             Limits limits) {
        return findBestAllocations(loadAdjustment, model, limits).stream().findFirst();
    }

    /**
     * Searches the space of possible allocations given a target relative load
     * and (optionally) cluster limits and returns the best alternative.
     *
     * @return the best allocations, if there are any possible legal allocations, fulfilling the target
     *         fully or partially, within the limits. The list contains the three best allocations, sorted from most to least preferred.
     */
    public List<AllocatableResources> findBestAllocations(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());
        List<AllocatableResources> bestAllocations = new ArrayList<>();
        var availableRealHostResources = nodeRepository.zone().cloud().dynamicProvisioning()
                                         ? nodeRepository.flavors().getFlavors().stream().map(Flavor::resources).toList()
                                         : nodeRepository.nodes().list().hosts().stream().map(host -> host.flavor().resources())
                                                         .map(hostResources -> maxResourcesOf(hostResources, model))
                                                         .toList();
        for (int groups = limits.min().groups(); groups <= limits.max().groups(); groups++) {
            for (int nodes = limits.min().nodes(); nodes <= limits.max().nodes(); nodes++) {
                if (nodes % groups != 0) continue;
                if ( ! limits.groupSize().includes(nodes / groups)) continue;
                var resources = new ClusterResources(nodes,
                                                     groups,
                                                     nodeResourcesWith(nodes, groups,
                                                                       limits, loadAdjustment, model));
                var allocatableResources = AllocatableResources.from(resources,
                                                                     model.application().id(),
                                                                     model.current().clusterSpec(),
                                                                     limits,
                                                                     availableRealHostResources,
                                                                     model,
                                                                     nodeRepository);
                if (allocatableResources.isEmpty()) continue;
                bestAllocations.add(allocatableResources.get());
            }
        }
        return bestAllocations.stream()
                .sorted((one, other) -> {
                    if (one.preferableTo(other, model))
                        return -1;
                    else if (other.preferableTo(one, model)) {
                        return 1;
                    }
                    return 0;
                })
                .limit(3)
                .toList();
    }

    /** Returns the max resources of a host one node may allocate. */
    private NodeResources maxResourcesOf(NodeResources hostResources, ClusterModel model) {
        if (nodeRepository.exclusiveAllocation(model.clusterSpec())) return hostResources;
        // static, shared hosts: Allocate at most half of the host cpu to simplify management
        return hostResources.withVcpu(hostResources.vcpu() / 2);
    }

    /**
     * For the observed load this instance is initialized with, returns the resources needed per node to be at
     * the target relative load, given a target node and group count.
     */
    private NodeResources nodeResourcesWith(int nodes,
                                            int groups,
                                            Limits limits,
                                            Load loadAdjustment,
                                            ClusterModel model) {
        var loadWithTarget = model.loadAdjustmentWith(nodes, groups, loadAdjustment);

        // Leave some headroom above the ideal allocation to avoid immediately needing to scale back up
        if (loadAdjustment.cpu() < 1 && (1.0 - loadWithTarget.cpu()) < headroomRequiredToScaleDown)
            loadAdjustment = loadAdjustment.withCpu(Math.min(1.0, loadAdjustment.cpu() * (1.0 + headroomRequiredToScaleDown)));
        if (loadAdjustment.memory() < 1 && (1.0 - loadWithTarget.memory()) < headroomRequiredToScaleDown)
            loadAdjustment = loadAdjustment.withMemory(Math.min(1.0, loadAdjustment.memory() * (1.0 + headroomRequiredToScaleDown)));
        if (loadAdjustment.disk() < 1 && (1.0 - loadWithTarget.disk()) < headroomRequiredToScaleDown)
            loadAdjustment = loadAdjustment.withDisk(Math.min(1.0, loadAdjustment.disk() * (1.0 + headroomRequiredToScaleDown)));

        loadWithTarget = model.loadAdjustmentWith(nodes, groups, loadAdjustment);

        var scaled = loadWithTarget.scaled(model.current().realResources().nodeResources());
        var nonScaled = limits.isEmpty() || limits.min().nodeResources().isUnspecified()
                        ? model.current().advertisedResources().nodeResources()
                        : limits.min().nodeResources(); // min=max for non-scaled
        return nonScaled.withVcpu(scaled.vcpu()).withMemoryGb(scaled.memoryGb()).withDiskGb(scaled.diskGb());
    }

    /** Returns a copy of the given limits where the minimum nodes are at least the given value when allowed */
    private Limits atLeast(int min, Limits limits) {
        if (limits.max().nodes() < min) return limits; // not allowed
        return limits.withMin(limits.min().withNodes(Math.max(min, limits.min().nodes())));
    }

}