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.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;
22 * A transaction chain attached to a Shard.
25 final class ShardDataTreeTransactionChain extends ShardDataTreeTransactionParent
26 implements Identifiable<LocalHistoryIdentifier> {
27 private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeTransactionChain.class);
28 private final LocalHistoryIdentifier chainId;
29 private final ShardDataTree dataTree;
31 private ReadWriteShardDataTreeTransaction previousTx;
32 private ReadWriteShardDataTreeTransaction openTransaction;
33 private boolean closed;
35 ShardDataTreeTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier, final ShardDataTree dataTree) {
36 this.chainId = Preconditions.checkNotNull(localHistoryIdentifier);
37 this.dataTree = Preconditions.checkNotNull(dataTree);
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 LOG.debug("Opening an unchained snapshot in {}", chainId);
46 return dataTree.takeSnapshot();
49 LOG.debug("Reusing a chained snapshot in {}", chainId);
50 return previousTx.getSnapshot();
53 ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
54 final DataTreeSnapshot snapshot = getSnapshot();
55 LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
57 return new ReadOnlyShardDataTreeTransaction(txId, snapshot);
60 ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
61 final DataTreeSnapshot snapshot = getSnapshot();
62 LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
64 openTransaction = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
65 return openTransaction;
73 protected void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
74 if (transaction instanceof ReadWriteShardDataTreeTransaction) {
75 Preconditions.checkState(openTransaction != null,
76 "Attempted to abort transaction %s while none is outstanding", transaction);
77 LOG.debug("Aborted transaction {}", transaction);
78 openTransaction = null;
83 protected ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
84 Preconditions.checkState(openTransaction != null,
85 "Attempted to finish transaction %s while none is outstanding", transaction);
87 // dataTree is finalizing ready the transaction, we just record it for the next
88 // transaction in chain
89 final ShardDataTreeCohort delegate = dataTree.finishTransaction(transaction);
90 openTransaction = null;
91 previousTx = transaction;
92 LOG.debug("Committing transaction {}", transaction);
94 return new ChainedCommitCohort(this, transaction, delegate);
98 public String toString() {
99 return MoreObjects.toStringHelper(this).add("id", chainId).toString();
102 void clearTransaction(final ReadWriteShardDataTreeTransaction transaction) {
103 if (transaction.equals(previousTx)) {
109 public LocalHistoryIdentifier getIdentifier() {
114 ShardDataTreeCohort createReadyCohort(final TransactionIdentifier txId, final DataTreeModification modification) {
115 return dataTree.createReadyCohort(txId, modification);