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