Search in sources :

Example 1 with Lock

use of com.yahoo.vespa.curator.Lock in project vespa by vespa-engine.

the class CuratorDatabase method lock.

/**
 * Create a reentrant lock
 */
// Locks are not cached in the in-memory state
public Lock lock(Path path, Duration timeout) {
    Lock lock = locks.computeIfAbsent(path, (pathArg) -> new Lock(pathArg.getAbsolute(), curator));
    lock.acquire(timeout);
    return lock;
}
Also used : Lock(com.yahoo.vespa.curator.Lock)

Example 2 with Lock

use of com.yahoo.vespa.curator.Lock in project vespa by vespa-engine.

the class JobControl method setActive.

/**
 * Set a job active or inactive
 */
public void setActive(String jobSimpleClassName, boolean active) {
    try (Lock lock = curator.lockInactiveJobs()) {
        Set<String> inactiveJobs = curator.readInactiveJobs();
        if (active)
            inactiveJobs.remove(jobSimpleClassName);
        else
            inactiveJobs.add(jobSimpleClassName);
        curator.writeInactiveJobs(inactiveJobs);
    }
}
Also used : Lock(com.yahoo.vespa.curator.Lock)

Example 3 with Lock

use of com.yahoo.vespa.curator.Lock in project vespa by vespa-engine.

the class ApplicationController method createApplication.

/**
 * Creates a new application for an existing tenant.
 *
 * @throws IllegalArgumentException if the application already exists
 */
public Application createApplication(ApplicationId id, Optional<NToken> token) {
    if (// TODO: Support instances properly
    !(id.instance().isDefault() || id.instance().value().matches("\\d+")))
        throw new UnsupportedOperationException("Only the instance names 'default' and names which are just the PR number are supported at the moment");
    try (Lock lock = lock(id)) {
        // Validate only application names which do not already exist.
        if (asList(id.tenant()).stream().noneMatch(application -> application.id().application().equals(id.application())))
            com.yahoo.vespa.hosted.controller.api.identifiers.ApplicationId.validate(id.application().value());
        Optional<Tenant> tenant = controller.tenants().tenant(new TenantId(id.tenant().value()));
        if (!tenant.isPresent())
            throw new IllegalArgumentException("Could not create '" + id + "': This tenant does not exist");
        if (get(id).isPresent())
            throw new IllegalArgumentException("Could not create '" + id + "': Application already exists");
        if (// VESPA-1945
        get(dashToUnderscore(id)).isPresent())
            throw new IllegalArgumentException("Could not create '" + id + "': Application " + dashToUnderscore(id) + " already exists");
        if (id.instance().isDefault() && tenant.get().isAthensTenant()) {
            // Only create the athens application for "default" instances.
            if (!token.isPresent())
                throw new IllegalArgumentException("Could not create '" + id + "': No NToken provided");
            ZmsClient zmsClient = zmsClientFactory.createZmsClientWithAuthorizedServiceToken(token.get());
            zmsClient.addApplication(tenant.get().getAthensDomain().get(), new com.yahoo.vespa.hosted.controller.api.identifiers.ApplicationId(id.application().value()));
        }
        LockedApplication application = new LockedApplication(new Application(id), lock);
        store(application);
        log.info("Created " + application);
        return application;
    }
}
Also used : Lock(com.yahoo.vespa.curator.Lock) RotationLock(com.yahoo.vespa.hosted.controller.rotation.RotationLock) TenantId(com.yahoo.vespa.hosted.controller.api.identifiers.TenantId) Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) ZmsClient(com.yahoo.vespa.hosted.controller.api.integration.athenz.ZmsClient)

Example 4 with Lock

use of com.yahoo.vespa.curator.Lock in project vespa by vespa-engine.

the class TenantController method deleteTenant.

public void deleteTenant(TenantId id, Optional<NToken> token) {
    try (Lock lock = lock(id)) {
        if (!tenant(id).isPresent())
            // TODO: Change exception and message
            throw new NotExistsException(id);
        if (!controller.applications().asList(TenantName.from(id.id())).isEmpty())
            throw new IllegalArgumentException("Could not delete tenant '" + id + "': This tenant has active applications");
        Tenant tenant = tenant(id).get();
        if (tenant.isAthensTenant() && !token.isPresent())
            throw new IllegalArgumentException("Could not delete tenant '" + id + "': No NToken provided");
        try {
            db.deleteTenant(id);
        } catch (PersistenceException e) {
            // TODO: Don't allow these to leak out
            throw new RuntimeException(e);
        }
        if (tenant.isAthensTenant())
            athenzClientFactory.createZmsClientWithAuthorizedServiceToken(token.get()).deleteTenant(tenant.getAthensDomain().get());
        log.info("Deleted " + tenant);
    }
}
Also used : Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) PersistenceException(com.yahoo.vespa.hosted.controller.persistence.PersistenceException) Lock(com.yahoo.vespa.curator.Lock)

Example 5 with Lock

use of com.yahoo.vespa.curator.Lock in project vespa by vespa-engine.

the class ZookeeperStatusService method lockApplicationInstance_forCurrentThreadOnly.

MutableStatusRegistry lockApplicationInstance_forCurrentThreadOnly(ApplicationInstanceReference applicationInstanceReference, long timeoutSeconds) {
    String lockPath = applicationInstanceLock2Path(applicationInstanceReference);
    Lock lock = new Lock(lockPath, curator);
    lock.acquire(Duration.ofSeconds(timeoutSeconds));
    try {
        return new ZkMutableStatusRegistry(lock, applicationInstanceReference);
    } catch (Throwable t) {
        // In case the constructor throws an exception.
        lock.close();
        throw t;
    }
}
Also used : Lock(com.yahoo.vespa.curator.Lock)

Aggregations

Lock (com.yahoo.vespa.curator.Lock)12 Tenant (com.yahoo.vespa.hosted.controller.api.Tenant)4 TenantId (com.yahoo.vespa.hosted.controller.api.identifiers.TenantId)3 ApplicationId (com.yahoo.config.provision.ApplicationId)2 ZmsClient (com.yahoo.vespa.hosted.controller.api.integration.athenz.ZmsClient)2 ImmutableList (com.google.common.collect.ImmutableList)1 Pair (com.yahoo.collections.Pair)1 Version (com.yahoo.component.Version)1 DeploymentSpec (com.yahoo.config.application.api.DeploymentSpec)1 ValidationId (com.yahoo.config.application.api.ValidationId)1 Environment (com.yahoo.config.provision.Environment)1 TenantName (com.yahoo.config.provision.TenantName)1 NToken (com.yahoo.vespa.athenz.api.NToken)1 ActivateResult (com.yahoo.vespa.hosted.controller.api.ActivateResult)1 DeployOptions (com.yahoo.vespa.hosted.controller.api.application.v4.model.DeployOptions)1 EndpointStatus (com.yahoo.vespa.hosted.controller.api.application.v4.model.EndpointStatus)1 ConfigChangeActions (com.yahoo.vespa.hosted.controller.api.application.v4.model.configserverbindings.ConfigChangeActions)1 DeploymentId (com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId)1 Hostname (com.yahoo.vespa.hosted.controller.api.identifiers.Hostname)1 RevisionId (com.yahoo.vespa.hosted.controller.api.identifiers.RevisionId)1