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 com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FluentFuture;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.ArrayList;
14 import java.util.Collection;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
24 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Read-Write Transaction, which is composed of several
32 * {@link DOMStoreWriteTransaction} transactions. A sub-transaction is selected by
33 * {@link LogicalDatastoreType} type parameter in:
36 * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
37 * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
38 * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
42 * {@link #submit()} will result in invocation of
43 * {@link DOMDataCommitImplementation#submit(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction, Iterable)}
45 * {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for
46 * underlying transactions.
48 * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as subtransaction.
50 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
51 AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataTreeWriteTransaction {
52 @SuppressWarnings("rawtypes")
53 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
54 AbstractDOMForwardedTransactionFactory> IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(
55 DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
56 @SuppressWarnings("rawtypes")
57 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER =
58 AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
59 private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
60 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
63 * Implementation of real commit. It also acts as an indication that
64 * the transaction is running -- which we flip atomically using
65 * {@link #IMPL_UPDATER}.
67 private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
70 * Future task of transaction commit. It starts off as null, but is
71 * set appropriately on {@link #submit()} and {@link #cancel()} via
72 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
75 * Lazy set is safe for use because it is only referenced to in the
76 * {@link #cancel()} slow path, where we will busy-wait for it. The
77 * fast path gets the benefit of a store-store barrier instead of the
78 * usual store-load barrier.
80 private volatile Future<?> commitFuture;
82 protected DOMForwardedWriteTransaction(final Object identifier,
83 final Map<LogicalDatastoreType, T> backingTxs, final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
84 super(identifier, backingTxs);
85 this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
89 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
90 final NormalizedNode<?, ?> data) {
91 checkRunning(commitImpl);
92 getSubtransaction(store).write(path, data);
96 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
97 checkRunning(commitImpl);
98 getSubtransaction(store).delete(path);
102 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
103 final NormalizedNode<?, ?> data) {
104 checkRunning(commitImpl);
105 getSubtransaction(store).merge(path, data);
109 public boolean cancel() {
110 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
112 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
113 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
114 closeSubtransactions();
118 // The transaction is in process of being submitted or cancelled. Busy-wait
119 // for the corresponding future.
122 future = commitFuture;
123 } while (future == null);
125 return future.cancel(false);
129 @SuppressWarnings("checkstyle:IllegalCatch")
130 public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
131 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
134 final Collection<T> txns = getSubtransactions();
135 final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
137 FluentFuture<? extends CommitInfo> ret;
139 for (final DOMStoreWriteTransaction txn : txns) {
140 cohorts.add(txn.ready());
143 ret = impl.commit(this, cohorts);
144 } catch (RuntimeException e) {
145 ret = FluentFutures.immediateFailedFluentFuture(
146 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
148 FUTURE_UPDATER.lazySet(this, ret);
152 private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
153 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());