diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-02-05 10:42:44 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-02-05 10:42:44 +0100 |
commit | 457f985f1a48b754cc86e12e8a8ca39d9f4d0fa2 (patch) | |
tree | c2f75ec771f0921972b0cebe2940221f2fd6b136 | |
parent | 36bb3685d794918f988d43a2aca978513f215f66 (diff) |
Move transactionally
-rw-r--r-- | node-repository/src/main/java/com/yahoo/vespa/hosted/provision/NodeRepository.java | 51 |
1 files changed, 45 insertions, 6 deletions
diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/NodeRepository.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/NodeRepository.java index 5222269ef42..92e070aa7b0 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/NodeRepository.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/NodeRepository.java @@ -515,7 +515,13 @@ public class NodeRepository extends AbstractComponent { * transaction commits. */ public List<Node> deactivate(List<Node> nodes, ApplicationTransaction transaction) { - return db.writeTo(State.inactive, nodes, Agent.application, Optional.empty(), transaction.nested()); + var stateless = NodeList.copyOf(nodes).stateless(); + var stateful = NodeList.copyOf(nodes).stateful(); + List<Node> written = new ArrayList<>(); + // written.addAll(deallocate(stateless.asList(), Agent.application, "Deactivated by application", transaction.nested())); + written.addAll(db.writeTo(State.inactive, nodes, Agent.application, Optional.empty(), transaction.nested())); + return written; + } /** Removes this application: Active nodes are deactivated while all non-active nodes are set dirty. */ @@ -568,11 +574,22 @@ public class NodeRepository extends AbstractComponent { * @throws IllegalArgumentException if the node has hardware failure */ public Node deallocate(Node node, Agent agent, String reason) { + NestedTransaction transaction = new NestedTransaction(); + Node deallocated = deallocate(node, agent, reason, transaction); + transaction.commit(); + return deallocated; + } + + public List<Node> deallocate(List<Node> nodes, Agent agent, String reason, NestedTransaction transaction) { + return nodes.stream().map(node -> deallocate(node, agent, reason, transaction)).collect(Collectors.toList()); + } + + public Node deallocate(Node node, Agent agent, String reason, NestedTransaction transaction) { if (node.state() != State.parked && agent != Agent.operator && (node.status().wantToDeprovision() || retiredByOperator(node))) - return park(node.hostname(), false, agent, reason); + return park(node.hostname(), false, agent, reason, transaction); else - return db.writeTo(State.dirty, node, agent, Optional.of(reason)); + return db.writeTo(State.dirty, List.of(node), agent, Optional.of(reason), transaction).get(0); } private static boolean retiredByOperator(Node node) { @@ -608,7 +625,14 @@ public class NodeRepository extends AbstractComponent { * @throws NoSuchNodeException if the node is not found */ public Node park(String hostname, boolean keepAllocation, Agent agent, String reason) { - return move(hostname, keepAllocation, State.parked, agent, Optional.of(reason)); + NestedTransaction transaction = new NestedTransaction(); + Node parked = park(hostname, keepAllocation, agent, reason, transaction); + transaction.commit(); + return parked; + } + + public Node park(String hostname, boolean keepAllocation, Agent agent, String reason, NestedTransaction transaction) { + return move(hostname, keepAllocation, State.parked, agent, Optional.of(reason), transaction); } /** @@ -655,6 +679,14 @@ public class NodeRepository extends AbstractComponent { } private Node move(String hostname, boolean keepAllocation, State toState, Agent agent, Optional<String> reason) { + NestedTransaction transaction = new NestedTransaction(); + Node moved = move(hostname, keepAllocation, toState, agent, reason, transaction); + transaction.commit(); + return moved; + } + + private Node move(String hostname, boolean keepAllocation, State toState, Agent agent, Optional<String> reason, + NestedTransaction transaction) { Node node = getNode(hostname).orElseThrow(() -> new NoSuchNodeException("Could not move " + hostname + " to " + toState + ": Node not found")); @@ -662,10 +694,17 @@ public class NodeRepository extends AbstractComponent { node = node.withoutAllocation(); } - return move(node, toState, agent, reason); + return move(node, toState, agent, reason, transaction); } private Node move(Node node, State toState, Agent agent, Optional<String> reason) { + NestedTransaction transaction = new NestedTransaction(); + Node moved = move(node, toState, agent, reason, transaction); + transaction.commit(); + return moved; + } + + private Node move(Node node, State toState, Agent agent, Optional<String> reason, NestedTransaction transaction) { if (toState == Node.State.active && node.allocation().isEmpty()) illegal("Could not set " + node + " active. It has no allocation."); @@ -678,7 +717,7 @@ public class NodeRepository extends AbstractComponent { illegal("Could not set " + node + " active: Same cluster and index as " + currentActive); } } - return db.writeTo(toState, node, agent, reason); + return db.writeTo(toState, List.of(node), agent, reason, transaction).get(0); } } |