eefebb6af8f6c65638cee9d6e19d965e9a58e0d4
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeTransactionChain.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
18
19 /**
20  * A transaction chain attached to a Shard.
21  */
22 @NotThreadSafe
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;
27
28     private ReadWriteShardDataTreeTransaction previousTx;
29     private ReadWriteShardDataTreeTransaction openTransaction;
30     private boolean closed;
31
32     ShardDataTreeTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier, final ShardDataTree dataTree) {
33         this.dataTree = Preconditions.checkNotNull(dataTree);
34         this.chainId = Preconditions.checkNotNull(localHistoryIdentifier);
35     }
36
37     private DataTreeSnapshot getSnapshot() {
38         Preconditions.checkState(!closed, "TransactionChain %s has been closed", this);
39         Preconditions.checkState(openTransaction == null, "Transaction %s is open", openTransaction);
40
41         if (previousTx == null) {
42             return dataTree.takeSnapshot();
43         } else {
44             return previousTx.getSnapshot();
45         }
46     }
47
48     ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
49         final DataTreeSnapshot snapshot = getSnapshot();
50         LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
51
52         return new ReadOnlyShardDataTreeTransaction(txId, snapshot);
53     }
54
55     ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
56         final DataTreeSnapshot snapshot = getSnapshot();
57         LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
58
59         openTransaction = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
60         return openTransaction;
61     }
62
63     void close() {
64         closed = true;
65     }
66
67     @Override
68     protected void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
69         if (transaction instanceof ReadWriteShardDataTreeTransaction) {
70             Preconditions.checkState(openTransaction != null, "Attempted to abort transaction %s while none is outstanding", transaction);
71             LOG.debug("Aborted transaction {}", transaction);
72             openTransaction = null;
73         }
74     }
75
76     @Override
77     protected ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
78         Preconditions.checkState(openTransaction != null, "Attempted to finish transaction %s while none is outstanding", transaction);
79
80         // dataTree is finalizing ready the transaction, we just record it for the next
81         // transaction in chain
82         final ShardDataTreeCohort delegate = dataTree.finishTransaction(transaction);
83         openTransaction = null;
84         previousTx = transaction;
85         LOG.debug("Committing transaction {}", transaction);
86
87         return new ChainedCommitCohort(this, transaction, delegate);
88     }
89
90     @Override
91     public String toString() {
92         return MoreObjects.toStringHelper(this).add("id", chainId).toString();
93     }
94
95     void clearTransaction(ReadWriteShardDataTreeTransaction transaction) {
96         if (transaction.equals(previousTx)) {
97             previousTx = null;
98         }
99     }
100 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.