ЛР 2: PostgreSQL, сессии, Valkey и исследование восстановления

This commit is contained in:
2026-10-30 21:03:37 +03:00
parent 65003b6119
commit 5b47c579cc
18 changed files with 1330 additions and 32 deletions
+35
View File
@@ -0,0 +1,35 @@
Лабораторная 2 — фактический прогон
Дата: 2026-10-30
Среда: Docker Desktop 29.8.2; Docker VM ARM64, 8 CPU, 4 106 604 544 bytes RAM; лимиты отдельных сервисов не заданы.
Образы: python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0; postgres:16.4-bookworm; valkey/valkey:8.0.1-alpine.
Python packages в работающем API совпали с requirements.txt: argon2-cffi 23.1.0, bindings 26.1.0, cffi 2.1.1, pycparser 3.12, psycopg/binary 3.2.3, psycopg-pool 3.3.3, redis 5.2.1, typing_extensions 4.16.0.
make check
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
docker compose --env-file .env config --quiet
PASS (exit 0)
docker compose --env-file .env up -d --build
PASS; API, PostgreSQL and Valkey reported healthy. API published only on 127.0.0.1:8080.
make test BASE_URL=http://127.0.0.1:8080
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
make track2
PASS. Full plans, six write trials, row counts, backup and restore timings are in track2-results.json.
Seed: 100,000 messages; 80,000 in one large chat and 20,000 across 200 small chats.
Restore: separate PostgreSQL 16.4 process/service and persistent restore_data volume.
make quality BASE_URL=http://127.0.0.1:8080
PASS: registration race returned exactly 1 HTTP 201 and 11 HTTP 409; invalid chat creation left no chat row.
EXPLAIN (ANALYZE, BUFFERS): Index Only Scan using messages_history_keyset; 50 rows; Execution Time 0.046 ms; Heap Fetches 0.
make test-dependencies BASE_URL=http://127.0.0.1:8080
PASS: Valkey outage fell back to PostgreSQL; PostgreSQL outage returned bounded HTTP 503 and recovered with the same session.
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://127.0.0.1:8080
PASS: persistence preserved confirmed data and existing session after restarting API, PostgreSQL and Valkey; volumes retained.
make test-ttl BASE_URL=http://127.0.0.1:8080 (SESSION_TTL_SECONDS=2, temporary Compose env file)
PASS: session expired at the configured two-second TTL. The regular .env was restored to SESSION_TTL_SECONDS=3600 and the final smoke passed again.
+46
View File
@@ -0,0 +1,46 @@
2026-10-30 — финальная проверка оформления и переноса собственных тестов в student-tests
$ make check
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
Exit code: 0
$ docker compose --env-file .env config --quiet
Exit code: 0
$ make test BASE_URL=http://127.0.0.1:8080
python3 tests/smoke.py --base-url "http://127.0.0.1:8080"
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
Exit code: 0
$ make quality BASE_URL=http://127.0.0.1:8080
python3 student-tests/quality.py --base-url "http://127.0.0.1:8080"
{
"registration_race": {
"201": 1,
"409": 11
},
"chat_rollback": "PASS",
"history_index_plan": [
"Limit (cost=0.42..2.85 rows=50 width=24) (actual time=0.057..0.062 rows=50 loops=1)",
" Buffers: shared hit=2 read=2",
" -> Index Only Scan using messages_history_keyset on messages (cost=0.42..3894.60 rows=79896 width=24) (actual time=0.056..0.059 rows=50 loops=1)",
" Index Cond: (chat_id = 'f316dbbe-85f1-4587-454d-bfe607cf01de'::uuid)",
" Heap Fetches: 0",
" Buffers: shared hit=2 read=2",
"Planning:",
" Buffers: shared hit=110",
"Planning Time: 0.263 ms",
"Execution Time: 0.073 ms"
]
}
Exit code: 0
$ make test-dependencies BASE_URL=http://127.0.0.1:8080
python3 student-tests/dependencies.py --base-url "http://127.0.0.1:8080"
PASS: Valkey outage fell back to PostgreSQL; PostgreSQL outage returned bounded 503 and recovered with the same session
Exit code: 0
$ make track2
PASS: row counts and selected messages match; full plans and measured values saved in evidence/track2-results.json
Exit code: 0
+512
View File
@@ -0,0 +1,512 @@
{
"seed": "100000 messages, fixed seed/tag lab2_2026",
"restore_target": "separate PostgreSQL 16.4 service with its own restore_data volume",
"index_plans": [
{
"index": "A chat,created_at,id",
"total_runtime_ms": 0.04,
"shared_read_blocks": 3,
"shared_hit_blocks": 3,
"index_size": "5776 kB",
"plan": {
"Node Type": "Limit",
"Parallel Aware": false,
"Async Capable": false,
"Startup Cost": 0.42,
"Total Cost": 13.59,
"Plan Rows": 50,
"Plan Width": 178,
"Actual Startup Time": 0.033,
"Actual Total Time": 0.04,
"Actual Rows": 50,
"Actual Loops": 1,
"Shared Hit Blocks": 3,
"Shared Read Blocks": 3,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"Plans": [
{
"Node Type": "Index Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Scan Direction": "Forward",
"Index Name": "bench_messages_lab2_2026_a",
"Relation Name": "bench_messages_lab2_2026",
"Alias": "bench_messages_lab2_2026",
"Startup Cost": 0.42,
"Total Cost": 12636.91,
"Plan Rows": 47969,
"Plan Width": 178,
"Actual Startup Time": 0.032,
"Actual Total Time": 0.037,
"Actual Rows": 50,
"Actual Loops": 1,
"Index Cond": "((chat_id = 'f316dbbe-85f1-4587-454d-bfe607cf01de'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:40+00'::timestamp with time zone, '1791a97a-8403-730e-e076-0489a2aeb992'::uuid)))",
"Rows Removed by Index Recheck": 0,
"Shared Hit Blocks": 3,
"Shared Read Blocks": 3,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
}
]
}
},
{
"index": "B covering history index",
"total_runtime_ms": 0.029,
"shared_read_blocks": 4,
"shared_hit_blocks": 4,
"index_size": "7544 kB",
"plan": {
"Node Type": "Limit",
"Parallel Aware": false,
"Async Capable": false,
"Startup Cost": 0.42,
"Total Cost": 14.03,
"Plan Rows": 50,
"Plan Width": 177,
"Actual Startup Time": 0.018,
"Actual Total Time": 0.029,
"Actual Rows": 50,
"Actual Loops": 1,
"Shared Hit Blocks": 4,
"Shared Read Blocks": 4,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"Plans": [
{
"Node Type": "Index Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Scan Direction": "Backward",
"Index Name": "bench_messages_lab2_2026_b",
"Relation Name": "bench_messages_lab2_2026",
"Alias": "bench_messages_lab2_2026",
"Startup Cost": 0.42,
"Total Cost": 13060.0,
"Plan Rows": 47981,
"Plan Width": 177,
"Actual Startup Time": 0.017,
"Actual Total Time": 0.027,
"Actual Rows": 50,
"Actual Loops": 1,
"Index Cond": "((chat_id = 'f316dbbe-85f1-4587-454d-bfe607cf01de'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:40+00'::timestamp with time zone, '1791a97a-8403-730e-e076-0489a2aeb992'::uuid)))",
"Rows Removed by Index Recheck": 0,
"Shared Hit Blocks": 4,
"Shared Read Blocks": 4,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
}
]
}
}
],
"write_cost": [
{
"trial": 1,
"index": "A",
"rows": 10000,
"execution_ms": 25.63,
"plan": {
"Node Type": "ModifyTable",
"Operation": "Insert",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "bench_messages_lab2_2026_wa1",
"Alias": "bench_messages_lab2_2026_wa1",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 0,
"Plan Width": 0,
"Actual Startup Time": 25.564,
"Actual Total Time": 25.565,
"Actual Rows": 0,
"Actual Loops": 1,
"Shared Hit Blocks": 41338,
"Shared Read Blocks": 5,
"Shared Dirtied Blocks": 249,
"Shared Written Blocks": 246,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"Plans": [
{
"Node Type": "Function Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Function Name": "generate_series",
"Alias": "g",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 10000,
"Plan Width": 572,
"Actual Startup Time": 0.505,
"Actual Total Time": 10.841,
"Actual Rows": 10000,
"Actual Loops": 1,
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
}
]
}
},
{
"trial": 1,
"index": "B",
"rows": 10000,
"execution_ms": 23.668,
"plan": {
"Node Type": "ModifyTable",
"Operation": "Insert",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "bench_messages_lab2_2026_wb1",
"Alias": "bench_messages_lab2_2026_wb1",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 0,
"Plan Width": 0,
"Actual Startup Time": 23.603,
"Actual Total Time": 23.603,
"Actual Rows": 0,
"Actual Loops": 1,
"Shared Hit Blocks": 41421,
"Shared Read Blocks": 6,
"Shared Dirtied Blocks": 291,
"Shared Written Blocks": 286,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"Plans": [
{
"Node Type": "Function Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Function Name": "generate_series",
"Alias": "g",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 10000,
"Plan Width": 572,
"Actual Startup Time": 0.913,
"Actual Total Time": 10.025,
"Actual Rows": 10000,
"Actual Loops": 1,
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
}
]
}
},
{
"trial": 2,
"index": "B",
"rows": 10000,
"execution_ms": 22.507,
"plan": {
"Node Type": "ModifyTable",
"Operation": "Insert",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "bench_messages_lab2_2026_wb2",
"Alias": "bench_messages_lab2_2026_wb2",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 0,
"Plan Width": 0,
"Actual Startup Time": 22.451,
"Actual Total Time": 22.451,
"Actual Rows": 0,
"Actual Loops": 1,
"Shared Hit Blocks": 41420,
"Shared Read Blocks": 6,
"Shared Dirtied Blocks": 291,
"Shared Written Blocks": 286,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"Plans": [
{
"Node Type": "Function Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Function Name": "generate_series",
"Alias": "g",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 10000,
"Plan Width": 572,
"Actual Startup Time": 0.302,
"Actual Total Time": 9.352,
"Actual Rows": 10000,
"Actual Loops": 1,
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
}
]
}
},
{
"trial": 2,
"index": "A",
"rows": 10000,
"execution_ms": 74.828,
"plan": {
"Node Type": "ModifyTable",
"Operation": "Insert",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "bench_messages_lab2_2026_wa2",
"Alias": "bench_messages_lab2_2026_wa2",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 0,
"Plan Width": 0,
"Actual Startup Time": 74.737,
"Actual Total Time": 74.738,
"Actual Rows": 0,
"Actual Loops": 1,
"Shared Hit Blocks": 41338,
"Shared Read Blocks": 5,
"Shared Dirtied Blocks": 249,
"Shared Written Blocks": 246,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"Plans": [
{
"Node Type": "Function Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Function Name": "generate_series",
"Alias": "g",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 10000,
"Plan Width": 572,
"Actual Startup Time": 0.32,
"Actual Total Time": 16.837,
"Actual Rows": 10000,
"Actual Loops": 1,
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
}
]
}
},
{
"trial": 3,
"index": "A",
"rows": 10000,
"execution_ms": 21.256,
"plan": {
"Node Type": "ModifyTable",
"Operation": "Insert",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "bench_messages_lab2_2026_wa3",
"Alias": "bench_messages_lab2_2026_wa3",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 0,
"Plan Width": 0,
"Actual Startup Time": 21.201,
"Actual Total Time": 21.202,
"Actual Rows": 0,
"Actual Loops": 1,
"Shared Hit Blocks": 41338,
"Shared Read Blocks": 5,
"Shared Dirtied Blocks": 249,
"Shared Written Blocks": 246,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"Plans": [
{
"Node Type": "Function Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Function Name": "generate_series",
"Alias": "g",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 10000,
"Plan Width": 572,
"Actual Startup Time": 0.34,
"Actual Total Time": 9.433,
"Actual Rows": 10000,
"Actual Loops": 1,
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
}
]
}
},
{
"trial": 3,
"index": "B",
"rows": 10000,
"execution_ms": 21.909,
"plan": {
"Node Type": "ModifyTable",
"Operation": "Insert",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "bench_messages_lab2_2026_wb3",
"Alias": "bench_messages_lab2_2026_wb3",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 0,
"Plan Width": 0,
"Actual Startup Time": 21.854,
"Actual Total Time": 21.855,
"Actual Rows": 0,
"Actual Loops": 1,
"Shared Hit Blocks": 41420,
"Shared Read Blocks": 6,
"Shared Dirtied Blocks": 291,
"Shared Written Blocks": 286,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"Plans": [
{
"Node Type": "Function Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Function Name": "generate_series",
"Alias": "g",
"Startup Cost": 0.0,
"Total Cost": 350.0,
"Plan Rows": 10000,
"Plan Width": 572,
"Actual Startup Time": 0.295,
"Actual Total Time": 9.481,
"Actual Rows": 10000,
"Actual Loops": 1,
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
}
]
}
}
],
"backup_bytes": 6882247,
"backup_seconds": 0.556,
"restore_seconds": 0.608,
"row_counts": {
"messenger": {
"users": 53,
"chats": 214,
"chat_members": 227,
"messages": 100015,
"sessions": 42
},
"messenger_restore_check": {
"users": 53,
"chats": 214,
"chat_members": 227,
"messages": 100015,
"sessions": 42
}
},
"first_ten_rows_match": true
}