+
+ @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));
+ }
+