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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.Futures;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
18 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.common.api.CommitInfo;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
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 {@link DOMStoreWriteTransaction} transactions. A sub-transaction
31 * is selected by {@link LogicalDatastoreType} type parameter in:
33 * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
34 * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
35 * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
39 * {@link #submit()} will result in invocation of
40 * {@link DOMDataCommitImplementation#submit(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction, Iterable)}
41 * invocation with all {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for underlying
44 * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as subtransaction.
46 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction>
47 extends AbstractDOMForwardedTransaction<T> implements DOMDataTreeWriteTransaction {
48 @SuppressWarnings("rawtypes")
49 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
50 AbstractDOMForwardedTransactionFactory> IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(
51 DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
52 @SuppressWarnings("rawtypes")
53 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER =
54 AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
55 private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
56 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
59 * Implementation of real commit. It also acts as an indication that the transaction is running -- which we flip
60 * atomically using {@link #IMPL_UPDATER}.
62 private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
65 * Future task of transaction commit. It starts off as null, but is set appropriately on {@link #submit()} and
66 * {@link #cancel()} via {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
68 * Lazy set is safe for use because it is only referenced to in the {@link #cancel()} slow path, where we will
69 * busy-wait for it. The fast path gets the benefit of a store-store barrier instead of the usual store-load
72 private volatile Future<?> commitFuture;
74 protected DOMForwardedWriteTransaction(final Object identifier,
75 final Function<LogicalDatastoreType, T> backingTxFactory,
76 final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
77 super(identifier, backingTxFactory);
78 this.commitImpl = requireNonNull(commitImpl, "commitImpl must not be null.");
82 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) {
83 checkRunning(commitImpl);
84 getSubtransaction(store).write(path, data);
88 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
89 checkRunning(commitImpl);
90 getSubtransaction(store).delete(path);
94 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) {
95 checkRunning(commitImpl);
96 getSubtransaction(store).merge(path, data);
100 public boolean cancel() {
101 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
103 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
104 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
105 closeSubtransactions();
109 // The transaction is in process of being submitted or cancelled. Busy-wait
110 // for the corresponding future.
113 future = commitFuture;
114 } while (future == null);
116 return future.cancel(false);
120 @SuppressWarnings("checkstyle:IllegalCatch")
121 public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
122 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
125 FluentFuture<? extends CommitInfo> ret;
126 final var tx = getSubtransaction();
128 ret = CommitInfo.emptyFluentFuture();
131 ret = impl.commit(this, tx.ready());
132 } catch (RuntimeException e) {
133 ret = immediateFailedFluentFuture(TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
137 FUTURE_UPDATER.lazySet(this, ret);
141 private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
142 checkState(impl != null, "Transaction %s is no longer running", getIdentifier());