2 * Copyright (c) 2014 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.mdsal.dom.spi.store;
10 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Preconditions;
14 import java.util.AbstractMap.SimpleEntry;
15 import java.util.Map.Entry;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Abstract implementation of the {@link DOMStoreTransactionChain} interface relying on {@link DataTreeSnapshot} supplier
24 * and backend commit coordinator.
26 * @param <T> transaction identifier type
29 public abstract class AbstractSnapshotBackedTransactionChain<T> extends TransactionReadyPrototype<T> implements DOMStoreTransactionChain {
30 private static abstract class State {
32 * Allocate a new snapshot.
34 * @return A new snapshot
36 protected abstract DataTreeSnapshot getSnapshot();
39 private static final class Idle extends State {
40 private final AbstractSnapshotBackedTransactionChain<?> chain;
42 Idle(final AbstractSnapshotBackedTransactionChain<?> chain) {
43 this.chain = Preconditions.checkNotNull(chain);
47 protected DataTreeSnapshot getSnapshot() {
48 return chain.takeSnapshot();
53 * We have a transaction out there.
55 private static final class Allocated extends State {
56 private static final AtomicReferenceFieldUpdater<Allocated, DataTreeSnapshot> SNAPSHOT_UPDATER =
57 AtomicReferenceFieldUpdater.newUpdater(Allocated.class, DataTreeSnapshot.class, "snapshot");
58 private final DOMStoreWriteTransaction transaction;
59 private volatile DataTreeSnapshot snapshot;
61 Allocated(final DOMStoreWriteTransaction transaction) {
62 this.transaction = Preconditions.checkNotNull(transaction);
65 public DOMStoreWriteTransaction getTransaction() {
70 protected DataTreeSnapshot getSnapshot() {
71 final DataTreeSnapshot ret = snapshot;
72 Preconditions.checkState(ret != null, "Previous transaction %s is not ready yet", transaction.getIdentifier());
76 void setSnapshot(final DataTreeSnapshot snapshot) {
77 final boolean success = SNAPSHOT_UPDATER.compareAndSet(this, null, snapshot);
78 Preconditions.checkState(success, "Transaction %s has already been marked as ready", transaction.getIdentifier());
83 * Chain is logically shut down, no further allocation allowed.
85 private static final class Shutdown extends State {
86 private final String message;
88 Shutdown(final String message) {
89 this.message = Preconditions.checkNotNull(message);
93 protected DataTreeSnapshot getSnapshot() {
94 throw new IllegalStateException(message);
98 @SuppressWarnings("rawtypes")
99 private static final AtomicReferenceFieldUpdater<AbstractSnapshotBackedTransactionChain, State> STATE_UPDATER =
100 AtomicReferenceFieldUpdater.newUpdater(AbstractSnapshotBackedTransactionChain.class, State.class, "state");
101 private static final Logger LOG = LoggerFactory.getLogger(AbstractSnapshotBackedTransactionChain.class);
102 private static final Shutdown CLOSED = new Shutdown("Transaction chain is closed");
103 private static final Shutdown FAILED = new Shutdown("Transaction chain has failed");
104 private final Idle idleState;
105 private volatile State state;
107 protected AbstractSnapshotBackedTransactionChain() {
108 idleState = new Idle(this);
112 private Entry<State, DataTreeSnapshot> getSnapshot() {
113 final State localState = state;
114 return new SimpleEntry<>(localState, localState.getSnapshot());
117 private boolean recordTransaction(final State expected, final DOMStoreWriteTransaction transaction) {
118 final State state = new Allocated(transaction);
119 return STATE_UPDATER.compareAndSet(this, expected, state);
123 public final DOMStoreReadTransaction newReadOnlyTransaction() {
124 return newReadOnlyTransaction(nextTransactionIdentifier());
127 protected DOMStoreReadTransaction newReadOnlyTransaction(T transactionId) {
128 final Entry<State, DataTreeSnapshot> entry = getSnapshot();
129 return SnapshotBackedTransactions.newReadTransaction(transactionId, getDebugTransactions(), entry.getValue());
133 public final DOMStoreReadWriteTransaction newReadWriteTransaction() {
134 return newReadWriteTransaction(nextTransactionIdentifier());
137 protected DOMStoreReadWriteTransaction newReadWriteTransaction(T transactionId) {
138 Entry<State, DataTreeSnapshot> entry;
139 DOMStoreReadWriteTransaction ret;
142 entry = getSnapshot();
143 ret = new SnapshotBackedReadWriteTransaction<T>(transactionId, getDebugTransactions(), entry.getValue(), this);
144 } while (!recordTransaction(entry.getKey(), ret));
150 public final DOMStoreWriteTransaction newWriteOnlyTransaction() {
151 return newWriteOnlyTransaction(nextTransactionIdentifier());
154 protected DOMStoreWriteTransaction newWriteOnlyTransaction(T transactionId) {
155 Entry<State, DataTreeSnapshot> entry;
156 DOMStoreWriteTransaction ret;
159 entry = getSnapshot();
160 ret = new SnapshotBackedWriteTransaction<T>(transactionId, getDebugTransactions(), entry.getValue(), this);
161 } while (!recordTransaction(entry.getKey(), ret));
167 protected final void transactionAborted(final SnapshotBackedWriteTransaction<T> tx) {
168 final State localState = state;
169 if (localState instanceof Allocated) {
170 final Allocated allocated = (Allocated)localState;
171 if (allocated.getTransaction().equals(tx)) {
172 final boolean success = STATE_UPDATER.compareAndSet(this, localState, idleState);
174 LOG.warn("Transaction {} aborted, but chain {} state already transitioned from {} to {}, very strange",
175 tx, this, localState, state);
182 protected final DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<T> tx, final DataTreeModification tree) {
183 final State localState = state;
185 if (localState instanceof Allocated) {
186 final Allocated allocated = (Allocated)localState;
187 final DOMStoreWriteTransaction transaction = allocated.getTransaction();
188 Preconditions.checkState(tx.equals(transaction), "Mis-ordered ready transaction %s last allocated was %s", tx, transaction);
189 allocated.setSnapshot(tree);
191 LOG.debug("Ignoring transaction {} readiness due to state {}", tx, localState);
194 return createCohort(tx, tree);
198 public final void close() {
199 final State localState = state;
202 Preconditions.checkState(!CLOSED.equals(localState), "Transaction chain {} has been closed", this);
204 if (FAILED.equals(localState)) {
205 LOG.debug("Ignoring user close in failed state");
208 } while (!STATE_UPDATER.compareAndSet(this, localState, CLOSED));
212 * Notify the base logic that a previously-submitted transaction has been committed successfully.
214 * @param transaction Transaction which completed successfully.
216 protected final void onTransactionCommited(final SnapshotBackedWriteTransaction<T> transaction) {
217 // If the committed transaction was the one we allocated last,
218 // we clear it and the ready snapshot, so the next transaction
219 // allocated refers to the data tree directly.
220 final State localState = state;
222 if (!(localState instanceof Allocated)) {
223 // This can legally happen if the chain is shut down before the transaction was committed
225 LOG.debug("Ignoring successful transaction {} in state {}", transaction, localState);
229 final Allocated allocated = (Allocated)localState;
230 final DOMStoreWriteTransaction tx = allocated.getTransaction();
231 if (!tx.equals(transaction)) {
232 LOG.debug("Ignoring non-latest successful transaction {} in state {}", transaction, allocated);
236 if (!STATE_UPDATER.compareAndSet(this, localState, idleState)) {
237 LOG.debug("Transaction chain {} has already transitioned from {} to {}, not making it idle", this, localState, state);
242 * Notify the base logic that a previously-submitted transaction has failed.
244 * @param transaction Transaction which failed.
245 * @param cause Failure cause
247 protected final void onTransactionFailed(final SnapshotBackedWriteTransaction<T> transaction, final Throwable cause) {
248 LOG.debug("Transaction chain {} failed on transaction {}", this, transaction, cause);
253 * Return the next transaction identifier.
255 * @return transaction identifier.
257 protected abstract T nextTransactionIdentifier();
260 * Inquire as to whether transactions should record their allocation context.
262 * @return True if allocation context should be recorded.
264 protected abstract boolean getDebugTransactions();
267 * Take a fresh {@link DataTreeSnapshot} from the backend.
269 * @return A new snapshot.
271 protected abstract DataTreeSnapshot takeSnapshot();
274 * Create a cohort for driving the transaction through the commit process.
276 * @param transaction Transaction handle
277 * @param modification {@link DataTreeModification} which needs to be applied to the backend
278 * @return A {@link DOMStoreThreePhaseCommitCohort} cohort.
280 protected abstract DOMStoreThreePhaseCommitCohort createCohort(final SnapshotBackedWriteTransaction<T> transaction, final DataTreeModification modification);