Merge "Create transaction on the backend datastore only when neccessary"
[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.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class DOMBrokerWriteOnlyTransaction<T extends DOMStoreWriteTransaction>
34         extends AbstractDOMBrokerTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
35
36     private static final AtomicReferenceFieldUpdater<DOMBrokerWriteOnlyTransaction, AbstractDOMTransactionFactory> IMPL_UPDATER =
37             AtomicReferenceFieldUpdater.newUpdater(DOMBrokerWriteOnlyTransaction.class, AbstractDOMTransactionFactory.class, "commitImpl");
38     @SuppressWarnings("rawtypes")
39     private static final AtomicReferenceFieldUpdater<DOMBrokerWriteOnlyTransaction, Future> FUTURE_UPDATER =
40             AtomicReferenceFieldUpdater.newUpdater(DOMBrokerWriteOnlyTransaction.class, Future.class, "commitFuture");
41     private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerWriteOnlyTransaction.class);
42     private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
43
44     /**
45      * Implementation of real commit. It also acts as an indication that
46      * the transaction is running -- which we flip atomically using
47      * {@link #IMPL_UPDATER}.
48      */
49     private volatile AbstractDOMTransactionFactory<?> commitImpl;
50
51     /**
52      * Future task of transaction commit. It starts off as null, but is
53      * set appropriately on {@link #submit()} and {@link #cancel()} via
54      * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
55      *
56      * Lazy set is safe for use because it is only referenced to in the
57      * {@link #cancel()} slow path, where we will busy-wait for it. The
58      * fast path gets the benefit of a store-store barrier instead of the
59      * usual store-load barrier.
60      */
61     private volatile Future<?> commitFuture;
62
63     protected DOMBrokerWriteOnlyTransaction(final Object identifier,
64                                             Map storeTxFactories, final AbstractDOMTransactionFactory<?> commitImpl) {
65         super(identifier, storeTxFactories);
66         this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
67     }
68
69     @Override
70     protected T createTransaction(LogicalDatastoreType key) {
71         // FIXME : Casting shouldn't be necessary here
72         return (T) getTxFactory(key).newWriteOnlyTransaction();
73     }
74
75     @Override
76     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
77         checkRunning(commitImpl);
78         getSubtransaction(store).write(path, data);
79     }
80
81     @Override
82     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
83         checkRunning(commitImpl);
84         getSubtransaction(store).delete(path);
85     }
86
87     @Override
88     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
89         checkRunning(commitImpl);
90         getSubtransaction(store).merge(path, data);
91     }
92
93     @Override
94     public boolean cancel() {
95         final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
96         if (impl != null) {
97             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
98             FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
99             closeSubtransactions();
100             return true;
101         }
102
103         // The transaction is in process of being submitted or cancelled. Busy-wait
104         // for the corresponding future.
105         Future<?> future;
106         do {
107             future = commitFuture;
108         } while (future == null);
109
110         return future.cancel(false);
111     }
112
113     @Deprecated
114     @Override
115     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
116         return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
117     }
118
119     @Override
120     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
121         final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
122         checkRunning(impl);
123
124         final Collection<T> txns = getSubtransactions();
125         final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
126
127         // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
128         for (DOMStoreWriteTransaction txn : txns) {
129             cohorts.add(txn.ready());
130         }
131
132         final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
133         FUTURE_UPDATER.lazySet(this, ret);
134         return ret;
135     }
136
137     private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
138         Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
139     }
140
141 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.