ADR 0045: The fulfilment flow and webhooks work in a Facility
Status: Accepted. Converts the fulfilment-flow consumers that ADR 0044 left on the bridge: fulfilment, waves, work, streaming, cross-dock and replenishment, with their schedulers, the outbox and the webhook relay. ADR 0046 converts the readers and documents (monitors, reporting, forecasting, simulation, slotting and the document archive) and removes the two bridges this record kept.
Context
The pick, wave, shipment, cross-dock, streaming and replenishment code found its rows by the owning
Business Unit. It read the request scope, or took a clientId, and the schedulers enumerated owners.
Where a caller held only an owner, it called the order, stock and layout ports through
OrganizationLookup.requireDefaultFacilityId, the bridge that
ADR 0044 kept for this work. The database already
stamped facility_id on the rows, but nothing in this flow named it, so a wave, pick route, cross-dock
order or shipment could in principle read or write across Facilities.
The webhook relay matched a subscription to an event by client_id, and its envelope named only the
owner. A receiver could not tell which Facility an operational event came from.
The end state is a Karyo that works with Company, Business Unit and Facility and not with the legacy stock owner. Karyo has no users, so a change here may be disruptive, and compatibility code has no reason to exist.
Decision
- A flow port names its Facility. The fulfilment, wave, work, streaming, cross-dock and
replenishment ports take a typed
FacilityId, or carry one in the request (WavePickRequest,OpenGroupShipmentRequest,WaveRef,FlushContext). Each ambient and explicit-clientIdmethod pair is one method, and every caller is converted.WorkProviderandWorkEligibilityResolvertake the Facility, so a work inbox lists only the Facility it is opened in. - The flow rows are Facility-owned.
PickOrder,Pick,Shipment,ShipmentOrder,ShippingUnit,ShippingUnitLine,Wave,ConsolidationGroup,ConsolidationLine,SortScan,WaveSelectionRule,CrossDockOrder,StreamBatch,WorkGroupandWorkGroupMemberextendFacilityEntity. Services, repositories and resources filter onfacility_id. A row in a Facility is unknown to a request in another. - No wave, pick route, cross-dock order or shipment crosses Facilities. A wave selects only the orders of its Facility. The picks, consolidation groups and shipments it produces carry that Facility, and the progress, expiry and release observers act in the Facility of the event they observe.
- A selection rule works only in its own Facility. A rule names the zones and locations of one
Facility, and an order strategy is shared by every Facility of the unit. A strategy that names a
rule of another Facility works only in that rule's Facility: in any other,
POST /wavesand the scheduler fail with a 422 problem (wave-rule-other-facility) that says the rule belongs to a different Facility.WaveSchedulerreads the order pool first and returns early for a Facility with nothing to wave, so it looks up no rule there. - The owner is the Business Unit of the Facility. A row written in a Facility names that
Facility's Business Unit as
client_id, as ADRs 0043 and 0044 decided.OrganizationLookup.requireBusinessUnitOfanswers it.client_idstays on the row as data. - Every scheduled job in the flow fans out per Facility.
WaveScheduler,StreamingReleaseScheduler,StockPurgeSchedulerandCrossDockExpirySweepenumerate the Facilities that have work;ReplenishmentSchedulerenumerates every active Facility in the organization registry (OrganizationLookup.activeFacilityIds), because its scan covers fix assignments and item-data areas and a list derived from one of them skipped the other. Each job runs each Facility in its own failure boundary: one Facility's failure is logged and the others still run. The enumerations (OrderReleasePort.facilitiesWithWaveEligibleOrders,StreamingReleasePort.facilitiesWithStreamingWork) cross scopes by design and are listed as such. The registry scope of these jobs isPER_FACILITY. Settings still resolve through the Business Unit of the Facility. - Facility events and master-data events are two scopes of outbox row.
OutboxServicehas no unscopedpublish.publishInFacilitywrites an operational event with its Facility and its Business Unit, andpublishForBusinessUnitwrites a master-data event (a product, a client, a template) that no single Facility owns.outbox_events.facility_idis null for the second kind. State events of the flow (WaveStateChangedEvent, the cross-dock events,PickClaimWarningAccepted,WavePickActivityEvent) carryfacilityIdand no longer carryclientId. - The webhook envelope is version 2, with no version 1. The body carries
schemaVersion,scopeType(FACILITYorBUSINESS_UNIT),facilityId(null for a master-data event),businessUnitId,companyCode, the aggregate fields anddata. The relay emits only this shape. The v1tenantIdfield and a drain window for old receivers do not exist: Karyo is unreleased and nobody receives v1. - A webhook subscription's audience is one Facility. It receives the operational events of that
Facility and the master-data events of the Facility's Business Unit, and nothing else.
webhook_subscription.facility_idandwebhook_delivery.facility_idare required, the subscription routes read and write only the Facility in the request, and the synthetic ping is a Facility event. - Two bridges stay for the follow-up, and no others.
BinBelowReorderDetector(monitors) and the demo and layout-setup callers still resolved a default Facility throughOrganizationLookup.requireDefaultFacilityIdandfindDefaultFacility. The follow-up that converted the readers (ADR 0046) removedrequireDefaultFacilityIdwith the last caller. The monitors and the document archive kept their Business Unit key until then, andMonitorEvaluatorstayedPER_OWNER.
Consequences
- Two Facilities of one Business Unit run their waves, pick orders, shipments, cross-dock orders and stream batches side by side, and one Facility's scheduler failure does not stop the other.
- A receiver tells the Facility of an event from the envelope, and a subscription never sees another Facility's operational events. A receiver that wants a whole Business Unit subscribes once per Facility.
- A test that depends on a Facility must provision one first (
TestOrganizations.facilityOf), and each test takes its ownclient_id, asAGENTS.mdrecords. config/scope-consumers.jsonshrinks: the converted ports leave the list of ports without a scope, the raw-scope entries of the converted files becomeoperationalFacilityIdor go, and theclient_idpredicate entries of the converted repositories are removed. The registry only shrinks.- The monitors, reporting, forecasting, simulation, slotting readers and the document archive read by Business Unit when this record was written, until ADR 0046 converted them.
Alternatives considered
- Emit envelope v1 and v2 for a drain window. Rejected: there are no receivers to drain, and two shapes would be compatibility code with no reader.
- One subscription for a whole Business Unit. Rejected: a Facility is the unit of operation, and a subscription that spans Facilities would reopen the cross-Facility reads this decision closes.
- Keep the owner-enumerating schedulers and resolve each default Facility. Rejected for the reason in ADR 0044: the bridge hides which Facility a job works in and fails for a Business Unit with two.
- Convert the readers and documents in the same change. Not done: the change was too large to review as one. The follow-up carries them.
Evidence
services/fulfillment-service/,services/wave-service/,services/work-service/,services/streaming-service/,services/crossdock-service/andservices/replenishment-service/- the ports under-apiand the implementations under-core, with each module'sFacilityRows.ktlibs/karyo-events/src/main/kotlin/com/karyo/events/outbox/OutboxService.kt- the two publishersservices/integration-hub-service/karyo-webhooks-api/src/main/kotlin/com/karyo/webhooks/dto/WebhookEnvelope.ktand.../karyo-webhooks-core/.../relay/WebhookFanoutScheduler.kt- the envelope and the audience ruleservices/karyo-app/src/main/resources/db/migration/common/V4__outbox_facility.sqland.../webhooks/V902__facility_audience.sqlconfig/scope-consumers.jsonandservices/karyo-app/src/test/kotlin/com/karyo/security/guards/- the registry and the guards that hold it
Related
- ADR 0006 and ADR 0007 - the module boundary the ports sit on
- ADR 0009 - the outbox the relay reads
- ADR 0039 - the registry
- Webhooks and the event catalogue - the contract a receiver sees
- Identity and tenancy