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.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)}
41 * {@link #submit()} will result in invocation of
42 * {@link DOMDataCommitImplementation#submit(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction, Iterable)}
44 * {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for
45 * underlying transactions.
47 * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as subtransaction.
49 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
50 AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataTreeWriteTransaction {
51 @SuppressWarnings("rawtypes")
52 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
53 AbstractDOMForwardedTransactionFactory> IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(
54 DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
55 @SuppressWarnings("rawtypes")
56 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER =
57 AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
58 private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
59 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
62 * Implementation of real commit. It also acts as an indication that
63 * the transaction is running -- which we flip atomically using
64 * {@link #IMPL_UPDATER}.
66 private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
69 * Future task of transaction commit. It starts off as null, but is
70 * set appropriately on {@link #submit()} and {@link #cancel()} via
71 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
74 * Lazy set is safe for use because it is only referenced to in the
75 * {@link #cancel()} slow path, where we will busy-wait for it. The
76 * fast path gets the benefit of a store-store barrier instead of the
77 * usual store-load barrier.
79 private volatile Future<?> commitFuture;
81 protected DOMForwardedWriteTransaction(final Object identifier,
82 final Map<LogicalDatastoreType, T> backingTxs, final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
83 super(identifier, backingTxs);
84 this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
88 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
89 final NormalizedNode<?, ?> data) {
90 checkRunning(commitImpl);
91 getSubtransaction(store).write(path, data);
95 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
96 checkRunning(commitImpl);
97 getSubtransaction(store).delete(path);
101 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
102 final NormalizedNode<?, ?> data) {
103 checkRunning(commitImpl);
104 getSubtransaction(store).merge(path, data);
108 public boolean cancel() {
109 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
111 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
112 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
113 closeSubtransactions();
117 // The transaction is in process of being submitted or cancelled. Busy-wait
118 // for the corresponding future.
121 future = commitFuture;
122 } while (future == null);
124 return future.cancel(false);
128 @SuppressWarnings("checkstyle:IllegalCatch")
129 public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
130 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
133 final Collection<T> txns = getSubtransactions();
134 final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
136 FluentFuture<? extends CommitInfo> ret;
138 for (final DOMStoreWriteTransaction txn : txns) {
139 cohorts.add(txn.ready());
142 ret = impl.commit(this, cohorts);
143 } catch (RuntimeException e) {
144 ret = FluentFuture.from(Futures.immediateFailedFuture(
145 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e)));
147 FUTURE_UPDATER.lazySet(this, ret);
151 private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
152 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());