# 2026-10-07 16:03:13 UTC, python 3.14.7, win32
$ docker version --format docker {{.Server.Version}}
# docker 29.8.1
== Stack: services, pinned images, health, non-root, Valkey persistence ==
$ docker compose ps -a --format table {{.Service}}	{{.Image}}	{{.Status}}
SERVICE    IMAGE                                                                                                STATUS
api        mtusi-messenger:lab2                                                                                 Up 58 seconds (healthy)
migrate    mtusi-messenger:lab2                                                                                 Exited (0) 59 seconds ago
postgres   postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336         Up About a minute (healthy)
valkey     valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2   Up About a minute (healthy)
$ docker compose config --format json
=> PASS: third-party images are pinned by tag and digest (postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336, valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2)

health: api=healthy, postgres=healthy, valkey=healthy
=> PASS: api, postgres and valkey are healthy

$ docker compose exec -T api id
uid=10001(app) gid=10001(app) groups=10001(app)
=> PASS: API process does not run as root

$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET appendonly
appendonly
yes
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET appendfsync
appendfsync
always
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET maxmemory-policy
maxmemory-policy
noeviction
=> PASS: Valkey persistence is AOF with fsync always, no eviction

== Migrations: schema, constraints, idempotent re-run ==
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT format('%s %s %s', conrelid::regclass, contype, conname) FROM pg_constraint WHERE conrelid::regclass::text IN ('users', 'chats', 'chat_members', 'messages') ORDER BY 1
chat_members f chat_members_chat_id_fkey
chat_members f chat_members_user_id_fkey
chat_members n chat_members_chat_id_not_null
chat_members n chat_members_position_not_null
chat_members n chat_members_user_id_not_null
chat_members p chat_members_pkey
chat_members u chat_members_position_key
chats c chats_title_length
chats n chats_created_at_not_null
chats n chats_id_not_null
chats n chats_title_not_null
chats p chats_pkey
messages c messages_text_length
messages f messages_sender_is_member
messages n messages_chat_id_not_null
messages n messages_created_at_not_null
messages n messages_id_not_null
messages n messages_sender_id_not_null
messages n messages_text_not_null
messages p messages_pkey
users c users_display_name_length
users c users_username_format
users n users_created_at_not_null
users n users_display_name_not_null
users n users_id_not_null
users n users_password_hash_not_null
users n users_username_not_null
users p users_pkey
users u users_username_key
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT indexrelid::regclass FROM pg_index WHERE indrelid::regclass::text IN ('chat_members', 'messages') ORDER BY 1
chat_members_pkey
chat_members_position_key
chat_members_user_id_idx
messages_pkey
messages_chat_history_idx
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT (SELECT count(*) FROM schema_migrations) || ' ' || (SELECT count(*) FROM users)
2 62
$ docker compose run --rm --no-deps migrate
Container mtusi-lab2-migrate-run-a18e9389d033 Creating 
 Container mtusi-lab2-migrate-run-a18e9389d033 Created 
INFO event=migrations_done applied_now=0 total=2
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT (SELECT count(*) FROM schema_migrations) || ' ' || (SELECT count(*) FROM users)
2 62
schema_migrations and users rows before/after: 2 62 / 2 62
=> PASS: re-running migrations applies nothing and duplicates nothing

== Rollback: chat creation fails on the second INSERT ==
POST /api/v1/chats with one unknown member                 -> 400 invalid_request in 0.01 s
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM chats WHERE title = 'rollback-probe-2654a5871a'
0
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM chats c WHERE NOT EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id = c.id)
0
$ docker compose logs --no-log-prefix --tail 20 api
rows in chats with this title: 0; chats without members in the whole table: 0; rollback logged: True
=> PASS: composite chat creation is rolled back as a whole

== Registration race: 20 parallel requests with one username ==
statuses: 201 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM users WHERE username = 'race_cf2989174348'
1
rows in users with this username: 1
=> PASS: exactly one registration wins, the other 19 get 409

== Connection pool: 60 parallel clients, at most DB_POOL_SIZE=10 connections to PostgreSQL ==
requests: 1019, statuses: {200: 1019}
API connections in pg_stat_activity, 11 samples: max 10
=> PASS: the pool never opens more than 10 connections and all requests succeed

