forked from mtusi2026/mtusi2026lab4
lab4: вложения в приватном S3 (SeaweedFS), метаданные в PostgreSQL
- compose: сервис s3 (SeaweedFS 4.46 по digest, volume s3data, внутренняя сеть storage,
web-интерфейсы только на 127.0.0.1 внутри контейнера), одноразовый s3-init;
- три пары ключей S3: admin (только bootstrap), api (Read+Write своего bucket),
reconciler (+List); bootstrap идемпотентен и проверяет приватность и права;
- миграция 0004: attachments (pending -> ready, случайный object_key), message_attachments,
post_message_v2 с проверкой вложений в той же транзакции;
- POST /chats/{id}/attachments: лимиты до чтения/декодирования, потоковая проверка PNG
(CRC + zlib до точного размера), JPEG через djpeg -strict, семафор загрузок;
- GET метаданных и /content (стриминг из S3, без redirect), readiness с S3,
HAProxy: деградированный режим без S3 через /health/ready/core;
- janitor незавершённых загрузок с grace period.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d916d09f7b
commit
335df4b2b2
+162
-14
@@ -1,14 +1,15 @@
|
||||
# Lab 3 stand: client -> HAProxy (lb) -> API x N (one scalable service `api`) -> PostgreSQL + Valkey.
|
||||
# Lab 4 stand: client -> HAProxy (lb) -> API x N (one scalable service `api`)
|
||||
# -> PostgreSQL (metadata, source of truth) + Valkey (sessions) + SeaweedFS S3 (bytes).
|
||||
# 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 healthy -> migrate (ONE run, exits 0) -> api replicas healthy -> lb.
|
||||
# Images are pinned by version AND digest. Passwords are passed only as secret files, and
|
||||
# Order: postgres/valkey/s3 healthy -> migrate + s3-init (ONE run each, exit 0) -> api healthy -> lb.
|
||||
# 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).
|
||||
# Two networks: `edge` (lb <-> api) and `data` (api/migrate <-> postgres/valkey, internal):
|
||||
# the LB cannot reach the stores, and the stores are not reachable from the edge.
|
||||
name: mtusi-lab3
|
||||
# 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
|
||||
|
||||
x-api-env: &api-env
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
@@ -27,8 +28,25 @@ x-api-env: &api-env
|
||||
DRAIN_SECONDS: ${DRAIN_SECONDS:-3}
|
||||
SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-8}
|
||||
DOCS_ENABLED: ${DOCS_ENABLED:-true}
|
||||
# Track 1 improvement: `function` = one SQL call per send (default), `multi` = old path (A/B).
|
||||
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_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
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}
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
UPLOAD_CONCURRENCY: ${UPLOAD_CONCURRENCY:-4}
|
||||
UPLOAD_QUEUE_TIMEOUT_SECONDS: ${UPLOAD_QUEUE_TIMEOUT_SECONDS:-5}
|
||||
UPLOAD_READ_TIMEOUT_SECONDS: ${UPLOAD_READ_TIMEOUT_SECONDS:-30}
|
||||
IMAGE_VERIFY_TIMEOUT_SECONDS: ${IMAGE_VERIFY_TIMEOUT_SECONDS:-10}
|
||||
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:-}
|
||||
|
||||
services:
|
||||
postgres:
|
||||
@@ -37,7 +55,7 @@ services:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
# pg_stat_statements: per-statement counters for the track 5 experiments (bench/track1).
|
||||
# pg_stat_statements: per-statement counters for the experiments.
|
||||
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "max_connections=${PG_MAX_CONNECTIONS:-100}"]
|
||||
secrets: [postgres_password]
|
||||
volumes:
|
||||
@@ -83,6 +101,39 @@ 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:
|
||||
image: chrislusf/seaweedfs:4.46@sha256:08d516132314207d10c8e37cbffc1f32b147d870169688734cc61c6231625b62
|
||||
entrypoint: ["/bin/sh", "/etc/seaweedfs/start.sh"]
|
||||
environment:
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
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
|
||||
volumes:
|
||||
- ./infra/seaweedfs/start.sh:/etc/seaweedfs/start.sh:ro
|
||||
- s3data:/data
|
||||
healthcheck:
|
||||
# Unauthenticated health endpoint of the S3 gateway (200 once it serves requests).
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8333/healthz"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
start_period: 10s
|
||||
stop_grace_period: 30s
|
||||
restart: unless-stopped
|
||||
networks: [storage]
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: "${S3_CPUS:-1.0}", memory: "${S3_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:
|
||||
@@ -90,7 +141,7 @@ services:
|
||||
context: .
|
||||
args:
|
||||
BUILD_REVISION: ${BUILD_REVISION:-dev}
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab3}
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
command: ["python", "-m", "messenger.migrate"]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
@@ -106,14 +157,55 @@ 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.
|
||||
s3-init:
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
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_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
|
||||
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
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
migrate:
|
||||
condition: service_completed_successfully # same image: built once by `migrate`
|
||||
healthcheck:
|
||||
disable: true
|
||||
restart: "no"
|
||||
networks: [storage]
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: "0.5", memory: 256m}
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
BUILD_REVISION: ${BUILD_REVISION:-dev}
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab3}
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
environment: *api-env
|
||||
secrets: [database_url, cache_url]
|
||||
secrets:
|
||||
- database_url
|
||||
- cache_url
|
||||
- source: s3_api_access_key_id
|
||||
target: s3_access_key_id
|
||||
- source: s3_api_secret_access_key
|
||||
target: s3_secret_access_key
|
||||
# No `ports` and no container_name: replicas are reachable only through `lb`,
|
||||
# and `--scale api=N` / deploy.replicas works. X-Instance-Id = container hostname.
|
||||
expose: ["8080"]
|
||||
@@ -128,15 +220,21 @@ services:
|
||||
condition: service_healthy
|
||||
valkey:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
condition: service_healthy
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
stop_grace_period: 15s # > DRAIN_SECONDS + SHUTDOWN_TIMEOUT_SECONDS (11 s)
|
||||
restart: unless-stopped
|
||||
networks: [edge, data]
|
||||
networks: [edge, data, storage]
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits: {cpus: "${API_CPUS:-1.0}", memory: "${API_MEMORY:-256m}"}
|
||||
# 512m: UPLOAD_CONCURRENCY (4) x 10 MiB bodies + one djpeg (<= JPEG_MAX_MEMORY_MB) + runtime;
|
||||
# measured in evidence/memory/ (bench/upload_memory.py).
|
||||
limits: {cpus: "${API_CPUS:-1.0}", memory: "${API_MEMORY:-512m}"}
|
||||
|
||||
lb:
|
||||
image: haproxy:3.2.26-alpine@sha256:254e6370f01cd654e0744edf607c67f33777c1b6a9dc0d4b28bcba7171460122
|
||||
@@ -145,7 +243,7 @@ services:
|
||||
ports:
|
||||
# The ONLY public entry point, on the loopback interfaces only. Both IPv4 and IPv6:
|
||||
# on Windows `localhost` resolves to ::1 first, and a refused ::1 connection costs ~2 s
|
||||
# per request before the fallback to 127.0.0.1 (measured: 2.0 s vs 0.007 s).
|
||||
# per request before the fallback to 127.0.0.1 (measured in lab 3: 2.0 s vs 0.007 s).
|
||||
- "127.0.0.1:${APP_PUBLISH_PORT:-8080}:8080"
|
||||
- "[::1]:${APP_PUBLISH_PORT:-8080}:8080"
|
||||
- "127.0.0.1:${LB_STATS_PORT:-8404}:8404" # local status page (UP/DOWN of replicas)
|
||||
@@ -164,14 +262,52 @@ 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:
|
||||
profiles: ["tools"]
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
entrypoint: ["python", "-m", "messenger.reconcile"]
|
||||
command: []
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
DB_TIMEOUT_SECONDS: ${RECONCILE_DB_TIMEOUT_SECONDS:-10}
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
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
|
||||
target: s3_access_key_id
|
||||
- source: s3_reconciler_secret_access_key
|
||||
target: s3_secret_access_key
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
networks: [data, storage]
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: "${RECONCILER_CPUS:-1.0}", memory: 256m}
|
||||
|
||||
networks:
|
||||
edge:
|
||||
data:
|
||||
internal: true # no route outside: stores talk only to api/migrate
|
||||
storage:
|
||||
internal: true # S3 API reachable only by api, s3-init, reconciler
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
valkeydata:
|
||||
s3data:
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
@@ -182,3 +318,15 @@ secrets:
|
||||
file: ./secrets/database_url
|
||||
cache_url:
|
||||
file: ./secrets/cache_url
|
||||
s3_admin_access_key_id:
|
||||
file: ./secrets/s3_admin_access_key_id
|
||||
s3_admin_secret_access_key:
|
||||
file: ./secrets/s3_admin_secret_access_key
|
||||
s3_api_access_key_id:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user