Linehaul Transportation runbook
Linehaul Transportation is the retail network's middle-mile bounded context (a domain-driven design study; see the disclaimer in the
README.md): it turns lane schedules into a rolling 14-day horizon of scheduled departures, including a PICKUP departure at every cutoff of an
active vendor node, tracks each departure's booked and remaining package capacity, books locked fulfillment assignments onto pickup departures, and
builds loads of packages that are closed, dispatched and received. It calls no other service: it keeps local copies of the node-topology,
order-sourcing and shipping-and-tracking facts (ADR 0003) and publishes every change
as a CloudEvents 1.0 event through a transactional outbox (ADR 0004). Settings, probes and
shutdown semantics are on Runtime and Configuration; this page is the operator's view.
Runtime shape
Three binaries, three independent deployables from one image (/app/api, /app/mcp, /app/a2a):
| Binary | Chart component | Port | Role |
|---|---|---|---|
cmd/api | api Deployment (always on) | :8080 | REST, the three Kafka consumers, the outbox relay and the horizon roll. The only binary that writes. |
cmd/mcp | mcp.enabled (default off) | :8090 | Read-only MCP server (Streamable HTTP on /mcp and /). No Kafka, no relay, no writes. |
cmd/a2a | a2a.enabled (default off) | :8093 | Read-only A2A agent (JSON-RPC on / and /a2a, Agent Card on /.well-known/agent-card.json). No Kafka, no relay, no writes. |
The chart also has an optional frontend component (nginx serving the linehaul_mfe console remote, no env, default off).
cmd/api (cmd/api/main.go, cmd/api/serve.go) is the composition root of everything the service does:
- REST on
:8080(internal/adapters/inbound/http/server.go): the operations ofapis/openapi.yamlon a chi router:POST /lane-schedules,POST /lane-schedules/{laneId}/retirements,POST /departures/{departureId}/cancellations,PUT /departures/{departureId}/capacity,POST /loads,POST /loads/{loadId}/packages,POST /loads/{loadId}/closures,POST /loads/{loadId}/dispatches,POST /loads/{loadId}/receipts,GET /departures,GET /departures/{departureId}. Every POST sits behind the Idempotency-Key middleware (24 h replay window,idempotency_keystable; a RETRYABLE refusal leaves no row, ADR 0015, so a key row count of 0 after such a refusal is correct). No auth, by fleet decision. - Transactional-outbox relay (
internal/adapters/outbound/outbox/relay.go): drains unpublishedoutbox_eventsrows to Kafka everyOUTBOX_RELAY_INTERVAL(batch 100,FOR UPDATE SKIP LOCKEDclaims). - Three Kafka consumers (
internal/adapters/inbound/kafka/consumer.go), each under its own mandatory consumer group. - Horizon roll (the generation sweep):
GenerateDepartures(internal/application/usecases/generate.go) runs once at boot and then everyGENERATE_INTERVAL; it extends the 14-day departure horizon of every ACTIVE lane schedule and pickup-eligible vendor node. A rerun schedules nothing new (ADR 0002 A1/A6), which is why it is safe that it runs in every replica. Each run logshorizon rollat INFO withscheduledandfailedcounts. There is no separate sweeper job or CronJob.
The relay, the horizon roll and the consumers are workers: if any of them returns on its own, the process exits non-zero so Kubernetes restarts it
(cmd/api/serve.go: startWorkers / serveUntilSignal).
Dependencies
- Postgres: every write is a transaction.
DATABASE_URLis the pooled DSN (PgBouncer transaction pooling in the fleet);MIGRATIONS_DATABASE_URLis a direct, session-mode DSN used only for the boot migrations, because golang-migrate's advisory lock cannot survive transaction pooling (cmd/api/main.go,migrate; retried up to 30 times, 1 s apart, while Postgres starts). Both come from the Secretlinehaul-transportation-dbviasecretKeyRef; the migrations key isoptional: absent, the binary falls back toDATABASE_URL.cmd/mcpandcmd/a2aalso run the (idempotent) migrations on start and read the same database. - Kafka: one broker platform-wide. See the topic table below. Topics are auto-created by the first writer (the relay writer sets
AllowAutoTopicCreation;internal/adapters/outbound/kafka/health.go): no topic exists until the first publish, and that is normal, not an outage. - No sibling REST calls: the service depends on nothing else (ADR 0003).
Migrations
Embedded in the image (internal/adapters/outbound/postgres/migrations/) and applied by golang-migrate on every boot of every binary over the direct
DSN; there is no separate migration job. Concurrent boots are serialised by golang-migrate's advisory lock. They create departures,
lane_schedules, loads, load_packages, expected_volumes, node_views, lane_views, outbox_events, processed_events and
idempotency_keys (see Entity-relationship). Never point MIGRATIONS_DATABASE_URL at the pooled listener.
Kafka topics and consumer groups
| Direction | Topic | Event types | Consumer group (chart value) |
|---|---|---|---|
| consumed | retailnetwork.node-topology.events | NodeRegistered, NodeOperationalProfileDeclared, NodeActivated, NodeSuspended, NodeRetired, LaneOpened, LaneClosed | NODE_TOPOLOGY_CONSUMER_GROUP (config.nodeTopologyConsumerGroup), deployed as linehaul-transportation-node-topology |
| consumed | retailnetwork.order-sourcing.events | FulfillmentReleasedToNode, FulfillmentReassigned, FulfillmentLocked, FulfillmentCancelled | ORDER_SOURCING_CONSUMER_GROUP, deployed as linehaul-transportation-order-sourcing |
| consumed | retailnetwork.shipping-and-tracking.events | LabelIssued, LabelVoided | SHIPPING_AND_TRACKING_CONSUMER_GROUP, deployed as linehaul-transportation-shipping-and-tracking |
| published | retailnetwork.linehaul-transportation.events | DepartureScheduled, DepartureCapacityChanged, DepartureCancelled, BookingMoved, BookingFailed, LoadDeparted, LoadArrived | - |
| published | retailnetwork.linehaul-transportation.analytics | DepartureDeparted, DepartureArrived, LoadClosed (analytics only) | - |
| dead letters | <consumed topic>.dlq (for example retailnetwork.order-sourcing.events.dlq) | the original message plus x-dlq-* headers | - |
Consumer group ids must be stable and service-prefixed, never derived from the pod name: a rescheduled pod then resumes from its committed offsets.
The Kafka message key is the aggregate id and the CloudEvents subject equals it.
Health
GET /healthzis liveness only:200 {"status":"ok"}while the process serves, even mid-drain or with both dependencies down (internal/adapters/inbound/http/server.go).cmd/mcpandcmd/a2aserve only/healthz(they have no readiness endpoint).GET /readyz(cmd/apionly,internal/adapters/inbound/http/readiness.go) is200 {"status":"ready"}only while the drain gate is open AND Postgres answers a ping AND a Kafka broker answers a topic-less metadata request (verdict cached 3 s, one round bounded 2 s; the check names no topics, so probing never creates one). It answers 503{"status":"not_ready"}once shutdown began; otherwise 503application/problem+jsondependency-unavailablenaming the failing dependencies.- Writes while a dependency is down: with Kafka down, a REST write is still accepted: the aggregate rows and the already-encoded events commit
together in Postgres and wait in
outbox_eventsuntil the relay can publish. With Postgres down, no write is accepted and/readyzfails. In-cluster the 503 also pulls the pod from the Service endpoints; the handler itself only needs Postgres.
Configuration in the chart
cmd/api/config.go reads and validates the settings (a missing or invalid value is a boot error that names the variable, raised before anything is
opened or bound). The chart renders each from charts/linehaul-transportation/values.yaml:
| Env var | Chart value |
|---|---|
HTTP_ADDR, LOG_LEVEL | config.httpAddr, config.logLevel |
DATABASE_URL | database.url, or database.existingSecret + database.existingSecretKey (secretKeyRef) |
MIGRATIONS_DATABASE_URL | database.migrationsExistingSecretKey (optional secretKeyRef) |
KAFKA_BROKERS | kafka.brokers |
NODE_TOPOLOGY_CONSUMER_GROUP, ORDER_SOURCING_CONSUMER_GROUP, SHIPPING_AND_TRACKING_CONSUMER_GROUP | config.nodeTopologyConsumerGroup, config.orderSourcingConsumerGroup, config.shippingAndTrackingConsumerGroup |
OUTBOX_RELAY_INTERVAL, OUTBOX_MAX_ATTEMPTS, GENERATE_INTERVAL, PACKAGES_PER_ORDER, SHUTDOWN_DRAIN_DELAY | config.outboxRelayInterval, config.outboxMaxAttempts, config.generateInterval, config.packagesPerOrder, config.shutdownDrainDelay |
OTEL_*, SERVICE_VERSION, ENVIRONMENT | otel.enabled, otel.endpoint, otel.serviceName, image.tag, environment |
MCP_ADDR (cmd/mcp) | mcp.httpAddr |
A2A_ADDR, A2A_AGENT_URL (cmd/a2a) | a2a.httpAddr, a2a.agentUrl (empty: the in-cluster Service DNS, http://<release>-a2a.<ns>.svc.cluster.local:8093/) |
The chart refuses to render with an empty consumer group, broker list or database URL; durations accept a Go duration ("500ms") or plain seconds
("5"); terminationGracePeriodSeconds (40) must exceed drain delay + 10 s HTTP drain + 10 s workers drain.
Alerts and dashboards
See Observability for the full list. In short:
- Grafana dashboard uid
warehouse-linehaul-transportation(warehouse-infra): Kong rate/errors/latency, service HTTP RED, thelinehaul_loads_dispatched_totalbusiness row, Go runtime and Loki panels. - Alerts:
WarehouseKong5xxRatioHigh,WarehousePodRestartingand the four SLO burn-rate alerts (availability and latency, fast and slow). - Honest gap: the only business instrument is
linehaul.loads.dispatched. Outbox backlog, consumer lag and dead letters are not metered and not alerted: use the SQL and the log patterns below.
Failure modes
Pod crash-looping
- Symptom:
CrashLoopBackOffon theapiDeployment. - Diagnosis:
kubectl -n warehouse-systems logs deploy/linehaul-transportation --previous. The common causes are boot errors (every missing or invalid env var is named in the first log line,cmd/api/config.go), migrations failing the ~30 s retry window (Postgres not up, wrongMIGRATIONS_DATABASE_URL) or a consumer that cannot dial Kafka at start (worker stopped on its own). The startup probe on/healthzallows 2 s x 45 for the migration window (values.yaml). - Action: fix the chart value or the Secret, then restart (below).
Postgres unreachable
- Symptom:
/readyz503dependency-unavailablenamingpostgres; REST writes fail; ERRORoutbox relay pass failed; ERRORhorizon roll failed; consumers retry handling. - Action: nothing to drain service-side: the relay, the horizon roll and the consumers retry on their own intervals once Postgres answers; the outbox lives in Postgres, so nothing accepted could be lost.
Kafka unreachable
- Behaviour: REST writes are still accepted and queued; rows accumulate in
outbox_eventswithpublished_at IS NULLand publish in id order once the broker returns;/readyz503 nameskafka. The relay logsoutbox relay pass failedeach pass and retries everyOUTBOX_RELAY_INTERVAL. Consumers tolerate a stalled or reset connection (WARNbroker connection stalled or lost, capped backoff, up to 10 consecutive failures) but a refused connection is fatal for that consumer: the worker returns, the process logsworker stopped on its ownand exits, and Kubernetes restarts it until the broker is back. - Action: restore the broker and watch the outbox tail drain (SQL below). Ordinary outage rows need no re-arm; only rows that hit the attempt cap do.
Outbox relay stuck / a row parked at max attempts
Table outbox_events (columns that matter: published_at TIMESTAMPTZ, attempts INTEGER, last_error TEXT).
- Symptom: a missing event downstream while later events of the same aggregate arrived. A row whose send failed
OUTBOX_MAX_ATTEMPTS(20) times is excluded from future claims and logged ONCE at ERROR withoutbox_id,topic,event_type,key,attemptsand the last error. Later rows, same key included, overtake it: a gap, not a stall. - Diagnosis:
SELECT id, topic, event_type, key, attempts, last_error, created_at FROM outbox_events WHERE published_at IS NULL ORDER BY id; - Action: fix the cause (usually a broker blip or a topic still auto-creating), then re-arm:
UPDATE outbox_events SET attempts = 0 WHERE id = <id>;. A relay that logs nothing at all is a dead worker, which exits the pod; the pod-restart alert catches it.
Dead letters, consumer retries and lag
- ERROR
<consumer> handling failed; retrying the same message(withattempt,max_attempts5,retry_in,partition,offset) is a transient handler failure retried on the same message with capped backoff 200 ms to 5 s; after 5 attempts the message is dead-lettered to<consumed topic>.dlqwithx-dlq-source-topic,x-dlq-source-partition,x-dlq-source-offset,x-dlq-errorandx-dlq-failed-atheaders and the offset is committed. A message that can never be applied (undecodable data, a value the domain refuses) is dead-lettered at once (ERRORmessage can never be applied; dead-lettering it without retry). The dead-letter publish itself is retried forever, so a message is never dropped silently and the partition stays blocked while it cannot be dead-lettered. - Replay: inspect the
.dlqtopic, fix the cause, then produce the message value back to the source topic. It is idempotent:processed_eventsclaims on the CloudEvents id, in the same transaction as the effect. - WARN
skipping an event that can never be appliedandskipping a message that is not a valid CloudEventare deterministic and already committed past. - A consumed topic whose producer has not written yet is an empty topic (topics are auto-created by the first writer): zero lag on it is a normal early state. Per-consumer progress is observable only as the group's committed offsets plus these log lines.
- A lock whose pickup departure is unknown, closed, cancelled or full is not a failure: the booking moves to the next pickup from the same origin
(
BookingMoved) or fails (BookingFailed); the node never changes after lock (ADR 0002 A14).
Generation sweep not producing
- Symptom: departures stop appearing although lane schedules or vendor nodes exist.
- Diagnosis: every run logs
horizon rollwithscheduled/failed.failed > 0names the broken schedule or node in the following log lines.scheduled = 0withfailed = 0means the horizon is already full (a rerun schedules nothing new by design), no ACTIVE lane schedule (lane_schedules.state), no OPEN lane or known node (lane_views,node_views), or no pickup-eligible node; confirmGENERATE_INTERVALwas not stretched. - Action: fix the schedule or node named in the logs; the next roll picks it up. A restart forces an immediate pass (the roll also runs at boot).
Routine procedures
- Restart (picks up new env/Secret values and runs the boot horizon roll):
kubectl -n warehouse-systems rollout restart deploy/linehaul-transportation(and-mcp,-a2awhen enabled). - Logs: Grafana Loki,
app=linehaul-transportation(JSON slog;LOG_LEVEL=debugraises verbosity). Patterns:outbox relay pass failed,reached the attempt cap,handling failed; retrying,horizon roll,worker stopped on its own. - REST through the fleet gateway: Kong route
/api/linehaul-transportation(prefix stripped), for exampleGET /api/linehaul-transportation/healthzandGET /api/linehaul-transportation/departures. Optional routes:/api/mcp/linehaul-transportation(rewritten to/mcp) whenmcp.route.enabled,/api/a2a/linehaul-transportation(rewritten to/) whena2a.route.enabled. - Outbox backlog:
SELECT count(*), min(created_at) FROM outbox_events WHERE published_at IS NULL;(a small, recent tail is normal). - Idempotency keys older than 24 h are purged on the next keyed write; nothing to schedule.
- Rolling the chart:
helm lint charts/linehaul-transportation -f charts/linehaul-transportation/ci/default-values.yaml, thenbash charts/linehaul-transportation/tests/render_test.sh. Packaging and first-release checks: Before the first release.