lab5: RabbitMQ, relay и worker в стенде; reconciler lab4 убран

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

broker: RabbitMQ 4.3.7 (тег+digest), quorum queue + DLX/DLQ из definitions, пользователи
relay (только publish в exchange) и worker (только consume), без management UI и портов.
relay: outbox -> publish с publisher confirm, reaper истёкших lease, страховка от потерянных
сообщений. worker: claim + heartbeat lease, варианты, публикация с fencing, ack после COMMIT,
graceful stop, уборка staging и мусора попыток. S3: два bucket и четыре identity
(admin, api, worker, auditor) с проверкой модели доступа в s3-init. Отдельная сеть queue.
Reconciler lab4 (трек прошлой лабы) удалён: его роль выполняет уборка по журналу попыток.
This commit is contained in:
Alexey Bodanyuk
2026-10-31 16:12:52 +03:00
parent 655f264104
commit bc32a50967
15 changed files with 1058 additions and 141 deletions
+225 -42
View File
@@ -1,15 +1,20 @@
# Lab 4 stand: client -> HAProxy (lb) -> API x N (one scalable service `api`)
# -> PostgreSQL (metadata, source of truth) + Valkey (sessions) + SeaweedFS S3 (bytes).
# Lab 5 stand: client -> HAProxy (lb) -> API x N (scalable service `api`)
# -> PostgreSQL (metadata, jobs, outbox: source of truth) + Valkey (sessions)
# + SeaweedFS S3 (staging bucket and final bucket, both private)
# relay : outbox in PostgreSQL -> RabbitMQ (publisher confirms) [service `relay`]
# broker : RabbitMQ 4.3, durable quorum queue + dead-letter queue [service `broker`]
# worker : RabbitMQ -> variants -> final bucket + PostgreSQL, then ack [service `worker`]
# From a clean clone (details and Windows notes: REPORT.md, docs/WINDOWS.md):
# python scripts/init_secrets.py # once: random local credentials into ./secrets (git-ignored)
# cp .env.example .env # optional overrides (API_REPLICAS, limits, ...)
# docker compose --env-file .env up -d --build --wait
# Order: postgres/valkey/s3 healthy -> migrate + s3-init (ONE run each, exit 0) -> api healthy -> lb.
# Order: postgres/valkey/s3/broker healthy -> migrate + s3-init (ONE run each, exit 0)
# -> api healthy -> lb; relay and worker start after migrate/s3-init.
# Images are pinned by version AND digest. Passwords/keys are passed only as secret files, and
# every service gets only the secrets it needs. Only the LB publishes ports (loopback only).
# Three networks: `edge` (lb <-> api), `data` (api/migrate/reconciler <-> postgres/valkey) and
# `storage` (api/s3-init/reconciler <-> s3); data and storage are internal (no route outside).
name: mtusi-lab4
# Networks: `edge` (lb <-> api); internal (no route outside): `data` (postgres/valkey),
# `storage` (s3), `queue` (broker). The API is NOT on `queue`: it never talks to the broker.
name: mtusi-lab5
x-api-env: &api-env
APP_ENV: ${APP_ENV:-lab}
@@ -29,10 +34,11 @@ x-api-env: &api-env
SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-8}
DOCS_ENABLED: ${DOCS_ENABLED:-true}
MESSAGE_WRITE_PATH: ${MESSAGE_WRITE_PATH:-function}
# Lab 4: S3 (the API's own Read+Write key pair, see infra/seaweedfs/start.sh) and upload limits.
# S3: the API's own key pair (Write staging + Read final, see infra/seaweedfs/start.sh).
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_STAGING_BUCKET: ${S3_STAGING_BUCKET:-messenger-staging}
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS:-3}
@@ -46,8 +52,33 @@ x-api-env: &api-env
JPEG_MAX_MEMORY_MB: ${JPEG_MAX_MEMORY_MB:-256}
PENDING_GRACE_SECONDS: ${PENDING_GRACE_SECONDS:-900}
JANITOR_INTERVAL_SECONDS: ${JANITOR_INTERVAL_SECONDS:-60}
# Level 4 failure experiments only (compose.faults.yaml); empty = no injected failures.
FAULT_POINTS: ${FAULT_POINTS:-}
# Lab 5: pipeline version and attempt budget of the jobs created by uploads.
PIPELINE_VERSION: ${PIPELINE_VERSION:-1}
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
# Failure experiments only (student-tests, bench/); empty = no injected failures.
FAULT_POINTS: ${API_FAULT_POINTS:-}
# Settings of the pipeline shared by relay and worker (docs/PIPELINE.md).
x-pipeline-env: &pipeline-env
APP_ENV: ${APP_ENV:-lab}
LOG_LEVEL: ${LOG_LEVEL:-info}
DATABASE_URL_FILE: /run/secrets/database_url
BROKER_URL_FILE: /run/secrets/broker_url
DB_POOL_MAX: ${PIPELINE_DB_POOL_MAX:-6}
DB_TIMEOUT_SECONDS: ${DB_TIMEOUT_SECONDS:-3}
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_STAGING_BUCKET: ${S3_STAGING_BUCKET:-messenger-staging}
S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS:-3}
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
JOB_LEASE_SECONDS: ${JOB_LEASE_SECONDS:-30}
JOB_RETRY_BASE_SECONDS: ${JOB_RETRY_BASE_SECONDS:-2}
JOB_RETRY_MAX_SECONDS: ${JOB_RETRY_MAX_SECONDS:-60}
QUEUED_REPUBLISH_SECONDS: ${QUEUED_REPUBLISH_SECONDS:-300}
services:
postgres:
@@ -102,15 +133,15 @@ services:
resources:
limits: {cpus: "${VALKEY_CPUS:-0.5}", memory: 256m}
# Lab 4: S3-compatible object storage (SeaweedFS: master + volume + filer + S3 gateway in one
# process). Bytes of the attachments live ONLY here, on the named volume s3data. Identities
# and exposure: infra/seaweedfs/start.sh. Not published on the host; its web UIs listen on
# 127.0.0.1 inside the container only.
# S3-compatible object storage (SeaweedFS: master + volume + filer + S3 gateway in one process).
# Bytes live ONLY here, on the named volume s3data: staging bucket (originals accepted before
# 202) and final bucket (published originals and variants). Identities: infra/seaweedfs/start.sh.
s3:
image: chrislusf/seaweedfs:4.46@sha256:08d516132314207d10c8e37cbffc1f32b147d870169688734cc61c6231625b62
entrypoint: ["/bin/sh", "/etc/seaweedfs/start.sh"]
environment:
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_STAGING_BUCKET: ${S3_STAGING_BUCKET:-messenger-staging}
# Go heap target below the container limit: the GC returns memory before the cgroup OOM.
GOMEMLIMIT: ${S3_GOMEMLIMIT:-600MiB}
secrets:
@@ -118,8 +149,10 @@ services:
- s3_admin_secret_access_key
- s3_api_access_key_id
- s3_api_secret_access_key
- s3_reconciler_access_key_id
- s3_reconciler_secret_access_key
- s3_worker_access_key_id
- s3_worker_secret_access_key
- s3_auditor_access_key_id
- s3_auditor_secret_access_key
volumes:
- ./infra/seaweedfs/start.sh:/etc/seaweedfs/start.sh:ro
- s3data:/data
@@ -137,6 +170,34 @@ services:
resources:
limits: {cpus: "${S3_CPUS:-1.0}", memory: "${S3_MEMORY:-1g}"}
# Lab 5: RabbitMQ. Topology (exchange, quorum queue, DLX/DLQ) and users come from definition
# files imported at boot (infra/rabbitmq, secrets/rabbitmq_users.json). Messages of the quorum
# queue live on the named volume rabbitmqdata. No management UI, no host port.
broker:
image: rabbitmq:4.3.7-alpine@sha256:b1f997f74103d08fc1f20a13890dc44fe6cfa51d528cf521b480ada988d3ffa5
hostname: broker # node name rabbit@broker: stable across container re-creation
secrets:
- source: rabbitmq_users
target: /etc/rabbitmq/definitions.d/users.json
volumes:
- ./infra/rabbitmq/rabbitmq.conf:/etc/rabbitmq/conf.d/20-lab5.conf:ro
- ./infra/rabbitmq/enabled_plugins:/etc/rabbitmq/enabled_plugins:ro
- ./infra/rabbitmq/topology.json:/etc/rabbitmq/definitions.d/topology.json:ro
- rabbitmqdata:/var/lib/rabbitmq
healthcheck:
# The node runs and the AMQP listener accepts connections.
test: ["CMD-SHELL", "rabbitmq-diagnostics -q ping && rabbitmq-diagnostics -q check_port_connectivity"]
interval: 5s
timeout: 10s
retries: 30
start_period: 20s
stop_grace_period: 30s
restart: unless-stopped
networks: [queue]
deploy:
resources:
limits: {cpus: "${BROKER_CPUS:-1.0}", memory: "${BROKER_MEMORY:-512m}"}
# One-shot schema migration: runs ONCE per `up`, before any API replica starts
# (api depends_on service_completed_successfully). Replicas never migrate themselves.
migrate:
@@ -144,7 +205,7 @@ services:
context: .
args:
BUILD_REVISION: ${BUILD_REVISION:-dev}
image: ${API_IMAGE:-mtusi-messenger:lab4}
image: ${API_IMAGE:-mtusi-messenger:lab5}
command: ["python", "-m", "messenger.migrate"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
@@ -160,28 +221,33 @@ services:
resources:
limits: {cpus: "0.5", memory: 256m}
# One-shot, idempotent S3 bootstrap: bucket, sentinel object, verification of the access model
# (private bucket, least-privilege keys). The ONLY holder of the admin key pair besides s3.
# One-shot, idempotent S3 bootstrap: both buckets, sentinel object, verification of the access
# model (private buckets, least-privilege keys). The ONLY holder of the admin key pair besides s3.
s3-init:
image: ${API_IMAGE:-mtusi-messenger:lab4}
image: ${API_IMAGE:-mtusi-messenger:lab5}
command: ["python", "-m", "messenger.s3_bootstrap"]
environment:
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_STAGING_BUCKET: ${S3_STAGING_BUCKET:-messenger-staging}
S3_ADMIN_ACCESS_KEY_ID_FILE: /run/secrets/s3_admin_access_key_id
S3_ADMIN_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_admin_secret_access_key
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_api_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_api_secret_access_key
S3_RECONCILER_ACCESS_KEY_ID_FILE: /run/secrets/s3_reconciler_access_key_id
S3_RECONCILER_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_reconciler_secret_access_key
S3_WORKER_ACCESS_KEY_ID_FILE: /run/secrets/s3_worker_access_key_id
S3_WORKER_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_worker_secret_access_key
S3_AUDITOR_ACCESS_KEY_ID_FILE: /run/secrets/s3_auditor_access_key_id
S3_AUDITOR_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_auditor_secret_access_key
secrets:
- s3_admin_access_key_id
- s3_admin_secret_access_key
- s3_api_access_key_id
- s3_api_secret_access_key
- s3_reconciler_access_key_id
- s3_reconciler_secret_access_key
- s3_worker_access_key_id
- s3_worker_secret_access_key
- s3_auditor_access_key_id
- s3_auditor_secret_access_key
depends_on:
s3:
condition: service_healthy
@@ -200,7 +266,7 @@ services:
context: .
args:
BUILD_REVISION: ${BUILD_REVISION:-dev}
image: ${API_IMAGE:-mtusi-messenger:lab4}
image: ${API_IMAGE:-mtusi-messenger:lab5}
environment: *api-env
secrets:
- database_url
@@ -236,7 +302,7 @@ services:
replicas: ${API_REPLICAS:-2}
resources:
# 512m: UPLOAD_CONCURRENCY (4) x 10 MiB bodies + one djpeg (<= JPEG_MAX_MEMORY_MB) + runtime;
# measured in evidence/memory/ (bench/upload_memory.py).
# measured in lab 4 (bench/upload_memory.py). No resizing happens in the API.
limits: {cpus: "${API_CPUS:-1.0}", memory: "${API_MEMORY:-512m}"}
# No swap on top of the limit (Docker's default would allow as much swap again): the RAM bound
# of the experiments is a real bound, and running out of it is visible, not hidden in swap.
@@ -268,29 +334,112 @@ services:
resources:
limits: {cpus: "${LB_CPUS:-1.0}", memory: 128m}
# Track 2 (lab 4): reconciler of S3 objects vs PostgreSQL metadata. Not a daemon: started on
# demand, dry-run by default (read-only on both stores):
# docker compose run --rm reconciler # report only
# docker compose run --rm reconciler --apply # fix: orphans, stale intents, quarantine
reconciler:
# Lab 5: outbox relay + lease reaper (src/messenger/relay.py). One process; a second one would
# be safe (SKIP LOCKED) but is not needed. Database + broker only, no S3 keys.
relay:
image: ${API_IMAGE:-mtusi-messenger:lab5}
command: ["python", "-m", "messenger.relay"]
environment:
<<: *pipeline-env
RELAY_POLL_SECONDS: ${RELAY_POLL_SECONDS:-0.5}
FAULT_POINTS: ${RELAY_FAULT_POINTS:-}
secrets:
- database_url
- source: broker_url_relay
target: broker_url
healthcheck:
test: ["CMD", "python", "-m", "messenger.healthcheck", "--file", "/tmp/heartbeat", "--max-age", "30"]
interval: 5s
timeout: 3s
retries: 3
start_period: 10s
depends_on:
postgres:
condition: service_healthy
broker:
condition: service_healthy
migrate:
condition: service_completed_successfully
stop_grace_period: 10s
restart: unless-stopped
networks: [data, queue]
deploy:
resources:
limits: {cpus: "${RELAY_CPUS:-0.5}", memory: 256m}
# Lab 5: image worker (src/messenger/worker.py). Scalable (`--scale worker=2`): competing
# consumers of one queue; the database lease decides who works on a job.
worker:
image: ${API_IMAGE:-mtusi-messenger:lab5}
command: ["python", "-m", "messenger.worker"]
environment:
<<: *pipeline-env
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-2}
WORKER_SHUTDOWN_SECONDS: ${WORKER_SHUTDOWN_SECONDS:-20}
IMAGE_MAX_MEMORY_MB: ${IMAGE_MAX_MEMORY_MB:-768}
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400}
ORPHAN_GRACE_SECONDS: ${ORPHAN_GRACE_SECONDS:-600}
CLEANUP_INTERVAL_SECONDS: ${CLEANUP_INTERVAL_SECONDS:-30}
FAULT_POINTS: ${WORKER_FAULT_POINTS:-}
secrets:
- database_url
- source: broker_url_worker
target: broker_url
- source: s3_worker_access_key_id
target: s3_access_key_id
- source: s3_worker_secret_access_key
target: s3_secret_access_key
healthcheck:
test: ["CMD", "python", "-m", "messenger.healthcheck", "--file", "/tmp/heartbeat", "--max-age", "30"]
interval: 5s
timeout: 3s
retries: 3
start_period: 10s
depends_on:
postgres:
condition: service_healthy
broker:
condition: service_healthy
s3:
condition: service_healthy
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
stop_grace_period: 30s # > WORKER_SHUTDOWN_SECONDS (20 s) + release
restart: unless-stopped
networks: [data, storage, queue]
deploy:
replicas: ${WORKER_REPLICAS:-1}
resources:
# WORKER_CONCURRENCY (2) child processes x IMAGE_MAX_MEMORY_MB (768) address space + worker.
limits: {cpus: "${WORKER_CPUS:-2.0}", memory: "${WORKER_MEMORY:-1536m}"}
memswap_limit: ${WORKER_MEMORY:-1536m}
# Track 2: read-only checker of the pipeline invariants (database in read-only transactions,
# S3 auditor key: Read + List, no Write). On demand: docker compose run --rm auditor
auditor:
profiles: ["tools"]
image: ${API_IMAGE:-mtusi-messenger:lab4}
entrypoint: ["python", "-m", "messenger.reconcile"]
image: ${API_IMAGE:-mtusi-messenger:lab5}
entrypoint: ["python", "-m", "messenger.audit"]
command: []
environment:
DATABASE_URL_FILE: /run/secrets/database_url
DB_TIMEOUT_SECONDS: ${RECONCILE_DB_TIMEOUT_SECONDS:-10}
DB_TIMEOUT_SECONDS: ${AUDIT_DB_TIMEOUT_SECONDS:-30}
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_STAGING_BUCKET: ${S3_STAGING_BUCKET:-messenger-staging}
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS:-3}
secrets:
- database_url
- source: s3_reconciler_access_key_id
- source: s3_auditor_access_key_id
target: s3_access_key_id
- source: s3_reconciler_secret_access_key
- source: s3_auditor_secret_access_key
target: s3_secret_access_key
depends_on:
postgres:
@@ -301,19 +450,43 @@ services:
networks: [data, storage]
deploy:
resources:
limits: {cpus: "${RECONCILER_CPUS:-1.0}", memory: 256m}
limits: {cpus: "1.0", memory: 256m}
# Operator CLI (replay of failed jobs, reprocess with a newer pipeline, failed registry):
# docker compose run --rm jobctl list-failed
# docker compose run --rm jobctl replay <attachment_id> --operator alice --reason "..."
jobctl:
profiles: ["tools"]
image: ${API_IMAGE:-mtusi-messenger:lab5}
entrypoint: ["python", "-m", "messenger.jobctl"]
command: []
environment:
DATABASE_URL_FILE: /run/secrets/database_url
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
secrets: [database_url]
depends_on:
postgres:
condition: service_healthy
restart: "no"
networks: [data]
deploy:
resources:
limits: {cpus: "0.5", memory: 128m}
networks:
edge:
data:
internal: true # no route outside: stores talk only to api/migrate
internal: true # no route outside: stores talk only to the services above
storage:
internal: true # S3 API reachable only by api, s3-init, reconciler
internal: true # S3 API reachable only by api, s3-init, worker, auditor
queue:
internal: true # broker reachable only by relay and worker
volumes:
pgdata:
valkeydata:
s3data:
rabbitmqdata:
secrets:
postgres_password:
@@ -332,7 +505,17 @@ secrets:
file: ./secrets/s3_api_access_key_id
s3_api_secret_access_key:
file: ./secrets/s3_api_secret_access_key
s3_reconciler_access_key_id:
file: ./secrets/s3_reconciler_access_key_id
s3_reconciler_secret_access_key:
file: ./secrets/s3_reconciler_secret_access_key
s3_worker_access_key_id:
file: ./secrets/s3_worker_access_key_id
s3_worker_secret_access_key:
file: ./secrets/s3_worker_secret_access_key
s3_auditor_access_key_id:
file: ./secrets/s3_auditor_access_key_id
s3_auditor_secret_access_key:
file: ./secrets/s3_auditor_secret_access_key
rabbitmq_users:
file: ./secrets/rabbitmq_users.json
broker_url_relay:
file: ./secrets/broker_url_relay
broker_url_worker:
file: ./secrets/broker_url_worker