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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.concurrent.NotThreadSafe;
13 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * A transaction chain attached to a Shard.
23 final class ShardDataTreeTransactionChain extends ShardDataTreeTransactionParent {
24 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeTransactionChain.class);
25 private final ShardDataTree dataTree;
26 private final LocalHistoryIdentifier chainId;
28 private ReadWriteShardDataTreeTransaction previousTx;
29 private ReadWriteShardDataTreeTransaction openTransaction;
30 private boolean closed;
32 ShardDataTreeTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier, final ShardDataTree dataTree) {
33 this.dataTree = Preconditions.checkNotNull(dataTree);
34 this.chainId = Preconditions.checkNotNull(localHistoryIdentifier);
37 private DataTreeSnapshot getSnapshot() {
38 Preconditions.checkState(!closed, "TransactionChain %s has been closed", this);
39 Preconditions.checkState(openTransaction == null, "Transaction %s is open", openTransaction);
41 if (previousTx == null) {
42 return dataTree.takeSnapshot();
44 return previousTx.getSnapshot();
48 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
49 final DataTreeSnapshot snapshot = getSnapshot();
50 LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
52 return new ReadOnlyShardDataTreeTransaction(txId, snapshot);
55 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
56 final DataTreeSnapshot snapshot = getSnapshot();
57 LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
59 openTransaction = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
60 return openTransaction;
68 protected void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
69 if (transaction instanceof ReadWriteShardDataTreeTransaction) {
70 Preconditions.checkState(openTransaction != null,
71 "Attempted to abort transaction %s while none is outstanding", transaction);
72 LOG.debug("Aborted transaction {}", transaction);
73 openTransaction = null;
78 protected ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
79 Preconditions.checkState(openTransaction != null,
80 "Attempted to finish transaction %s while none is outstanding", transaction);
82 // dataTree is finalizing ready the transaction, we just record it for the next
83 // transaction in chain
84 final ShardDataTreeCohort delegate = dataTree.finishTransaction(transaction);
85 openTransaction = null;
86 previousTx = transaction;
87 LOG.debug("Committing transaction {}", transaction);
89 return new ChainedCommitCohort(this, transaction, delegate);
93 public String toString() {
94 return MoreObjects.toStringHelper(this).add("id", chainId).toString();
97 void clearTransaction(ReadWriteShardDataTreeTransaction transaction) {
98 if (transaction.equals(previousTx)) {