+ currentState = CLOSED_STATE;
+
+ // Send a close transaction chain request to each and every shard
+
+ getActorUtils().broadcast(version -> new CloseTransactionChain(getHistoryId(), version).toSerializable(),
+ CloseTransactionChain.class);
+ }
+
+ private TransactionProxy allocateWriteTransaction(final TransactionType type) {
+ State localState = currentState;
+ localState.checkReady();
+
+ final TransactionProxy ret = new TransactionProxy(this, type);
+ currentState = new Allocated(ret.getIdentifier(), localState.previousFuture());
+ return ret;
+ }
+
+ @Override
+ protected LocalTransactionChain factoryForShard(final String shardName, final ActorSelection shardLeader,
+ final ReadOnlyDataTree dataTree) {
+ final LocalTransactionChain ret = new LocalTransactionChain(this, shardLeader, dataTree);
+ LOG.debug("Allocated transaction chain {} for shard {} leader {}", ret, shardName, shardLeader);
+ return ret;
+ }
+
+ /**
+ * This method is overridden to ensure the previous Tx's ready operations complete
+ * before we initiate the next Tx in the chain to avoid creation failures if the
+ * previous Tx's ready operations haven't completed yet.
+ */
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Override
+ protected Future<PrimaryShardInfo> findPrimaryShard(final String shardName, final TransactionIdentifier txId) {
+ // Read current state atomically
+ final State localState = currentState;
+
+ // 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, getActorUtils().getClientDispatcher());
+ return returnPromise.future();
+ }
+
+ private <T> Future<T> combineFutureWithPossiblePriorReadOnlyTxFutures(final Future<T> future,
+ final TransactionIdentifier txId) {
+ return priorReadOnlyTxPromises.isEmpty() || priorReadOnlyTxPromises.containsKey(txId) ? future
+ // Tough luck, we need do some work
+ : combineWithPriorReadOnlyTxFutures(future, txId);
+ }
+
+ // Split out of the common path
+ private <T> Future<T> combineWithPriorReadOnlyTxFutures(final Future<T> future, final TransactionIdentifier txId) {
+ // Take a stable snapshot, and check if we raced
+ final List<Entry<TransactionIdentifier, Promise<Object>>> priorReadOnlyTxPromiseEntries =
+ new ArrayList<>(priorReadOnlyTxPromises.entrySet());
+ if (priorReadOnlyTxPromiseEntries.isEmpty()) {
+ return future;
+ }
+
+ final 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());
+ }
+
+ final Future<Iterable<Object>> combinedFutures = Futures.sequence(priorReadOnlyTxFutures,
+ getActorUtils().getClientDispatcher());
+
+ final Promise<T> returnPromise = Futures.promise();
+ final OnComplete<Iterable<Object>> onComplete = new OnComplete<>() {
+ @Override
+ public void onComplete(final Throwable failure, final Iterable<Object> notUsed) {
+ LOG.debug("Tx: {} - prior read-only Tx futures complete", txId);
+
+ // Complete the returned Promise with the original Future.
+ returnPromise.completeWith(future);
+ }
+ };
+
+ combinedFutures.onComplete(onComplete, getActorUtils().getClientDispatcher());
+ return returnPromise.future();
+ }
+
+ @Override
+ protected <T> void onTransactionReady(final TransactionIdentifier transaction,
+ final Collection<Future<T>> cohortFutures) {
+ final State localState = currentState;
+ checkState(localState instanceof Allocated, "Readying transaction %s while state is %s", transaction,
+ localState);
+ final TransactionIdentifier currentTx = ((Allocated)localState).getIdentifier();
+ 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 = Futures.sequence(cohortFutures, getActorUtils().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);
+ }
+ }, getActorUtils().getClientDispatcher());
+ }
+
+ @Override
+ protected void onTransactionContextCreated(final TransactionIdentifier transactionId) {
+ Promise<Object> promise = priorReadOnlyTxPromises.remove(transactionId);
+ if (promise != null) {
+ promise.success(null);
+ }