forked from mtusi2026/mtusi2026lab4
- S3: SeaweedFS 4.51 (MinIO-образы недоступны), приватный bucket, отдельные ограниченные ключи API, служебные порты закрыты; идемпотентный s3-init с проверкой приватности и прогревом записи - вложения: потоковая загрузка PNG/JPEG, лимиты 10 MiB / 40 Мпикс, тип по содержимому, проверка filename, скачивание только участникам - согласованность PG/S3: pending -> PUT -> ready, компенсация, sweeper с grace и dry-run; attachment_ids в сообщениях, FK по чату - back-pressure на S3 PUT после найденного OOM SeaweedFS - тесты student-tests, evidence, REPORT.md, DEMO.md
243 lines
7.4 KiB
YAML
243 lines
7.4 KiB
YAML
name: messenger-lab4
|
|
|
|
x-s3-env: &s3-env
|
|
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
|
|
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
|
|
|
|
services:
|
|
postgres:
|
|
image: postgres:17.6-alpine3.22
|
|
environment:
|
|
POSTGRES_DB: messenger
|
|
POSTGRES_USER: messenger
|
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
|
secrets: [postgres_password]
|
|
volumes:
|
|
- pgdata:/var/lib/postgresql/data
|
|
networks: [data]
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
|
interval: 2s
|
|
timeout: 3s
|
|
retries: 30
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "1.0", memory: 512M}
|
|
stop_grace_period: 30s
|
|
restart: unless-stopped
|
|
|
|
valkey:
|
|
image: valkey/valkey:8.1.4-alpine3.22
|
|
command: ["valkey-server", "/run/secrets/valkey_conf"]
|
|
secrets: [valkey_conf]
|
|
networks: [data]
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "valkey-cli ping 2>&1 | grep -qE 'PONG|NOAUTH'"]
|
|
interval: 2s
|
|
timeout: 3s
|
|
retries: 30
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "0.25", memory: 96M}
|
|
restart: unless-stopped
|
|
|
|
s3:
|
|
image: chrislusf/seaweedfs:4.51
|
|
# master/volume/filer (and their web UIs) listen only on loopback inside the container;
|
|
# only the S3 gateway is reachable, and only on the internal data network. No published ports.
|
|
command:
|
|
- server
|
|
- -dir=/data
|
|
- -ip=127.0.0.1
|
|
- -ip.bind=127.0.0.1
|
|
- -s3
|
|
- -s3.ip.bind=0.0.0.0
|
|
- -s3.port=9000
|
|
- -s3.config=/run/secrets/seaweedfs_s3_config
|
|
- -master.volumeSizeLimitMB=1024
|
|
- -volume.max=0
|
|
# back-pressure instead of unbounded buffering of in-flight uploads
|
|
- -s3.concurrentUploadLimitMB=128
|
|
- -filer.concurrentUploadLimitMB=128
|
|
- -volume.concurrentUploadLimitMB=128
|
|
environment:
|
|
# Go GC soft limit below the cgroup limit; without it the heap grew to ~95% of 768M under 40 uploads
|
|
GOMEMLIMIT: 450MiB
|
|
secrets: [seaweedfs_s3_config]
|
|
volumes:
|
|
- s3data:/data
|
|
networks: [data]
|
|
healthcheck:
|
|
# /healthz alone turns green before the volume server re-registers after a restart (reads then hang),
|
|
# so also require a data node in the master topology (loopback-only endpoint inside the container).
|
|
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:9000/healthz && wget -q -O - http://127.0.0.1:9333/dir/status | grep -q '\"DataNodes\":\\[{'"]
|
|
interval: 2s
|
|
timeout: 3s
|
|
start_period: 5s
|
|
retries: 30
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "1.0", memory: 768M}
|
|
stop_grace_period: 20s
|
|
restart: unless-stopped
|
|
|
|
s3-init:
|
|
build: .
|
|
image: mtusi-messenger:lab4
|
|
command: ["python", "-m", "app.s3_bootstrap"]
|
|
environment:
|
|
<<: *s3-env
|
|
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
|
|
secrets: [s3_admin_access_key_id, s3_admin_secret_access_key]
|
|
networks: [data]
|
|
depends_on:
|
|
s3:
|
|
condition: service_healthy
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "0.25", memory: 128M}
|
|
restart: "no"
|
|
|
|
migrate:
|
|
build: .
|
|
image: mtusi-messenger:lab4
|
|
command: ["python", "-m", "app.migrate"]
|
|
environment:
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
secrets: [database_url]
|
|
networks: [data]
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "0.25", memory: 128M}
|
|
restart: "no"
|
|
|
|
api:
|
|
build: .
|
|
image: mtusi-messenger:lab4
|
|
environment:
|
|
<<: *s3-env
|
|
APP_HOST: 0.0.0.0
|
|
APP_PORT: "8080"
|
|
APP_ENV: ${APP_ENV:-lab}
|
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
|
SESSION_STORE: postgres
|
|
SESSION_CACHE_MAX_SECONDS: ${SESSION_CACHE_MAX_SECONDS:-300}
|
|
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
CACHE_URL_FILE: /run/secrets/cache_url
|
|
DB_POOL_MAX: ${DB_POOL_MAX:-10}
|
|
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
|
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
|
UPLOAD_CONCURRENCY: ${UPLOAD_CONCURRENCY:-16}
|
|
DECODE_CONCURRENCY: ${DECODE_CONCURRENCY:-1}
|
|
UPLOAD_SPOOL_BYTES: ${UPLOAD_SPOOL_BYTES:-1048576}
|
|
S3_PUT_CONCURRENCY: ${S3_PUT_CONCURRENCY:-4}
|
|
secrets: [database_url, cache_url, s3_access_key_id, s3_secret_access_key]
|
|
networks: [edge, data]
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
valkey:
|
|
condition: service_healthy
|
|
migrate:
|
|
condition: service_completed_successfully
|
|
s3-init:
|
|
condition: service_completed_successfully
|
|
healthcheck:
|
|
test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200 else 1)"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
start_period: 5s
|
|
retries: 3
|
|
deploy:
|
|
replicas: ${API_REPLICAS:-2}
|
|
resources:
|
|
limits: {cpus: "0.5", memory: 384M}
|
|
stop_signal: SIGTERM
|
|
stop_grace_period: 10s
|
|
restart: unless-stopped
|
|
|
|
sweeper:
|
|
build: .
|
|
image: mtusi-messenger:lab4
|
|
command: ["python", "-m", "app.sweeper"]
|
|
healthcheck:
|
|
# no HTTP server: healthy while a pass finished recently (one JSON line per pass in the logs)
|
|
test: ["CMD", "python", "-c", "import os,sys,time; sys.exit(0 if time.time()-os.path.getmtime('/tmp/sweeper-heartbeat') < 3*int(os.environ.get('SWEEP_INTERVAL_SECONDS','60'))+30 else 1)"]
|
|
interval: 10s
|
|
timeout: 3s
|
|
start_period: 10s
|
|
retries: 3
|
|
environment:
|
|
<<: *s3-env
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
ORPHAN_GRACE_SECONDS: ${ORPHAN_GRACE_SECONDS:-900}
|
|
SWEEP_INTERVAL_SECONDS: ${SWEEP_INTERVAL_SECONDS:-60}
|
|
secrets: [database_url, s3_access_key_id, s3_secret_access_key]
|
|
networks: [data]
|
|
depends_on:
|
|
migrate:
|
|
condition: service_completed_successfully
|
|
s3-init:
|
|
condition: service_completed_successfully
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "0.25", memory: 128M}
|
|
restart: unless-stopped
|
|
|
|
lb:
|
|
image: haproxy:3.2.4-alpine
|
|
volumes:
|
|
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
|
ports:
|
|
- "127.0.0.1:${APP_PORT:-8080}:8080"
|
|
networks: [edge]
|
|
depends_on:
|
|
api:
|
|
condition: service_started
|
|
healthcheck:
|
|
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8405/lb-health"]
|
|
interval: 5s
|
|
timeout: 2s
|
|
retries: 3
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "0.5", memory: 128M}
|
|
restart: unless-stopped
|
|
|
|
networks:
|
|
edge: {}
|
|
data:
|
|
internal: true
|
|
|
|
volumes:
|
|
pgdata:
|
|
s3data:
|
|
|
|
secrets:
|
|
postgres_password:
|
|
file: ./secrets/postgres_password
|
|
database_url:
|
|
file: ./secrets/database_url
|
|
cache_url:
|
|
file: ./secrets/cache_url
|
|
valkey_conf:
|
|
file: ./secrets/valkey.conf
|
|
s3_access_key_id:
|
|
file: ./secrets/s3_access_key_id
|
|
s3_secret_access_key:
|
|
file: ./secrets/s3_secret_access_key
|
|
s3_admin_access_key_id:
|
|
file: ./secrets/s3_admin_access_key_id
|
|
s3_admin_secret_access_key:
|
|
file: ./secrets/s3_admin_secret_access_key
|
|
seaweedfs_s3_config:
|
|
file: ./secrets/seaweedfs_s3.json
|