Search in sources :

Example 1 with NestedTransaction

use of com.yahoo.transaction.NestedTransaction in project vespa by vespa-engine.

the class ApplicationRepository method remove.

/**
 * Removes a previously deployed application
 *
 * @return true if the application was found and removed, false if it was not present
 * @throws RuntimeException if the remove transaction fails. This method is exception safe.
 */
public boolean remove(ApplicationId applicationId) {
    Optional<Tenant> owner = Optional.ofNullable(tenants.getTenant(applicationId.tenant()));
    if (!owner.isPresent())
        return false;
    TenantApplications tenantApplications = owner.get().getApplicationRepo();
    if (!tenantApplications.listApplications().contains(applicationId))
        return false;
    // TODO: Push lookup logic down
    long sessionId = tenantApplications.getSessionIdForApplication(applicationId);
    LocalSessionRepo localSessionRepo = owner.get().getLocalSessionRepo();
    LocalSession session = localSessionRepo.getSession(sessionId);
    if (session == null)
        return false;
    NestedTransaction transaction = new NestedTransaction();
    localSessionRepo.removeSession(session.getSessionId(), transaction);
    // TODO: Not unit tested
    session.delete(transaction);
    // TODO: Not unit tested
    transaction.add(new Rotations(owner.get().getCurator(), owner.get().getPath()).delete(applicationId));
    // (When rotations are updated in zk, we need to redeploy the zone app, on the right config server
    // this is done asynchronously in application maintenance by the node repository)
    transaction.add(tenantApplications.deleteApplication(applicationId));
    hostProvisioner.ifPresent(provisioner -> provisioner.remove(transaction, applicationId));
    transaction.onCommitted(() -> log.log(LogLevel.INFO, "Deleted " + applicationId));
    transaction.commit();
    return true;
}
Also used : Tenant(com.yahoo.vespa.config.server.tenant.Tenant) LocalSessionRepo(com.yahoo.vespa.config.server.session.LocalSessionRepo) LocalSession(com.yahoo.vespa.config.server.session.LocalSession) NestedTransaction(com.yahoo.transaction.NestedTransaction) TenantApplications(com.yahoo.vespa.config.server.application.TenantApplications) Rotations(com.yahoo.vespa.config.server.tenant.Rotations)

Example 2 with NestedTransaction

use of com.yahoo.transaction.NestedTransaction in project vespa by vespa-engine.

the class Deployment method activate.

/**
 * Activates this. If it is not already prepared, this will call prepare first.
 */
@Override
public void activate() {
    if (!prepared)
        prepare();
    TimeoutBudget timeoutBudget = new TimeoutBudget(clock, timeout);
    long sessionId = session.getSessionId();
    validateSessionStatus(session);
    ActivateLock activateLock = tenant.getActivateLock();
    boolean activateLockAcquired = false;
    try {
        log.log(LogLevel.DEBUG, "Trying to acquire lock " + activateLock + " for session " + sessionId);
        activateLockAcquired = activateLock.acquire(timeoutBudget, ignoreLockFailure);
        if (!activateLockAcquired) {
            throw new ActivationConflictException("Did not get activate lock for session " + sessionId + " within " + timeout);
        }
        log.log(LogLevel.DEBUG, "Lock acquired " + activateLock + " for session " + sessionId);
        NestedTransaction transaction = new NestedTransaction();
        transaction.add(deactivateCurrentActivateNew(applicationRepository.getActiveSession(session.getApplicationId()), session, ignoreSessionStaleFailure));
        if (hostProvisioner.isPresent()) {
            hostProvisioner.get().activate(transaction, session.getApplicationId(), session.getAllocatedHosts().getHosts());
        }
        transaction.commit();
        session.waitUntilActivated(timeoutBudget);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new InternalServerException("Error activating application", e);
    } finally {
        if (activateLockAcquired) {
            log.log(LogLevel.DEBUG, "Trying to release lock " + activateLock + " for session " + sessionId);
            activateLock.release();
            log.log(LogLevel.DEBUG, "Lock released " + activateLock + " for session " + sessionId);
        }
    }
    log.log(LogLevel.INFO, session.logPre() + "Session " + sessionId + " activated successfully using " + (hostProvisioner.isPresent() ? hostProvisioner.get() : "no host provisioner") + ". Config generation " + session.getMetaData().getGeneration());
}
Also used : ActivationConflictException(com.yahoo.vespa.config.server.ActivationConflictException) InternalServerException(com.yahoo.vespa.config.server.http.InternalServerException) NestedTransaction(com.yahoo.transaction.NestedTransaction) TimeoutBudget(com.yahoo.vespa.config.server.TimeoutBudget) InternalServerException(com.yahoo.vespa.config.server.http.InternalServerException) ActivationConflictException(com.yahoo.vespa.config.server.ActivationConflictException) ActivateLock(com.yahoo.vespa.config.server.tenant.ActivateLock)

