2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
14 import java.util.Optional;
15 import java.util.SortedSet;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.yangtools.concepts.Identifiable;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * A transaction chain attached to a Shard. This class is NOT thread-safe.
28 final class ShardDataTreeTransactionChain extends ShardDataTreeTransactionParent
29 implements Identifiable<LocalHistoryIdentifier> {
31 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeTransactionChain.class);
32 private final LocalHistoryIdentifier chainId;
33 private final ShardDataTree dataTree;
35 private ReadWriteShardDataTreeTransaction previousTx;
36 private ReadWriteShardDataTreeTransaction openTransaction;
37 private boolean closed;
39 ShardDataTreeTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier, final ShardDataTree dataTree) {
40 chainId = requireNonNull(localHistoryIdentifier);
41 this.dataTree = requireNonNull(dataTree);
44 private DataTreeSnapshot getSnapshot() {
45 checkState(!closed, "TransactionChain %s has been closed", this);
46 checkState(openTransaction == null, "Transaction %s is open", openTransaction);
48 if (previousTx == null) {
49 LOG.debug("Opening an unchained snapshot in {}", chainId);
50 return dataTree.takeSnapshot();
53 LOG.debug("Reusing a chained snapshot in {}", chainId);
54 return previousTx.getSnapshot();
57 @NonNull ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
58 final DataTreeSnapshot snapshot = getSnapshot();
59 LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
61 return new ReadOnlyShardDataTreeTransaction(this, txId, snapshot);
64 @NonNull ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
65 final DataTreeSnapshot snapshot = getSnapshot();
66 LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
68 final var ret = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
69 openTransaction = ret;
75 LOG.debug("Closing chain {}", chainId);
79 void abortFromTransactionActor(final AbstractShardDataTreeTransaction<?> transaction) {
80 if (transaction instanceof ReadWriteShardDataTreeTransaction) {
81 checkState(openTransaction != null, "Attempted to abort transaction %s while none is outstanding",
83 LOG.debug("Aborted open transaction {}", transaction);
84 openTransaction = null;
89 void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction, final Runnable callback) {
90 abortFromTransactionActor(transaction);
91 dataTree.abortTransaction(transaction, callback);
95 ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction,
96 final Optional<SortedSet<String>> participatingShardNames) {
97 checkState(openTransaction != null, "Attempted to finish transaction %s while none is outstanding",
100 // dataTree is finalizing ready the transaction, we just record it for the next
101 // transaction in chain
102 final ShardDataTreeCohort delegate = dataTree.finishTransaction(transaction, participatingShardNames);
103 openTransaction = null;
104 previousTx = transaction;
105 LOG.debug("Committing transaction {}", transaction);
107 return new ChainedCommitCohort(this, transaction, delegate);
111 public String toString() {
112 return MoreObjects.toStringHelper(this).add("id", chainId).toString();
115 void clearTransaction(final ReadWriteShardDataTreeTransaction transaction) {
116 if (transaction.equals(previousTx)) {
122 public LocalHistoryIdentifier getIdentifier() {
127 ShardDataTreeCohort createFailedCohort(final TransactionIdentifier txId, final DataTreeModification mod,
128 final Exception failure) {
129 return dataTree.createFailedCohort(txId, mod, failure);
133 ShardDataTreeCohort createReadyCohort(final TransactionIdentifier txId, final DataTreeModification mod,
134 final Optional<SortedSet<String>> participatingShardNames) {
135 return dataTree.createReadyCohort(txId, mod, participatingShardNames);