use of com.yahoo.transaction.NestedTransaction 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;
}
use of com.yahoo.transaction.NestedTransaction 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()));
}
use of com.yahoo.transaction.NestedTransaction in project vespa by vespa-engine.
the class CuratorDatabaseClient method writeTo.
/**
* Writes the given nodes and returns a copy of the incoming nodes in their persisted state.
*
* @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(List<Node> nodes, Agent agent, Optional<String> reason) {
if (nodes.isEmpty())
return Collections.emptyList();
List<Node> writtenNodes = new ArrayList<>(nodes.size());
try (NestedTransaction nestedTransaction = new NestedTransaction()) {
Map<Node.State, List<Node>> nodesByState = nodes.stream().collect(Collectors.groupingBy(Node::state));
for (Map.Entry<Node.State, List<Node>> entry : nodesByState.entrySet()) {
writtenNodes.addAll(writeTo(entry.getKey(), entry.getValue(), agent, reason, nestedTransaction));
}
nestedTransaction.commit();
}
return writtenNodes;
}
Aggregations