Merge "Do not use ActorSystem.actorFor as it is deprecated"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionContextImpl.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.Mapper;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Lists;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.List;
17 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
18 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
19 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
20 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
22 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
23 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
28 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
29 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
30 import org.opendaylight.controller.cluster.datastore.modification.Modification;
31 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
32 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import scala.concurrent.Future;
40
41 public class TransactionContextImpl extends AbstractTransactionContext {
42     private static final Logger LOG = LoggerFactory.getLogger(TransactionContextImpl.class);
43
44     private final String transactionChainId;
45     private final ActorContext actorContext;
46     private final ActorSelection actor;
47     private final boolean isTxActorLocal;
48     private final short remoteTransactionVersion;
49
50     private final OperationCompleter operationCompleter;
51     private BatchedModifications batchedModifications;
52
53     protected TransactionContextImpl(ActorSelection actor, TransactionIdentifier identifier,
54             String transactionChainId, ActorContext actorContext, SchemaContext schemaContext, boolean isTxActorLocal,
55             short remoteTransactionVersion, OperationCompleter operationCompleter) {
56         super(identifier);
57         this.actor = actor;
58         this.transactionChainId = transactionChainId;
59         this.actorContext = actorContext;
60         this.isTxActorLocal = isTxActorLocal;
61         this.remoteTransactionVersion = remoteTransactionVersion;
62         this.operationCompleter = operationCompleter;
63     }
64
65     private Future<Object> completeOperation(Future<Object> operationFuture){
66         operationFuture.onComplete(this.operationCompleter, actorContext.getClientDispatcher());
67         return operationFuture;
68     }
69
70
71     private ActorSelection getActor() {
72         return actor;
73     }
74
75     protected ActorContext getActorContext() {
76         return actorContext;
77     }
78
79     protected short getRemoteTransactionVersion() {
80         return remoteTransactionVersion;
81     }
82
83     protected Future<Object> executeOperationAsync(SerializableMessage msg) {
84         return completeOperation(actorContext.executeOperationAsync(getActor(), isTxActorLocal ? msg : msg.toSerializable()));
85     }
86
87     @Override
88     public void closeTransaction() {
89         LOG.debug("Tx {} closeTransaction called", identifier);
90
91         actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable());
92     }
93
94     @Override
95     public Future<ActorSelection> readyTransaction() {
96         LOG.debug("Tx {} readyTransaction called with {} previous recorded operations pending",
97                 identifier, recordedOperationFutures.size());
98
99         // Send the remaining batched modifications if any.
100
101         sendAndRecordBatchedModifications();
102
103         // Send the ReadyTransaction message to the Tx actor.
104
105         Future<Object> readyReplyFuture = executeOperationAsync(ReadyTransaction.INSTANCE);
106
107         return combineRecordedOperationsFutures(readyReplyFuture);
108     }
109
110     protected Future<ActorSelection> combineRecordedOperationsFutures(final Future<Object> withLastReplyFuture) {
111         // Combine all the previously recorded put/merge/delete operation reply Futures and the
112         // ReadyTransactionReply Future into one Future. If any one fails then the combined
113         // Future will fail. We need all prior operations and the ready operation to succeed
114         // in order to attempt commit.
115
116         List<Future<Object>> futureList = Lists.newArrayListWithCapacity(recordedOperationFutures.size() + 1);
117         futureList.addAll(recordedOperationFutures);
118         futureList.add(withLastReplyFuture);
119
120         Future<Iterable<Object>> combinedFutures = akka.dispatch.Futures.sequence(futureList,
121                 actorContext.getClientDispatcher());
122
123         // Transform the combined Future into a Future that returns the cohort actor path from
124         // the ReadyTransactionReply. That's the end result of the ready operation.
125
126         return combinedFutures.transform(new Mapper<Iterable<Object>, ActorSelection>() {
127             @Override
128             public ActorSelection checkedApply(Iterable<Object> notUsed) {
129                 LOG.debug("Tx {} readyTransaction: pending recorded operations succeeded",
130                         identifier);
131
132                 // At this point all the Futures succeeded and we need to extract the cohort
133                 // actor path from the ReadyTransactionReply. For the recorded operations, they
134                 // don't return any data so we're only interested that they completed
135                 // successfully. We could be paranoid and verify the correct reply types but
136                 // that really should never happen so it's not worth the overhead of
137                 // de-serializing each reply.
138
139                 // Note the Future get call here won't block as it's complete.
140                 Object serializedReadyReply = withLastReplyFuture.value().get().get();
141                 if (serializedReadyReply instanceof ReadyTransactionReply) {
142                     return actorContext.actorSelection(((ReadyTransactionReply)serializedReadyReply).getCohortPath());
143                 } else if(serializedReadyReply instanceof BatchedModificationsReply) {
144                     return actorContext.actorSelection(((BatchedModificationsReply)serializedReadyReply).getCohortPath());
145                 } else if(serializedReadyReply.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
146                     ReadyTransactionReply reply = ReadyTransactionReply.fromSerializable(serializedReadyReply);
147                     String cohortPath = deserializeCohortPath(reply.getCohortPath());
148                     return actorContext.actorSelection(cohortPath);
149                 } else {
150                     // Throwing an exception here will fail the Future.
151                     throw new IllegalArgumentException(String.format("%s: Invalid reply type %s",
152                             identifier, serializedReadyReply.getClass()));
153                 }
154             }
155         }, TransactionProxy.SAME_FAILURE_TRANSFORMER, actorContext.getClientDispatcher());
156     }
157
158     protected String deserializeCohortPath(String cohortPath) {
159         return cohortPath;
160     }
161
162     private void batchModification(Modification modification) {
163         if(batchedModifications == null) {
164             batchedModifications = new BatchedModifications(identifier.toString(), remoteTransactionVersion,
165                     transactionChainId);
166         }
167
168         batchedModifications.addModification(modification);
169
170         if(batchedModifications.getModifications().size() >=
171                 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
172             sendAndRecordBatchedModifications();
173         }
174     }
175
176     private void sendAndRecordBatchedModifications() {
177         Future<Object> sentFuture = sendBatchedModifications();
178         if(sentFuture != null) {
179             recordedOperationFutures.add(sentFuture);
180         }
181     }
182
183     protected Future<Object> sendBatchedModifications() {
184         return sendBatchedModifications(false);
185     }
186
187     protected Future<Object> sendBatchedModifications(boolean ready) {
188         Future<Object> sent = null;
189         if(batchedModifications != null) {
190             if(LOG.isDebugEnabled()) {
191                 LOG.debug("Tx {} sending {} batched modifications, ready: {}", identifier,
192                         batchedModifications.getModifications().size(), ready);
193             }
194
195             batchedModifications.setReady(ready);
196             sent = executeOperationAsync(batchedModifications);
197
198             batchedModifications = new BatchedModifications(identifier.toString(), remoteTransactionVersion,
199                     transactionChainId);
200         }
201
202         return sent;
203     }
204
205     @Override
206     public void deleteData(YangInstanceIdentifier path) {
207         LOG.debug("Tx {} deleteData called path = {}", identifier, path);
208
209         batchModification(new DeleteModification(path));
210     }
211
212     @Override
213     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
214         LOG.debug("Tx {} mergeData called path = {}", identifier, path);
215
216         batchModification(new MergeModification(path, data));
217     }
218
219     @Override
220     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
221         LOG.debug("Tx {} writeData called path = {}", identifier, path);
222
223         batchModification(new WriteModification(path, data));
224     }
225
226     @Override
227     public void readData(
228             final YangInstanceIdentifier path,final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture ) {
229
230         LOG.debug("Tx {} readData called path = {}", identifier, path);
231
232         // Send the remaining batched modifications if any.
233
234         sendAndRecordBatchedModifications();
235
236         // If there were any previous recorded put/merge/delete operation reply Futures then we
237         // must wait for them to successfully complete. This is necessary to honor the read
238         // uncommitted semantics of the public API contract. If any one fails then fail the read.
239
240         if(recordedOperationFutures.isEmpty()) {
241             finishReadData(path, returnFuture);
242         } else {
243             LOG.debug("Tx {} readData: verifying {} previous recorded operations",
244                     identifier, recordedOperationFutures.size());
245
246             // Note: we make a copy of recordedOperationFutures to be on the safe side in case
247             // Futures#sequence accesses the passed List on a different thread, as
248             // recordedOperationFutures is not synchronized.
249
250             Future<Iterable<Object>> combinedFutures = akka.dispatch.Futures.sequence(
251                     Lists.newArrayList(recordedOperationFutures),
252                     actorContext.getClientDispatcher());
253
254             OnComplete<Iterable<Object>> onComplete = new OnComplete<Iterable<Object>>() {
255                 @Override
256                 public void onComplete(Throwable failure, Iterable<Object> notUsed)
257                         throws Throwable {
258                     if(failure != null) {
259                         LOG.debug("Tx {} readData: a recorded operation failed: {}",
260                                 identifier, failure);
261                         returnFuture.setException(new ReadFailedException(
262                                 "The read could not be performed because a previous put, merge,"
263                                 + "or delete operation failed", failure));
264                     } else {
265                         finishReadData(path, returnFuture);
266                     }
267                 }
268             };
269
270             combinedFutures.onComplete(onComplete, actorContext.getClientDispatcher());
271         }
272
273     }
274
275     private void finishReadData(final YangInstanceIdentifier path,
276             final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture) {
277
278         LOG.debug("Tx {} finishReadData called path = {}", identifier, path);
279
280         OnComplete<Object> onComplete = new OnComplete<Object>() {
281             @Override
282             public void onComplete(Throwable failure, Object readResponse) throws Throwable {
283                 if(failure != null) {
284                     LOG.debug("Tx {} read operation failed: {}", identifier, failure);
285                     returnFuture.setException(new ReadFailedException(
286                             "Error reading data for path " + path, failure));
287
288                 } else {
289                     LOG.debug("Tx {} read operation succeeded", identifier, failure);
290
291                     if (readResponse instanceof ReadDataReply) {
292                         ReadDataReply reply = (ReadDataReply) readResponse;
293                         returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
294
295                     } else if (ReadDataReply.isSerializedType(readResponse)) {
296                         ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
297                         returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
298
299                     } else {
300                         returnFuture.setException(new ReadFailedException(
301                             "Invalid response reading data for path " + path));
302                     }
303                 }
304             }
305         };
306
307         Future<Object> readFuture = executeOperationAsync(new ReadData(path));
308
309         readFuture.onComplete(onComplete, actorContext.getClientDispatcher());
310     }
311
312     @Override
313     public void dataExists(final YangInstanceIdentifier path, final SettableFuture<Boolean> returnFuture) {
314
315         LOG.debug("Tx {} dataExists called path = {}", identifier, path);
316
317         // Send the remaining batched modifications if any.
318
319         sendAndRecordBatchedModifications();
320
321         // If there were any previous recorded put/merge/delete operation reply Futures then we
322         // must wait for them to successfully complete. This is necessary to honor the read
323         // uncommitted semantics of the public API contract. If any one fails then fail this
324         // request.
325
326         if(recordedOperationFutures.isEmpty()) {
327             finishDataExists(path, returnFuture);
328         } else {
329             LOG.debug("Tx {} dataExists: verifying {} previous recorded operations",
330                     identifier, recordedOperationFutures.size());
331
332             // Note: we make a copy of recordedOperationFutures to be on the safe side in case
333             // Futures#sequence accesses the passed List on a different thread, as
334             // recordedOperationFutures is not synchronized.
335
336             Future<Iterable<Object>> combinedFutures = akka.dispatch.Futures.sequence(
337                     Lists.newArrayList(recordedOperationFutures),
338                     actorContext.getClientDispatcher());
339             OnComplete<Iterable<Object>> onComplete = new OnComplete<Iterable<Object>>() {
340                 @Override
341                 public void onComplete(Throwable failure, Iterable<Object> notUsed)
342                         throws Throwable {
343                     if(failure != null) {
344                         LOG.debug("Tx {} dataExists: a recorded operation failed: {}",
345                                 identifier, failure);
346                         returnFuture.setException(new ReadFailedException(
347                                 "The data exists could not be performed because a previous "
348                                 + "put, merge, or delete operation failed", failure));
349                     } else {
350                         finishDataExists(path, returnFuture);
351                     }
352                 }
353             };
354
355             combinedFutures.onComplete(onComplete, actorContext.getClientDispatcher());
356         }
357     }
358
359     private void finishDataExists(final YangInstanceIdentifier path,
360             final SettableFuture<Boolean> returnFuture) {
361
362         LOG.debug("Tx {} finishDataExists called path = {}", identifier, path);
363
364         OnComplete<Object> onComplete = new OnComplete<Object>() {
365             @Override
366             public void onComplete(Throwable failure, Object response) throws Throwable {
367                 if(failure != null) {
368                     LOG.debug("Tx {} dataExists operation failed: {}", identifier, failure);
369                     returnFuture.setException(new ReadFailedException(
370                             "Error checking data exists for path " + path, failure));
371                 } else {
372                     LOG.debug("Tx {} dataExists operation succeeded", identifier, failure);
373
374                     if (response instanceof DataExistsReply) {
375                         returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
376
377                     } else if (response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
378                         returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
379
380                     } else {
381                         returnFuture.setException(new ReadFailedException(
382                                 "Invalid response checking exists for path " + path));
383                     }
384                 }
385             }
386         };
387
388         Future<Object> future = executeOperationAsync(new DataExists(path));
389
390         future.onComplete(onComplete, actorContext.getClientDispatcher());
391     }
392 }