Search in sources :

Example 11 with CuratorTransaction

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

the class CuratorDatabaseClient method addNodesInState.

/**
 * Adds a set of nodes. Rollbacks/fails transaction if any node is not in the expected state.
 */
public List<Node> addNodesInState(List<Node> nodes, Node.State expectedState) {
    NestedTransaction transaction = new NestedTransaction();
    CuratorTransaction curatorTransaction = curatorDatabase.newCuratorTransactionIn(transaction);
    for (Node node : nodes) {
        if (node.state() != expectedState)
            throw new IllegalArgumentException(node + " is not in the " + node.state() + " state");
        node = node.with(node.history().recordStateTransition(null, expectedState, Agent.system, clock.instant()));
        curatorTransaction.add(CuratorOperations.create(toPath(node).getAbsolute(), nodeSerializer.toJson(node)));
    }
    transaction.commit();
    for (Node node : nodes) log.log(LogLevel.INFO, "Added " + node);
    return nodes;
}
Also used : CuratorTransaction(com.yahoo.vespa.curator.transaction.CuratorTransaction) Node(com.yahoo.vespa.hosted.provision.Node) NestedTransaction(com.yahoo.transaction.NestedTransaction)

Example 12 with CuratorTransaction

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

the class CuratorDatabaseClient method removeNodes.

/**
 * Removes multiple nodes in a single transaction.
 *
 * @param nodes list of the nodes to remove
 */
public void removeNodes(List<Node> nodes) {
    NestedTransaction transaction = new NestedTransaction();
    for (Node node : nodes) {
        Path path = toPath(node.state(), node.hostname());
        CuratorTransaction curatorTransaction = curatorDatabase.newCuratorTransactionIn(transaction);
        curatorTransaction.add(CuratorOperations.delete(path.getAbsolute()));
    }
    transaction.commit();
    nodes.forEach(node -> log.log(LogLevel.INFO, "Removed node " + node.hostname() + " in state " + node.state()));
}
Also used : Path(com.yahoo.path.Path) CuratorTransaction(com.yahoo.vespa.curator.transaction.CuratorTransaction) Node(com.yahoo.vespa.hosted.provision.Node) NestedTransaction(com.yahoo.transaction.NestedTransaction)

Example 13 with CuratorTransaction

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

the class CuratorDatabaseClient method writeTo.

/**
 * Adds to the given transaction operations to write the given nodes to the given state,
 * and returns a copy of the nodes in the state they will have if the transaction is committed.
 *
 * @param  toState the state to write the nodes to
 * @param  nodes the list of nodes to write
 * @param  agent the agent causing this change
 * @param  reason an optional reason to be logged, for humans
 * @param  transaction the transaction to which write operations are added by this
 * @return the nodes in their state as it will be written if committed
 */
public List<Node> writeTo(Node.State toState, List<Node> nodes, Agent agent, Optional<String> reason, NestedTransaction transaction) {
    if (nodes.isEmpty())
        return nodes;
    List<Node> writtenNodes = new ArrayList<>(nodes.size());
    CuratorTransaction curatorTransaction = curatorDatabase.newCuratorTransactionIn(transaction);
    for (Node node : nodes) {
        Node newNode = new Node(node.openStackId(), node.ipAddresses(), node.additionalIpAddresses(), node.hostname(), node.parentHostname(), node.flavor(), newNodeStatus(node, toState), toState, toState.isAllocated() ? node.allocation() : Optional.empty(), node.history().recordStateTransition(node.state(), toState, agent, clock.instant()), node.type());
        curatorTransaction.add(CuratorOperations.delete(toPath(node).getAbsolute())).add(CuratorOperations.create(toPath(toState, newNode.hostname()).getAbsolute(), nodeSerializer.toJson(newNode)));
        writtenNodes.add(newNode);
    }
    transaction.onCommitted(() -> {
        // schedule logging on commit of nodes which changed state
        for (Node node : nodes) {
            if (toState != node.state())
                log.log(LogLevel.INFO, agent + " moved " + node + " to " + toState + reason.map(s -> ": " + s).orElse(""));
        }
    });
    return writtenNodes;
}
Also used : CuratorTransaction(com.yahoo.vespa.curator.transaction.CuratorTransaction) Node(com.yahoo.vespa.hosted.provision.Node) ArrayList(java.util.ArrayList)

Aggregations

CuratorTransaction (com.yahoo.vespa.curator.transaction.CuratorTransaction)13 NestedTransaction (com.yahoo.transaction.NestedTransaction)9 Node (com.yahoo.vespa.hosted.provision.Node)4 HostSpec (com.yahoo.config.provision.HostSpec)2 ClusterMembership (com.yahoo.config.provision.ClusterMembership)1 ClusterSpec (com.yahoo.config.provision.ClusterSpec)1 Path (com.yahoo.path.Path)1 MockCurator (com.yahoo.vespa.curator.mock.MockCurator)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1