dfd6680045e2d997744e903861dea7c6bc8fb24f
[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.concepts.Identifiable;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * A transaction chain attached to a Shard.
23  */
24 @NotThreadSafe
25 final class ShardDataTreeTransactionChain extends ShardDataTreeTransactionParent
26         implements Identifiable<LocalHistoryIdentifier> {
27
28     private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeTransactionChain.class);
29     private final LocalHistoryIdentifier chainId;
30     private final ShardDataTree dataTree;
31
32     private ReadWriteShardDataTreeTransaction previousTx;
33     private ReadWriteShardDataTreeTransaction openTransaction;
34     private boolean closed;
35
36     ShardDataTreeTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier, final ShardDataTree dataTree) {
37         this.chainId = Preconditions.checkNotNull(localHistoryIdentifier);
38         this.dataTree = Preconditions.checkNotNull(dataTree);
39     }
40
41     private DataTreeSnapshot getSnapshot() {
42         Preconditions.checkState(!closed, "TransactionChain %s has been closed", this);
43         Preconditions.checkState(openTransaction == null, "Transaction %s is open", openTransaction);
44
45         if (previousTx == null) {
46             LOG.debug("Opening an unchained snapshot in {}", chainId);
47             return dataTree.takeSnapshot();
48         }
49
50         LOG.debug("Reusing a chained snapshot in {}", chainId);
51         return previousTx.getSnapshot();
52     }
53
54     ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
55         final DataTreeSnapshot snapshot = getSnapshot();
56         LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
57
58         return new ReadOnlyShardDataTreeTransaction(this, txId, snapshot);
59     }
60
61     ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
62         final DataTreeSnapshot snapshot = getSnapshot();
63         LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
64
65         openTransaction = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
66         return openTransaction;
67     }
68
69     void close() {
70         closed = true;
71         LOG.debug("Closing chain {}", chainId);
72     }
73
74     @Override
75     void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction, final Runnable callback) {
76         if (transaction instanceof ReadWriteShardDataTreeTransaction) {
77             Preconditions.checkState(openTransaction != null,
78                     "Attempted to abort transaction %s while none is outstanding", transaction);
79             LOG.debug("Aborted open transaction {}", transaction);
80             openTransaction = null;
81         }
82
83         dataTree.abortTransaction(transaction, callback);
84     }
85
86     @Override
87     void purgeTransaction(final TransactionIdentifier id, final Runnable callback) {
88         dataTree.purgeTransaction(id, callback);
89     }
90
91     @Override
92     ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
93         Preconditions.checkState(openTransaction != null,
94                 "Attempted to finish transaction %s while none is outstanding", transaction);
95
96         // dataTree is finalizing ready the transaction, we just record it for the next
97         // transaction in chain
98         final ShardDataTreeCohort delegate = dataTree.finishTransaction(transaction);
99         openTransaction = null;
100         previousTx = transaction;
101         LOG.debug("Committing transaction {}", transaction);
102
103         return new ChainedCommitCohort(this, transaction, delegate);
104     }
105
106     @Override
107     public String toString() {
108         return MoreObjects.toStringHelper(this).add("id", chainId).toString();
109     }
110
111     void clearTransaction(final ReadWriteShardDataTreeTransaction transaction) {
112         if (transaction.equals(previousTx)) {
113             previousTx = null;
114         }
115     }
116
117     @Override
118     public LocalHistoryIdentifier getIdentifier() {
119         return chainId;
120     }
121
122     @Override
123     ShardDataTreeCohort createReadyCohort(final TransactionIdentifier txId, final DataTreeModification modification) {
124         return dataTree.createReadyCohort(txId, modification);
125     }
126 }