+ // There are no outstanding futures, shortcut
+ Future<?> previous = localState.previousFuture();
+ if (previous == null) {
+ return combineFutureWithPossiblePriorReadOnlyTxFutures(parent.findPrimaryShard(shardName, txId), txId);
+ }
+
+ final String previousTransactionId;
+
+ if (localState instanceof Pending) {
+ previousTransactionId = ((Pending) localState).getIdentifier().toString();
+ LOG.debug("Tx: {} - waiting for ready futures with pending Tx {}", txId, previousTransactionId);
+ } else {
+ previousTransactionId = "";
+ LOG.debug("Waiting for ready futures on chain {}", getHistoryId());
+ }
+
+ previous = combineFutureWithPossiblePriorReadOnlyTxFutures(previous, txId);
+
+ // Add a callback for completion of the combined Futures.
+ final Promise<PrimaryShardInfo> returnPromise = 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("Tx: {} - ready future failed for previous Tx {}", txId, previousTransactionId);
+ returnPromise.failure(failure);
+ } else {
+ LOG.debug("Tx: {} - previous Tx {} readied - proceeding to FindPrimaryShard",
+ txId, previousTransactionId);
+
+ // Send the FindPrimaryShard message and use the resulting Future to complete the
+ // returned Promise.
+ returnPromise.completeWith(parent.findPrimaryShard(shardName, txId));
+ }
+ }
+ };
+
+ previous.onComplete(onComplete, getActorContext().getClientDispatcher());
+ return returnPromise.future();
+ }
+
+ private <T> Future<T> combineFutureWithPossiblePriorReadOnlyTxFutures(final Future<T> future,
+ final TransactionIdentifier txId) {
+ if (!priorReadOnlyTxPromises.containsKey(txId) && !priorReadOnlyTxPromises.isEmpty()) {
+ Collection<Entry<TransactionIdentifier, Promise<Object>>> priorReadOnlyTxPromiseEntries =
+ new ArrayList<>(priorReadOnlyTxPromises.entrySet());
+ if (priorReadOnlyTxPromiseEntries.isEmpty()) {
+ return future;
+ }
+
+ List<Future<Object>> priorReadOnlyTxFutures = new ArrayList<>(priorReadOnlyTxPromiseEntries.size());
+ for (Entry<TransactionIdentifier, Promise<Object>> entry: priorReadOnlyTxPromiseEntries) {
+ LOG.debug("Tx: {} - waiting on future for prior read-only Tx {}", txId, entry.getKey());
+ priorReadOnlyTxFutures.add(entry.getValue().future());
+ }