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.controller.md.sal.dom.broker.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Supplier;
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.ArrayList;
16 import java.util.Collection;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
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:
37 * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
38 * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
39 * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
43 * {@link #commit()} will result in invocation of
44 * {@link DOMDataCommitImplementation#submit(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction,
45 * Iterable)} invocation with all {@link org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort}
46 * for underlying transactions.
48 * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as
51 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
52 AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
53 @SuppressWarnings("rawtypes")
54 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
55 AbstractDOMForwardedTransactionFactory>
56 IMPL_UPDATER = AtomicReferenceFieldUpdater
57 .newUpdater(DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
58 @SuppressWarnings("rawtypes")
59 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER
60 = AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
61 private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
62 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
65 * Implementation of real commit. It also acts as an indication that
66 * the transaction is running -- which we flip atomically using
67 * {@link #IMPL_UPDATER}.
69 private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
72 * Future task of transaction commit. It starts off as null, but is
73 * set appropriately on {@link #commit()} and {@link #cancel()} via
74 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
77 * Lazy set is safe for use because it is only referenced to in the
78 * {@link #cancel()} slow path, where we will busy-wait for it. The
79 * fast path gets the benefit of a store-store barrier instead of the
80 * usual store-load barrier.
82 private volatile Future<?> commitFuture;
84 protected DOMForwardedWriteTransaction(final Object identifier, final Map<LogicalDatastoreType, T> backingTxs,
85 final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
86 super(identifier, backingTxs);
87 this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
91 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
92 final NormalizedNode<?, ?> data) {
93 checkRunning(commitImpl);
94 getSubtransaction(store).write(path, data);
98 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
99 checkRunning(commitImpl);
100 getSubtransaction(store).delete(path);
104 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
105 final NormalizedNode<?, ?> data) {
106 checkRunning(commitImpl);
107 getSubtransaction(store).merge(path, data);
111 public boolean cancel() {
112 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
114 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
115 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
116 closeSubtransactions();
120 // The transaction is in process of being submitted or cancelled. Busy-wait
121 // for the corresponding future.
124 future = commitFuture;
125 } while (future == null);
127 return future.cancel(false);
131 public FluentFuture<? extends CommitInfo> commit() {
132 return FluentFuture.from(doCommit(CommitInfo::empty));
135 @SuppressWarnings("checkstyle:IllegalCatch")
136 private <V> ListenableFuture<V> doCommit(Supplier<V> futureValueSupplier) {
137 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
140 final Collection<T> txns = getSubtransactions();
141 final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
143 ListenableFuture<V> ret;
145 for (DOMStoreWriteTransaction txn : txns) {
146 cohorts.add(txn.ready());
149 ret = impl.commit(this, cohorts, futureValueSupplier);
150 } catch (RuntimeException e) {
151 ret = FluentFuture.from(Futures.immediateFailedFuture(
152 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e)));
154 FUTURE_UPDATER.lazySet(this, ret);
158 private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
159 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());