== Timeouts: chat row locked for 6 s by another session, API answers 503 after lock_timeout=3s ==
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c "BEGIN; SELECT 1 FROM chats WHERE id = '39c32dc4-57b4-4e7d-bd72-0a66c79c4681' FOR UPDATE; SELECT pg_sleep(6); ROLLBACK;"   (in the background)
POST message while the chat row is locked                  -> 503 unavailable in 3.01 s
POST message after the lock is released                    -> 201  in 0.01 s
=> PASS: waiting for a lock is bounded: 503 after about 3 s, then writes work again (3.01 s)

== Logs: useful fields, no message texts, passwords or tokens ==
$ docker compose logs --no-log-prefix api
$ docker compose logs --no-log-prefix --tail 3 api
2026-10-07T16:03:31.696Z INFO request_id=0bfad325574345cb84467a9bf688cd43 method=POST path=/api/v1/auth/sessions status=201 duration_ms=24.2 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
2026-10-07T16:03:31.723Z INFO request_id=4e28cb5a57d14d9fb0b0839c99a4c452 method=POST path=/api/v1/chats status=201 duration_ms=4.3 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
2026-10-07T16:03:31.754Z INFO request_id=52dc2bcc6bc248c5b5a581e8f405b0bf method=POST path=/api/v1/chats/87f1c2f5-e97b-4bfd-b4b7-26bb0a60d62c/messages status=201 duration_ms=5.3 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
searched 1834 log lines; found: nothing
=> PASS: logs contain request_id/status/duration and no secrets

== Session expiry: second API instance with SESSION_TTL_SECONDS=2 ==
$ docker compose --profile expiry up -d api-short-ttl
Container mtusi-lab2-postgres-1 Running 
 Container mtusi-lab2-valkey-1 Running 
 Container mtusi-lab2-api-short-ttl-1 Creating 
 Container mtusi-lab2-api-short-ttl-1 Created 
 Container mtusi-lab2-postgres-1 Waiting 
 Container mtusi-lab2-postgres-1 Healthy 
 Container mtusi-lab2-migrate-1 Starting 
 Container mtusi-lab2-migrate-1 Started 
 Container mtusi-lab2-migrate-1 Waiting 
 Container mtusi-lab2-postgres-1 Waiting 
 Container mtusi-lab2-valkey-1 Waiting 
 Container mtusi-lab2-postgres-1 Healthy 
 Container mtusi-lab2-valkey-1 Healthy 
 Container mtusi-lab2-migrate-1 Exited 
 Container mtusi-lab2-api-short-ttl-1 Starting 
 Container mtusi-lab2-api-short-ttl-1 Started
GET /users/me right after login                            -> 200  in 0.02 s
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" PTTL session:abfd1b21ab7e148541270050cbe792d4a26a2b73815f9487c01330eb5080b958
1767
PTTL of the session key in Valkey (ms): 1767
GET /users/me 3 s later                                    -> 401 unauthorized in 0.02 s
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" EXISTS session:abfd1b21ab7e148541270050cbe792d4a26a2b73815f9487c01330eb5080b958
0
session key still exists in Valkey: 0
=> PASS: session expires after SESSION_TTL_SECONDS and the key disappears from Valkey

$ docker compose --profile expiry rm -sf api-short-ttl
== Valkey is down: bounded 503, no anonymous access, recovery with the same token ==
$ docker compose stop valkey
Container mtusi-lab2-valkey-1 Stopping 
 Container mtusi-lab2-valkey-1 Stopped
GET /users/me with a valid token                           -> 503 unavailable in 1.02 s
POST /auth/sessions with valid credentials                 -> 503 unavailable in 1.03 s
GET /users/me without a token                              -> 401 unauthorized in 0.00 s
POST /users (needs only PostgreSQL)                        -> 201  in 0.04 s
GET /health/ready                                          -> 503 unavailable in 1.03 s
GET /health/live                                           -> 200  in 0.00 s
slowest of 3 more authenticated requests: 1.04 s
$ docker compose start valkey
Container mtusi-lab2-valkey-1 Starting 
 Container mtusi-lab2-valkey-1 Started
same token after Valkey is back: 200
=> PASS: without Valkey: 503 for sessions, 401 for anonymous, registration still works

=> PASS: after Valkey restarts the session is still valid (AOF)

== PostgreSQL is down: bounded 503, recovery without restarting the API ==
$ docker compose stop postgres
Container mtusi-lab2-postgres-1 Stopping 
 Container mtusi-lab2-postgres-1 Stopped
