0050. Transfer allocation release — the cancel-after-allocation compensation leg
Status
Accepted
Context
ADR 0030 built the allocation leg of the network transfer saga as a
command/reply exchange: network-inventory-planning publishes
TransferAllocationRequested, inventory-storage holds the stock and
answers TransferStockAllocated / TransferStockAllocationRejected.
But the saga had no compensation path: a transfer cancelled (or stuck,
or abandoned) AFTER its allocation left the origin reservation holding
stock in this service until lazy expiry noticed the timeout — hours of
phantom unusable stock, and no way for planning to close the saga
cleanly. network-inventory-planning's ADR-0003 documented exactly this
limitation: "a CANCELLED-after-allocation path still needs an explicit
reservation revocation command."
Decision
A second command/reply leg, the mirror image of the allocation leg.
-
New consumed command. The transfer consumer (
internal/adapters/inbound/kafka/transfer_consumer.go, same topic, same consumer group, same at-least-once checklist) now also dispatches the full typecom.warehouse.wes.network-inventory-planning.transfer.TransferAllocationReleaseRequestedwithdata{transfer_id, transfer_line_id, reservation_id, origin_site_id, sku, quantity, reason}. -
New use case
ReleaseTransferAllocation. It loads thetransfer_allocationsledger row bytransfer_line_id— the allocation ledger, not the command, is the record of WHAT was decided and which reservation holds the stock — validates the command against it, then resolves the reservation through RevokeReservation's existing logic: lazy expiry first (expireIfDue), thenRevoke()+releaseAllocations. The reservation save, the ledger row and the outbox row commit in ONE UnitOfWork. -
A second ledger,
transfer_allocation_releases(migration 0039), DB-unique ontransfer_line_id, gives the release leg the allocation leg's exact idempotency discipline: a byte-identical replay returns the original outcome and republishes nothing; the same line id with a DIFFERENT payload is answered with the original outcome and reasonIDEMPOTENCY_CONFLICTwhile the original decision stands. -
New published fact.
com.warehouse.wms.inventory-storage.reservation.TransferStockAllocationReleasedonwarehouse.inventory.eventsthrough the transactional outbox, key andsubject=transfer_line_id(mirroringTransferStockAllocationRejected).datais{transfer_id, transfer_line_id, reservation_id, origin_site_id, sku, quantity, outcome, reason}withoutcomethe CLOSED setRELEASED|ALREADY_RESOLVED— a reservation that had already expired (lazily resolved here) or been confirmed means the hold is already gone, which settles the saga as success just the same.reasoncarries the command's cancellation reason through. -
Deterministic refusals publish nothing. A release command naming a
transfer_line_idthis service never decided (ErrUnknownTransferLine), or one disagreeing with the allocation ledger row (ErrTransferReleaseMismatch), is logged by the consumer and committed past — never retried, never wedging the partition, and no event: there is no decision to correlate an answer to. -
No
ReservationRevokedon this leg. Transfer reservations never emittedStockReservedon the integration topic (the allocation leg publishesTransferStockAllocatedinstead), so emittingReservationRevokedhere would make wes-work-planning's observed-usable projection increment a count it never decremented.TransferStockAllocationReleasedis the only event this leg publishes; the reservation's own lifecycle events stay off the wire for transfer demand refs.
Consequences
- A transfer cancelled after allocation settles within seconds, and its origin stock is usable again at once (or confirmed gone) — no more stranded reservations waiting on lazy expiry.
- The
RELEASINGsaga state in network-inventory-planning has a deterministic settling fact for both outcomes. - Replays and conflicts are answered without touching stock twice; the
ledger pair (
transfer_allocations,transfer_allocation_releases) is the full audit trail of the line's allocation and release.