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.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
18 * A transaction chain attached to a Shard.
21 final class ShardDataTreeTransactionChain extends ShardDataTreeTransactionParent {
22 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeTransactionChain.class);
23 private final ShardDataTree dataTree;
24 private final String chainId;
26 private ReadWriteShardDataTreeTransaction previousTx;
27 private ReadWriteShardDataTreeTransaction openTransaction;
28 private boolean closed;
30 ShardDataTreeTransactionChain(final String chainId, final ShardDataTree dataTree) {
31 this.dataTree = Preconditions.checkNotNull(dataTree);
32 this.chainId = Preconditions.checkNotNull(chainId);
35 private DataTreeSnapshot getSnapshot() {
36 Preconditions.checkState(!closed, "TransactionChain %s has been closed", this);
37 Preconditions.checkState(openTransaction == null, "Transaction %s is open", openTransaction);
39 if (previousTx == null) {
40 return dataTree.getDataTree().takeSnapshot();
42 return previousTx.getSnapshot();
46 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId) {
47 final DataTreeSnapshot snapshot = getSnapshot();
48 LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
50 return new ReadOnlyShardDataTreeTransaction(txId, snapshot);
53 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId) {
54 final DataTreeSnapshot snapshot = getSnapshot();
55 LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
57 openTransaction = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
58 return openTransaction;
66 protected void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
67 if (transaction instanceof ReadWriteShardDataTreeTransaction) {
68 Preconditions.checkState(openTransaction != null, "Attempted to abort transaction %s while none is outstanding", transaction);
69 LOG.debug("Aborted transaction {}", transaction);
70 openTransaction = null;
75 protected ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
76 Preconditions.checkState(openTransaction != null, "Attempted to finish transaction %s while none is outstanding", transaction);
78 // dataTree is finalizing ready the transaction, we just record it for the next
79 // transaction in chain
80 final ShardDataTreeCohort delegate = dataTree.finishTransaction(transaction);
81 openTransaction = null;
82 previousTx = transaction;
83 LOG.debug("Committing transaction {}", transaction);
85 return new ChainedCommitCohort(this, transaction, delegate);
89 public String toString() {
90 return MoreObjects.toStringHelper(this).add("id", chainId).toString();
93 void clearTransaction(ReadWriteShardDataTreeTransaction transaction) {
94 if (transaction.equals(previousTx)) {