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