GET /users/me with a valid token                           -> 503 unavailable in 3.01 s
POST /chats                                                -> 503 unavailable in 3.01 s
GET /users/me without a token                              -> 401 unauthorized in 0.02 s
GET /health/ready                                          -> 503 unavailable in 3.00 s
slowest of 3 more requests: 3.02 s
$ docker compose start postgres
Container mtusi-lab2-postgres-1 Starting 
 Container mtusi-lab2-postgres-1 Started
same token after PostgreSQL is back: 200
=> PASS: without PostgreSQL: bounded 503, anonymous still 401

=> PASS: API recovers by itself when PostgreSQL is back

== Full restart: public tests/resilience.py with the restart hook ==
$ docker volume inspect -f {{.Name}} {{.CreatedAt}} mtusi-lab2_pgdata mtusi-lab2_valkeydata
mtusi-lab2_pgdata 2026-10-07T16:01:52Z
mtusi-lab2_valkeydata 2026-10-07T16:01:52Z
$ C:\Users\Dima Chernikov\AppData\Local\Programs\Python\Python314\python.exe tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8080
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.

PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
$ docker volume inspect -f {{.Name}} {{.CreatedAt}} mtusi-lab2_pgdata mtusi-lab2_valkeydata
mtusi-lab2_pgdata 2026-10-07T16:01:52Z
mtusi-lab2_valkeydata 2026-10-07T16:01:52Z
api      StartedAt 2026-10-07T16:02:15.275449332Z -> 2026-10-07T16:04:16.634415601Z
postgres StartedAt 2026-10-07T16:04:06.046215674Z -> 2026-10-07T16:04:10.037996963Z
valkey   StartedAt 2026-10-07T16:03:46.643248943Z -> 2026-10-07T16:04:10.103443189Z
=> PASS: resilience test passes after restarting api, postgres and valkey with the same volumes (hook scripts/restart.cmd, 10.5 s)

== SIGTERM: docker compose stop api ==
$ docker compose stop -t 10 api
Container mtusi-lab2-api-1 Stopping 
 Container mtusi-lab2-api-1 Stopped
$ docker compose logs --no-log-prefix --tail 2 api
2026-10-07T16:04:18.852Z INFO event=shutdown_started signal=SIGTERM
2026-10-07T16:04:19.121Z INFO event=shutdown_finished clean=True duration_ms=269
$ docker compose start api
Container mtusi-lab2-postgres-1 Waiting 
 Container mtusi-lab2-postgres-1 Healthy 
 Container mtusi-lab2-migrate-1 Starting 
 Container mtusi-lab2-migrate-1 Started 
 Container mtusi-lab2-migrate-1 Waiting 
 Container mtusi-lab2-postgres-1 Waiting 
 Container mtusi-lab2-valkey-1 Waiting 
 Container mtusi-lab2-postgres-1 Healthy 
 Container mtusi-lab2-valkey-1 Healthy 
 Container mtusi-lab2-migrate-1 Exited 
 Container mtusi-lab2-api-1 Starting 
 Container mtusi-lab2-api-1 Started
stop took 0.80 s, exit code 0, new request refused: True, ready again: True
=> PASS: SIGTERM stops the API cleanly within 10 s

== Summary ==
PASS  third-party images are pinned by tag and digest
PASS  api, postgres and valkey are healthy
PASS  API process does not run as root
PASS  Valkey persistence is AOF with fsync always, no eviction
PASS  re-running migrations applies nothing and duplicates nothing
PASS  composite chat creation is rolled back as a whole
PASS  exactly one registration wins, the other 19 get 409
PASS  the pool never opens more than 10 connections and all requests succeed
PASS  waiting for a lock is bounded: 503 after about 3 s, then writes work again
PASS  logs contain request_id/status/duration and no secrets
PASS  session expires after SESSION_TTL_SECONDS and the key disappears from Valkey
PASS  without Valkey: 503 for sessions, 401 for anonymous, registration still works
PASS  after Valkey restarts the session is still valid (AOF)
PASS  without PostgreSQL: bounded 503, anonymous still 401
PASS  API recovers by itself when PostgreSQL is back
PASS  resilience test passes after restarting api, postgres and valkey with the same volumes
PASS  SIGTERM stops the API cleanly within 10 s
RESULT: PASS
