All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.vespa.hosted.provision.testutils.ServiceMonitorStub Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
// 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.testutils;

import com.yahoo.component.annotation.Inject;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.vespa.applicationmodel.ApplicationInstance;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceId;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceReference;
import com.yahoo.vespa.applicationmodel.ClusterId;
import com.yahoo.vespa.applicationmodel.ConfigId;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.applicationmodel.ServiceCluster;
import com.yahoo.vespa.applicationmodel.ServiceInstance;
import com.yahoo.vespa.applicationmodel.ServiceStatus;
import com.yahoo.vespa.applicationmodel.ServiceType;
import com.yahoo.vespa.applicationmodel.TenantId;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.service.monitor.ServiceModel;
import com.yahoo.vespa.service.monitor.ServiceMonitor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author bratseth
 */
public class ServiceMonitorStub implements ServiceMonitor {

    private final Map apps;
    private final NodeRepository nodeRepository;
    private final Set downHosts = new HashSet<>();

    /** Create a service monitor where all nodes are initially up */
    @Inject
    @SuppressWarnings("unused")
    public ServiceMonitorStub(NodeRepository nodeRepository) {
        this(Map.of(), nodeRepository);
    }

    /** Create a service monitor where all nodes are initially up */
    public ServiceMonitorStub(Map apps, NodeRepository nodeRepository) {
        this.apps = apps;
        this.nodeRepository = nodeRepository;
    }

    public void setHostDown(String hostname) {
        downHosts.add(hostname);
    }

    public void setHostUp(String hostname) {
        downHosts.remove(hostname);
    }

    private ServiceStatus getHostStatus(String hostname) {
        if (downHosts.contains(hostname)) return ServiceStatus.DOWN;
        return ServiceStatus.UP;
    }

    @Override
    public ServiceModel getServiceModelSnapshot() {
        return new ServiceModel(getAllApplicationInstances());
    }

    private Map getAllApplicationInstances() {
        // Convert apps information to the response payload to return
        Map status = new HashMap<>();
        for (Map.Entry app : apps.entrySet()) {
            Set serviceInstances = new HashSet<>();
            for (Node node : nodeRepository.nodes().list(Node.State.active).owner(app.getValue().id())) {
                serviceInstances.add(new ServiceInstance(new ConfigId("configid"),
                                                         new HostName(node.hostname()),
                                                         getHostStatus(node.hostname())));
            }
            Set serviceClusters = new HashSet<>();
            serviceClusters.add(new ServiceCluster(new ClusterId(app.getValue().clusterContexts().get(0).cluster().id().value()),
                                                   new ServiceType("serviceType"),
                                                   serviceInstances));
            TenantId tenantId = new TenantId(app.getKey().tenant().value());
            ApplicationInstanceId applicationInstanceId = new ApplicationInstanceId(app.getKey().application().value());
            status.put(new ApplicationInstanceReference(tenantId, applicationInstanceId),
                       new ApplicationInstance(tenantId, applicationInstanceId, serviceClusters));
        }
        return status;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy