BUG-5280: switch transaction IDs from String to TransactionIdentifier
[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 java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Queue;
24 import java.util.concurrent.ExecutionException;
25 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActorRegistry.CohortRegistryCommand;
26 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
28 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
29 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
30 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
31 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
32 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
33 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
34 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
35 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.yangtools.concepts.Identifier;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.slf4j.Logger;
40
41 /**
42  * Coordinates commits for a shard ensuring only one concurrent 3-phase commit.
43  *
44  * @author Thomas Pantelis
45  */
46 final class ShardCommitCoordinator {
47
48     // Interface hook for unit tests to replace or decorate the DOMStoreThreePhaseCommitCohorts.
49     public interface CohortDecorator {
50         ShardDataTreeCohort decorate(Identifier transactionID, ShardDataTreeCohort actual);
51     }
52
53     private final Map<Identifier, CohortEntry> cohortCache = new HashMap<>();
54
55     private CohortEntry currentCohortEntry;
56
57     private final ShardDataTree dataTree;
58
59     private final DataTreeCohortActorRegistry cohortRegistry = new DataTreeCohortActorRegistry();
60
61     // We use a LinkedList here to avoid synchronization overhead with concurrent queue impls
62     // since this should only be accessed on the shard's dispatcher.
63     private final Queue<CohortEntry> queuedCohortEntries = new LinkedList<>();
64
65     private int queueCapacity;
66
67     private final Logger log;
68
69     private final String name;
70
71     private final long cacheExpiryTimeoutInMillis;
72
73     // This is a hook for unit tests to replace or decorate the DOMStoreThreePhaseCommitCohorts.
74     private CohortDecorator cohortDecorator;
75
76     private ReadyTransactionReply readyTransactionReply;
77
78     private Runnable runOnPendingTransactionsComplete;
79
80     ShardCommitCoordinator(ShardDataTree dataTree, long cacheExpiryTimeoutInMillis, int queueCapacity, Logger log,
81             String name) {
82
83         this.queueCapacity = queueCapacity;
84         this.log = log;
85         this.name = name;
86         this.dataTree = Preconditions.checkNotNull(dataTree);
87         this.cacheExpiryTimeoutInMillis = cacheExpiryTimeoutInMillis;
88     }
89
90     int getQueueSize() {
91         return queuedCohortEntries.size();
92     }
93
94     int getCohortCacheSize() {
95         return cohortCache.size();
96     }
97
98     void setQueueCapacity(int queueCapacity) {
99         this.queueCapacity = queueCapacity;
100     }
101
102     private ReadyTransactionReply readyTransactionReply(Shard shard) {
103         if(readyTransactionReply == null) {
104             readyTransactionReply = new ReadyTransactionReply(Serialization.serializedActorPath(shard.self()));
105         }
106
107         return readyTransactionReply;
108     }
109
110     private boolean queueCohortEntry(CohortEntry cohortEntry, ActorRef sender, Shard shard) {
111         if(queuedCohortEntries.size() < queueCapacity) {
112             queuedCohortEntries.offer(cohortEntry);
113
114             log.debug("{}: Enqueued transaction {}, queue size {}", name, cohortEntry.getTransactionID(),
115                     queuedCohortEntries.size());
116
117             return true;
118         } else {
119             cohortCache.remove(cohortEntry.getTransactionID());
120
121             final RuntimeException ex = new RuntimeException(
122                     String.format("%s: Could not enqueue transaction %s - the maximum commit queue"+
123                                   " capacity %d has been reached.",
124                                   name, cohortEntry.getTransactionID(), queueCapacity));
125             log.error(ex.getMessage());
126             sender.tell(new Failure(ex), shard.self());
127             return false;
128         }
129     }
130
131     /**
132      * This method is called to ready a transaction that was prepared by ShardTransaction actor. It caches
133      * the prepared cohort entry for the given transactions ID in preparation for the subsequent 3-phase commit.
134      *
135      * @param ready the ForwardedReadyTransaction message to process
136      * @param sender the sender of the message
137      * @param shard the transaction's shard actor
138      * @param schema
139      */
140     void handleForwardedReadyTransaction(ForwardedReadyTransaction ready, ActorRef sender, Shard shard,
141             SchemaContext schema) {
142         log.debug("{}: Readying transaction {}, client version {}", name,
143                 ready.getTransactionID(), ready.getTxnClientVersion());
144
145         final ShardDataTreeCohort cohort = ready.getTransaction().ready();
146         final CohortEntry cohortEntry = new CohortEntry(ready.getTransactionID(), cohort, cohortRegistry, schema, ready.getTxnClientVersion());
147         cohortCache.put(cohortEntry.getTransactionID(), cohortEntry);
148
149         if(!queueCohortEntry(cohortEntry, sender, shard)) {
150             return;
151         }
152
153         if(ready.isDoImmediateCommit()) {
154             cohortEntry.setDoImmediateCommit(true);
155             cohortEntry.setReplySender(sender);
156             cohortEntry.setShard(shard);
157             handleCanCommit(cohortEntry);
158         } else {
159             // The caller does not want immediate commit - the 3-phase commit will be coordinated by the
160             // front-end so send back a ReadyTransactionReply with our actor path.
161             sender.tell(readyTransactionReply(shard), shard.self());
162         }
163     }
164
165     /**
166      * This method handles a BatchedModifications message for a transaction being prepared directly on the
167      * Shard actor instead of via a ShardTransaction actor. If there's no currently cached
168      * DOMStoreWriteTransaction, one is created. The batched modifications are applied to the write Tx. If
169      * the BatchedModifications is ready to commit then a DOMStoreThreePhaseCommitCohort is created.
170      *
171      * @param batched the BatchedModifications message to process
172      * @param sender the sender of the message
173      * @param shard the transaction's shard actor
174      */
175     void handleBatchedModifications(BatchedModifications batched, ActorRef sender, Shard shard, SchemaContext schema) {
176         CohortEntry cohortEntry = cohortCache.get(batched.getTransactionID());
177         if(cohortEntry == null) {
178             cohortEntry = new CohortEntry(batched.getTransactionID(),
179                     dataTree.newReadWriteTransaction(batched.getTransactionID()),
180                     cohortRegistry, schema,  batched.getVersion());
181             cohortCache.put(cohortEntry.getTransactionID(), cohortEntry);
182         }
183
184         if(log.isDebugEnabled()) {
185             log.debug("{}: Applying {} batched modifications for Tx {}", name,
186                     batched.getModifications().size(), batched.getTransactionID());
187         }
188
189         cohortEntry.applyModifications(batched.getModifications());
190
191         if(batched.isReady()) {
192             if(cohortEntry.getLastBatchedModificationsException() != null) {
193                 cohortCache.remove(cohortEntry.getTransactionID());
194                 throw cohortEntry.getLastBatchedModificationsException();
195             }
196
197             if(cohortEntry.getTotalBatchedModificationsReceived() != batched.getTotalMessagesSent()) {
198                 cohortCache.remove(cohortEntry.getTransactionID());
199                 throw new IllegalStateException(String.format(
200                         "The total number of batched messages received %d does not match the number sent %d",
201                         cohortEntry.getTotalBatchedModificationsReceived(), batched.getTotalMessagesSent()));
202             }
203
204             if(!queueCohortEntry(cohortEntry, sender, shard)) {
205                 return;
206             }
207
208             if(log.isDebugEnabled()) {
209                 log.debug("{}: Readying Tx {}, client version {}", name,
210                         batched.getTransactionID(), batched.getVersion());
211             }
212
213             cohortEntry.ready(cohortDecorator, batched.isDoCommitOnReady());
214
215             if(batched.isDoCommitOnReady()) {
216                 cohortEntry.setReplySender(sender);
217                 cohortEntry.setShard(shard);
218                 handleCanCommit(cohortEntry);
219             } else {
220                 sender.tell(readyTransactionReply(shard), shard.self());
221             }
222         } else {
223             sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
224         }
225     }
226
227     /**
228      * This method handles {@link ReadyLocalTransaction} message. All transaction modifications have
229      * been prepared beforehand by the sender and we just need to drive them through into the
230      * dataTree.
231      *
232      * @param message the ReadyLocalTransaction message to process
233      * @param sender the sender of the message
234      * @param shard the transaction's shard actor
235      */
236     void handleReadyLocalTransaction(ReadyLocalTransaction message, ActorRef sender, Shard shard,
237             SchemaContext schema) {
238         final ShardDataTreeCohort cohort = new SimpleShardDataTreeCohort(dataTree, message.getModification(),
239                 message.getTransactionID());
240         final CohortEntry cohortEntry = new CohortEntry(message.getTransactionID(), cohort, cohortRegistry, schema,
241                 DataStoreVersions.CURRENT_VERSION);
242         cohortCache.put(cohortEntry.getTransactionID(), cohortEntry);
243         cohortEntry.setDoImmediateCommit(message.isDoCommitOnReady());
244
245         if(!queueCohortEntry(cohortEntry, sender, shard)) {
246             return;
247         }
248
249         log.debug("{}: Applying local modifications for Tx {}", name, message.getTransactionID());
250
251         if (message.isDoCommitOnReady()) {
252             cohortEntry.setReplySender(sender);
253             cohortEntry.setShard(shard);
254             handleCanCommit(cohortEntry);
255         } else {
256             sender.tell(readyTransactionReply(shard), shard.self());
257         }
258     }
259
260     Collection<BatchedModifications> createForwardedBatchedModifications(final BatchedModifications from,
261             final int maxModificationsPerBatch) {
262         CohortEntry cohortEntry = getAndRemoveCohortEntry(from.getTransactionID());
263         if(cohortEntry == null || cohortEntry.getTransaction() == null) {
264             return Collections.singletonList(from);
265         }
266
267         cohortEntry.applyModifications(from.getModifications());
268
269         final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
270         cohortEntry.getTransaction().getSnapshot().applyToCursor(new AbstractBatchedModificationsCursor() {
271             @Override
272             protected BatchedModifications getModifications() {
273                 if(newModifications.isEmpty() ||
274                         newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
275                     newModifications.add(new BatchedModifications(from.getTransactionID(), from.getVersion()));
276                 }
277
278                 return newModifications.getLast();
279             }
280         });
281
282         BatchedModifications last = newModifications.getLast();
283         last.setDoCommitOnReady(from.isDoCommitOnReady());
284         last.setReady(from.isReady());
285         last.setTotalMessagesSent(newModifications.size());
286         return newModifications;
287     }
288
289     private void handleCanCommit(CohortEntry cohortEntry) {
290         cohortEntry.updateLastAccessTime();
291
292         if(currentCohortEntry != null) {
293             // There's already a Tx commit in progress so we can't process this entry yet - but it's in the
294             // queue and will get processed after all prior entries complete.
295
296             if(log.isDebugEnabled()) {
297                 log.debug("{}: Commit for Tx {} already in progress - skipping canCommit for {} for now",
298                         name, currentCohortEntry.getTransactionID(), cohortEntry.getTransactionID());
299             }
300
301             return;
302         }
303
304         // No Tx commit currently in progress - check if this entry is the next one in the queue, If so make
305         // it the current entry and proceed with canCommit.
306         // Purposely checking reference equality here.
307         if(queuedCohortEntries.peek() == cohortEntry) {
308             currentCohortEntry = queuedCohortEntries.poll();
309             doCanCommit(currentCohortEntry);
310         } else {
311             if(log.isDebugEnabled()) {
312                 log.debug("{}: Tx {} is the next pending canCommit - skipping {} for now", name,
313                         queuedCohortEntries.peek() != null ? queuedCohortEntries.peek().getTransactionID() : "???",
314                                 cohortEntry.getTransactionID());
315             }
316         }
317     }
318
319     /**
320      * This method handles the canCommit phase for a transaction.
321      *
322      * @param transactionID the ID of the transaction to canCommit
323      * @param sender the actor to which to send the response
324      * @param shard the transaction's shard actor
325      */
326     void handleCanCommit(Identifier transactionID, final ActorRef sender, final Shard shard) {
327         // Lookup the cohort entry that was cached previously (or should have been) by
328         // transactionReady (via the ForwardedReadyTransaction message).
329         final CohortEntry cohortEntry = cohortCache.get(transactionID);
330         if(cohortEntry == null) {
331             // Either canCommit was invoked before ready(shouldn't happen)  or a long time passed
332             // between canCommit and ready and the entry was expired from the cache.
333             IllegalStateException ex = new IllegalStateException(
334                     String.format("%s: No cohort entry found for transaction %s", name, transactionID));
335             log.error(ex.getMessage());
336             sender.tell(new Failure(ex), shard.self());
337             return;
338         }
339
340         cohortEntry.setReplySender(sender);
341         cohortEntry.setShard(shard);
342
343         handleCanCommit(cohortEntry);
344     }
345
346     private void doCanCommit(final CohortEntry cohortEntry) {
347         boolean canCommit = false;
348         try {
349             canCommit = cohortEntry.canCommit();
350
351             log.debug("{}: canCommit for {}: {}", name, cohortEntry.getTransactionID(), canCommit);
352
353             if(cohortEntry.isDoImmediateCommit()) {
354                 if(canCommit) {
355                     doCommit(cohortEntry);
356                 } else {
357                     cohortEntry.getReplySender().tell(new Failure(new TransactionCommitFailedException(
358                                 "Can Commit failed, no detailed cause available.")), cohortEntry.getShard().self());
359                 }
360             } else {
361                 cohortEntry.getReplySender().tell(
362                         canCommit ? CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable() :
363                             CanCommitTransactionReply.no(cohortEntry.getClientVersion()).toSerializable(),
364                         cohortEntry.getShard().self());
365             }
366         } catch (Exception e) {
367             log.debug("{}: An exception occurred during canCommit", name, e);
368
369             Throwable failure = e;
370             if(e instanceof ExecutionException) {
371                 failure = e.getCause();
372             }
373
374             cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
375         } finally {
376             if(!canCommit) {
377                 // Remove the entry from the cache now.
378                 currentTransactionComplete(cohortEntry.getTransactionID(), true);
379             }
380         }
381     }
382
383     private boolean doCommit(CohortEntry cohortEntry) {
384         log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionID());
385
386         boolean success = false;
387
388         // We perform the preCommit phase here atomically with the commit phase. This is an
389         // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
390         // coordination of preCommit across shards in case of failure but preCommit should not
391         // normally fail since we ensure only one concurrent 3-phase commit.
392
393         try {
394             cohortEntry.preCommit();
395
396             cohortEntry.getShard().continueCommit(cohortEntry);
397
398             cohortEntry.updateLastAccessTime();
399
400             success = true;
401         } catch (Exception e) {
402             log.error("{} An exception occurred while preCommitting transaction {}",
403                     name, cohortEntry.getTransactionID(), e);
404             cohortEntry.getReplySender().tell(new Failure(e), cohortEntry.getShard().self());
405
406             currentTransactionComplete(cohortEntry.getTransactionID(), true);
407         }
408
409         return success;
410     }
411
412     /**
413      * This method handles the preCommit and commit phases for a transaction.
414      *
415      * @param transactionID the ID of the transaction to commit
416      * @param sender the actor to which to send the response
417      * @param shard the transaction's shard actor
418      * @return true if the transaction was successfully prepared, false otherwise.
419      */
420     boolean handleCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
421         // Get the current in-progress cohort entry in the commitCoordinator if it corresponds to
422         // this transaction.
423         final CohortEntry cohortEntry = getCohortEntryIfCurrent(transactionID);
424         if(cohortEntry == null) {
425             // We're not the current Tx - the Tx was likely expired b/c it took too long in
426             // between the canCommit and commit messages.
427             IllegalStateException ex = new IllegalStateException(
428                     String.format("%s: Cannot commit transaction %s - it is not the current transaction",
429                             name, transactionID));
430             log.error(ex.getMessage());
431             sender.tell(new Failure(ex), shard.self());
432             return false;
433         }
434
435         cohortEntry.setReplySender(sender);
436         return doCommit(cohortEntry);
437     }
438
439     void handleAbort(final Identifier transactionID, final ActorRef sender, final Shard shard) {
440         CohortEntry cohortEntry = getCohortEntryIfCurrent(transactionID);
441         if(cohortEntry != null) {
442             // We don't remove the cached cohort entry here (ie pass false) in case the Tx was
443             // aborted during replication in which case we may still commit locally if replication
444             // succeeds.
445             currentTransactionComplete(transactionID, false);
446         } else {
447             cohortEntry = getAndRemoveCohortEntry(transactionID);
448         }
449
450         if(cohortEntry == null) {
451             return;
452         }
453
454         log.debug("{}: Aborting transaction {}", name, transactionID);
455
456         final ActorRef self = shard.getSelf();
457         try {
458             cohortEntry.abort();
459
460             shard.getShardMBean().incrementAbortTransactionsCount();
461
462             if(sender != null) {
463                 sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
464             }
465         } catch (Exception e) {
466             log.error("{}: An exception happened during abort", name, e);
467
468             if(sender != null) {
469                 sender.tell(new Failure(e), self);
470             }
471         }
472     }
473
474     void checkForExpiredTransactions(final long timeout, final Shard shard) {
475         CohortEntry cohortEntry = getCurrentCohortEntry();
476         if(cohortEntry != null) {
477             if(cohortEntry.isExpired(timeout)) {
478                 log.warn("{}: Current transaction {} has timed out after {} ms - aborting",
479                         name, cohortEntry.getTransactionID(), timeout);
480
481                 handleAbort(cohortEntry.getTransactionID(), null, shard);
482             }
483         }
484
485         cleanupExpiredCohortEntries();
486     }
487
488     void abortPendingTransactions(final String reason, final Shard shard) {
489         if(currentCohortEntry == null && queuedCohortEntries.isEmpty()) {
490             return;
491         }
492
493         List<CohortEntry> cohortEntries = getAndClearPendingCohortEntries();
494
495         log.debug("{}: Aborting {} pending queued transactions", name, cohortEntries.size());
496
497         for(CohortEntry cohortEntry: cohortEntries) {
498             if(cohortEntry.getReplySender() != null) {
499                 cohortEntry.getReplySender().tell(new Failure(new RuntimeException(reason)), shard.self());
500             }
501         }
502     }
503
504     private List<CohortEntry> getAndClearPendingCohortEntries() {
505         List<CohortEntry> cohortEntries = new ArrayList<>();
506
507         if(currentCohortEntry != null) {
508             cohortEntries.add(currentCohortEntry);
509             cohortCache.remove(currentCohortEntry.getTransactionID());
510             currentCohortEntry = null;
511         }
512
513         for(CohortEntry cohortEntry: queuedCohortEntries) {
514             cohortEntries.add(cohortEntry);
515             cohortCache.remove(cohortEntry.getTransactionID());
516         }
517
518         queuedCohortEntries.clear();
519         return cohortEntries;
520     }
521
522     Collection<Object> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
523         if(currentCohortEntry == null && queuedCohortEntries.isEmpty()) {
524             return Collections.emptyList();
525         }
526
527         Collection<Object> messages = new ArrayList<>();
528         List<CohortEntry> cohortEntries = getAndClearPendingCohortEntries();
529         for(CohortEntry cohortEntry: cohortEntries) {
530             if(cohortEntry.isExpired(cacheExpiryTimeoutInMillis) || cohortEntry.isAborted()) {
531                 continue;
532             }
533
534             final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
535             cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
536                 @Override
537                 protected BatchedModifications getModifications() {
538                     if(newModifications.isEmpty() ||
539                             newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
540                         newModifications.add(new BatchedModifications(cohortEntry.getTransactionID(),
541                                 cohortEntry.getClientVersion()));
542         }
543
544                     return newModifications.getLast();
545                 }
546             });
547
548             if(!newModifications.isEmpty()) {
549                 BatchedModifications last = newModifications.getLast();
550                 last.setDoCommitOnReady(cohortEntry.isDoImmediateCommit());
551                 last.setReady(true);
552                 last.setTotalMessagesSent(newModifications.size());
553                 messages.addAll(newModifications);
554
555                 if(!cohortEntry.isDoImmediateCommit() && cohortEntry.getState() == CohortEntry.State.CAN_COMMITTED) {
556                     messages.add(new CanCommitTransaction(cohortEntry.getTransactionID(),
557                             cohortEntry.getClientVersion()));
558                 }
559
560                 if(!cohortEntry.isDoImmediateCommit() && cohortEntry.getState() == CohortEntry.State.PRE_COMMITTED) {
561                     messages.add(new CommitTransaction(cohortEntry.getTransactionID(),
562                             cohortEntry.getClientVersion()));
563                 }
564             }
565         }
566
567         return messages;
568     }
569
570     /**
571      * Returns the cohort entry for the Tx commit currently in progress if the given transaction ID
572      * matches the current entry.
573      *
574      * @param transactionID the ID of the transaction
575      * @return the current CohortEntry or null if the given transaction ID does not match the
576      *         current entry.
577      */
578     CohortEntry getCohortEntryIfCurrent(Identifier transactionID) {
579         if(isCurrentTransaction(transactionID)) {
580             return currentCohortEntry;
581         }
582
583         return null;
584     }
585
586     CohortEntry getCurrentCohortEntry() {
587         return currentCohortEntry;
588     }
589
590     CohortEntry getAndRemoveCohortEntry(Identifier transactionID) {
591         return cohortCache.remove(transactionID);
592     }
593
594     boolean isCurrentTransaction(Identifier transactionID) {
595         return currentCohortEntry != null &&
596                 currentCohortEntry.getTransactionID().equals(transactionID);
597     }
598
599     /**
600      * This method is called when a transaction is complete, successful or not. If the given
601      * given transaction ID matches the current in-progress transaction, the next cohort entry,
602      * if any, is dequeued and processed.
603      *
604      * @param transactionID the ID of the completed transaction
605      * @param removeCohortEntry if true the CohortEntry for the transaction is also removed from
606      *        the cache.
607      */
608     void currentTransactionComplete(Identifier transactionID, boolean removeCohortEntry) {
609         if(removeCohortEntry) {
610             cohortCache.remove(transactionID);
611         }
612
613         if(isCurrentTransaction(transactionID)) {
614             currentCohortEntry = null;
615
616             log.debug("{}: currentTransactionComplete: {}", name, transactionID);
617
618             maybeProcessNextCohortEntry();
619         }
620     }
621
622     private void maybeProcessNextCohortEntry() {
623         // Check if there's a next cohort entry waiting in the queue and if it is ready to commit. Also
624         // clean out expired entries.
625         final Iterator<CohortEntry> iter = queuedCohortEntries.iterator();
626         while(iter.hasNext()) {
627             final CohortEntry next = iter.next();
628             if(next.isReadyToCommit()) {
629                 if(currentCohortEntry == null) {
630                     if(log.isDebugEnabled()) {
631                         log.debug("{}: Next entry to canCommit {}", name, next);
632                     }
633
634                     iter.remove();
635                     currentCohortEntry = next;
636                     currentCohortEntry.updateLastAccessTime();
637                     doCanCommit(currentCohortEntry);
638                 }
639
640                 break;
641             } else if(next.isExpired(cacheExpiryTimeoutInMillis)) {
642                 log.warn("{}: canCommit for transaction {} was not received within {} ms - entry removed from cache",
643                         name, next.getTransactionID(), cacheExpiryTimeoutInMillis);
644             } else if(!next.isAborted()) {
645                 break;
646             }
647
648             iter.remove();
649             cohortCache.remove(next.getTransactionID());
650         }
651
652         maybeRunOperationOnPendingTransactionsComplete();
653     }
654
655     void cleanupExpiredCohortEntries() {
656         maybeProcessNextCohortEntry();
657     }
658
659     void setRunOnPendingTransactionsComplete(Runnable operation) {
660         runOnPendingTransactionsComplete = operation;
661         maybeRunOperationOnPendingTransactionsComplete();
662     }
663
664     private void maybeRunOperationOnPendingTransactionsComplete() {
665         if(runOnPendingTransactionsComplete != null && currentCohortEntry == null && queuedCohortEntries.isEmpty()) {
666             log.debug("{}: Pending transactions complete - running operation {}", name, runOnPendingTransactionsComplete);
667
668             runOnPendingTransactionsComplete.run();
669             runOnPendingTransactionsComplete = null;
670         }
671     }
672
673     @VisibleForTesting
674     void setCohortDecorator(CohortDecorator cohortDecorator) {
675         this.cohortDecorator = cohortDecorator;
676     }
677
678    void processCohortRegistryCommand(ActorRef sender, CohortRegistryCommand message) {
679         cohortRegistry.process(sender, message);
680     }
681 }