aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/provision/StaticProvisioner.java
blob: 14ba48cf7064a5ae1115758734186ed864e21b24 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.provision;

import com.yahoo.config.model.api.HostProvisioner;
import com.yahoo.config.provision.*;

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

/**
 * Host provisioning from an existing {@link AllocatedHosts} instance.
 *
 * @author bratseth
 */
public class StaticProvisioner implements HostProvisioner {

    private final AllocatedHosts allocatedHosts;
    
    /** The fallback provisioner to use for unknown clusters, or null to not fall back */
    private final HostProvisioner fallback;

    /**
     * Creates a static host provisioner which will fall back to using the given provisioner
     * if a request is made for nodes in a cluster which is not present in this allocation.
     */
    public StaticProvisioner(AllocatedHosts allocatedHosts, HostProvisioner fallback) {
        this.allocatedHosts = allocatedHosts;
        this.fallback = fallback;
    }

    @Override
    public HostSpec allocateHost(String alias) {
        throw new UnsupportedOperationException("Allocating a single host from provisioning info is not supported");
    }

    @Override
    public List<HostSpec> prepare(ClusterSpec cluster, Capacity capacity, ProvisionLogger logger) {
        List<HostSpec> hostsAlreadyAllocatedToCluster = 
                allocatedHosts.getHosts().stream()
                                         .filter(host -> host.membership().isPresent() && matches(host.membership().get().cluster(), cluster))
                                         .toList();
        if ( ! hostsAlreadyAllocatedToCluster.isEmpty()) 
            return hostsAlreadyAllocatedToCluster;
        else
            return fallback.prepare(cluster, capacity, logger);
    }

    private boolean matches(ClusterSpec nodeCluster, ClusterSpec requestedCluster) {
        if (requestedCluster.group().isPresent()) // we are requesting a specific group
            return nodeCluster.equals(requestedCluster);
        else // we are requesting nodes of all groups in this cluster
            return nodeCluster.satisfies(requestedCluster);
    }

}