Add bookmyshow module: seat booking with hold expiry and payment timeout, naive vs pessimistic vs optimistic inventories, race and deadlock demos, JDBC strategies on H2 (8 test classes, 8 transcripts)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5YFRisPHrLBR2dRu9
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
deb924febd
commit
c57031aaeb
@@ -0,0 +1,14 @@
|
||||
Happy path, one user at a time (show SH-1, 60 seats, hold TTL 5 minutes)
|
||||
========================================================================
|
||||
|
||||
A1 at the start: AVAILABLE v0
|
||||
startBooking(asha, A1+A2): BK-0001 PENDING_PAYMENT, Rs 440
|
||||
A1 after the hold: HELD by BK-0001 until T+05:00 v1
|
||||
A3 untouched: AVAILABLE v0
|
||||
startBooking(ravi, A2+A3): refused (seats not available for show SH-1: [A2, A3])
|
||||
A3 after ravi's refusal: AVAILABLE v0
|
||||
pay(BK-0001): CONFIRMED, booking is CONFIRMED
|
||||
A1 after payment: BOOKED by BK-0001 v2
|
||||
seat summary: {AVAILABLE=58, BOOKED=2, HELD=0}
|
||||
|
||||
Same log produced by NaiveInventory, PessimisticInventory and OptimisticInventory: true
|
||||
@@ -0,0 +1,20 @@
|
||||
NaiveInventory: two users request seat C5 at the same moment (barrier forces both reads before either write)
|
||||
====================================================================================================
|
||||
|
||||
holds granted for C5: 2 (a seat can only be sold once)
|
||||
both users now pay:
|
||||
outcomes (sorted): [CONFIRMED, HOLD_LOST_AFTER_PAYMENT]
|
||||
customers charged: 2
|
||||
customers refunded: 1
|
||||
C5 in the end: BOOKED by BK-000x v3
|
||||
|
||||
Neither thread did anything unusual. Each ran check, then act, and the barrier only decided
|
||||
that both checks finished before either act began.
|
||||
|
||||
PessimisticInventory, same two users at the same moment:
|
||||
holds granted for C5: 1
|
||||
loser sees: SeatUnavailableException
|
||||
|
||||
OptimisticInventory, same two users at the same moment:
|
||||
holds granted for C5: 1
|
||||
loser sees: SeatUnavailableException
|
||||
@@ -0,0 +1,21 @@
|
||||
Two requests for the same two seats, listed in opposite order: H1 asks [A1, A2], H2 asks [A2, A1]
|
||||
=================================================================================================
|
||||
|
||||
PessimisticInventory, AS_REQUESTED (each thread holds one lock, then waits for the other's):
|
||||
stuck until a lock wait timed out (300 ms): true
|
||||
at least one lock wait timed out: true
|
||||
requests that succeeded: at most 1 (never both; which one is timing dependent)
|
||||
Without the timeout both threads would wait forever.
|
||||
|
||||
PessimisticInventory, SORTED (both requests lock A1 first, so one simply waits):
|
||||
requests that succeeded: 1 of 2
|
||||
lock waits that timed out: 0
|
||||
|
||||
OptimisticInventory, AS_REQUESTED (each request takes one seat, then finds the other taken):
|
||||
results: [false, false]
|
||||
A1 / A2 afterwards: AVAILABLE / AVAILABLE (versions v2 / v2: held, then handed back)
|
||||
Both seats are free and both customers were turned away.
|
||||
|
||||
OptimisticInventory, SORTED (both start at A1, so only one can get past the first seat):
|
||||
requests that succeeded: 1 of 2
|
||||
HELD seats afterwards: 2
|
||||
@@ -0,0 +1,8 @@
|
||||
200 threads released at once, all asking for seat D4, repeated 30 times per inventory
|
||||
=====================================================================================
|
||||
|
||||
PessimisticInventory: rounds with exactly one winner: 30 of 30
|
||||
OptimisticInventory: rounds with exactly one winner: 30 of 30
|
||||
|
||||
Control, not asserted and timing dependent: NaiveInventory, no barrier inside it:
|
||||
NaiveInventory: rounds with exactly one winner: 10 of 30
|
||||
@@ -0,0 +1,24 @@
|
||||
400 users, each asking for 1 to 3 random seats of a 60-seat show (seeded plan), all released at once
|
||||
====================================================================================================
|
||||
|
||||
seats requested in total (seeded, identical for every inventory): 800
|
||||
|
||||
PessimisticInventory, SORTED:
|
||||
no seat granted to two requests: true
|
||||
every granted request owns all of its seats: true
|
||||
HELD seats == seats of granted requests: true
|
||||
AVAILABLE + HELD == 60: true
|
||||
|
||||
OptimisticInventory, SORTED:
|
||||
no seat granted to two requests: true
|
||||
every granted request owns all of its seats: true
|
||||
HELD seats == seats of granted requests: true
|
||||
AVAILABLE + HELD == 60: true
|
||||
|
||||
OptimisticInventory, AS_REQUESTED:
|
||||
no seat granted to two requests: true
|
||||
every granted request owns all of its seats: true
|
||||
HELD seats == seats of granted requests: true
|
||||
AVAILABLE + HELD == 60: true
|
||||
|
||||
How many requests were granted differs from run to run (it depends on the interleaving) and is deliberately not printed.
|
||||
@@ -0,0 +1,22 @@
|
||||
Hold expiry and payment timeout on a fake clock (TTL 5 minutes, no sleeping)
|
||||
============================================================================
|
||||
|
||||
1. Pay one second before the deadline
|
||||
hold placed T+00:00, expires T+05:00, pay pressed T+04:59
|
||||
outcome: CONFIRMED charges: 1 refunds: 0
|
||||
2. The deadline passes while Asha is on the payment page (she holds B1 and B2)
|
||||
T+04:59 B1 reads as HELD
|
||||
T+05:00 B1 reads as AVAILABLE (stored slot still says HELD: expiry is lazy)
|
||||
T+05:01 Ravi asks for B1: BK-0002 PENDING_PAYMENT
|
||||
T+05:02 Asha presses Pay: HOLD_EXPIRED_BEFORE_PAYMENT charges: 0
|
||||
B1: HELD by BK-0002 until T+10:01 v2
|
||||
B2: AVAILABLE v2
|
||||
3. Pay pressed at T+04:59, the gateway takes two minutes, nobody else wants the seat
|
||||
outcome: HOLD_LOST_AFTER_PAYMENT charges: 1 refunds: [BK-0001] booking: REFUNDED
|
||||
4. Same slow payment, but Ravi takes B4 while the gateway is still working on Asha's charge
|
||||
outcome: HOLD_LOST_AFTER_PAYMENT charges: 1 refunds: [BK-0001] booking: REFUNDED
|
||||
B4: HELD by BK-0002 until T+11:59 v2
|
||||
5. The card is declined
|
||||
outcome: PAYMENT_DECLINED booking: PAYMENT_FAILED B5 now: AVAILABLE
|
||||
|
||||
Same log produced by all three inventories: true
|
||||
@@ -0,0 +1,29 @@
|
||||
Pitfalls: partial holds, foreign releases, bad input, double Pay
|
||||
================================================================
|
||||
|
||||
1. Request [A1, A2, A3] while someone else holds A3
|
||||
NaiveInventory result false A1 AVAILABLE v0 A2 AVAILABLE v0
|
||||
PessimisticInventory result false A1 AVAILABLE v0 A2 AVAILABLE v0
|
||||
OptimisticInventory result false A1 AVAILABLE v2 A2 AVAILABLE v2
|
||||
(v0 means the seat was never touched; v2 means it was held and handed back during the attempt)
|
||||
|
||||
2. Someone who does not own the hold tries to release or confirm it
|
||||
NaiveInventory after Ravi's release and confirm: C1 is HELD by ASHA, Ravi's confirm returned false
|
||||
PessimisticInventory after Ravi's release and confirm: C1 is HELD by ASHA, Ravi's confirm returned false
|
||||
OptimisticInventory after Ravi's release and confirm: C1 is HELD by ASHA, Ravi's confirm returned false
|
||||
|
||||
3. A BOOKED seat is not released by release(), even by its owner
|
||||
NaiveInventory C2 after owner's release: BOOKED, a new hold on it returns false
|
||||
PessimisticInventory C2 after owner's release: BOOKED, a new hold on it returns false
|
||||
OptimisticInventory C2 after owner's release: BOOKED, a new hold on it returns false
|
||||
|
||||
4. Bad input (identical for all three inventories)
|
||||
duplicate seat ids: IllegalArgumentException: duplicate seat id in request: A1
|
||||
unknown seat: IllegalArgumentException: unknown seat Z99 for show SH-1
|
||||
no seats at all: IllegalArgumentException: no seats requested
|
||||
unknown show: IllegalArgumentException: unknown show: SH-404
|
||||
|
||||
5. Pay pressed 50 times at once for the same booking
|
||||
calls that confirmed: 1
|
||||
calls refused with IllegalStateException: 49
|
||||
times the card was charged: 1
|
||||
@@ -0,0 +1,9 @@
|
||||
32 connections released at once, all holding seat D4 of SH-1, repeated 20 times per strategy (H2 2.5.252)
|
||||
====================================================================================================
|
||||
|
||||
optimistic (SELECT, then UPDATE ... WHERE version = ?): rounds with exactly one winner: 20 of 20
|
||||
pessimistic (SELECT ... FOR UPDATE, then UPDATE): rounds with exactly one winner: 20 of 20
|
||||
conditional (one UPDATE ... WHERE state = 'AVAILABLE'): rounds with exactly one winner: 20 of 20
|
||||
|
||||
Control, not asserted and timing dependent:
|
||||
naive (SELECT, then UPDATE with no version or lock): rounds with exactly one winner: 3 of 20
|
||||
Reference in New Issue
Block a user