Fixup checkstyle
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardCommitCoordinator.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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 static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.Status.Failure;
14 import akka.serialization.Serialization;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.primitives.UnsignedLong;
17 import com.google.common.util.concurrent.FutureCallback;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.ArrayDeque;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Deque;
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.Map;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
29 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
30 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
31 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
32 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
33 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
34 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
35 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
36 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
37 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
38 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
39 import org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage;
40 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
41 import org.opendaylight.yangtools.concepts.Identifier;
42 import org.opendaylight.yangtools.yang.common.Empty;
43 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
44 import org.slf4j.Logger;
45
46 /**
47  * Coordinates commits for a shard ensuring only one concurrent 3-phase commit.
48  *
49  * @author Thomas Pantelis
50  */
51 final class ShardCommitCoordinator {
52
53     // Interface hook for unit tests to replace or decorate the ShardDataTreeCohorts.
54     @VisibleForTesting
55     public interface CohortDecorator {
56         ShardDataTreeCohort decorate(Identifier transactionID, ShardDataTreeCohort actual);
57     }
58
59     private final Map<Identifier, CohortEntry> cohortCache = new HashMap<>();
60
61     private final ShardDataTree dataTree;
62
63     private final Logger log;
64
65     private final String name;
66
67     // This is a hook for unit tests to replace or decorate the ShardDataTreeCohorts.
68     @VisibleForTesting
69     private CohortDecorator cohortDecorator;
70
71     private ReadyTransactionReply readyTransactionReply;
72
73     ShardCommitCoordinator(final ShardDataTree dataTree, final Logger log, final String name) {
74         this.log = log;
75         this.name = name;
76         this.dataTree = requireNonNull(dataTree);
77     }
78
79     int getCohortCacheSize() {
80         return cohortCache.size();
81     }
82
83     private String persistenceId() {
84         return dataTree.logContext();
85     }
86
87     private ReadyTransactionReply readyTransactionReply(final ActorRef cohort) {
88         if (readyTransactionReply == null) {
89             readyTransactionReply = new ReadyTransactionReply(Serialization.serializedActorPath(cohort));
90         }
91
92         return readyTransactionReply;
93     }
94
95     /**
96      * This method is called to ready a transaction that was prepared by ShardTransaction actor. It caches
97      * the prepared cohort entry for the given transactions ID in preparation for the subsequent 3-phase commit.
98      *
99      * @param ready the ForwardedReadyTransaction message to process
100      * @param sender the sender of the message
101      * @param shard the transaction's shard actor
102      */
103     void handleForwardedReadyTransaction(final ForwardedReadyTransaction ready, final ActorRef sender,
104             final Shard shard) {
105         log.debug("{}: Readying transaction {}, client version {}", name,
106                 ready.getTransactionId(), ready.getTxnClientVersion());
107
108         final ShardDataTreeCohort cohort = ready.getTransaction().ready(ready.getParticipatingShardNames());
109         final CohortEntry cohortEntry = CohortEntry.createReady(cohort, ready.getTxnClientVersion());
110         cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
111
112         if (ready.isDoImmediateCommit()) {
113             cohortEntry.setDoImmediateCommit(true);
114             cohortEntry.setReplySender(sender);
115             cohortEntry.setShard(shard);
116             handleCanCommit(cohortEntry);
117         } else {
118             // The caller does not want immediate commit - the 3-phase commit will be coordinated by the
119             // front-end so send back a ReadyTransactionReply with our actor path.
120             sender.tell(readyTransactionReply(shard.self()), shard.self());
121         }
122     }
123
124     /**
125      * This method handles a BatchedModifications message for a transaction being prepared directly on the
126      * Shard actor instead of via a ShardTransaction actor. If there's no currently cached
127      * DOMStoreWriteTransaction, one is created. The batched modifications are applied to the write Tx. If
128      * the BatchedModifications is ready to commit then a DOMStoreThreePhaseCommitCohort is created.
129      *
130      * @param batched the BatchedModifications message to process
131      * @param sender the sender of the message
132      */
133     @SuppressFBWarnings(value = "THROWS_METHOD_THROWS_RUNTIMEEXCEPTION", justification = "Replay of captured failure")
134     void handleBatchedModifications(final BatchedModifications batched, final ActorRef sender, final Shard shard) {
135         CohortEntry cohortEntry = cohortCache.get(batched.getTransactionId());
136         if (cohortEntry == null || cohortEntry.isSealed()) {
137             cohortEntry = CohortEntry.createOpen(dataTree.newReadWriteTransaction(batched.getTransactionId()),
138                 batched.getVersion());
139             cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
140         }
141
142         if (log.isDebugEnabled()) {
143             log.debug("{}: Applying {} batched modifications for Tx {}", name,
144                     batched.getModifications().size(), batched.getTransactionId());
145         }
146
147         cohortEntry.applyModifications(batched.getModifications());
148
149         if (batched.isReady()) {
150             if (cohortEntry.getLastBatchedModificationsException() != null) {
151                 cohortCache.remove(cohortEntry.getTransactionId());
152                 throw cohortEntry.getLastBatchedModificationsException();
153             }
154
155             if (cohortEntry.getTotalBatchedModificationsReceived() != batched.getTotalMessagesSent()) {
156                 cohortCache.remove(cohortEntry.getTransactionId());
157                 throw new IllegalStateException(String.format(
158                         "The total number of batched messages received %d does not match the number sent %d",
159                         cohortEntry.getTotalBatchedModificationsReceived(), batched.getTotalMessagesSent()));
160             }
161
162             if (log.isDebugEnabled()) {
163                 log.debug("{}: Readying Tx {} of {} operations, client version {}", name,
164                         batched.getTransactionId(), cohortEntry.getTotalOperationsProcessed(), batched.getVersion());
165             }
166
167             cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
168             cohortEntry.ready(batched.getParticipatingShardNames(), cohortDecorator);
169
170             if (batched.isDoCommitOnReady()) {
171                 cohortEntry.setReplySender(sender);
172                 cohortEntry.setShard(shard);
173                 handleCanCommit(cohortEntry);
174             } else {
175                 sender.tell(readyTransactionReply(shard.self()), shard.self());
176             }
177         } else {
178             sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
179         }
180     }
181
182     /**
183      * This method handles {@link ReadyLocalTransaction} message. All transaction modifications have
184      * been prepared beforehand by the sender and we just need to drive them through into the
185      * dataTree.
186      *
187      * @param message the ReadyLocalTransaction message to process
188      * @param sender the sender of the message
189      * @param shard the transaction's shard actor
190      */
191     void handleReadyLocalTransaction(final ReadyLocalTransaction message, final ActorRef sender, final Shard shard) {
192         final TransactionIdentifier txId = message.getTransactionId();
193         final ShardDataTreeCohort cohort = dataTree.newReadyCohort(txId, message.getModification(),
194                 message.getParticipatingShardNames());
195         final CohortEntry cohortEntry = CohortEntry.createReady(cohort, DataStoreVersions.CURRENT_VERSION);
196         cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
197         cohortEntry.setDoImmediateCommit(message.isDoCommitOnReady());
198
199         log.debug("{}: Applying local modifications for Tx {}", name, txId);
200
201         if (message.isDoCommitOnReady()) {
202             cohortEntry.setReplySender(sender);
203             cohortEntry.setShard(shard);
204             handleCanCommit(cohortEntry);
205         } else {
206             sender.tell(readyTransactionReply(shard.self()), shard.self());
207         }
208     }
209
210     Collection<BatchedModifications> createForwardedBatchedModifications(final BatchedModifications from,
211             final int maxModificationsPerBatch) {
212         CohortEntry cohortEntry = cohortCache.remove(from.getTransactionId());
213         if (cohortEntry == null || cohortEntry.getTransaction() == null) {
214             return Collections.singletonList(from);
215         }
216
217         cohortEntry.applyModifications(from.getModifications());
218
219         final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
220         cohortEntry.getTransaction().getSnapshot().applyToCursor(new AbstractBatchedModificationsCursor() {
221             @Override
222             protected BatchedModifications getModifications() {
223                 if (newModifications.isEmpty()
224                         || newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
225                     newModifications.add(new BatchedModifications(from.getTransactionId(), from.getVersion()));
226                 }
227
228                 return newModifications.getLast();
229             }
230         });
231
232         BatchedModifications last = newModifications.getLast();
233         last.setDoCommitOnReady(from.isDoCommitOnReady());
234         if (from.isReady()) {
235             last.setReady(from.getParticipatingShardNames());
236         }
237         last.setTotalMessagesSent(newModifications.size());
238         return newModifications;
239     }
240
241     private void handleCanCommit(final CohortEntry cohortEntry) {
242         cohortEntry.canCommit(new FutureCallback<>() {
243             @Override
244             public void onSuccess(final Empty result) {
245                 log.debug("{}: canCommit for {}: success", name, cohortEntry.getTransactionId());
246
247                 if (cohortEntry.isDoImmediateCommit()) {
248                     doCommit(cohortEntry);
249                 } else {
250                     cohortEntry.getReplySender().tell(
251                         CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable(),
252                         cohortEntry.getShard().self());
253                 }
254             }
255
256             @Override
257             public void onFailure(final Throwable failure) {
258                 log.debug("{}: An exception occurred during canCommit for {}", name, cohortEntry.getTransactionId(),
259                     failure);
260
261                 cohortCache.remove(cohortEntry.getTransactionId());
262                 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
263             }
264         });
265     }
266
267     /**
268      * This method handles the canCommit phase for a transaction.
269      *
270      * @param transactionID the ID of the transaction to canCommit
271      * @param sender the actor to which to send the response
272      * @param shard the transaction's shard actor
273      */
274     void handleCanCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
275         // Lookup the cohort entry that was cached previously (or should have been) by
276         // transactionReady (via the ForwardedReadyTransaction message).
277         final CohortEntry cohortEntry = cohortCache.get(transactionID);
278         if (cohortEntry == null) {
279             // Either canCommit was invoked before ready (shouldn't happen) or a long time passed
280             // between canCommit and ready and the entry was expired from the cache or it was aborted.
281             IllegalStateException ex = new IllegalStateException(
282                     String.format("%s: Cannot canCommit transaction %s - no cohort entry found", name, transactionID));
283             log.error("{}: Inconsistency during transaction {} canCommit", name, transactionID, ex);
284             sender.tell(new Failure(ex), shard.self());
285             return;
286         }
287
288         cohortEntry.setReplySender(sender);
289         cohortEntry.setShard(shard);
290
291         handleCanCommit(cohortEntry);
292     }
293
294     void doCommit(final CohortEntry cohortEntry) {
295         log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionId());
296
297         // We perform the preCommit phase here atomically with the commit phase. This is an
298         // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
299         // coordination of preCommit across shards in case of failure but preCommit should not
300         // normally fail since we ensure only one concurrent 3-phase commit.
301         cohortEntry.preCommit(new FutureCallback<DataTreeCandidate>() {
302             @Override
303             public void onSuccess(final DataTreeCandidate candidate) {
304                 finishCommit(cohortEntry.getReplySender(), cohortEntry);
305             }
306
307             @Override
308             public void onFailure(final Throwable failure) {
309                 log.error("{} An exception occurred while preCommitting transaction {}", name,
310                         cohortEntry.getTransactionId(), failure);
311
312                 cohortCache.remove(cohortEntry.getTransactionId());
313                 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
314             }
315         });
316     }
317
318     void finishCommit(final @NonNull ActorRef sender, final @NonNull CohortEntry cohortEntry) {
319         log.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionId());
320
321         cohortEntry.commit(new FutureCallback<UnsignedLong>() {
322             @Override
323             public void onSuccess(final UnsignedLong result) {
324                 final TransactionIdentifier txId = cohortEntry.getTransactionId();
325                 log.debug("{}: Transaction {} committed as {}, sending response to {}", persistenceId(), txId, result,
326                     sender);
327
328                 cohortCache.remove(cohortEntry.getTransactionId());
329                 sender.tell(CommitTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(),
330                     cohortEntry.getShard().self());
331             }
332
333             @Override
334             public void onFailure(final Throwable failure) {
335                 final TransactionIdentifier txId = cohortEntry.getTransactionId();
336                 log.error("{}, An exception occurred while committing transaction {}", persistenceId(), txId, failure);
337
338                 cohortCache.remove(cohortEntry.getTransactionId());
339                 sender.tell(new Failure(failure), cohortEntry.getShard().self());
340             }
341         });
342     }
343
344     /**
345      * This method handles the preCommit and commit phases for a transaction.
346      *
347      * @param transactionID the ID of the transaction to commit
348      * @param sender the actor to which to send the response
349      * @param shard the transaction's shard actor
350      */
351     void handleCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
352         final CohortEntry cohortEntry = cohortCache.get(transactionID);
353         if (cohortEntry == null) {
354             // Either a long time passed between canCommit and commit and the entry was expired from the cache
355             // or it was aborted.
356             IllegalStateException ex = new IllegalStateException(
357                     String.format("%s: Cannot commit transaction %s - no cohort entry found", name, transactionID));
358             log.error("{}: Inconsistency during transaction {} commit", name, transactionID, ex);
359             sender.tell(new Failure(ex), shard.self());
360             return;
361         }
362
363         cohortEntry.setReplySender(sender);
364         doCommit(cohortEntry);
365     }
366
367     @SuppressWarnings("checkstyle:IllegalCatch")
368     void handleAbort(final Identifier transactionID, final ActorRef sender, final Shard shard) {
369         CohortEntry cohortEntry = cohortCache.remove(transactionID);
370         if (cohortEntry == null) {
371             return;
372         }
373
374         log.debug("{}: Aborting transaction {}", name, transactionID);
375
376         final ActorRef self = shard.getSelf();
377         cohortEntry.abort(new FutureCallback<>() {
378             @Override
379             public void onSuccess(final Empty result) {
380                 if (sender != null) {
381                     sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
382                 }
383             }
384
385             @Override
386             public void onFailure(final Throwable failure) {
387                 log.error("{}: An exception happened during abort", name, failure);
388
389                 if (sender != null) {
390                     sender.tell(new Failure(failure), self);
391                 }
392             }
393         });
394
395         shard.getShardMBean().incrementAbortTransactionsCount();
396     }
397
398     void checkForExpiredTransactions(final long timeout, final Shard shard) {
399         cohortCache.values().removeIf(CohortEntry::isFailed);
400     }
401
402     void abortPendingTransactions(final String reason, final Shard shard) {
403         final Failure failure = new Failure(new RuntimeException(reason));
404         Collection<ShardDataTreeCohort> pending = dataTree.getAndClearPendingTransactions();
405
406         log.debug("{}: Aborting {} pending queued transactions", name, pending.size());
407
408         for (ShardDataTreeCohort cohort : pending) {
409             CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
410             if (cohortEntry == null) {
411                 continue;
412             }
413
414             if (cohortEntry.getReplySender() != null) {
415                 cohortEntry.getReplySender().tell(failure, shard.self());
416             }
417         }
418
419         cohortCache.clear();
420     }
421
422     Collection<?> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
423         final Collection<VersionedExternalizableMessage> messages = new ArrayList<>();
424         for (ShardDataTreeCohort cohort : dataTree.getAndClearPendingTransactions()) {
425             CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
426             if (cohortEntry == null) {
427                 continue;
428             }
429
430             final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
431             cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
432                 @Override
433                 protected BatchedModifications getModifications() {
434                     final BatchedModifications lastBatch = newMessages.peekLast();
435
436                     if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
437                         return lastBatch;
438                     }
439
440                     // Allocate a new message
441                     final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionId(),
442                         cohortEntry.getClientVersion());
443                     newMessages.add(ret);
444                     return ret;
445                 }
446             });
447
448             final BatchedModifications last = newMessages.peekLast();
449             if (last != null) {
450                 final boolean immediate = cohortEntry.isDoImmediateCommit();
451                 last.setDoCommitOnReady(immediate);
452                 last.setReady(cohortEntry.getParticipatingShardNames());
453                 last.setTotalMessagesSent(newMessages.size());
454
455                 messages.addAll(newMessages);
456
457                 if (!immediate) {
458                     switch (cohort.getState()) {
459                         case CAN_COMMIT_COMPLETE:
460                         case CAN_COMMIT_PENDING:
461                             messages.add(new CanCommitTransaction(cohortEntry.getTransactionId(),
462                                 cohortEntry.getClientVersion()));
463                             break;
464                         case PRE_COMMIT_COMPLETE:
465                         case PRE_COMMIT_PENDING:
466                             messages.add(new CommitTransaction(cohortEntry.getTransactionId(),
467                                 cohortEntry.getClientVersion()));
468                             break;
469                         default:
470                             break;
471                     }
472                 }
473             }
474         }
475
476         return messages;
477     }
478
479     @VisibleForTesting
480     void setCohortDecorator(final CohortDecorator cohortDecorator) {
481         this.cohortDecorator = cohortDecorator;
482     }
483 }