Example 3 with NestedTransaction

use of com.yahoo.transaction.NestedTransaction in project vespa by vespa-engine.

the class CuratorDatabaseClient method writeTo.

/**
 * Writes the given nodes to the given state (whether or not they are already in this state or another),
 * and returns a copy of the incoming nodes in their persisted state.
 *
 * @param  toState the state to write the nodes to
 * @param  nodes the list of nodes to write
 * @param  agent the agent causing this change
 * @return the nodes in their persisted state
 */
public List<Node> writeTo(Node.State toState, List<Node> nodes, Agent agent, Optional<String> reason) {
    try (NestedTransaction nestedTransaction = new NestedTransaction()) {
        List<Node> writtenNodes = writeTo(toState, nodes, agent, reason, nestedTransaction);
        nestedTransaction.commit();
        return writtenNodes;
    }
}
Also used : Node(com.yahoo.vespa.hosted.provision.Node) NestedTransaction(com.yahoo.transaction.NestedTransaction)

Example 4 with NestedTransaction

use of com.yahoo.transaction.NestedTransaction in project vespa by vespa-engine.

the class CuratorDatabaseTest method commitCreate.

private void commitCreate(String path, CuratorDatabase database) {
    NestedTransaction t = new NestedTransaction();
    CuratorTransaction c = database.newCuratorTransactionIn(t);
    c.add(CuratorOperations.create(path));
    t.commit();
}
Also used : CuratorTransaction(com.yahoo.vespa.curator.transaction.CuratorTransaction) NestedTransaction(com.yahoo.transaction.NestedTransaction)

Example 5 with NestedTransaction

use of com.yahoo.transaction.NestedTransaction in project vespa by vespa-engine.

the class CuratorDatabaseTest method testThatCounterIncreasesAlsoOnCommitFailureFromExistingTransaction.

@Test
public void testThatCounterIncreasesAlsoOnCommitFailureFromExistingTransaction() throws Exception {
    MockCurator curator = new MockCurator();
    CuratorDatabase database = new CuratorDatabase(curator, Path.fromString("/"), true);
    assertEquals(0L, (long) curator.counter("/changeCounter").get().get().postValue());
    try {
        NestedTransaction t = new NestedTransaction();
        CuratorTransaction separateC = new CuratorTransaction(curator);
        // fail as parent does not exist
        separateC.add(CuratorOperations.create("/1/2"));
        t.add(separateC);
        CuratorTransaction c = database.newCuratorTransactionIn(t);
        // does not fail
        c.add(CuratorOperations.create("/1"));
        t.commit();
        fail("Expected exception");
    } catch (Exception expected) {
    // expected because the parent does not exist
    }
    assertEquals(1L, (long) curator.counter("/changeCounter").get().get().postValue());
}
Also used : CuratorTransaction(com.yahoo.vespa.curator.transaction.CuratorTransaction) NestedTransaction(com.yahoo.transaction.NestedTransaction) MockCurator(com.yahoo.vespa.curator.mock.MockCurator) Test(org.junit.Test)

Aggregations

NestedTransaction (com.yahoo.transaction.NestedTransaction)18 CuratorTransaction (com.yahoo.vespa.curator.transaction.CuratorTransaction)9 Node (com.yahoo.vespa.hosted.provision.Node)5 HostSpec (com.yahoo.config.provision.HostSpec)4 Test (org.junit.Test)4 ApplicationId (com.yahoo.config.provision.ApplicationId)3 Zone (com.yahoo.config.provision.Zone)3 ClusterMembership (com.yahoo.config.provision.ClusterMembership)1 ClusterSpec (com.yahoo.config.provision.ClusterSpec)1 HostFilter (com.yahoo.config.provision.HostFilter)1 Path (com.yahoo.path.Path)1 ActivationConflictException (com.yahoo.vespa.config.server.ActivationConflictException)1 TimeoutBudget (com.yahoo.vespa.config.server.TimeoutBudget)1 TenantApplications (com.yahoo.vespa.config.server.application.TenantApplications)1 InternalServerException (com.yahoo.vespa.config.server.http.InternalServerException)1 LocalSession (com.yahoo.vespa.config.server.session.LocalSession)1 LocalSessionRepo (com.yahoo.vespa.config.server.session.LocalSessionRepo)1 ActivateLock (com.yahoo.vespa.config.server.tenant.ActivateLock)1 Rotations (com.yahoo.vespa.config.server.tenant.Rotations)1 Tenant (com.yahoo.vespa.config.server.tenant.Tenant)1