ЛР2: PostgreSQL, сессии и Valkey (на 4)
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
API мессенджера на FastAPI + asyncpg, миграции, Argon2id, Bearer-сессии в PostgreSQL с кэшем Valkey, compose-стенд, restart.sh, тесты уровня 4, EXPLAIN и отчёт. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
# EXPLAIN evidence, 2026-10-29T15:35:04Z, commit 65003b6
|
||||
== Объём данных
|
||||
total_messages | big_chat_messages | table_size | index_size
|
||||
----------------+-------------------+------------+------------
|
||||
50029 | 20000 | 5096 kB | 4112 kB
|
||||
(1 row)
|
||||
|
||||
== 1. Первая страница (запрос API без cursor, limit=50 -> LIMIT 51)
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.017..0.046 rows=51 loops=1)
|
||||
Buffers: shared hit=6
|
||||
-> Index Scan using messages_chat_created_id_idx on messages (actual time=0.016..0.037 rows=51 loops=1)
|
||||
Index Cond: (chat_id = 'ad832a44-34ce-416b-a5f8-f8e528316fd6'::uuid)
|
||||
Buffers: shared hit=6
|
||||
Planning:
|
||||
Buffers: shared hit=6
|
||||
Planning Time: 0.156 ms
|
||||
Execution Time: 0.072 ms
|
||||
(9 rows)
|
||||
|
||||
== 2. Страница 301 через keyset cursor (запрос API с cursor)
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.015..0.036 rows=51 loops=1)
|
||||
Buffers: shared hit=5
|
||||
-> Index Scan using messages_chat_created_id_idx on messages (actual time=0.015..0.029 rows=51 loops=1)
|
||||
Index Cond: ((chat_id = 'ad832a44-34ce-416b-a5f8-f8e528316fd6'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, '1e3006cf-e446-47db-80e3-a34c031790a8'::uuid)))
|
||||
Buffers: shared hit=5
|
||||
Planning Time: 0.232 ms
|
||||
Execution Time: 0.086 ms
|
||||
(7 rows)
|
||||
|
||||
== 3. Для сравнения: та же страница через OFFSET (НЕ используется в API)
|
||||
QUERY PLAN
|
||||
---------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=4.002..4.015 rows=51 loops=1)
|
||||
Buffers: shared hit=313
|
||||
-> Index Scan using messages_chat_created_id_idx on messages (actual time=0.017..3.277 rows=15051 loops=1)
|
||||
Index Cond: (chat_id = 'ad832a44-34ce-416b-a5f8-f8e528316fd6'::uuid)
|
||||
Buffers: shared hit=313
|
||||
Planning Time: 0.088 ms
|
||||
Execution Time: 4.035 ms
|
||||
(7 rows)
|
||||
|
||||
== 4. Для сравнения: keyset-запрос без индекса (DROP INDEX внутри транзакции, затем ROLLBACK)
|
||||
QUERY PLAN
|
||||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=6.187..6.193 rows=51 loops=1)
|
||||
Buffers: shared hit=637
|
||||
-> Sort (actual time=6.185..6.188 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 31kB
|
||||
Buffers: shared hit=637
|
||||
-> Seq Scan on messages (actual time=1.966..5.520 rows=5000 loops=1)
|
||||
Filter: ((chat_id = 'ad832a44-34ce-416b-a5f8-f8e528316fd6'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, '1e3006cf-e446-47db-80e3-a34c031790a8'::uuid)))
|
||||
Rows Removed by Filter: 45029
|
||||
Buffers: shared hit=637
|
||||
Planning:
|
||||
Buffers: shared hit=5
|
||||
Planning Time: 0.096 ms
|
||||
Execution Time: 6.267 ms
|
||||
(14 rows)
|
||||
|
||||
== 5. Список чатов пользователя (запрос API GET /chats, limit=50)
|
||||
QUERY PLAN
|
||||
-----------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.230..0.420 rows=31 loops=1)
|
||||
Buffers: shared hit=33
|
||||
-> Result (actual time=0.229..0.415 rows=31 loops=1)
|
||||
Buffers: shared hit=33
|
||||
-> Sort (actual time=0.145..0.148 rows=31 loops=1)
|
||||
Sort Key: c.created_at, c.id
|
||||
Sort Method: quicksort Memory: 27kB
|
||||
Buffers: shared hit=2
|
||||
-> Hash Join (actual time=0.088..0.097 rows=31 loops=1)
|
||||
Hash Cond: (c.id = m.chat_id)
|
||||
Buffers: shared hit=2
|
||||
-> Seq Scan on chats c (actual time=0.005..0.008 rows=42 loops=1)
|
||||
Buffers: shared hit=1
|
||||
-> Hash (actual time=0.015..0.016 rows=31 loops=1)
|
||||
Buckets: 1024 Batches: 1 Memory Usage: 10kB
|
||||
Buffers: shared hit=1
|
||||
-> Seq Scan on chat_members m (actual time=0.006..0.010 rows=31 loops=1)
|
||||
Filter: (user_id = '264c4b72-36e7-41e2-895d-18a04eb55baf'::uuid)
|
||||
Rows Removed by Filter: 19
|
||||
Buffers: shared hit=1
|
||||
SubPlan 1
|
||||
-> Sort (actual time=0.006..0.006 rows=1 loops=31)
|
||||
Sort Key: cm.user_id
|
||||
Sort Method: quicksort Memory: 25kB
|
||||
Buffers: shared hit=31
|
||||
-> Seq Scan on chat_members cm (actual time=0.003..0.004 rows=1 loops=31)
|
||||
Filter: (chat_id = c.id)
|
||||
Rows Removed by Filter: 49
|
||||
Buffers: shared hit=31
|
||||
Planning:
|
||||
Buffers: shared hit=51
|
||||
Planning Time: 0.700 ms
|
||||
Execution Time: 0.495 ms
|
||||
(33 rows)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
# Fresh empty volume check, 2026-10-29T15:38:02Z (separate throwaway project mtusi-lab2-fresh)
|
||||
migrate-1 | apply 0001_init
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
removed objects: 6
|
||||
@@ -0,0 +1,13 @@
|
||||
# Grade-4 student tests, 2026-10-29T15:36:49Z
|
||||
$ sh scripts/test-grade4.sh
|
||||
PASS test_wrong_password (0.4s)
|
||||
PASS test_logout (0.6s)
|
||||
PASS test_short_ttl (3.2s)
|
||||
PASS test_username_race (1.6s)
|
||||
PASS test_chat_rollback (1.0s)
|
||||
PASS test_cursor_rules (0.8s)
|
||||
PASS test_valkey_cache_role (1.5s)
|
||||
PASS test_revoke_during_cache_partition (1.9s)
|
||||
PASS test_valkey_down (4.2s)
|
||||
PASS test_postgres_down (9.0s)
|
||||
10/10 passed
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"lab": 2,
|
||||
"workload": "closed-loop POST messages; no automatic retries",
|
||||
"requested_duration_seconds": 20.0,
|
||||
"elapsed_seconds": 20.134,
|
||||
"concurrency": 4,
|
||||
"successful_requests": 4920,
|
||||
"failed_requests": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 244.368,
|
||||
"successful_latency_ms": {
|
||||
"p50": 15.203,
|
||||
"p95": 24.258,
|
||||
"p99": 28.968
|
||||
},
|
||||
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
# Migrations re-run evidence, 2026-10-29T15:36:22Z
|
||||
$ docker compose run --rm migrate
|
||||
skip 0001_init
|
||||
$ select version, checksum, applied_at from schema_migrations
|
||||
version | checksum | applied_at
|
||||
-----------+------------------+-------------------------------
|
||||
0001_init | b0f6ac0177a064da | 2026-10-29 15:27:31.980855+00
|
||||
(1 row)
|
||||
|
||||
$ \d messages
|
||||
Table "public.messages"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
------------+--------------------------+-----------+----------+-------------------
|
||||
id | uuid | | not null | gen_random_uuid()
|
||||
chat_id | uuid | | not null |
|
||||
sender_id | uuid | | not null |
|
||||
text | text | | not null |
|
||||
created_at | timestamp with time zone | | not null | clock_timestamp()
|
||||
Indexes:
|
||||
"messages_pkey" PRIMARY KEY, btree (id)
|
||||
"messages_chat_created_id_idx" btree (chat_id, created_at, id)
|
||||
Check constraints:
|
||||
"messages_text_check" CHECK (char_length(text) >= 1 AND char_length(text) <= 4000)
|
||||
Foreign-key constraints:
|
||||
"messages_sender_is_member" FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# Public checks, 2026-10-29T15:36:27Z (run inside tester container: python 3.12 + GNU make)
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
python3 tests/smoke.py --base-url "http://api:8080"
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://api: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.
|
||||
@@ -0,0 +1,35 @@
|
||||
# Restart hook evidence, 2026-10-29T15:35:55Z
|
||||
$ docker compose ps (before)
|
||||
SERVICE STATUS
|
||||
api Up 23 seconds (healthy)
|
||||
postgres Up 26 seconds (healthy)
|
||||
valkey Up 27 seconds (healthy)
|
||||
$ docker volume inspect mtusi-lab2_pgdata (before)
|
||||
mtusi-lab2_pgdata created 2026-10-29T15:27:24Z
|
||||
$ messages count (before)
|
||||
50029
|
||||
$ sh scripts/restart.sh
|
||||
before:
|
||||
/mtusi-lab2-api-1 2026-10-29T15:35:32.425887132Z
|
||||
/mtusi-lab2-postgres-1 2026-10-29T15:35:28.694054577Z
|
||||
/mtusi-lab2-valkey-1 2026-10-29T15:35:28.598642747Z
|
||||
after:
|
||||
/mtusi-lab2-api-1 2026-10-29T15:36:04.421672285Z
|
||||
/mtusi-lab2-postgres-1 2026-10-29T15:36:00.44755603Z
|
||||
/mtusi-lab2-valkey-1 2026-10-29T15:36:00.560733883Z
|
||||
$ docker compose ps (after)
|
||||
SERVICE STATUS
|
||||
api Up 1 second (health: starting)
|
||||
postgres Up 5 seconds (healthy)
|
||||
valkey Up 5 seconds (healthy)
|
||||
$ docker volume inspect mtusi-lab2_pgdata (after)
|
||||
mtusi-lab2_pgdata created 2026-10-29T15:27:24Z
|
||||
$ messages count (after)
|
||||
50029
|
||||
$ docker compose logs postgres --since 1m | grep -E "shut down|ready to accept"
|
||||
postgres-1 | 2026-10-29 15:35:28.065 UTC [1] LOG: database system is shut down
|
||||
postgres-1 | 2026-10-29 15:35:29.149 UTC [29] LOG: database system was shut down at 2026-10-29 15:35:28 UTC
|
||||
postgres-1 | 2026-10-29 15:35:29.158 UTC [1] LOG: database system is ready to accept connections
|
||||
postgres-1 | 2026-10-29 15:35:59.971 UTC [1] LOG: database system is shut down
|
||||
postgres-1 | 2026-10-29 15:36:00.824 UTC [31] LOG: database system was shut down at 2026-10-29 15:35:59 UTC
|
||||
postgres-1 | 2026-10-29 15:36:00.835 UTC [1] LOG: database system is ready to accept connections
|
||||
Reference in New Issue
Block a user