com.yahoo.vespa.config.server.provision.StaticProvisioner Maven / Gradle / Ivy
// 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;
/**
* 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 List prepare(ClusterSpec cluster, Capacity capacity, ProvisionLogger logger) {
List 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);
}
}