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.broker;
10 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
11 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
15 import com.google.common.base.Preconditions;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import java.util.ArrayList;
19 import java.util.Collection;
21 import java.util.concurrent.Future;
22 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Read-Write Transaction, which is composed of several
31 * {@link DOMStoreWriteTransaction} transactions. A sub-transaction is selected by
32 * {@link LogicalDatastoreType} type parameter in:
35 * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
36 * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
37 * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
40 * {@link #submit()} will result in invocation of
41 * {@link DOMDataCommitImplementation#submit(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction, Iterable)}
43 * {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for
44 * underlying transactions.
46 * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as subtransaction.
48 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
49 AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataTreeWriteTransaction {
50 @SuppressWarnings("rawtypes")
51 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, AbstractDOMForwardedTransactionFactory> IMPL_UPDATER =
52 AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
53 @SuppressWarnings("rawtypes")
54 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER =
55 AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
56 private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
57 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
60 * Implementation of real commit. It also acts as an indication that
61 * the transaction is running -- which we flip atomically using
62 * {@link #IMPL_UPDATER}.
64 private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
67 * Future task of transaction commit. It starts off as null, but is
68 * set appropriately on {@link #submit()} and {@link #cancel()} via
69 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
71 * Lazy set is safe for use because it is only referenced to in the
72 * {@link #cancel()} slow path, where we will busy-wait for it. The
73 * fast path gets the benefit of a store-store barrier instead of the
74 * usual store-load barrier.
76 private volatile Future<?> commitFuture;
78 protected DOMForwardedWriteTransaction(final Object identifier,
79 final Map<LogicalDatastoreType, T> backingTxs, final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
80 super(identifier, backingTxs);
81 this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
85 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
86 checkRunning(commitImpl);
87 getSubtransaction(store).write(path, data);
91 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
92 checkRunning(commitImpl);
93 getSubtransaction(store).delete(path);
97 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
98 checkRunning(commitImpl);
99 getSubtransaction(store).merge(path, data);
103 public boolean cancel() {
104 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
106 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
107 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
108 closeSubtransactions();
112 // The transaction is in process of being submitted or cancelled. Busy-wait
113 // for the corresponding future.
116 future = commitFuture;
117 } while (future == null);
119 return future.cancel(false);
123 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
124 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
127 final Collection<T> txns = getSubtransactions();
128 final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
130 // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
131 for (final DOMStoreWriteTransaction txn : txns) {
132 cohorts.add(txn.ready());
135 final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
136 FUTURE_UPDATER.lazySet(this, ret);
140 private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
141 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());