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
9 package org.opendaylight.controller.cluster.databroker;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
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.TransactionStatus;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class DOMBrokerWriteOnlyTransaction<T extends DOMStoreWriteTransaction>
35 extends AbstractDOMBrokerTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
37 private static final AtomicReferenceFieldUpdater<DOMBrokerWriteOnlyTransaction, AbstractDOMTransactionFactory> IMPL_UPDATER =
38 AtomicReferenceFieldUpdater.newUpdater(DOMBrokerWriteOnlyTransaction.class, AbstractDOMTransactionFactory.class, "commitImpl");
39 @SuppressWarnings("rawtypes")
40 private static final AtomicReferenceFieldUpdater<DOMBrokerWriteOnlyTransaction, Future> FUTURE_UPDATER =
41 AtomicReferenceFieldUpdater.newUpdater(DOMBrokerWriteOnlyTransaction.class, Future.class, "commitFuture");
42 private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerWriteOnlyTransaction.class);
43 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
46 * Implementation of real commit. It also acts as an indication that
47 * the transaction is running -- which we flip atomically using
48 * {@link #IMPL_UPDATER}.
50 private volatile AbstractDOMTransactionFactory<?> commitImpl;
53 * Future task of transaction commit. It starts off as null, but is
54 * set appropriately on {@link #submit()} and {@link #cancel()} via
55 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
57 * Lazy set is safe for use because it is only referenced to in the
58 * {@link #cancel()} slow path, where we will busy-wait for it. The
59 * fast path gets the benefit of a store-store barrier instead of the
60 * usual store-load barrier.
62 private volatile Future<?> commitFuture;
64 protected DOMBrokerWriteOnlyTransaction(final Object identifier,
65 final Map storeTxFactories, final AbstractDOMTransactionFactory<?> commitImpl) {
66 super(identifier, storeTxFactories);
67 this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
71 protected T createTransaction(final LogicalDatastoreType key) {
72 // FIXME : Casting shouldn't be necessary here
73 return (T) getTxFactory(key).newWriteOnlyTransaction();
77 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, 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 final PathArgument lastArg = path.getLastPathArgument();
86 Preconditions.checkArgument(
87 (lastArg == data.getIdentifier()) || (lastArg != null && lastArg.equals(data.getIdentifier())),
88 "Instance identifier references %s but data identifier is %s", lastArg, data);
92 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
93 checkRunning(commitImpl);
94 getSubtransaction(store).delete(path);
98 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
99 checkRunning(commitImpl);
100 checkInstanceIdentifierReferencesData(path, data);
101 getSubtransaction(store).merge(path, data);
105 public boolean cancel() {
106 final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
108 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
109 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
110 closeSubtransactions();
114 // The transaction is in process of being submitted or cancelled. Busy-wait
115 // for the corresponding future.
118 future = commitFuture;
119 } while (future == null);
121 return future.cancel(false);
126 public ListenableFuture<RpcResult<TransactionStatus>> commit() {
127 return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
131 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
132 final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
135 final Collection<T> txns = getSubtransactions();
136 final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
138 // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
139 for (final DOMStoreWriteTransaction txn : txns) {
140 cohorts.add(txn.ready());
143 final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
144 FUTURE_UPDATER.lazySet(this, ret);
148 private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
149 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());