Add food-delivery module: order state table, FIFO dispatcher, nearest / rated / longest-idle rider strategies, atomic stock and kitchen capacity, idempotent placing, seeded simulation (13 test classes, 19 transcripts)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5BMpRV6przXHwuKTz
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
76012dd8c3
commit
3336e2ba15
@@ -0,0 +1,16 @@
|
||||
One order, from placed to delivered
|
||||
===================================
|
||||
|
||||
Meera (4,4) orders from Biryani House (2,3), 3 km away
|
||||
food Rs 547.00 + delivery Rs 44.00 + tax Rs 27.35 = Rs 618.35
|
||||
|
||||
Event log
|
||||
t=00 O-0001 - -> PLACED
|
||||
t=02 O-0001 PLACED -> ACCEPTED
|
||||
t=02 O-0001 partner P1 assigned
|
||||
t=03 O-0001 ACCEPTED -> PREPARING
|
||||
t=14 O-0001 PREPARING -> READY
|
||||
t=15 O-0001 READY -> PICKED_UP
|
||||
t=21 O-0001 PICKED_UP -> DELIVERED
|
||||
|
||||
After delivery: Imran is AVAILABLE at (4,4), idle since minute 21, deliveries 1
|
||||
@@ -0,0 +1,14 @@
|
||||
Order state machine: which of the 64 moves are legal
|
||||
====================================================
|
||||
|
||||
from \ to PLA ACC PRE REA PIC DEL REJ CAN
|
||||
PLACED . yes . . . . yes yes
|
||||
ACCEPTED . . yes . . . . yes
|
||||
PREPARING . . . yes . . . .
|
||||
READY . . . . yes . . .
|
||||
PICKED_UP . . . . . yes . .
|
||||
DELIVERED . . . . . . . .
|
||||
REJECTED . . . . . . . .
|
||||
CANCELLED . . . . . . . .
|
||||
|
||||
legal moves: 8 of 64
|
||||
@@ -0,0 +1,9 @@
|
||||
Illegal moves: the exact message, and the status afterwards
|
||||
===========================================================
|
||||
|
||||
PLACED, markReady : O-0001 cannot move from PLACED to READY; allowed from PLACED: [ACCEPTED, REJECTED, CANCELLED]
|
||||
status afterwards : PLACED
|
||||
|
||||
PREPARING, cancel : O-0001 cannot move from PREPARING to CANCELLED; allowed from PREPARING: [READY]
|
||||
|
||||
DELIVERED, cancel : O-0001 cannot move from DELIVERED to CANCELLED; allowed from DELIVERED: []
|
||||
@@ -0,0 +1,6 @@
|
||||
READY to PICKED_UP is legal on paper, but not without a rider
|
||||
=============================================================
|
||||
|
||||
all riders offline, order READY
|
||||
pickUp: O-0001 has no delivery partner yet, so it cannot be picked up
|
||||
status afterwards: READY
|
||||
@@ -0,0 +1,9 @@
|
||||
Bills: delivery by distance, free delivery from Rs 599, tax rounded half up
|
||||
===========================================================================
|
||||
|
||||
food Rs 99.00 6 km -> food Rs 99.00 + delivery Rs 68.00 + tax Rs 4.95 = Rs 171.95
|
||||
food Rs 547.00 3 km -> food Rs 547.00 + delivery Rs 44.00 + tax Rs 27.35 = Rs 618.35
|
||||
food Rs 599.00 12 km -> food Rs 599.00 + delivery Rs 0.00 + tax Rs 29.95 = Rs 628.95
|
||||
food Rs 598.99 12 km -> food Rs 598.99 + delivery Rs 116.00 + tax Rs 29.95 = Rs 744.94
|
||||
food Rs 100.10 1 km -> food Rs 100.10 + delivery Rs 28.00 + tax Rs 5.01 = Rs 133.11
|
||||
food Rs 100.30 1 km -> food Rs 100.30 + delivery Rs 28.00 + tax Rs 5.02 = Rs 133.32
|
||||
@@ -0,0 +1,15 @@
|
||||
Bad requests: the exact message for each, and the stock afterwards
|
||||
==================================================================
|
||||
|
||||
blank key InvalidOrder: an idempotency key is required
|
||||
unknown customer NotFound: customer C9 not found
|
||||
unknown restaurant NotFound: restaurant R9 not found
|
||||
empty cart InvalidOrder: the cart is empty
|
||||
item from another menu InvalidOrder: Biryani House has no item p1
|
||||
quantity zero InvalidOrder: quantity for Chicken Biryani must be 1 to 20, got 0
|
||||
quantity 21 InvalidOrder: quantity for Chicken Biryani must be 1 to 20, got 21
|
||||
24 km away InvalidOrder: Pizza Point is 24 km away; we deliver up to 15 km
|
||||
more than the stock OutOfStock: b1: wanted 6, only 5 left
|
||||
closed restaurant Dosa Corner is closed
|
||||
|
||||
two-line cart where the second line is short: Veg Biryani stock still 3
|
||||
@@ -0,0 +1,20 @@
|
||||
Search from Meera at (4,4): nearest first versus top rated first
|
||||
================================================================
|
||||
|
||||
blank query, 10 km, NEAREST
|
||||
Biryani House 3 km rating 4.4
|
||||
Dosa Corner 6 km rating 4.6
|
||||
Pizza Point 6 km rating 4.1
|
||||
Burger Lab 6 km rating 3.9
|
||||
|
||||
blank query, 10 km, TOP_RATED
|
||||
Dosa Corner 6 km rating 4.6
|
||||
Biryani House 3 km rating 4.4
|
||||
Pizza Point 6 km rating 4.1
|
||||
Burger Lab 6 km rating 3.9
|
||||
|
||||
query "burger"
|
||||
Burger Lab: [Chicken Burger, Veg Burger]
|
||||
|
||||
radius 4 km: [Biryani House]
|
||||
radius 4 km after Biryani House closes: 0 results
|
||||
@@ -0,0 +1,12 @@
|
||||
Same order, same three free riders, three strategies
|
||||
====================================================
|
||||
|
||||
Order pickup: Biryani House (2,3)
|
||||
P1 Imran at (3,3) 1 km away rating 4.2 idle since minute 30
|
||||
P2 Kavya at (6,2) 5 km away rating 4.8 idle since minute 20
|
||||
P3 Sunil at (2,6) 3 km away rating 4.5 idle since minute 10
|
||||
|
||||
nearest -> P1
|
||||
rated<=5km -> P2 (all three are within 5 km, so the best rating wins)
|
||||
rated<=2km -> P1 (only P1 is within 2 km)
|
||||
longest-idle -> P3 (P3 has waited longest)
|
||||
@@ -0,0 +1,4 @@
|
||||
rated<=0km: nobody is that close, so it falls back to the nearest
|
||||
=================================================================
|
||||
|
||||
assigned: P1
|
||||
@@ -0,0 +1,17 @@
|
||||
One rider, three orders: the queue and what frees it
|
||||
====================================================
|
||||
|
||||
three orders accepted, one rider (P1) online
|
||||
O-0001 rider P1
|
||||
O-0002 rider null
|
||||
O-0003 rider null
|
||||
waiting: [O-0002, O-0003]
|
||||
|
||||
O-0001 delivered at minute 20, P1 free again
|
||||
O-0002 rider P1
|
||||
O-0003 rider null
|
||||
waiting: [O-0003]
|
||||
|
||||
P3 comes online at minute 20
|
||||
O-0003 rider P3
|
||||
waiting: []
|
||||
@@ -0,0 +1,8 @@
|
||||
A cancelled order does not take a rider when one appears
|
||||
========================================================
|
||||
|
||||
no riders online; waiting: [O-0001, O-0002]
|
||||
O-0001 cancelled, then P1 comes online
|
||||
O-0001 rider null, status CANCELLED
|
||||
O-0002 rider P1
|
||||
waiting: []
|
||||
@@ -0,0 +1,9 @@
|
||||
Cancel and reject give back stock, kitchen slot and rider
|
||||
=========================================================
|
||||
|
||||
start : stock b1=10, kitchen slots used=0, P1=AVAILABLE
|
||||
placed 3 x b1 : stock b1=7, kitchen slots used=0, P1=AVAILABLE
|
||||
cancelled while PLACED : stock b1=10, kitchen slots used=0, P1=AVAILABLE
|
||||
placed 4, accepted : stock b1=6, kitchen slots used=1, P1=BUSY (rider P1)
|
||||
cancelled while ACCEPTED : stock b1=10, kitchen slots used=0, P1=AVAILABLE
|
||||
placed 2, rejected : stock b1=10, kitchen slots used=0, P1=AVAILABLE
|
||||
@@ -0,0 +1,7 @@
|
||||
16 buyers, 10 portions, 5000 rounds
|
||||
===================================
|
||||
|
||||
Timing dependent: the naive counts differ from run to run; the locked counts never do.
|
||||
|
||||
naive (check, then write): rounds that sold more than 10: 21 of 5000, worst round sold 12
|
||||
locked (check and write together): rounds that oversold: 0, undersold: 0 of 5000
|
||||
@@ -0,0 +1,8 @@
|
||||
16 orders reach for one free rider, 5000 rounds
|
||||
===============================================
|
||||
|
||||
Timing dependent: the naive count differs from run to run; the compare-and-set count never does.
|
||||
|
||||
naive, check then write, nothing in between: rounds where more than one order got the rider: 0 of 5000, most winners in one round 1
|
||||
naive, with a yield between the check and the write: rounds where more than one order got the rider: 389 of 5000, most winners in one round 7
|
||||
atomic (compare-and-set), no gap needed: rounds with anything other than one winner: 0 of 5000
|
||||
@@ -0,0 +1,6 @@
|
||||
200 orders accepted from 8 threads, 20 riders
|
||||
=============================================
|
||||
|
||||
orders with a rider: 20
|
||||
distinct riders used : 20
|
||||
still waiting : 180
|
||||
@@ -0,0 +1,7 @@
|
||||
Burger Lab cooks 2 at a time: 50 orders, 16 threads tapping accept
|
||||
==================================================================
|
||||
|
||||
rounds: 300
|
||||
accepted per round: 2 (every round)
|
||||
refused per round : 48 (every round)
|
||||
totals: accepted 600, refused 14400
|
||||
@@ -0,0 +1,7 @@
|
||||
The restaurant rejects while the customer cancels, 2000 rounds
|
||||
==============================================================
|
||||
|
||||
The thread released first tends to win, so the two roles swap threads every round to exercise both orders.
|
||||
Who wins depends on timing; that exactly one wins, and that stock comes back once, never does.
|
||||
|
||||
rounds: 2000, reject won 1000, cancel won 1000
|
||||
@@ -0,0 +1,6 @@
|
||||
Double tap and client retries: one key, one order
|
||||
=================================================
|
||||
|
||||
same key twice: O-0001 and O-0001, stock left 8
|
||||
new key : O-0002, stock left 6
|
||||
16 threads, one key: distinct orders 1, stock left 5
|
||||
@@ -0,0 +1,25 @@
|
||||
12 restaurants, 20x20 km grid, 240 minutes of orders, seed 42
|
||||
=============================================================
|
||||
|
||||
Every strategy faces the identical orders. Columns: average and 90th percentile minutes from placing to door,
|
||||
average km a rider rides empty to the restaurant, average minutes ready food waits for a rider, average minutes
|
||||
a rider waits at the restaurant for the food, longest the waiting queue got, deliveries per rider (fewest-most).
|
||||
|
||||
Comfortable: 40 riders, 200 orders
|
||||
strategy avg min p90 min empty km food wait rider wt max queue per rider
|
||||
nearest 26.4 34 4.1 0.4 6.3 0 2-8
|
||||
rated<=5km 26.4 34 4.3 0.4 5.9 0 0-8
|
||||
longest-idle 35.8 54 10.9 9.8 2.2 0 4-7
|
||||
|
||||
Tight: 30 riders, 200 orders
|
||||
strategy avg min p90 min empty km food wait rider wt max queue per rider
|
||||
nearest 30.4 43 7.3 4.5 3.9 1 4-8
|
||||
rated<=5km 29.6 40 6.8 3.6 4.0 0 4-9
|
||||
longest-idle 42.0 59 12.5 16.1 1.4 13 5-8
|
||||
|
||||
Saturated: 30 riders, 300 orders
|
||||
strategy avg min p90 min empty km food wait rider wt max queue per rider
|
||||
nearest 93.1 147 12.6 67.4 0.5 90 8-12
|
||||
rated<=5km 98.5 156 13.0 72.8 0.4 99 7-12
|
||||
longest-idle 93.1 147 12.6 67.4 0.5 90 8-12
|
||||
|
||||
Reference in New Issue
Block a user