780d940128b7eafe48cb38f87e8cf102bde370c3
[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 com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
17 import org.opendaylight.controller.sal.core.spi.data.ForwardingDOMStoreThreePhaseCommitCohort;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * A transaction chain attached to a Shard.
24  */
25 @NotThreadSafe
26 final class ShardDataTreeTransactionChain extends ShardDataTreeTransactionParent {
27     private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeTransactionChain.class);
28     private final ShardDataTree dataTree;
29     private final String chainId;
30
31     private ReadWriteShardDataTreeTransaction previousTx;
32     private ReadWriteShardDataTreeTransaction openTransaction;
33     private boolean closed;
34
35     ShardDataTreeTransactionChain(final String chainId, final ShardDataTree dataTree) {
36         this.dataTree = Preconditions.checkNotNull(dataTree);
37         this.chainId = Preconditions.checkNotNull(chainId);
38     }
39
40     private DataTreeSnapshot getSnapshot() {
41         Preconditions.checkState(!closed, "TransactionChain %s has been closed", this);
42         Preconditions.checkState(openTransaction == null, "Transaction %s is open", openTransaction);
43
44         if (previousTx == null) {
45             return dataTree.getDataTree().takeSnapshot();
46         } else {
47             return previousTx.getSnapshot();
48         }
49     }
50
51     ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId) {
52         final DataTreeSnapshot snapshot = getSnapshot();
53         LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
54
55         return new ReadOnlyShardDataTreeTransaction(txId, snapshot);
56     }
57
58     ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId) {
59         final DataTreeSnapshot snapshot = getSnapshot();
60         LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
61
62         openTransaction = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
63         return openTransaction;
64     }
65
66     void close() {
67         closed = true;
68     }
69
70     @Override
71     protected void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
72         if (transaction instanceof ReadWriteShardDataTreeTransaction) {
73             Preconditions.checkState(openTransaction != null, "Attempted to abort transaction %s while none is outstanding", transaction);
74             LOG.debug("Aborted transaction {}", transaction);
75             openTransaction = null;
76         }
77     }
78
79     @Override
80     protected DOMStoreThreePhaseCommitCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
81         Preconditions.checkState(openTransaction != null, "Attempted to finish transaction %s while none is outstanding", transaction);
82
83         // dataTree is finalizing ready the transaction, we just record it for the next
84         // transaction in chain
85         final DOMStoreThreePhaseCommitCohort delegate = dataTree.finishTransaction(transaction);
86         openTransaction = null;
87         previousTx = transaction;
88         LOG.debug("Committing transaction {}", transaction);
89
90         return new CommitCohort(transaction, delegate);
91     }
92
93     @Override
94     public String toString() {
95         return MoreObjects.toStringHelper(this).add("id", chainId).toString();
96     }
97
98     private final class CommitCohort extends ForwardingDOMStoreThreePhaseCommitCohort {
99         private final ReadWriteShardDataTreeTransaction transaction;
100         private final DOMStoreThreePhaseCommitCohort delegate;
101
102         CommitCohort(final ReadWriteShardDataTreeTransaction transaction, final DOMStoreThreePhaseCommitCohort delegate) {
103             this.transaction = Preconditions.checkNotNull(transaction);
104             this.delegate = Preconditions.checkNotNull(delegate);
105         }
106
107         @Override
108         protected DOMStoreThreePhaseCommitCohort delegate() {
109             return delegate;
110         }
111
112         @Override
113         public ListenableFuture<Void> commit() {
114             final ListenableFuture<Void> ret = super.commit();
115
116             Futures.addCallback(ret, new FutureCallback<Void>() {
117                 @Override
118                 public void onSuccess(Void result) {
119                     if (transaction.equals(previousTx)) {
120                         previousTx = null;
121                     }
122                     LOG.debug("Committed transaction {}", transaction);
123                 }
124
125                 @Override
126                 public void onFailure(Throwable t) {
127                     LOG.error("Transaction {} commit failed, cannot recover", transaction, t);
128                 }
129             });
130
131             return ret;
132         }
133     }
134 }