ЛР 3 — БВТ2304 — Черников

HAProxy перед двумя репликами API, X-Instance-Id, общая идемпотентность отправки
сообщения в PostgreSQL, двухшаговая остановка реплики по SIGTERM, лимиты CPU/RAM,
хуки stop-one и restart. Проверки отказов, эксперимент и поочерёдное обновление
в scripts/lab3_checks.py, отчёт в REPORT.md, результаты в evidence/, команды для
показа в DEMO.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 18:08:30 +03:00
co-authored by Claude Opus 5.5
parent 0ab3a341aa
commit 54ee33352d
48 changed files with 4967 additions and 24 deletions
+94
View File
@@ -0,0 +1,94 @@
# Build and test from a fresh clone of the solution branch, separate compose project with new volumes.
# Date: 2026-10-07 22:32 UTC; host Windows 11, Docker Desktop; clone made with core.autocrlf=true (as on the author machine).
# COMPOSE_PROJECT_NAME=mtusi-lab3-clean, load balancer on 127.0.0.1:8090 (stats 8494): the main stand is not touched.
$ git clone --branch solution/bvt2304-chernikov <repo> clean3
[exit code 0]
$ git log --oneline -1
5ae5211 ЛР 3 — БВТ2304 — Черников
[exit code 0]
$ python scripts/init_secrets.py
created secrets/postgres_password
created secrets/valkey_password
written secrets/database_url, secrets/valkey_auth, secrets/cache_url
[exit code 0]
$ cp .env.example .env
$ docker compose up -d --build --wait
Container mtusi-lab3-clean-postgres-1 Healthy
Container mtusi-lab3-clean-postgres-1 Healthy
Container mtusi-lab3-clean-valkey-1 Healthy
Container mtusi-lab3-clean-migrate-1 Exited
Container mtusi-lab3-clean-api-1 Healthy
Container mtusi-lab3-clean-api-2 Healthy
Container mtusi-lab3-clean-valkey-1 Healthy
Container mtusi-lab3-clean-postgres-1 Healthy
Container mtusi-lab3-clean-lb-1 Healthy
Container mtusi-lab3-clean-migrate-1 Exited
Container mtusi-lab3-clean-api-1 Healthy
Container mtusi-lab3-clean-api-2 Healthy
[exit code 0]
$ docker compose ps
NAME STATUS PORTS
mtusi-lab3-clean-api-1 Up 6 seconds (healthy) 8080/tcp
mtusi-lab3-clean-api-2 Up 6 seconds (healthy) 8080/tcp
mtusi-lab3-clean-lb-1 Up 1 second 127.0.0.1:8090->8080/tcp, 127.0.0.1:8494->8404/tcp
mtusi-lab3-clean-postgres-1 Up 16 seconds (healthy) 5432/tcp
mtusi-lab3-clean-valkey-1 Up 16 seconds (healthy) 6379/tcp
[exit code 0]
$ docker compose logs --no-log-prefix migrate
INFO event=migration_applied version=0001_schema duration_ms=12
INFO event=migration_applied version=0002_messages_history_index duration_ms=4
INFO event=migration_applied version=0003_idempotency_keys duration_ms=5
INFO event=migrations_done applied_now=3 total=3
[exit code 0]
$ docker compose run --rm --no-deps migrate
INFO event=migrations_done applied_now=0 total=3
[exit code 0]
$ python -X utf8 tests/smoke.py --base-url http://127.0.0.1:8090
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
[exit code 0]
$ BASE_URL=http://127.0.0.1:8090 python -X utf8 -m unittest discover -s student-tests
Ran 86 tests in 19.577s
OK
[exit code 0]
$ python -X utf8 tests/resilience.py --mode failover --action-script scripts/stop-one.cmd --base-url http://127.0.0.1:8090
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 111, "failed_attempts": 0, "first_recovery_seconds": 4.432}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
[exit code 0]
$ docker compose ps -a (one api replica is stopped by the hook)
mtusi-lab3-clean-api-1 Up 49 seconds (healthy)
mtusi-lab3-clean-api-2 Exited (0) 11 seconds ago
[exit code 0]
$ docker compose up -d --wait (restore the replica)
Container mtusi-lab3-clean-api-2 Started
Container mtusi-lab3-clean-api-1 Healthy
Container mtusi-lab3-clean-api-2 Healthy
Container mtusi-lab3-clean-api-1 Healthy
Container mtusi-lab3-clean-api-2 Healthy
[exit code 0]
$ python -X utf8 tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8090
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
[exit code 0]
$ docker compose down -v (removes only the mtusi-lab3-clean project and its volumes)
Volume mtusi-lab3-clean_pgdata Removed
Volume mtusi-lab3-clean_valkeydata Removed
[exit code 0]
# exit codes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
RESULT: PASS
+96
View File
@@ -0,0 +1,96 @@
# 2026-10-07 22:30:37 UTC, PostgreSQL 18.6 (Debian 18.6-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
# target chat 0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae: 10000 messages; noise chat 8efe2cac-fee0-4974-b5a0-197ee563140a: 20000 messages
# inserted and analyzed in 1.0 s
messages total: 55175, in target chat: 10000
== 1. First page, middle page and last page: index range scan, no sort ==
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae' ORDER BY created_at, id LIMIT 51;
QUERY PLAN
------------------------------------------------------------------------------------------------------------
Limit (actual time=0.045..0.183 rows=51.00 loops=1)
Buffers: shared hit=54
-> Index Scan using messages_chat_history_idx on messages (actual time=0.042..0.174 rows=51.00 loops=1)
Index Cond: (chat_id = '0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae'::uuid)
Index Searches: 1
Buffers: shared hit=54
Planning:
Buffers: shared hit=144
Planning Time: 0.722 ms
Execution Time: 0.220 ms
(10 rows)
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae' AND (created_at, id) > ('2026-10-07 22:30:43.623934+00', 'c51eae37-23c0-412f-8b9e-233311a6374d') ORDER BY created_at, id LIMIT 51;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.020..0.033 rows=51.00 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (actual time=0.019..0.027 rows=51.00 loops=1)
Index Cond: ((chat_id = '0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 22:30:43.623934+00'::timestamp with time zone, 'c51eae37-23c0-412f-8b9e-233311a6374d'::uuid)))
Index Searches: 1
Buffers: shared hit=5
Planning Time: 0.068 ms
Execution Time: 0.059 ms
(8 rows)
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae' AND (created_at, id) > ('2026-10-07 22:30:48.635476+00', '61827eb5-875e-47be-b232-4b344ab1b5df') ORDER BY created_at, id LIMIT 51;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.031..0.039 rows=20.00 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (actual time=0.030..0.036 rows=20.00 loops=1)
Index Cond: ((chat_id = '0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 22:30:48.635476+00'::timestamp with time zone, '61827eb5-875e-47be-b232-4b344ab1b5df'::uuid)))
Index Searches: 1
Buffers: shared hit=5
Planning Time: 0.077 ms
Execution Time: 0.054 ms
(8 rows)
== 2. Generic plan of the parameterized query (how a prepared statement with $1..$4 runs) ==
SET plan_cache_mode = force_generic_plan;
SET
PREPARE history_page(uuid, timestamptz, uuid, int) AS
SELECT id, chat_id, sender_id, text, created_at FROM messages
WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4;
PREPARE
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) EXECUTE history_page('0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae', '2026-10-07 22:30:43.623934+00', 'c51eae37-23c0-412f-8b9e-233311a6374d', 51);
QUERY PLAN
------------------------------------------------------------------------------------------------------------
Limit (actual time=0.038..0.051 rows=51.00 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (actual time=0.032..0.040 rows=51.00 loops=1)
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Index Searches: 1
Buffers: shared hit=5
Planning:
Buffers: shared hit=141
Planning Time: 0.836 ms
Execution Time: 0.107 ms
(10 rows)
== 3. For comparison: the same middle page without the index (rolled back) ==
BEGIN;
BEGIN
DROP INDEX messages_chat_history_idx;
DROP INDEX
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae' AND (created_at, id) > ('2026-10-07 22:30:43.623934+00', 'c51eae37-23c0-412f-8b9e-233311a6374d') ORDER BY created_at, id LIMIT 51;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=6.627..6.638 rows=51.00 loops=1)
Buffers: shared hit=809
-> Sort (actual time=6.624..6.629 rows=51.00 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=809
-> Seq Scan on messages (actual time=3.200..6.151 rows=5000.00 loops=1)
Filter: ((chat_id = '0026e6c1-47d7-4b72-ae1b-a7d3c5a493ae'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 22:30:43.623934+00'::timestamp with time zone, 'c51eae37-23c0-412f-8b9e-233311a6374d'::uuid)))
Rows Removed by Filter: 50175
Buffers: shared hit=803
Planning:
Buffers: shared hit=108
Planning Time: 0.468 ms
Execution Time: 6.680 ms
(14 rows)
ROLLBACK;
ROLLBACK
index still exists: true
+301
View File
@@ -0,0 +1,301 @@
# 2026-10-07 22:25:25 UTC, python 3.14.7, win32
# docker 29.8.1
== Stack: replicas, pinned images, health, limits, published ports, secrets ==
$ docker compose ps -a --format table {{.Name}} {{.Image}} {{.Status}} {{.Ports}}
NAME IMAGE STATUS PORTS
mtusi-lab3-api-3 mtusi-messenger:lab3 Up 6 minutes (healthy) 8080/tcp
mtusi-lab3-api-4 mtusi-messenger:lab3 Up 6 minutes (healthy) 8080/tcp
mtusi-lab3-lb-1 haproxy:3.4.5-trixie@sha256:76928c0d6b39bdd5f1c15d519cf48c47a6aa18c1dc552f7af1c146d2aa003c14 Up 6 minutes 127.0.0.1:8080->8080/tcp, 127.0.0.1:8404->8404/tcp
mtusi-lab3-migrate-1 sha256:c2a29f803684ad58f400a42d32e5c262fa44580101e65188ef92ca6015e17885 Exited (0) 6 minutes ago
mtusi-lab3-postgres-1 postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336 Up 6 minutes (healthy) 5432/tcp
mtusi-lab3-valkey-1 valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2 Up 6 minutes (healthy) 6379/tcp
$ docker compose config --format json
=> PASS: third-party images are pinned by tag and digest (haproxy:3.4.5-trixie@sha256:76928c0d6b39bdd5f1c15d519cf48c47a6aa18c1dc552f7af1c146d2aa003c14, postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336, valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2)
api replicas: mtusi-lab3-api-3 (68717763b8c6), mtusi-lab3-api-4 (7fc81857cf65)
=> PASS: at least two api replicas of one compose service, no container_name (mtusi-lab3-api-3, mtusi-lab3-api-4)
health: mtusi-lab3-api-3=healthy, mtusi-lab3-api-4=healthy, postgres=healthy, valkey=healthy
=> PASS: all api replicas, postgres and valkey are healthy
=> PASS: API processes do not run as root (mtusi-lab3-api-3 uid=10001, mtusi-lab3-api-4 uid=10001)
container cpus memory published ports
lb 0.50 128 MB 127.0.0.1:8080->8080/tcp 127.0.0.1:8404->8404/tcp
mtusi-lab3-api-3 0.50 256 MB -
mtusi-lab3-api-4 0.50 256 MB -
postgres 1.00 512 MB -
valkey 0.50 256 MB -
=> PASS: every container has CPU and memory limits
=> PASS: only the load balancer publishes ports, and only on 127.0.0.1
secrets per service: api=cache_url,database_url; lb=-; migrate=database_url; postgres=postgres_password; valkey=valkey_auth
=> PASS: secrets are given only where needed
$ 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: run once by the migrate service, idempotent re-run ==
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT version || ' ' || applied_at FROM schema_migrations ORDER BY version
0001_schema 2026-10-07 22:03:12.021632+00
0002_messages_history_index 2026-10-07 22:03:12.034748+00
0003_idempotency_keys 2026-10-07 22:03:12.038316+00
$ 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 = 'idempotency_keys' ORDER BY 1
idempotency_keys c idempotency_keys_key_format
idempotency_keys f idempotency_keys_message_id_fkey
idempotency_keys f idempotency_keys_user_id_fkey
idempotency_keys n idempotency_keys_created_at_not_null
idempotency_keys n idempotency_keys_key_not_null
idempotency_keys n idempotency_keys_message_id_not_null
idempotency_keys n idempotency_keys_method_not_null
idempotency_keys n idempotency_keys_path_not_null
idempotency_keys n idempotency_keys_request_hash_not_null
idempotency_keys n idempotency_keys_user_id_not_null
idempotency_keys p idempotency_keys_pkey
$ 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)
3 205
$ docker compose run --rm --no-deps migrate
Container mtusi-lab3-migrate-run-ee50296698bb Creating
Container mtusi-lab3-migrate-run-ee50296698bb Created
INFO event=migrations_done applied_now=0 total=3
$ 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)
3 205
schema_migrations and users rows before/after: 3 205 / 3 205
=> PASS: re-running migrations applies nothing and duplicates nothing
== Balancing: one session, many replicas ==
X-Instance-Id 68717763b8c6 (mtusi-lab3-api-3): 20 responses with status 200
X-Instance-Id 7fc81857cf65 (mtusi-lab3-api-4): 20 responses with status 200
=> PASS: 40 requests with one token are served by every replica without a new login
== Idempotency: 20 parallel POSTs with one key through the load balancer ==
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM messages WHERE chat_id = '83406a9f-af29-4189-bd5a-80f48d1d064f'
1
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM idempotency_keys WHERE key = '8eaaba5dfe6645a895251432314215c9'
1
statuses: [201]; distinct message ids: 1; replicas that answered: 68717763b8c6, 7fc81857cf65
messages in the chat: 1; idempotency_keys rows for the key: 1
same key, another body -> 409 conflict in 0.01 s, X-Instance-Id 7fc81857cf65
=> PASS: one message for 20 concurrent requests with one key on several replicas
=> PASS: the same key with another body is rejected with 409
== Rollback: chat creation fails on the second INSERT ==
POST /api/v1/chats with one unknown member -> 400 invalid_request in 0.02 s, X-Instance-Id 68717763b8c6
$ 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-197e464657'
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
rows in chats with this title: 0; chats without members: 0
=> 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; replicas: 68717763b8c6, 7fc81857cf65
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM users WHERE username = 'race_9ef48474bc80'
1
rows in users with this username: 1
=> PASS: exactly one registration wins, the other 19 get 409
== Connection pools: 60 parallel clients, at most 10 connections per replica (2 replicas) ==
requests: 563, statuses: {200: 563}
API connections in pg_stat_activity, 9 samples: max 20
=> PASS: pools never open more than 10 connections per replica 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 = '6026c0e8-0d1a-4e86-ae54-da84dc874367' FOR UPDATE; SELECT pg_sleep(6); ROLLBACK;" (in the background)
POST message while the chat row is locked -> 503 unavailable in 3.02 s, X-Instance-Id 68717763b8c6
POST message after the lock is released -> 201 in 0.02 s, X-Instance-Id 7fc81857cf65
=> PASS: waiting for a lock is bounded: 503 after about 3 s, then writes work again (3.02 s)
== Logs: useful fields, no message texts, passwords or tokens (all api replicas and lb) ==
$ docker compose logs --no-log-prefix api lb
$ docker compose logs --tail 2 api
api-3 | 2026-10-07T22:25:54.557Z INFO request_id=966a547d1ac94f6c82eff4c595ee9f9b method=POST path=/api/v1/chats status=201 duration_ms=5.8 user_id=44136183-c19b-4b5a-adbb-6858489b0330
api-4 | 2026-10-07T22:25:54.437Z INFO request_id=964da93e470f47caa94279cd2e9e7516 method=POST path=/api/v1/chats/6026c0e8-0d1a-4e86-ae54-da84dc874367/messages status=201 duration_ms=12.0 user_id=ac7c4c7a-41aa-401d-bdd5-39d27066e9b3
api-4 | 2026-10-07T22:25:54.543Z INFO request_id=23d18bd6174749a99f9fb1dd222bdfb7 method=POST path=/api/v1/auth/sessions status=201 duration_ms=39.0 user_id=44136183-c19b-4b5a-adbb-6858489b0330
api-3 | 2026-10-07T22:25:54.583Z INFO request_id=61b9245ffbf24e6ea1d5c0e46ddd1e80 method=POST path=/api/v1/chats/2bdbc8f5-9be8-449d-940b-5c97ef9a6943/messages status=201 duration_ms=10.7 user_id=44136183-c19b-4b5a-adbb-6858489b0330
$ docker compose logs --no-log-prefix --tail 2 lb
172.19.0.1:57694 [07/Oct/2026:22:25:54.549] public api/api1 0/0/1/6/7 201 373 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/chats HTTP/1.1"
172.19.0.1:57710 [07/Oct/2026:22:25:54.570] public api/api1 0/0/0/13/13 201 457 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/chats/2bdbc8f5-9be8-449d-940b-5c97ef9a6943/messages HTTP/1.1"
searched 41389 log lines; found: nothing
=> PASS: logs contain request_id/status/duration and no secrets
== Session expiry: api replicas recreated with SESSION_TTL_SECONDS=2, then restored ==
$ docker compose up -d --no-deps --wait api
GET /users/me right after login -> 200 in 0.01 s, X-Instance-Id 911800b97ae9
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" PTTL session:6b03ee75d9f79c514394462e72c39c21a3d11ace901c5a42c89b5ef93cd136fb
1694
PTTL of the session key in Valkey (ms): 1694
GET /users/me 3 s later -> 401 unauthorized in 0.02 s, X-Instance-Id ba820ebd9c68
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" EXISTS session:6b03ee75d9f79c514394462e72c39c21a3d11ace901c5a42c89b5ef93cd136fb
0
session key still exists in Valkey: 0
=> PASS: session expires after SESSION_TTL_SECONDS and the key disappears from Valkey
$ docker compose up -d --no-deps --wait api
== Valkey is down ==
$ docker compose stop valkey
Container mtusi-lab3-valkey-1 Stopping
Container mtusi-lab3-valkey-1 Stopped
GET /users/me with a valid token (first seconds) -> 503 unavailable in 1.02 s, X-Instance-Id 34f46eb4cd91
GET /users/me with a valid token (after 3 s) -> 503 unavailable in 0.01 s, X-Instance-Id -
GET /users/me without a token -> 503 unavailable in 0.01 s, X-Instance-Id -
GET /health/ready -> 503 unavailable in 0.00 s, X-Instance-Id -
slowest of 3 more authenticated requests: 0.01 s
$ docker compose start valkey
Container mtusi-lab3-valkey-1 Starting
Container mtusi-lab3-valkey-1 Started
same token after valkey is back: 200 after 1.5 s
=> PASS: without valkey: bounded 503, nothing is served as anonymous
=> PASS: the stand recovers by itself when valkey is back, the session is still valid
== PostgreSQL is down ==
$ docker compose stop postgres
Container mtusi-lab3-postgres-1 Stopping
Container mtusi-lab3-postgres-1 Stopped
GET /users/me with a valid token (first seconds) -> 503 unavailable in 3.02 s, X-Instance-Id cc69293773ec
GET /users/me with a valid token (after 3 s) -> 503 unavailable in 0.01 s, X-Instance-Id -
GET /users/me without a token -> 503 unavailable in 0.01 s, X-Instance-Id -
GET /health/ready -> 503 unavailable in 0.01 s, X-Instance-Id -
slowest of 3 more authenticated requests: 0.01 s
$ docker compose start postgres
Container mtusi-lab3-postgres-1 Starting
Container mtusi-lab3-postgres-1 Started
same token after postgres is back: 200 after 5.6 s
=> PASS: without postgres: bounded 503, nothing is served as anonymous
=> PASS: the stand recovers by itself when postgres is back, the session is still valid
== Public failover test: tests/resilience.py --mode failover, repeated until every replica was the target ==
$ C:\Users\Dima Chernikov\AppData\Local\Programs\Python\Python314\python.exe -X utf8 tests/resilience.py --mode failover --action-script scripts/stop-one.cmd --base-url http://127.0.0.1:8080
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.␍
{"observed_instances_before": 2, "successful_writes": 112, "failed_attempts": 0, "first_recovery_seconds": 3.827}␍
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
attempt 1: stopped container cc69293773ec (mtusi-lab3-api-4), State: exited, ExitCode 0
=> PASS: public failover test passes (attempt 1) (17.1 s)
$ docker compose up -d --wait
$ C:\Users\Dima Chernikov\AppData\Local\Programs\Python\Python314\python.exe -X utf8 tests/resilience.py --mode failover --action-script scripts/stop-one.cmd --base-url http://127.0.0.1:8080
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.␍
{"observed_instances_before": 2, "successful_writes": 106, "failed_attempts": 0, "first_recovery_seconds": 4.132}␍
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
attempt 2: stopped container 34f46eb4cd91 (mtusi-lab3-api-3), State: exited, ExitCode 0
=> PASS: public failover test passes (attempt 2) (16.7 s)
$ docker compose up -d --wait
=> PASS: every replica was stopped by the hook at least once (mtusi-lab3-api-3, mtusi-lab3-api-4)
== Experiment: stop, then kill, each replica in turn under load; restore before the next one ==
$ docker stop 34f46eb4cd91
$ docker start 34f46eb4cd91
{"replica": "mtusi-lab3-api-3", "lb_slot": "api2", "action": "docker stop", "command_s": 3.93, "baseline_ok_rps": 148.2, "during_ok_rps": 81.3, "errors_during": 0, "requests_during": 970, "max_gap_s": 0.18, "lb_marked_down_s": 1.0, "last_served_s": 0.97, "back_in_rotation_s": 5.19, "after_ok_rps": 218.1, "errors_after": 0}
$ docker stop cc69293773ec
$ docker start cc69293773ec
{"replica": "mtusi-lab3-api-4", "lb_slot": "api1", "action": "docker stop", "command_s": 3.96, "baseline_ok_rps": 171.4, "during_ok_rps": 83.8, "errors_during": 0, "requests_during": 1002, "max_gap_s": 0.1, "lb_marked_down_s": 1.09, "last_served_s": 1.02, "back_in_rotation_s": 5.34, "after_ok_rps": 165.3, "errors_after": 0}
$ docker kill 34f46eb4cd91
$ docker start 34f46eb4cd91
{"replica": "mtusi-lab3-api-3", "lb_slot": "api2", "action": "docker kill", "command_s": 0.4, "baseline_ok_rps": 202.0, "during_ok_rps": 69.4, "errors_during": 2, "requests_during": 585, "max_gap_s": 0.93, "lb_marked_down_s": 0.47, "last_served_s": 0.1, "back_in_rotation_s": 5.47, "after_ok_rps": 123.8, "errors_after": 0}
$ docker kill cc69293773ec
$ docker start cc69293773ec
{"replica": "mtusi-lab3-api-4", "lb_slot": "api1", "action": "docker kill", "command_s": 0.43, "baseline_ok_rps": 151.4, "during_ok_rps": 67.6, "errors_during": 3, "requests_during": 573, "max_gap_s": 0.98, "lb_marked_down_s": 0.42, "last_served_s": 0.07, "back_in_rotation_s": 5.49, "after_ok_rps": 156.2, "errors_after": 0}
replica action cmd,s rps0 rps errors gap,s down,s back,s rps1
mtusi-lab3-api-3 docker stop 3.93 148.2 81.3 0 0.18 1.0 5.19 218.1
mtusi-lab3-api-4 docker stop 3.96 171.4 83.8 0 0.1 1.09 5.34 165.3
mtusi-lab3-api-3 docker kill 0.4 202.0 69.4 2 0.93 0.47 5.47 123.8
mtusi-lab3-api-4 docker kill 0.43 151.4 67.6 3 0.98 0.42 5.49 156.2
acknowledged writes: 4858, found in history: 4858, duplicates: 0
=> PASS: graceful stop of each replica: no client errors, LB removes it within 3 s
=> PASS: kill of each replica: LB removes it within 3 s, writes go on within 10 s
=> PASS: every replica comes back into rotation after docker start
=> PASS: no acknowledged write is lost or duplicated (retries reuse the key)
== Rolling update under load: one replica at a time, new image, migrations once ==
$ docker compose build api
image before 4f0ecfa64963, after e85c788ea2be, APP_VERSION=rolling-1791412151
$ docker compose run --rm --no-deps migrate
Container mtusi-lab3-migrate-run-132d5c300c9b Creating
Container mtusi-lab3-migrate-run-132d5c300c9b Created
INFO event=migrations_done applied_now=0 total=3
$ docker compose up -d --no-deps --no-recreate --scale api=3 api
$ docker stop 34f46eb4cd91
$ docker rm 34f46eb4cd91
replaced mtusi-lab3-api-3 (34f46eb4cd91) -> 27757842cabd
$ docker compose up -d --no-deps --no-recreate --scale api=3 api
$ docker stop cc69293773ec
$ docker rm cc69293773ec
replaced mtusi-lab3-api-4 (cc69293773ec) -> 45ad15a9929a
during the update: {"requests": 5582, "ok": 5582, "errors": 0, "ok_rps": 190.1}
final replicas: mtusi-lab3-api-5 27757842cabd version=rolling-1791412151, mtusi-lab3-api-6 45ad15a9929a version=rolling-1791412151
acknowledged writes: 2791, found in history: 2791, duplicates: 0
=> PASS: migrations are applied once and the change is backward compatible
=> PASS: every replica was replaced one by one by the new image under load
=> PASS: no client errors and no lost or duplicated writes during the rolling update (5582 requests, 190.1 ok RPS)
== Full restart: public tests/resilience.py --mode persistence with the restart hook ==
$ docker volume inspect -f {{.Name}} {{.CreatedAt}} mtusi-lab3_pgdata mtusi-lab3_valkeydata
mtusi-lab3_pgdata 2026-10-07T22:03:02Z
mtusi-lab3_valkeydata 2026-10-07T22:03:02Z
$ C:\Users\Dima Chernikov\AppData\Local\Programs\Python\Python314\python.exe -X utf8 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-lab3_pgdata mtusi-lab3_valkeydata
mtusi-lab3_pgdata 2026-10-07T22:03:02Z
mtusi-lab3_valkeydata 2026-10-07T22:03:02Z
lb StartedAt 22:19:18.913 -> 22:30:10.030
mtusi-lab3-api-5 StartedAt 22:29:22.591 -> 22:30:06.409
mtusi-lab3-api-6 StartedAt 22:29:34.442 -> 22:30:06.102
postgres StartedAt 22:26:35.615 -> 22:29:56.621
valkey StartedAt 22:26:26.521 -> 22:29:56.526
=> PASS: persistence test passes after restarting every replica, postgres, valkey and lb with the same volumes (hook scripts/restart.cmd, 19.9 s)
== Summary ==
PASS third-party images are pinned by tag and digest
PASS at least two api replicas of one compose service, no container_name
PASS all api replicas, postgres and valkey are healthy
PASS API processes do not run as root
PASS every container has CPU and memory limits
PASS only the load balancer publishes ports, and only on 127.0.0.1
PASS secrets are given only where needed
PASS Valkey persistence is AOF with fsync always, no eviction
PASS re-running migrations applies nothing and duplicates nothing
PASS 40 requests with one token are served by every replica without a new login
PASS one message for 20 concurrent requests with one key on several replicas
PASS the same key with another body is rejected with 409
PASS composite chat creation is rolled back as a whole
PASS exactly one registration wins, the other 19 get 409
PASS pools never open more than 10 connections per replica 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: bounded 503, nothing is served as anonymous
PASS the stand recovers by itself when valkey is back, the session is still valid
PASS without postgres: bounded 503, nothing is served as anonymous
PASS the stand recovers by itself when postgres is back, the session is still valid
PASS public failover test passes (attempt 1)
PASS public failover test passes (attempt 2)
PASS every replica was stopped by the hook at least once
PASS graceful stop of each replica: no client errors, LB removes it within 3 s
PASS kill of each replica: LB removes it within 3 s, writes go on within 10 s
PASS every replica comes back into rotation after docker start
PASS no acknowledged write is lost or duplicated (retries reuse the key)
PASS migrations are applied once and the change is backward compatible
PASS every replica was replaced one by one by the new image under load
PASS no client errors and no lost or duplicated writes during the rolling update
PASS persistence test passes after restarting every replica, postgres, valkey and lb with the same volumes
RESULT: PASS
+19
View File
@@ -0,0 +1,19 @@
{
"base_url": "http://127.0.0.1:8080",
"clients": 10,
"messages_per_client": 10,
"sent": 100,
"accepted_201": 100,
"history_total": 100,
"history_unique_ids": 100,
"history_unique_texts": 100,
"history_matches_accepted": true,
"senders_in_history": 10,
"min_messages_per_sender": 10,
"max_messages_per_sender": 10,
"ordered_by_created_at_and_id": true,
"pages_read": 15,
"page_size": 7,
"send_elapsed_seconds": 0.647,
"ok": true
}
+132
View File
@@ -0,0 +1,132 @@
# Tests through the load balancer of the compose stand (docker compose up -d --build --wait), 2 API replicas.
# Date: 2026-10-07 22:24 UTC
# Host: Windows 11, Docker Desktop engine 29.8.1, Compose 5.5.1, Python 3.14.7 for the test clients;
# Intel Core Ultra 9 185H (16 cores, 22 threads), 31 GiB RAM, no CPU/RAM limits on containers.
# Code: working tree of the solution branch (commit is given in REPORT.md).
$ python -X utf8 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
[exit code 0]
$ python -X utf8 tests/smoke.py --base-url http://127.0.0.1:8080
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
[exit code 0]
$ BASE_URL=http://127.0.0.1:8080 python -X utf8 -m unittest discover -s student-tests -v
test_authentication_is_checked_before_body_and_resource (test_access.AuthenticationOrderTest.test_authentication_is_checked_before_body_and_resource) ... ok
test_registration_does_not_need_a_token (test_access.AuthenticationOrderTest.test_registration_does_not_need_a_token) ... ok
test_chat_list_contains_only_own_chats (test_access.ForeignChatTest.test_chat_list_contains_only_own_chats) ... ok
test_every_member_can_read_and_write (test_access.ForeignChatTest.test_every_member_can_read_and_write) ... ok
test_foreign_chat_looks_like_missing_chat (test_access.ForeignChatTest.test_foreign_chat_looks_like_missing_chat) ... ok
test_invalid_chat_id_is_400 (test_access.ForeignChatTest.test_invalid_chat_id_is_400) ... ok
test_outsider_cannot_get_chat (test_access.ForeignChatTest.test_outsider_cannot_get_chat) ... ok
test_outsider_cannot_read_history (test_access.ForeignChatTest.test_outsider_cannot_read_history) ... ok
test_outsider_cannot_write (test_access.ForeignChatTest.test_outsider_cannot_write) ... ok
test_unknown_route_is_404 (test_access.ForeignChatTest.test_unknown_route_is_404) ... ok
test_x_user_id_does_not_open_a_foreign_chat (test_access.ForeignChatTest.test_x_user_id_does_not_open_a_foreign_chat) ... ok
test_errors_also_carry_instance_id (test_balancing.BalancingTest.test_errors_also_carry_instance_id) ... ok
test_message_written_on_one_replica_is_read_on_another (test_balancing.BalancingTest.test_message_written_on_one_replica_is_read_on_another) ... ok
test_one_session_is_served_by_several_replicas (test_balancing.BalancingTest.test_one_session_is_served_by_several_replicas) ... ok
test_100_messages_from_10_clients (test_concurrency.ConcurrencyTest.test_100_messages_from_10_clients) ... ok
test_chats_created_in_parallel_are_all_listed (test_concurrency.ConcurrencyTest.test_chats_created_in_parallel_are_all_listed) ... ok
test_same_username_registered_in_parallel_wins_once (test_concurrency.ConcurrencyTest.test_same_username_registered_in_parallel_wins_once) ... ok
test_failed_validation_is_not_remembered (test_idempotency.IdempotencyTest.test_failed_validation_is_not_remembered) ... ok
test_invalid_keys_are_400 (test_idempotency.IdempotencyTest.test_invalid_keys_are_400) ... ok
test_json_formatting_does_not_matter (test_idempotency.IdempotencyTest.test_json_formatting_does_not_matter) ... ok
test_key_format_limits_are_accepted (test_idempotency.IdempotencyTest.test_key_format_limits_are_accepted) ... ok
test_key_scope_is_user_and_chat (test_idempotency.IdempotencyTest.test_key_scope_is_user_and_chat) ... ok
test_missing_key_is_400 (test_idempotency.IdempotencyTest.test_missing_key_is_400) ... ok
test_parallel_requests_with_one_key_create_one_message (test_idempotency.IdempotencyTest.test_parallel_requests_with_one_key_create_one_message) ... ok
test_replay_checks_the_session_again (test_idempotency.IdempotencyTest.test_replay_checks_the_session_again) ... ok
test_replay_returns_the_same_message (test_idempotency.IdempotencyTest.test_replay_returns_the_same_message) ... ok
test_same_key_with_another_body_is_409 (test_idempotency.IdempotencyTest.test_same_key_with_another_body_is_409) ... ok
test_chats_are_paged_in_creation_order (test_pagination.ChatListPaginationTest.test_chats_are_paged_in_creation_order) ... ok
test_cursor_of_another_user_is_400 (test_pagination.ChatListPaginationTest.test_cursor_of_another_user_is_400) ... ok
test_default_limit_is_50 (test_pagination.DefaultLimitTest.test_default_limit_is_50) ... ok
test_empty_history (test_pagination.GrowingHistoryTest.test_empty_history) ... ok
test_messages_sent_during_walk_are_not_lost (test_pagination.GrowingHistoryTest.test_messages_sent_during_walk_are_not_lost) ... ok
test_cursor_from_another_chat_is_400 (test_pagination.MessagePaginationTest.test_cursor_from_another_chat_is_400) ... ok
test_default_and_maximum_limit (test_pagination.MessagePaginationTest.test_default_and_maximum_limit) ... ok
test_invalid_cursor_is_400 (test_pagination.MessagePaginationTest.test_invalid_cursor_is_400) ... ok
test_invalid_limit_is_400 (test_pagination.MessagePaginationTest.test_invalid_limit_is_400) ... ok
test_last_full_page_has_no_cursor (test_pagination.MessagePaginationTest.test_last_full_page_has_no_cursor) ... ok
test_limit_can_change_between_pages (test_pagination.MessagePaginationTest.test_limit_can_change_between_pages) ... ok
test_one_message_per_page (test_pagination.MessagePaginationTest.test_one_message_per_page) ... ok
test_walk_over_several_pages (test_pagination.MessagePaginationTest.test_walk_over_several_pages) ... ok
test_basic_is_rejected_on_business_endpoints (test_sessions.BearerTest.test_basic_is_rejected_on_business_endpoints) ... ok
test_protected_endpoints_need_a_token (test_sessions.BearerTest.test_protected_endpoints_need_a_token) ... ok
test_scheme_name_is_case_insensitive (test_sessions.BearerTest.test_scheme_name_is_case_insensitive) ... ok
test_token_selects_the_user (test_sessions.BearerTest.test_token_selects_the_user) ... ok
test_unknown_or_malformed_token (test_sessions.BearerTest.test_unknown_or_malformed_token) ... ok
test_x_user_id_is_ignored (test_sessions.BearerTest.test_x_user_id_is_ignored) ... ok
test_credentials_in_json_body_are_not_accepted (test_sessions.LoginTest.test_credentials_in_json_body_are_not_accepted) ... ok
test_each_login_gives_a_new_token (test_sessions.LoginTest.test_each_login_gives_a_new_token) ... ok
test_malformed_basic_header (test_sessions.LoginTest.test_malformed_basic_header) ... ok
test_password_with_colons_and_unicode (test_sessions.LoginTest.test_password_with_colons_and_unicode) ... ok
test_registration_never_returns_password (test_sessions.LoginTest.test_registration_never_returns_password) ... ok
test_successful_login (test_sessions.LoginTest.test_successful_login) ... ok
test_unknown_user_gets_the_same_answer (test_sessions.LoginTest.test_unknown_user_gets_the_same_answer) ... ok
test_wrong_password (test_sessions.LoginTest.test_wrong_password) ... ok
test_login_again_after_logout (test_sessions.LogoutTest.test_login_again_after_logout) ... ok
test_logout_revokes_only_the_current_token (test_sessions.LogoutTest.test_logout_revokes_only_the_current_token) ... ok
test_bad_member_ids (test_validation.ChatValidationTest.test_bad_member_ids) ... ok
test_bad_titles (test_validation.ChatValidationTest.test_bad_titles) ... ok
test_caller_must_be_a_member (test_validation.ChatValidationTest.test_caller_must_be_a_member) ... ok
test_duplicate_members (test_validation.ChatValidationTest.test_duplicate_members) ... ok
test_unknown_member_rolls_back_the_whole_chat (test_validation.ChatValidationTest.test_unknown_member_rolls_back_the_whole_chat) ... ok
test_valid_chat_keeps_member_order (test_validation.ChatValidationTest.test_valid_chat_keeps_member_order) ... ok
test_extra_fields (test_validation.InvalidJsonTest.test_extra_fields) ... ok
test_invalid_utf8 (test_validation.InvalidJsonTest.test_invalid_utf8) ... ok
test_json_that_is_not_an_object (test_validation.InvalidJsonTest.test_json_that_is_not_an_object) ... ok
test_lone_surrogate_is_rejected_not_crashed (test_validation.InvalidJsonTest.test_lone_surrogate_is_rejected_not_crashed) ... ok
test_malformed_json_on_message (test_validation.InvalidJsonTest.test_malformed_json_on_message) ... ok
test_malformed_json_on_register (test_validation.InvalidJsonTest.test_malformed_json_on_register) ... ok
test_nul_character_is_rejected_not_crashed (test_validation.InvalidJsonTest.test_nul_character_is_rejected_not_crashed) ... ok
test_wrong_content_type (test_validation.InvalidJsonTest.test_wrong_content_type) ... ok
test_wrong_types (test_validation.InvalidJsonTest.test_wrong_types) ... ok
test_empty_message_is_400 (test_validation.MessageValidationTest.test_empty_message_is_400) ... ok
test_missing_or_wrong_text (test_validation.MessageValidationTest.test_missing_or_wrong_text) ... ok
test_nul_in_text_is_400 (test_validation.MessageValidationTest.test_nul_in_text_is_400) ... ok
test_oversized_body_is_413 (test_validation.MessageValidationTest.test_oversized_body_is_413) ... ok
test_sender_id_in_body_is_rejected (test_validation.MessageValidationTest.test_sender_id_in_body_is_rejected) ... ok
test_text_length_limit (test_validation.MessageValidationTest.test_text_length_limit) ... ok
test_unicode_text_is_kept (test_validation.MessageValidationTest.test_unicode_text_is_kept) ... ok
test_bad_display_names (test_validation.UserValidationTest.test_bad_display_names) ... ok
test_bad_passwords (test_validation.UserValidationTest.test_bad_passwords) ... ok
test_bad_usernames (test_validation.UserValidationTest.test_bad_usernames) ... ok
test_duplicate_username_is_409 (test_validation.UserValidationTest.test_duplicate_username_is_409) ... ok
test_missing_fields (test_validation.UserValidationTest.test_missing_fields) ... ok
test_password_length_limits_are_accepted (test_validation.UserValidationTest.test_password_length_limits_are_accepted) ... ok
test_unicode_display_name_is_kept (test_validation.UserValidationTest.test_unicode_display_name_is_kept) ... ok
test_username_length_limits_are_accepted (test_validation.UserValidationTest.test_username_length_limits_are_accepted) ... ok
----------------------------------------------------------------------
Ran 86 tests in 18.104s
OK
[exit code 0]
$ BASE_URL=http://127.0.0.1:8080 python student-tests/race.py --output evidence/race.json
{
"base_url": "http://127.0.0.1:8080",
"clients": 10,
"messages_per_client": 10,
"sent": 100,
"accepted_201": 100,
"history_total": 100,
"history_unique_ids": 100,
"history_unique_texts": 100,
"history_matches_accepted": true,
"senders_in_history": 10,
"min_messages_per_sender": 10,
"max_messages_per_sender": 10,
"ordered_by_created_at_and_id": true,
"pages_read": 15,
"page_size": 7,
"send_elapsed_seconds": 0.647,
"ok": true
}
PASS
[exit code 0]
# exit codes: [0, 0, 0, 0]