Deprecate CloseTransactionChain protobuff message
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionChainProxy.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 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorSelection;
11 import akka.dispatch.Futures;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20 import java.util.concurrent.atomic.AtomicInteger;
21 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
22 import java.util.function.Function;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionChainIdentifier;
25 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
26 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
27 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import scala.concurrent.Future;
37 import scala.concurrent.Promise;
38
39 /**
40  * A chain of {@link TransactionProxy}s. It allows a single open transaction to be open
41  * at a time. For remote transactions, it also tracks the outstanding readiness requests
42  * towards the shard and unblocks operations only after all have completed.
43  */
44 final class TransactionChainProxy extends AbstractTransactionContextFactory<LocalTransactionChain> implements DOMStoreTransactionChain {
45     private static abstract class State {
46         /**
47          * Check if it is okay to allocate a new transaction.
48          * @throws IllegalStateException if a transaction may not be allocated.
49          */
50         abstract void checkReady();
51
52         /**
53          * Return the future which needs to be waited for before shard information
54          * is returned (which unblocks remote transactions).
55          * @return Future to wait for, or null of no wait is necessary
56          */
57         abstract Future<?> previousFuture();
58     }
59
60     private static abstract class Pending extends State {
61         private final TransactionIdentifier transaction;
62         private final Future<?> previousFuture;
63
64         Pending(final TransactionIdentifier transaction, final Future<?> previousFuture) {
65             this.previousFuture = previousFuture;
66             this.transaction = Preconditions.checkNotNull(transaction);
67         }
68
69         @Override
70         final Future<?> previousFuture() {
71             return previousFuture;
72         }
73
74         final TransactionIdentifier getIdentifier() {
75             return transaction;
76         }
77     }
78
79     private static final class Allocated extends Pending {
80         Allocated(final TransactionIdentifier transaction, final Future<?> previousFuture) {
81             super(transaction, previousFuture);
82         }
83
84         @Override
85         void checkReady() {
86             throw new IllegalStateException(String.format("Previous transaction %s is not ready yet", getIdentifier()));
87         }
88     }
89
90     private static final class Submitted extends Pending {
91         Submitted(final TransactionIdentifier transaction, final Future<?> previousFuture) {
92             super(transaction, previousFuture);
93         }
94
95         @Override
96         void checkReady() {
97             // Okay to allocate
98         }
99     }
100
101     private static abstract class DefaultState extends State {
102         @Override
103         final Future<?> previousFuture() {
104             return null;
105         }
106     }
107
108     private static final State IDLE_STATE = new DefaultState() {
109         @Override
110         void checkReady() {
111             // Okay to allocate
112         }
113     };
114
115     private static final State CLOSED_STATE = new DefaultState() {
116         @Override
117         void checkReady() {
118             throw new TransactionChainClosedException("Transaction chain has been closed");
119         }
120     };
121
122     private static final Logger LOG = LoggerFactory.getLogger(TransactionChainProxy.class);
123     private static final AtomicInteger CHAIN_COUNTER = new AtomicInteger();
124     private static final AtomicReferenceFieldUpdater<TransactionChainProxy, State> STATE_UPDATER =
125             AtomicReferenceFieldUpdater.newUpdater(TransactionChainProxy.class, State.class, "currentState");
126
127     private final TransactionChainIdentifier transactionChainId;
128     private final TransactionContextFactory parent;
129     private volatile State currentState = IDLE_STATE;
130
131     /**
132      * This map holds Promise instances for each read-only tx. It is used to maintain ordering of tx creates
133      * wrt to read-only tx's between this class and a LocalTransactionChain since they're bridged by
134      * asynchronous futures. Otherwise, in the following scenario, eg:
135      *
136      *   1) Create write tx1 on chain
137      *   2) do write and submit
138      *   3) Create read-only tx2 on chain and issue read
139      *   4) Create write tx3 on chain, do write but do not submit
140      *
141      * if the sequence/timing is right, tx3 may create its local tx on the LocalTransactionChain before tx2,
142      * which results in tx2 failing b/c tx3 isn't ready yet. So maintaining ordering prevents this issue
143      * (see Bug 4774).
144      * <p>
145      * A Promise is added via newReadOnlyTransaction. When the parent class completes the primary shard
146      * lookup and creates the TransactionContext (either success or failure), onTransactionContextCreated is
147      * called which completes the Promise. A write tx that is created prior to completion will wait on the
148      * Promise's Future via findPrimaryShard.
149      */
150     private final ConcurrentMap<TransactionIdentifier, Promise<Object>> priorReadOnlyTxPromises = new ConcurrentHashMap<>();
151
152     TransactionChainProxy(final TransactionContextFactory parent) {
153         super(parent.getActorContext());
154
155         transactionChainId = new TransactionChainIdentifier(parent.getActorContext().getCurrentMemberName(), CHAIN_COUNTER.incrementAndGet());
156         this.parent = parent;
157     }
158
159     public String getTransactionChainId() {
160         return transactionChainId.toString();
161     }
162
163     @Override
164     public DOMStoreReadTransaction newReadOnlyTransaction() {
165         currentState.checkReady();
166         TransactionProxy transactionProxy = new TransactionProxy(this, TransactionType.READ_ONLY);
167         priorReadOnlyTxPromises.put(transactionProxy.getIdentifier(), Futures.<Object>promise());
168         return transactionProxy;
169     }
170
171     @Override
172     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
173         getActorContext().acquireTxCreationPermit();
174         return allocateWriteTransaction(TransactionType.READ_WRITE);
175     }
176
177     @Override
178     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
179         getActorContext().acquireTxCreationPermit();
180         return allocateWriteTransaction(TransactionType.WRITE_ONLY);
181     }
182
183     @Override
184     public void close() {
185         currentState = CLOSED_STATE;
186
187         // Send a close transaction chain request to each and every shard
188
189         getActorContext().broadcast(new Function<Short, Object>() {
190             @Override
191             public Object apply(Short version) {
192                 return new CloseTransactionChain(transactionChainId.toString(), version).toSerializable();
193             }
194         });
195     }
196
197     private TransactionProxy allocateWriteTransaction(final TransactionType type) {
198         State localState = currentState;
199         localState.checkReady();
200
201         final TransactionProxy ret = new TransactionProxy(this, type);
202         currentState = new Allocated(ret.getIdentifier(), localState.previousFuture());
203         return ret;
204     }
205
206     @Override
207     protected LocalTransactionChain factoryForShard(final String shardName, final ActorSelection shardLeader, final DataTree dataTree) {
208         final LocalTransactionChain ret = new LocalTransactionChain(this, shardLeader, dataTree);
209         LOG.debug("Allocated transaction chain {} for shard {} leader {}", ret, shardName, shardLeader);
210         return ret;
211     }
212
213     /**
214      * This method is overridden to ensure the previous Tx's ready operations complete
215      * before we initiate the next Tx in the chain to avoid creation failures if the
216      * previous Tx's ready operations haven't completed yet.
217      */
218     @SuppressWarnings({ "unchecked", "rawtypes" })
219     @Override
220     protected Future<PrimaryShardInfo> findPrimaryShard(final String shardName, final TransactionIdentifier txId) {
221         // Read current state atomically
222         final State localState = currentState;
223
224         // There are no outstanding futures, shortcut
225         Future<?> previous = localState.previousFuture();
226         if (previous == null) {
227             return combineFutureWithPossiblePriorReadOnlyTxFutures(parent.findPrimaryShard(shardName, txId), txId);
228         }
229
230         final String previousTransactionId;
231
232         if(localState instanceof Pending){
233             previousTransactionId = ((Pending) localState).getIdentifier().toString();
234             LOG.debug("Tx: {} - waiting for ready futures with pending Tx {}", txId, previousTransactionId);
235         } else {
236             previousTransactionId = "";
237             LOG.debug("Waiting for ready futures on chain {}", getTransactionChainId());
238         }
239
240         previous = combineFutureWithPossiblePriorReadOnlyTxFutures(previous, txId);
241
242         // Add a callback for completion of the combined Futures.
243         final Promise<PrimaryShardInfo> returnPromise = Futures.promise();
244
245         final OnComplete onComplete = new OnComplete() {
246             @Override
247             public void onComplete(final Throwable failure, final Object notUsed) {
248                 if (failure != null) {
249                     // A Ready Future failed so fail the returned Promise.
250                     LOG.error("Tx: {} - ready future failed for previous Tx {}", txId, previousTransactionId);
251                     returnPromise.failure(failure);
252                 } else {
253                     LOG.debug("Tx: {} - previous Tx {} readied - proceeding to FindPrimaryShard",
254                             txId, previousTransactionId);
255
256                     // Send the FindPrimaryShard message and use the resulting Future to complete the
257                     // returned Promise.
258                     returnPromise.completeWith(parent.findPrimaryShard(shardName, txId));
259                 }
260             }
261         };
262
263         previous.onComplete(onComplete, getActorContext().getClientDispatcher());
264         return returnPromise.future();
265     }
266
267     private <T> Future<T> combineFutureWithPossiblePriorReadOnlyTxFutures(final Future<T> future,
268             final TransactionIdentifier txId) {
269         if(!priorReadOnlyTxPromises.containsKey(txId) && !priorReadOnlyTxPromises.isEmpty()) {
270             Collection<Entry<TransactionIdentifier, Promise<Object>>> priorReadOnlyTxPromiseEntries =
271                     new ArrayList<>(priorReadOnlyTxPromises.entrySet());
272             if(priorReadOnlyTxPromiseEntries.isEmpty()) {
273                 return future;
274             }
275
276             List<Future<Object>> priorReadOnlyTxFutures = new ArrayList<>(priorReadOnlyTxPromiseEntries.size());
277             for(Entry<TransactionIdentifier, Promise<Object>> entry: priorReadOnlyTxPromiseEntries) {
278                 LOG.debug("Tx: {} - waiting on future for prior read-only Tx {}", txId, entry.getKey());
279                 priorReadOnlyTxFutures.add(entry.getValue().future());
280             }
281
282             Future<Iterable<Object>> combinedFutures = Futures.sequence(priorReadOnlyTxFutures,
283                     getActorContext().getClientDispatcher());
284
285             final Promise<T> returnPromise = Futures.promise();
286             final OnComplete<Iterable<Object>> onComplete = new OnComplete<Iterable<Object>>() {
287                 @Override
288                 public void onComplete(final Throwable failure, final Iterable<Object> notUsed) {
289                     LOG.debug("Tx: {} - prior read-only Tx futures complete", txId);
290
291                     // Complete the returned Promise with the original Future.
292                     returnPromise.completeWith(future);
293                 }
294             };
295
296             combinedFutures.onComplete(onComplete, getActorContext().getClientDispatcher());
297             return returnPromise.future();
298         } else {
299             return future;
300         }
301     }
302
303     @Override
304     protected <T> void onTransactionReady(final TransactionIdentifier transaction, final Collection<Future<T>> cohortFutures) {
305         final State localState = currentState;
306         Preconditions.checkState(localState instanceof Allocated, "Readying transaction %s while state is %s", transaction, localState);
307         final TransactionIdentifier currentTx = ((Allocated)localState).getIdentifier();
308         Preconditions.checkState(transaction.equals(currentTx), "Readying transaction %s while %s is allocated", transaction, currentTx);
309
310         // Transaction ready and we are not waiting for futures -- go to idle
311         if (cohortFutures.isEmpty()) {
312             currentState = IDLE_STATE;
313             return;
314         }
315
316         // Combine the ready Futures into 1
317         final Future<Iterable<T>> combined = Futures.sequence(cohortFutures, getActorContext().getClientDispatcher());
318
319         // Record the we have outstanding futures
320         final State newState = new Submitted(transaction, combined);
321         currentState = newState;
322
323         // Attach a completion reset, but only if we do not allocate a transaction
324         // in-between
325         combined.onComplete(new OnComplete<Iterable<T>>() {
326             @Override
327             public void onComplete(final Throwable arg0, final Iterable<T> arg1) {
328                 STATE_UPDATER.compareAndSet(TransactionChainProxy.this, newState, IDLE_STATE);
329             }
330         }, getActorContext().getClientDispatcher());
331     }
332
333     @Override
334     protected void onTransactionContextCreated(@Nonnull TransactionIdentifier transactionId) {
335         Promise<Object> promise = priorReadOnlyTxPromises.remove(transactionId);
336         if(promise != null) {
337             promise.success(null);
338         }
339     }
340
341     @Override
342     protected TransactionIdentifier nextIdentifier() {
343         return transactionChainId.newTransactionIdentifier();
344     }
345 }