- // The transaction does not exist and we are about to create it, check sequence number
- if (request.getSequence() != 0) {
- LOG.debug("{}: no transaction state present, unexpected request {}", persistenceId(), request);
- throw UNSEQUENCED_START;
- }
-
- tx = createTransaction(request, id);
- transactions.put(id, tx);
- } else {
- final Optional<TransactionSuccess<?>> maybeReplay = tx.replaySequence(request.getSequence());
- if (maybeReplay.isPresent()) {
- final TransactionSuccess<?> replay = maybeReplay.get();
- LOG.debug("{}: envelope {} replaying response {}", persistenceId(), envelope, replay);
- return replay;
- }
+ // This should never happen because the purge callback removes the transaction and puts it into
+ // purged transactions in one go. If it does, we warn about the situation and
+ LOG.warn("{}: transaction {} not tracked in {}, but not present in active transactions", persistenceId,
+ id, purgedTransactions);
+ purgedTransactions.add(Range.singleton(ul));
+ return new TransactionPurgeResponse(id, request.getSequence());
+ }
+
+ tree.purgeTransaction(id, () -> {
+ purgedTransactions.add(Range.singleton(ul));
+ transactions.remove(id);
+ LOG.debug("{}: finished purging transaction {}", persistenceId(), id);
+ envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
+ });
+ return null;
+ }
+
+ if (purgedTransactions.contains(ul)) {
+ LOG.warn("{}: Request {} is contained purged transactions {}", persistenceId, request, purgedTransactions);
+ throw new DeadTransactionException(purgedTransactions);
+ }
+ final Boolean closed = closedTransactions.get(ul);
+ if (closed != null) {
+ final boolean successful = closed.booleanValue();
+ LOG.debug("{}: Request {} refers to a {} transaction", persistenceId, request, successful ? "successful"
+ : "failed");
+ throw new ClosedTransactionException(successful);
+ }
+
+ FrontendTransaction tx = transactions.get(id);
+ if (tx == null) {
+ // The transaction does not exist and we are about to create it, check sequence number
+ if (request.getSequence() != 0) {
+ LOG.warn("{}: no transaction state present, unexpected request {}", persistenceId(), request);
+ throw new OutOfOrderRequestException(0);
+ }
+
+ tx = createTransaction(request, id);
+ transactions.put(id, tx);
+ } else if (!(request instanceof IncrementTransactionSequenceRequest)) {
+ final Optional<TransactionSuccess<?>> maybeReplay = tx.replaySequence(request.getSequence());
+ if (maybeReplay.isPresent()) {
+ final TransactionSuccess<?> replay = maybeReplay.get();
+ LOG.debug("{}: envelope {} replaying response {}", persistenceId(), envelope, replay);
+ return replay;