2773a3e3bfd21ad4559bb6baf05c1d48661cfe9c
[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 akka.actor.ActorRef;
11 import akka.actor.Status.Failure;
12 import akka.serialization.Serialization;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.UnsignedLong;
16 import com.google.common.util.concurrent.FutureCallback;
17 import java.util.ArrayDeque;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Deque;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.Map;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.TimeoutException;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
30 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
31 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
32 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
33 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
34 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
35 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
36 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
37 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
38 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
39 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
40 import org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage;
41 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
42 import org.opendaylight.yangtools.concepts.Identifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.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 = Preconditions.checkNotNull(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();
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     void handleBatchedModifications(final BatchedModifications batched, final ActorRef sender, final Shard shard) {
134         CohortEntry cohortEntry = cohortCache.get(batched.getTransactionId());
135         if (cohortEntry == null) {
136             cohortEntry = CohortEntry.createOpen(dataTree.newReadWriteTransaction(batched.getTransactionId()),
137                 batched.getVersion());
138             cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
139         }
140
141         if (log.isDebugEnabled()) {
142             log.debug("{}: Applying {} batched modifications for Tx {}", name,
143                     batched.getModifications().size(), batched.getTransactionId());
144         }
145
146         cohortEntry.applyModifications(batched.getModifications());
147
148         if (batched.isReady()) {
149             if (cohortEntry.getLastBatchedModificationsException() != null) {
150                 cohortCache.remove(cohortEntry.getTransactionId());
151                 throw cohortEntry.getLastBatchedModificationsException();
152             }
153
154             if (cohortEntry.getTotalBatchedModificationsReceived() != batched.getTotalMessagesSent()) {
155                 cohortCache.remove(cohortEntry.getTransactionId());
156                 throw new IllegalStateException(String.format(
157                         "The total number of batched messages received %d does not match the number sent %d",
158                         cohortEntry.getTotalBatchedModificationsReceived(), batched.getTotalMessagesSent()));
159             }
160
161             if (log.isDebugEnabled()) {
162                 log.debug("{}: Readying Tx {}, client version {}", name,
163                         batched.getTransactionId(), batched.getVersion());
164             }
165
166             cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
167             cohortEntry.ready(cohortDecorator);
168
169             if (batched.isDoCommitOnReady()) {
170                 cohortEntry.setReplySender(sender);
171                 cohortEntry.setShard(shard);
172                 handleCanCommit(cohortEntry);
173             } else {
174                 sender.tell(readyTransactionReply(shard.self()), shard.self());
175             }
176         } else {
177             sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
178         }
179     }
180
181     /**
182      * This method handles {@link ReadyLocalTransaction} message. All transaction modifications have
183      * been prepared beforehand by the sender and we just need to drive them through into the
184      * dataTree.
185      *
186      * @param message the ReadyLocalTransaction message to process
187      * @param sender the sender of the message
188      * @param shard the transaction's shard actor
189      */
190     void handleReadyLocalTransaction(final ReadyLocalTransaction message, final ActorRef sender, final Shard shard) {
191         final ShardDataTreeCohort cohort = dataTree.createReadyCohort(message.getTransactionId(),
192             message.getModification());
193         final CohortEntry cohortEntry = CohortEntry.createReady(cohort, DataStoreVersions.CURRENT_VERSION);
194         cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
195         cohortEntry.setDoImmediateCommit(message.isDoCommitOnReady());
196
197         log.debug("{}: Applying local modifications for Tx {}", name, message.getTransactionId());
198
199         if (message.isDoCommitOnReady()) {
200             cohortEntry.setReplySender(sender);
201             cohortEntry.setShard(shard);
202             handleCanCommit(cohortEntry);
203         } else {
204             sender.tell(readyTransactionReply(shard.self()), shard.self());
205         }
206     }
207
208     Collection<BatchedModifications> createForwardedBatchedModifications(final BatchedModifications from,
209             final int maxModificationsPerBatch) {
210         CohortEntry cohortEntry = cohortCache.remove(from.getTransactionId());
211         if (cohortEntry == null || cohortEntry.getTransaction() == null) {
212             return Collections.singletonList(from);
213         }
214
215         cohortEntry.applyModifications(from.getModifications());
216
217         final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
218         cohortEntry.getTransaction().getSnapshot().applyToCursor(new AbstractBatchedModificationsCursor() {
219             @Override
220             protected BatchedModifications getModifications() {
221                 if (newModifications.isEmpty()
222                         || newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
223                     newModifications.add(new BatchedModifications(from.getTransactionId(), from.getVersion()));
224                 }
225
226                 return newModifications.getLast();
227             }
228         });
229
230         BatchedModifications last = newModifications.getLast();
231         last.setDoCommitOnReady(from.isDoCommitOnReady());
232         last.setReady(from.isReady());
233         last.setTotalMessagesSent(newModifications.size());
234         return newModifications;
235     }
236
237     private void handleCanCommit(final CohortEntry cohortEntry) {
238         cohortEntry.canCommit(new FutureCallback<Void>() {
239             @Override
240             public void onSuccess(final Void result) {
241                 log.debug("{}: canCommit for {}: success", name, cohortEntry.getTransactionId());
242
243                 if (cohortEntry.isDoImmediateCommit()) {
244                     doCommit(cohortEntry);
245                 } else {
246                     cohortEntry.getReplySender().tell(
247                         CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable(),
248                         cohortEntry.getShard().self());
249                 }
250             }
251
252             @Override
253             public void onFailure(final Throwable failure) {
254                 log.debug("{}: An exception occurred during canCommit for {}: {}", name,
255                         cohortEntry.getTransactionId(), failure);
256
257                 cohortCache.remove(cohortEntry.getTransactionId());
258                 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
259             }
260         });
261     }
262
263     /**
264      * This method handles the canCommit phase for a transaction.
265      *
266      * @param transactionID the ID of the transaction to canCommit
267      * @param sender the actor to which to send the response
268      * @param shard the transaction's shard actor
269      */
270     void handleCanCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
271         // Lookup the cohort entry that was cached previously (or should have been) by
272         // transactionReady (via the ForwardedReadyTransaction message).
273         final CohortEntry cohortEntry = cohortCache.get(transactionID);
274         if (cohortEntry == null) {
275             // Either canCommit was invoked before ready (shouldn't happen) or a long time passed
276             // between canCommit and ready and the entry was expired from the cache or it was aborted.
277             IllegalStateException ex = new IllegalStateException(
278                     String.format("%s: Cannot canCommit transaction %s - no cohort entry found", name, transactionID));
279             log.error(ex.getMessage());
280             sender.tell(new Failure(ex), shard.self());
281             return;
282         }
283
284         cohortEntry.setReplySender(sender);
285         cohortEntry.setShard(shard);
286
287         handleCanCommit(cohortEntry);
288     }
289
290     private void doCommit(final CohortEntry cohortEntry) {
291         log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionId());
292
293         // We perform the preCommit phase here atomically with the commit phase. This is an
294         // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
295         // coordination of preCommit across shards in case of failure but preCommit should not
296         // normally fail since we ensure only one concurrent 3-phase commit.
297         cohortEntry.preCommit(new FutureCallback<DataTreeCandidate>() {
298             @Override
299             public void onSuccess(final DataTreeCandidate candidate) {
300                 finishCommit(cohortEntry.getReplySender(), cohortEntry);
301             }
302
303             @Override
304             public void onFailure(final Throwable failure) {
305                 log.error("{} An exception occurred while preCommitting transaction {}", name,
306                         cohortEntry.getTransactionId(), failure);
307
308                 cohortCache.remove(cohortEntry.getTransactionId());
309                 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
310             }
311         });
312     }
313
314     private void finishCommit(@Nonnull final ActorRef sender, @Nonnull final CohortEntry cohortEntry) {
315         log.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionId());
316
317         cohortEntry.commit(new FutureCallback<UnsignedLong>() {
318             @Override
319             public void onSuccess(final UnsignedLong result) {
320                 final TransactionIdentifier txId = cohortEntry.getTransactionId();
321                 log.debug("{}: Transaction {} committed as {}, sending response to {}", persistenceId(), txId, result,
322                     sender);
323
324                 cohortCache.remove(cohortEntry.getTransactionId());
325                 sender.tell(CommitTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(),
326                     cohortEntry.getShard().self());
327             }
328
329             @Override
330             public void onFailure(final Throwable failure) {
331                 log.error("{}, An exception occurred while committing transaction {}", persistenceId(),
332                         cohortEntry.getTransactionId(), failure);
333
334                 cohortCache.remove(cohortEntry.getTransactionId());
335                 sender.tell(new Failure(failure), cohortEntry.getShard().self());
336             }
337         });
338     }
339
340     /**
341      * This method handles the preCommit and commit phases for a transaction.
342      *
343      * @param transactionID the ID of the transaction to commit
344      * @param sender the actor to which to send the response
345      * @param shard the transaction's shard actor
346      */
347     void handleCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
348         final CohortEntry cohortEntry = cohortCache.get(transactionID);
349         if (cohortEntry == null) {
350             // Either a long time passed between canCommit and commit and the entry was expired from the cache
351             // or it was aborted.
352             IllegalStateException ex = new IllegalStateException(
353                     String.format("%s: Cannot commit transaction %s - no cohort entry found", name, transactionID));
354             log.error(ex.getMessage());
355             sender.tell(new Failure(ex), shard.self());
356             return;
357         }
358
359         cohortEntry.setReplySender(sender);
360         doCommit(cohortEntry);
361     }
362
363     @SuppressWarnings("checkstyle:IllegalCatch")
364     void handleAbort(final Identifier transactionID, final ActorRef sender, final Shard shard) {
365         CohortEntry cohortEntry = cohortCache.remove(transactionID);
366         if (cohortEntry == null) {
367             return;
368         }
369
370         log.debug("{}: Aborting transaction {}", name, transactionID);
371
372         final ActorRef self = shard.getSelf();
373         try {
374             cohortEntry.abort();
375
376             shard.getShardMBean().incrementAbortTransactionsCount();
377
378             if (sender != null) {
379                 sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
380             }
381         } catch (InterruptedException | ExecutionException | TimeoutException e) {
382             log.error("{}: An exception happened during abort", name, e);
383
384             if (sender != null) {
385                 sender.tell(new Failure(e), self);
386             }
387         }
388     }
389
390     void checkForExpiredTransactions(final long timeout, final Shard shard) {
391         Iterator<CohortEntry> iter = cohortCache.values().iterator();
392         while (iter.hasNext()) {
393             CohortEntry cohortEntry = iter.next();
394             if (cohortEntry.isFailed()) {
395                 iter.remove();
396             }
397         }
398     }
399
400     void abortPendingTransactions(final String reason, final Shard shard) {
401         final Failure failure = new Failure(new RuntimeException(reason));
402         Collection<ShardDataTreeCohort> pending = dataTree.getAndClearPendingTransactions();
403
404         log.debug("{}: Aborting {} pending queued transactions", name, pending.size());
405
406         for (ShardDataTreeCohort cohort : pending) {
407             CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
408             if (cohortEntry == null) {
409                 continue;
410             }
411
412             if (cohortEntry.getReplySender() != null) {
413                 cohortEntry.getReplySender().tell(failure, shard.self());
414             }
415         }
416
417         cohortCache.clear();
418     }
419
420     Collection<?> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
421         final Collection<VersionedExternalizableMessage> messages = new ArrayList<>();
422         for (ShardDataTreeCohort cohort : dataTree.getAndClearPendingTransactions()) {
423             CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
424             if (cohortEntry == null) {
425                 continue;
426             }
427
428             final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
429             cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
430                 @Override
431                 protected BatchedModifications getModifications() {
432                     final BatchedModifications lastBatch = newMessages.peekLast();
433
434                     if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
435                         return lastBatch;
436                     }
437
438                     // Allocate a new message
439                     final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionId(),
440                         cohortEntry.getClientVersion());
441                     newMessages.add(ret);
442                     return ret;
443                 }
444             });
445
446             final BatchedModifications last = newMessages.peekLast();
447             if (last != null) {
448                 final boolean immediate = cohortEntry.isDoImmediateCommit();
449                 last.setDoCommitOnReady(immediate);
450                 last.setReady(true);
451                 last.setTotalMessagesSent(newMessages.size());
452
453                 messages.addAll(newMessages);
454
455                 if (!immediate) {
456                     switch (cohort.getState()) {
457                         case CAN_COMMIT_COMPLETE:
458                         case CAN_COMMIT_PENDING:
459                             messages.add(new CanCommitTransaction(cohortEntry.getTransactionId(),
460                                 cohortEntry.getClientVersion()));
461                             break;
462                         case PRE_COMMIT_COMPLETE:
463                         case PRE_COMMIT_PENDING:
464                             messages.add(new CommitTransaction(cohortEntry.getTransactionId(),
465                                 cohortEntry.getClientVersion()));
466                             break;
467                         default:
468                             break;
469                     }
470                 }
471             }
472         }
473
474         return messages;
475     }
476
477     @VisibleForTesting
478     void setCohortDecorator(final CohortDecorator cohortDecorator) {
479         this.cohortDecorator = cohortDecorator;
480     }
481 }