The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S36 · V3·35

The Queue Is an Index

Volume 3, chapter 35. The kitchen queue was a PriorityQueue, and the server only ever removed its head. A test with three orders showed the cost: cancel the one in the middle, finish the first, and the kitchen is told to cook the cancelled one next. Restart the server and every open order vanishes from the kitchens. This stage rebuilds the queue as what it is, an index over the orders kept in two collections (a TreeSet for "what's next", a HashMap for "where is this order"), checks it against a model too simple to be wrong over 60,000 random operations, and adds a compiler rule for records that hold collections nobody copied.

6 parts · ~4 min to read
Updated Suggest a correction

Stage 36: the queue is an index

The brief

What you'll build.

  • KitchenQueue, rebuilt: a TreeSet<Ticket> ordered by promised time then arrival, and a HashMap<String, Ticket> from order id; put (add, or move keeping its place among equal times), remove from anywhere, next, peek; promise(OrderStatus) computes when an order is due from its state alone (placed: 30 minutes to accept; accepted: acceptance time plus prep time, so a delay moves it), and apply(order) queues or removes it;
  • the server: every change to an order goes through apply; the queues are rebuilt from the orders on start;
  • LangLint rule record-mutable: a record component that is a Collection or Map and isn't copied in a constructor;
  • the copies: compact constructors in seven records; LinkedHashMap copies where order matters;
  • tests: a model-based test of the queue, a promise test, a server test that cancels from the middle and restarts; three golden-master exchanges for the queue.

Why a real company needs this. A queue that keeps a cancelled order sends a cook to make food nobody will collect; a queue that empties on restart hides every waiting customer after a deploy. Both happen when a structure derived from the real data is treated as data in its own right.

Why now. Chapter 35 is collections: the four shapes, how each implementation works and what it costs, ordering, iteration, immutability and concurrency. Stage 4 chose a PriorityQueue for the kitchen queue, which was right for "what's next" and wrong for "remove this one".

What you'll have at the end.

  • a queue that follows every order, from any position, and survives a restart;
  • a test that compares it with a model under random operations;
  • every record's collections copied, and a rule that keeps it so;
  • ADR-0038: derived structures are rebuilt, never trusted.

Theory to own

  1. What a PriorityQueue is good at. Which operations are O(log n), which are O(n), and why? (The chapter, "Queues, deques and PriorityQueue"; Companion, Code walkthrough.)
  2. Two structures, one truth. When do you need a sorted set and a map together, and what must stay true between them? (The chapter, "TreeMap and TreeSet".)
  3. Comparators and sets. Why must the comparator for a TreeSet never return 0 for two different tickets? (The chapter, "Comparable and Comparator"; Exercises P4.)
  4. Copies and views. What does List.copyOf do that Collections.unmodifiableList doesn't, and what does neither do? (The chapter, "Immutable and unmodifiable collections".)

Build it

Step 1: make the bug visible

Write a server test: place three orders for one kitchen, cancel the second, accept and finish the first, ask the kitchen for the next order. Then one that restarts the server with an order waiting.

Checkpoint. Both fail, for the reason you expect.

Step 2: an index, not a list

Rebuild KitchenQueue as a TreeSet plus a HashMap, with put, remove, next, peek and promise. In the server, replace the add on placement and the head-only removal with apply, and rebuild the queues from the recovered orders in order-number order.

Checkpoint. 41 server tests, and the golden master unchanged.

Step 3: test it against a model

Write a test that runs random sequences of put, remove and next on the queue and on a plain list sorted on every read, and compares them after every step. Then break the queue on purpose and check the test notices. Add three GET /kitchens/k7/next exchanges to the golden master and record it.

Checkpoint. 47 core tests; golden master: 47 exchanges, identical.

Step 4: records that hold collections

Add record-mutable to LangLint with a test file, and run it on the codebase.

Checkpoint. langlint: 2 test files, every expected warning and no others, and 0 warnings in every module.

Senior and architect lens

1. A derived structure is a cache of a query. The kitchen queue answers "which open order is due first?" over the orders. Treat it like any cache: one place updates it, it can be rebuilt from the source, and a test compares it with the query.

2. ADR-0038: derived structures are rebuilt, never trusted. Write it with these points:

  • Source of truth: the order log; queues, indexes and counters are derived.
  • Rebuild: every derived structure can be rebuilt from the source on start, and is.
  • One update path: every change goes through one function (apply), not a list of special cases.
  • Model tests: a derived structure is tested against a simple model under random operations.
  • Copies at boundaries: records copy their collections; order-preserving copies where order is part of the contract.

3. Pick the structure by its operations, all of them. "What's next" chose the heap; "remove this one" and "move this one" were added later, by other features, without anyone revisiting the choice. Write down the operations a structure must support next to it.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
one lock for all kitchens queuesLock serialises every kitchen's queue stage 39 (concurrency: per-kitchen locks or a ConcurrentSkipListSet)
rebuild time rebuilding scans every order, open or not stage 42 (memory and performance: snapshots), stage 50 (persistence: a query)
Rum's series lists grow without bound memory stage 42
the queue is in memory one server only stage 50 (persistence), stage 52 (jobs)

Evidence

  • exp/36/queue-before.out holds the two failures; 41 server and 47 core tests now pass.
  • The two mutants in exp/36 fail the model test.
  • golden.py reports 47 exchanges, identical; the suite passes 44 tests.
  • exp/36/lint-before.txt lists the 11 findings; every module builds with 0 warnings.
  • ADR-0038 is committed.

Carry forward

  • KitchenQueue.apply is the only way an order changes a queue.
  • The model test is the pattern for every derived structure from here on (stage 54's caches).
  • record-mutable keeps records' collections copied.