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

com.yahoo.vespa.hosted.provision.testutils.OrchestratorMock 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.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.jdisc.test.TestTimer;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceReference;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.orchestrator.Host;
import com.yahoo.vespa.orchestrator.Orchestrator;
import com.yahoo.vespa.orchestrator.status.ApplicationInstanceStatus;
import com.yahoo.vespa.orchestrator.status.HostInfo;
import com.yahoo.vespa.orchestrator.status.HostStatus;

import java.time.Clock;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

/**
 * @author bratseth
 */
public class OrchestratorMock extends AbstractComponent implements Orchestrator {

    private final Map suspendedHosts = new HashMap<>();
    private final Set suspendedApplications = new HashSet<>();
    private final Clock clock;

    @Inject
    public OrchestratorMock() { this(new TestTimer(Instant.EPOCH).toUtcClock()); }

    public OrchestratorMock(Clock clock) {
        this.clock = clock;
    }

    @Override
    public Host getHost(HostName hostName) {
        return null;
    }

    @Override
    public HostStatus getNodeStatus(HostName hostName) {
        HostInfo hostInfo = suspendedHosts.get(hostName);
        return hostInfo == null ? HostStatus.NO_REMARKS : hostInfo.status();
    }

    @Override
    public HostInfo getHostInfo(ApplicationInstanceReference reference, HostName hostname) {
        HostInfo hostInfo = suspendedHosts.get(hostname);
        return hostInfo == null ? HostInfo.createNoRemarks() : hostInfo;
    }

    @Override
    public Function> getHostResolver() {
        return hostName -> Optional.of(suspendedHosts.getOrDefault(hostName, HostInfo.createNoRemarks()));
    }

    @Override
    public void setNodeStatus(HostName hostName, HostStatus state) {
        if (state == HostStatus.NO_REMARKS) {
            suspendedHosts.remove(hostName);
        } else {
            suspendedHosts.put(hostName, HostInfo.createSuspended(state, clock.instant()));
        }
    }

    @Override
    public void resume(HostName hostName) {
        suspendedHosts.remove(hostName);
    }

    @Override
    public void suspend(HostName hostName) {
        suspendedHosts.put(hostName, HostInfo.createSuspended(HostStatus.ALLOWED_TO_BE_DOWN, clock.instant()));
    }

    @Override
    public ApplicationInstanceStatus getApplicationInstanceStatus(ApplicationId appId) {
        return suspendedApplications.contains(appId)
               ? ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN : ApplicationInstanceStatus.NO_REMARKS;
    }

    @Override
    public Set getAllSuspendedApplications() {
        return Collections.unmodifiableSet(suspendedApplications);
    }

    @Override
    public void resume(ApplicationId appId) {
        suspendedApplications.remove(appId);
    }

    @Override
    public void suspend(ApplicationId appId) {
        suspendedApplications.add(appId);
    }

    @Override
    public void acquirePermissionToRemove(HostName hostName) { }

    @Override
    public void suspendAll(HostName parentHostname, List hostNames) {
        hostNames.forEach(this::suspend);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy