0052. Consume CapacityPlanSuperseded: a superseded plan stops constraining orders
Status
Accepted — implemented by the change that introduces this record. Extends ADR 0031 §1 (the last-writer-wins rule of the planned-capacity read model) and §2 (the consumer's dispatch table). Required by warehouse-planning ADR 0024 decision 5, and merged BEFORE warehouse-planning publishes the new type.
Context
warehouse-planning ADR 0024 decision 3: publishing a capacity plan when
another PUBLISHED plan already exists for the same
(site_id, location, path_id, window) moves the OLDER plan to a terminal
status SUPERSEDED. In the same unit of work it publishes
com.warehouse.wes.warehouse-planning.capacityplan.CapacityPlanSuperseded
on warehouse.warehouse-planning.events (and on its analytics topic):
data:plan_id(the OLDER plan),superseded_by(the newer plan's id),site_id,path_id,window_start,window_end.- Kafka key and CloudEvents
subject= the older plan's id. - On one publish the events come in this order: Superseded (of the older plan), Published (of the newer one), then ShortageDetected and BottleneckDetected when the newer plan is short.
ADR 0024 decision 5 requires every consumer to keep only the NEWEST published plan of a window. order-management, as it stands, does not:
PlannedCapacityConsumerkeys the read modelplanned_capacity_windowsbyplan_idand ignores every type it does not know — the new one included.order.CapacityConstraintslets EVERY published window withshortage > 0at the order's site constrain the order.
So once warehouse-planning publishes a replacement plan, the older plan would keep annotating orders as capacity-constrained forever, next to the newer one. ADR 0024 decision 5 makes this consumer change its own decision, and it must merge before the producer starts publishing.
Decision
-
A new read-model status,
SUPERSEDED, that never constrains.order.PlannedCapacitySupersededjoinsDRAFTandPUBLISHED;Validateaccepts it.Constraining()is unchanged —PUBLISHED && shortage > 0— so a SUPERSEDED row never constrains an order, by construction rather than by a new exception. -
SUPERSEDEDis terminal; it beats every status regardless of time.PlannedCapacityWindow.Supersedes(prev), still the one rule both adapters apply:- a SUPERSEDED
previs replaced by NOTHING — not a later Created, Published or ShortageDetected (a redelivery, a DLQ replay, an out-of-order delivery), and not a second Superseded (the firstsuperseded_bystands); - a SUPERSEDED write replaces any non-SUPERSEDED
prevwhatever the twotimes say — the plan is terminal at the producer, so its ordering against the plan's own earlier events does not matter; - between DRAFT and PUBLISHED the ADR 0031 rules are unchanged: a
DRAFT never replaces a PUBLISHED row, otherwise a later-or-equal
timewins.
- a SUPERSEDED
-
Superseding a known row keeps that row's facts. The Superseded event carries no
warehouse_id, quantities or bottleneck. When it lands on a row the model already holds, the stored row keeps everything it had (location, window, demand, capacity, shortage, bottleneck) and changes onlystatus→SUPERSEDED,superseded_byandAsOf(the Superseded event'stime). The merge is one pure domain function (PlannedCapacityWindow.ReplacementOf(prev)), so the Postgres and in-memory adapters cannot disagree, exactly as forSupersedes. -
A Superseded for a plan never seen inserts a tombstone. The events of the older plan may not have reached this consumer (a partition replay, a DLQ replay, an older plan published before this consumer existed). The Superseded event then inserts a SUPERSEDED row with only the fields the event carries:
plan_id,location= the event's optionallocation(the field the plan's other events carry), else itssite_id,path_id, the window,superseded_by, the eventtime;warehouse_idempty and every quantity zero. Because of rule 2, a Published or ShortageDetected for that plan arriving later is a no-op: it can never make the plan constraining. -
superseded_byis stored. A nullablesuperseded_bycolumn (migration0015, additive) holds the newer plan's id, forGET /planned-capacityand for the logs. The same migration widens the status CHECK toDRAFT | PUBLISHED | SUPERSEDEDand adds a CHECK that a SUPERSEDED row names its successor.Validateenforces the same: a SUPERSEDED window needs asuperseded_bythat differs from its ownplan_id; a payload that breaks it is dead-lettered like any other invalid payload (ADR 0031 §2). -
The consumer adds one case, same shape.
PlannedCapacityConsumerdispatches the new FULL type to a SUPERSEDED write. It decodesplan_id,superseded_by,site_id,path_id,window_start,window_end; the CloudEventssubjectmust equaldata.plan_id, as for the other types. Idempotency (CloudEventsid, one UnitOfWork), DLQ and retry are ADR 0031's, untouched.BottleneckDetectedand every unknown type stay ignored and committed. -
The newer plan needs nothing new. It arrives as its own CapacityPlanPublished (and CapacityShortageDetected when short) under its own
plan_id, and constrains exactly as today. -
The read side shows the status; the annotation cannot.
GET /planned-capacitylists SUPERSEDED rows like DRAFT ones (every status is listed, ADR 0031 §4), now with an optionalsupersededBy; the OpenAPIstatusenum gainsSUPERSEDED(a response enum value added — a warning, not an error, in the spec-breaking gate). The order'scapacityConstraintis built only from constraining rows, so it never names a superseded plan.
Consequences
Easier
- A replaced plan stops annotating orders the moment its Superseded is applied, and nothing that arrives afterwards can bring it back. The read model converges on "newest published plan of a window" without any cross-plan query or window matching in this repo: the producer names the older plan, and the row is keyed by that name.
- The decision still lives in two pure domain functions (
Supersedes,ReplacementOf) pinned by a table test over every (prev, next) status pair and by the mutation gate.
Harder / what this does NOT solve
- The producer now publishes the type (done). The consumer contract
test first read a LOCAL example built from ADR 0024's field list,
because warehouse-planning had not published the type. Follow-up done:
the pin is refreshed and
consumer_contract_capacity_plan_superseded_test.goasserts against warehouse-planning's own pinned example (E1 the topic documents the type, E2 the example decodes throughsupersededRequestFromwith subject =plan_idandlocationandwarehouse_idpresent, E3 the envelope, E4 additive fields); the local example is deleted. - A tombstone is listed under the plan's location. Its
locationis the event'slocationwhen present, else itssite_id, and its quantities are zero:GET /planned-capacityshows it as a SUPERSEDED row with no shortage. That is the truth this context knows about a plan it never saw published. - A down migration forgets superseded plans. Restoring the old CHECK
needs the SUPERSEDED rows gone, so
0015's down deletes them; a rolled-back binary would then accept a redelivered Published for such a plan as before this ADR. A rollback is a deliberate operator act; the trade-off is recorded here. - The rule is per plan id, not per window. This context never decides which of two published plans of a window is newer; it trusts the producer's Superseded. A producer that replaced a plan without publishing it would leave both constraining, as before this ADR.
- No new outbound event. Superseding a plan raises nothing on this context's own topics; the change is visible on the read model and the REST read only.