+ }, actorUtils.getClientDispatcher());
+ }
+
+ return cohortsResolvedFuture;
+ }
+
+ @Override
+ public ListenableFuture<Boolean> canCommit() {
+ LOG.debug("Tx {} canCommit", transactionId);
+
+ final SettableFuture<Boolean> returnFuture = SettableFuture.create();
+
+ // The first phase of canCommit is to gather the list of cohort actor paths that will
+ // participate in the commit. buildCohortPathsList combines the cohort path Futures into
+ // one Future which we wait on asynchronously here. The cohort actor paths are
+ // extracted from ReadyTransactionReply messages by the Futures that were obtained earlier
+ // and passed to us from upstream processing. If any one fails then we'll fail canCommit.
+
+ Futures.addCallback(resolveCohorts(), new FutureCallback<Void>() {
+ @Override
+ public void onSuccess(final Void notUsed) {
+ finishCanCommit(returnFuture);
+ }
+
+ @Override
+ public void onFailure(final Throwable failure) {
+ returnFuture.setException(failure);
+ }
+ }, MoreExecutors.directExecutor());
+
+ return returnFuture;
+ }
+
+ @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+ justification = "https://github.com/spotbugs/spotbugs/issues/811")
+ private void finishCanCommit(final SettableFuture<Boolean> returnFuture) {
+ LOG.debug("Tx {} finishCanCommit", transactionId);
+
+ // For empty transactions return immediately
+ if (cohorts.size() == 0) {
+ LOG.debug("Tx {}: canCommit returning result true", transactionId);
+ returnFuture.set(Boolean.TRUE);
+ return;
+ }
+
+ commitOperationCallback = new TransactionRateLimitingCallback(actorUtils);
+ commitOperationCallback.run();
+
+ final Iterator<CohortInfo> iterator = cohorts.iterator();
+
+ final OnComplete<Object> onComplete = new OnComplete<Object>() {
+ @Override
+ public void onComplete(final Throwable failure, final Object response) {
+ if (failure != null) {
+ LOG.debug("Tx {}: a canCommit cohort Future failed", transactionId, failure);
+
+ returnFuture.setException(failure);
+ commitOperationCallback.failure();
+ return;
+ }
+
+ // Only the first call to pause takes effect - subsequent calls before resume are no-ops. So
+ // this means we'll only time the first transaction canCommit which should be fine.
+ commitOperationCallback.pause();
+
+ boolean result = true;
+ if (CanCommitTransactionReply.isSerializedType(response)) {
+ CanCommitTransactionReply reply = CanCommitTransactionReply.fromSerializable(response);
+
+ LOG.debug("Tx {}: received {}", transactionId, response);
+
+ if (!reply.getCanCommit()) {
+ result = false;
+ }
+ } else {
+ LOG.error("Unexpected response type {}", response.getClass());
+ returnFuture.setException(new IllegalArgumentException(
+ String.format("Unexpected response type %s", response.getClass())));
+ return;
+ }
+
+ if (iterator.hasNext() && result) {
+ sendCanCommitTransaction(iterator.next(), this);
+ } else {
+ LOG.debug("Tx {}: canCommit returning result: {}", transactionId, result);
+ returnFuture.set(Boolean.valueOf(result));
+ }