2 * Copyright (c) 2015 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.cluster.databroker;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.util.concurrent.FluentFuture;
16 import com.google.common.util.concurrent.Futures;
17 import java.util.ArrayList;
18 import java.util.Collection;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
22 import org.opendaylight.mdsal.common.api.CommitInfo;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
25 import org.opendaylight.mdsal.dom.broker.TransactionCommitFailedExceptionMapper;
26 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
27 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionFactory;
28 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public abstract class AbstractDOMBrokerWriteTransaction<T extends DOMStoreWriteTransaction>
36 extends AbstractDOMBrokerTransaction<T> implements DOMDataTreeWriteTransaction {
38 @SuppressWarnings("rawtypes")
39 private static final AtomicReferenceFieldUpdater<AbstractDOMBrokerWriteTransaction, AbstractDOMTransactionFactory>
40 IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(AbstractDOMBrokerWriteTransaction.class,
41 AbstractDOMTransactionFactory.class, "commitImpl");
42 @SuppressWarnings("rawtypes")
43 private static final AtomicReferenceFieldUpdater<AbstractDOMBrokerWriteTransaction, Future> FUTURE_UPDATER =
44 AtomicReferenceFieldUpdater.newUpdater(AbstractDOMBrokerWriteTransaction.class, Future.class,
46 private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMBrokerWriteTransaction.class);
47 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
50 * Implementation of real commit. It also acts as an indication that
51 * the transaction is running -- which we flip atomically using
52 * {@link #IMPL_UPDATER}.
54 private volatile AbstractDOMTransactionFactory<?> commitImpl;
57 * Future task of transaction commit. It starts off as null, but is
58 * set appropriately on {@link #submit()} and {@link #cancel()} via
59 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
61 * Lazy set is safe for use because it is only referenced to in the
62 * {@link #cancel()} slow path, where we will busy-wait for it. The
63 * fast path gets the benefit of a store-store barrier instead of the
64 * usual store-load barrier.
66 private volatile Future<?> commitFuture;
68 protected AbstractDOMBrokerWriteTransaction(final Object identifier,
69 final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories,
70 final AbstractDOMTransactionFactory<?> commitImpl) {
71 super(identifier, storeTxFactories);
72 this.commitImpl = requireNonNull(commitImpl, "commitImpl must not be null.");
76 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
77 final NormalizedNode<?, ?> data) {
78 checkRunning(commitImpl);
79 checkInstanceIdentifierReferencesData(path,data);
80 getSubtransaction(store).write(path, data);
83 private static void checkInstanceIdentifierReferencesData(final YangInstanceIdentifier path,
84 final NormalizedNode<?, ?> data) {
85 checkArgument(data != null, "Attempted to store null data at %s", path);
86 final PathArgument lastArg = path.getLastPathArgument();
87 if (lastArg != null) {
88 checkArgument(lastArg.equals(data.getIdentifier()),
89 "Instance identifier references %s but data identifier is %s", lastArg, data);
94 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
95 checkRunning(commitImpl);
96 getSubtransaction(store).delete(path);
100 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
101 final NormalizedNode<?, ?> data) {
102 checkRunning(commitImpl);
103 checkInstanceIdentifierReferencesData(path, data);
104 getSubtransaction(store).merge(path, data);
108 public boolean cancel() {
109 final AbstractDOMTransactionFactory<?> 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;
123 while (future == null);
125 return future.cancel(false);
129 @SuppressWarnings("checkstyle:IllegalCatch")
130 public FluentFuture<? extends CommitInfo> commit() {
131 final AbstractDOMTransactionFactory<?> 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 T txn : txns) {
140 cohorts.add(txn.ready());
143 ret = impl.commit(this, cohorts);
144 } catch (RuntimeException e) {
145 ret = FluentFuture.from(Futures.immediateFailedFuture(
146 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e)));
148 FUTURE_UPDATER.lazySet(this, ret);
152 private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
153 checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
157 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
158 return super.addToStringAttributes(toStringHelper).add("running", commitImpl == null);