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;
}
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);
}
}
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;
}
}
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);
}
}
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;
}
}
Aggregations