+
+ final String previousTransactionId;
+
+ if(localState instanceof Pending){
+ previousTransactionId = ((Pending) localState).getIdentifier().toString();
+ LOG.debug("Waiting for ready futures with pending Tx {}", previousTransactionId);
+ } else {
+ previousTransactionId = "";
+ LOG.debug("Waiting for ready futures on chain {}", getTransactionChainId());
+ }
+
+ // Add a callback for completion of the combined Futures.
+ final Promise<PrimaryShardInfo> returnPromise = akka.dispatch.Futures.promise();
+
+ final OnComplete onComplete = new OnComplete() {
+ @Override
+ public void onComplete(final Throwable failure, final Object notUsed) {
+ if (failure != null) {
+ // A Ready Future failed so fail the returned Promise.
+ LOG.error("Ready future failed for Tx {}", previousTransactionId);
+ returnPromise.failure(failure);
+ } else {
+ LOG.debug("Previous Tx {} readied - proceeding to FindPrimaryShard",
+ previousTransactionId);
+
+ // Send the FindPrimaryShard message and use the resulting Future to complete the
+ // returned Promise.
+ returnPromise.completeWith(parent.findPrimaryShard(shardName));
+ }
+ }
+ };
+
+ previous.onComplete(onComplete, getActorContext().getClientDispatcher());
+ return returnPromise.future();
+ }
+
+ @Override
+ protected <T> void onTransactionReady(final TransactionIdentifier transaction, final Collection<Future<T>> cohortFutures) {
+ final State localState = currentState;
+ Preconditions.checkState(localState instanceof Allocated, "Readying transaction %s while state is %s", transaction, localState);
+ final TransactionIdentifier currentTx = ((Allocated)localState).getIdentifier();
+ Preconditions.checkState(transaction.equals(currentTx), "Readying transaction %s while %s is allocated", transaction, currentTx);
+
+ // Transaction ready and we are not waiting for futures -- go to idle
+ if (cohortFutures.isEmpty()) {
+ currentState = IDLE_STATE;
+ return;
+ }
+
+ // Combine the ready Futures into 1
+ final Future<Iterable<T>> combined = akka.dispatch.Futures.sequence(
+ cohortFutures, getActorContext().getClientDispatcher());
+
+ // Record the we have outstanding futures
+ final State newState = new Submitted(transaction, combined);
+ currentState = newState;
+
+ // Attach a completion reset, but only if we do not allocate a transaction
+ // in-between
+ combined.onComplete(new OnComplete<Iterable<T>>() {
+ @Override
+ public void onComplete(final Throwable arg0, final Iterable<T> arg1) {
+ STATE_UPDATER.compareAndSet(TransactionChainProxy.this, newState, IDLE_STATE);
+ }
+ }, getActorContext().getClientDispatcher());
+ }
+
+ @Override
+ protected TransactionIdentifier nextIdentifier() {
+ return transactionChainId.newTransactionIdentifier();