df32a2d54267af7be5eab321845ae72026762129
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / DOMBrokerWriteOnlyTransaction.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.databroker;
10
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;
17 import java.util.Map;
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;
33
34 public class DOMBrokerWriteOnlyTransaction<T extends DOMStoreWriteTransaction>
35         extends AbstractDOMBrokerTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
36
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();
44
45     /**
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}.
49      */
50     private volatile AbstractDOMTransactionFactory<?> commitImpl;
51
52     /**
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)}.
56      *
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.
61      */
62     private volatile Future<?> commitFuture;
63
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.");
68     }
69
70     @Override
71     protected T createTransaction(final LogicalDatastoreType key) {
72         // FIXME : Casting shouldn't be necessary here
73         return (T) getTxFactory(key).newWriteOnlyTransaction();
74     }
75
76     @Override
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);
81     }
82
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);
89     }
90
91     @Override
92     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
93         checkRunning(commitImpl);
94         getSubtransaction(store).delete(path);
95     }
96
97     @Override
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);
102     }
103
104     @Override
105     public boolean cancel() {
106         final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
107         if (impl != null) {
108             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
109             FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
110             closeSubtransactions();
111             return true;
112         }
113
114         // The transaction is in process of being submitted or cancelled. Busy-wait
115         // for the corresponding future.
116         Future<?> future;
117         do {
118             future = commitFuture;
119         } while (future == null);
120
121         return future.cancel(false);
122     }
123
124     @Deprecated
125     @Override
126     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
127         return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
128     }
129
130     @Override
131     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
132         final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
133         checkRunning(impl);
134
135         final Collection<T> txns = getSubtransactions();
136         final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
137
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());
141         }
142
143         final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
144         FUTURE_UPDATER.lazySet(this, ret);
145         return ret;
146     }
147
148     private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
149         Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
150     }
151
152 }