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 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;
23 * A transaction chain attached to a Shard.
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;
31 private ReadWriteShardDataTreeTransaction previousTx;
32 private ReadWriteShardDataTreeTransaction openTransaction;
33 private boolean closed;
35 ShardDataTreeTransactionChain(final String chainId, final ShardDataTree dataTree) {
36 this.dataTree = Preconditions.checkNotNull(dataTree);
37 this.chainId = Preconditions.checkNotNull(chainId);
40 private DataTreeSnapshot getSnapshot() {
41 Preconditions.checkState(!closed, "TransactionChain %s has been closed", this);
42 Preconditions.checkState(openTransaction == null, "Transaction %s is open", openTransaction);
44 if (previousTx == null) {
45 return dataTree.getDataTree().takeSnapshot();
47 return previousTx.getSnapshot();
51 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId) {
52 final DataTreeSnapshot snapshot = getSnapshot();
53 LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
55 return new ReadOnlyShardDataTreeTransaction(txId, snapshot);
58 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId) {
59 final DataTreeSnapshot snapshot = getSnapshot();
60 LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
62 openTransaction = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
63 return openTransaction;
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;
80 protected DOMStoreThreePhaseCommitCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
81 Preconditions.checkState(openTransaction != null, "Attempted to finish transaction %s while none is outstanding", transaction);
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);
90 return new CommitCohort(transaction, delegate);
94 public String toString() {
95 return MoreObjects.toStringHelper(this).add("id", chainId).toString();
98 private final class CommitCohort extends ForwardingDOMStoreThreePhaseCommitCohort {
99 private final ReadWriteShardDataTreeTransaction transaction;
100 private final DOMStoreThreePhaseCommitCohort delegate;
102 CommitCohort(final ReadWriteShardDataTreeTransaction transaction, final DOMStoreThreePhaseCommitCohort delegate) {
103 this.transaction = Preconditions.checkNotNull(transaction);
104 this.delegate = Preconditions.checkNotNull(delegate);
108 protected DOMStoreThreePhaseCommitCohort delegate() {
113 public ListenableFuture<Void> commit() {
114 final ListenableFuture<Void> ret = super.commit();
116 Futures.addCallback(ret, new FutureCallback<Void>() {
118 public void onSuccess(Void result) {
119 if (transaction.equals(previousTx)) {
122 LOG.debug("Committed transaction {}", transaction);
126 public void onFailure(Throwable t) {
127 LOG.error("Transaction {} commit failed, cannot recover", transaction, t);