diff --git a/evidence/lab2_track2_explain.md b/evidence/lab2_track2_explain.md index 9ddb612c..80e74f70 100644 --- a/evidence/lab2_track2_explain.md +++ b/evidence/lab2_track2_explain.md @@ -2,103 +2,107 @@ ## big chat ``` -Limit (cost=4726.04..4731.85 rows=51 width=69) (actual time=28.316..32.077 rows=51.00 loops=1) - Buffers: shared hit=1639 - -> Gather Merge (cost=4726.04..9161.77 rows=38920 width=69) (actual time=28.313..32.066 rows=51.00 loops=1) +Limit (cost=4887.94..4893.75 rows=51 width=69) (actual time=36.242..39.932 rows=51.00 loops=1) + Buffers: shared hit=2028 + -> Gather Merge (cost=4887.94..6211.25 rows=11611 width=69) (actual time=36.240..39.926 rows=51.00 loops=1) Workers Planned: 1 Workers Launched: 1 - Buffers: shared hit=1639 - -> Sort (cost=3726.03..3783.26 rows=22894 width=69) (actual time=6.294..6.298 rows=25.50 loops=2) + Buffers: shared hit=2028 + -> Sort (cost=3887.93..3905.01 rows=6830 width=69) (actual time=7.197..7.202 rows=25.50 loops=2) Sort Key: created_at, id - Sort Method: top-N heapsort Memory: 38kB - Buffers: shared hit=1639 + Sort Method: top-N heapsort Memory: 35kB + Buffers: shared hit=2028 Worker 0: Sort Method: quicksort Memory: 25kB - -> Parallel Seq Scan on messages (cost=0.00..2962.24 rows=22894 width=69) (actual time=0.010..4.738 rows=13636.50 loops=2) - Filter: ((chat_id = '79d37065-bfb0-4cb1-8f1e-e79533f724ff'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:03:17.148852+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) - Rows Removed by Filter: 51364 - Buffers: shared hit=1624 + -> Parallel Seq Scan on messages (cost=0.00..3660.07 rows=6830 width=69) (actual time=3.983..6.075 rows=20000.00 loops=2) + Filter: ((chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Rows Removed by Filter: 60000 + Buffers: shared hit=2013 Planning: Buffers: shared hit=88 -Planning Time: 1.487 ms -Execution Time: 32.133 ms +Planning Time: 1.550 ms +Execution Time: 39.988 ms ``` ## small chat ``` -Limit (cost=3899.08..3899.09 rows=6 width=69) (actual time=7.785..7.787 rows=1.00 loops=1) - Buffers: shared hit=1630 - -> Sort (cost=3899.08..3899.09 rows=6 width=69) (actual time=7.783..7.783 rows=1.00 loops=1) +Limit (cost=4660.63..4660.64 rows=5 width=69) (actual time=36.246..41.037 rows=1.00 loops=1) + Buffers: shared hit=2019 + -> Sort (cost=4660.63..4660.64 rows=5 width=69) (actual time=36.242..41.032 rows=1.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 25kB - Buffers: shared hit=1630 - -> Seq Scan on messages (cost=0.00..3899.00 rows=6 width=69) (actual time=0.056..7.764 rows=1.00 loops=1) - Filter: ((chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) - Rows Removed by Filter: 129999 - Buffers: shared hit=1624 + Buffers: shared hit=2019 + -> Gather (cost=1000.00..4660.57 rows=5 width=69) (actual time=0.522..41.001 rows=1.00 loops=1) + Workers Planned: 1 + Workers Launched: 1 + Buffers: shared hit=2013 + -> Parallel Seq Scan on messages (cost=0.00..3660.07 rows=3 width=69) (actual time=0.052..5.988 rows=0.50 loops=2) + Filter: ((chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Rows Removed by Filter: 80000 + Buffers: shared hit=2013 Planning: Buffers: shared hit=88 -Planning Time: 1.182 ms -Execution Time: 7.820 ms +Planning Time: 1.837 ms +Execution Time: 41.093 ms ``` ## no_index ``` -Limit (cost=4475.27..4475.40 rows=51 width=69) (actual time=12.589..12.596 rows=51.00 loops=1) - Buffers: shared hit=729 - -> Sort (cost=4475.27..4572.12 rows=38739 width=69) (actual time=12.587..12.589 rows=51.00 loops=1) +Limit (cost=4135.73..4135.86 rows=51 width=69) (actual time=9.172..9.177 rows=51.00 loops=1) + Buffers: shared hit=863 + -> Sort (cost=4135.73..4164.10 rows=11346 width=69) (actual time=9.169..9.172 rows=51.00 loops=1) Sort Key: created_at, id - Sort Method: top-N heapsort Memory: 38kB - Buffers: shared hit=729 - -> Bitmap Heap Scan on messages (cost=609.04..3182.85 rows=38739 width=69) (actual time=0.891..8.050 rows=27273.00 loops=1) - Recheck Cond: (chat_id = '79d37065-bfb0-4cb1-8f1e-e79533f724ff'::uuid) - Filter: (ROW(created_at, id) > ROW('2026-10-02 15:03:17.148852+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)) - Rows Removed by Filter: 27272 - Heap Blocks: exact=675 - Buffers: shared hit=723 - -> Bitmap Index Scan on idx_messages_chat_only (cost=0.00..599.36 rows=54275 width=0) (actual time=0.823..0.823 rows=54545.00 loops=1) - Index Cond: (chat_id = '79d37065-bfb0-4cb1-8f1e-e79533f724ff'::uuid) + Sort Method: top-N heapsort Memory: 35kB + Buffers: shared hit=863 + -> Bitmap Heap Scan on messages (cost=676.65..3757.21 rows=11346 width=69) (actual time=2.808..6.995 rows=40000.00 loops=1) + Recheck Cond: (chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid) + Filter: (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)) + Rows Removed by Filter: 21818 + Heap Blocks: exact=803 + Buffers: shared hit=857 + -> Bitmap Index Scan on idx_messages_chat_only (cost=0.00..673.82 rows=61003 width=0) (actual time=0.873..0.873 rows=61818.00 loops=1) + Index Cond: (chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid) Index Searches: 1 - Buffers: shared hit=48 + Buffers: shared hit=54 Planning: Buffers: shared hit=100 -Planning Time: 2.045 ms -Execution Time: 12.641 ms +Planning Time: 1.562 ms +Execution Time: 9.220 ms ``` ## chat_only ``` -Limit (cost=23.89..23.91 rows=8 width=69) (actual time=0.037..0.037 rows=1.00 loops=1) +Limit (cost=28.62..28.64 rows=11 width=69) (actual time=0.040..0.041 rows=1.00 loops=1) Buffers: shared hit=9 - -> Sort (cost=23.89..23.91 rows=8 width=69) (actual time=0.035..0.035 rows=1.00 loops=1) + -> Sort (cost=28.62..28.64 rows=11 width=69) (actual time=0.038..0.038 rows=1.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 25kB Buffers: shared hit=9 - -> Index Scan using idx_messages_chat_only on messages (cost=0.29..23.77 rows=8 width=69) (actual time=0.015..0.015 rows=1.00 loops=1) + -> Index Scan using idx_messages_chat_only on messages (cost=0.29..28.43 rows=11 width=69) (actual time=0.017..0.017 rows=1.00 loops=1) Index Cond: (chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)) Index Searches: 1 Buffers: shared hit=3 Planning: Buffers: shared hit=100 -Planning Time: 2.240 ms -Execution Time: 0.066 ms +Planning Time: 1.450 ms +Execution Time: 0.069 ms ``` ## full (chat_id, created_at, id) ``` -Limit (cost=0.42..9.42 rows=51 width=69) (actual time=0.101..0.118 rows=51.00 loops=1) - Buffers: shared hit=5 read=1 - -> Index Scan using idx_messages_chat_created on messages (cost=0.42..6871.59 rows=38939 width=69) (actual time=0.098..0.112 rows=51.00 loops=1) - Index Cond: ((chat_id = '79d37065-bfb0-4cb1-8f1e-e79533f724ff'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:03:17.148852+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) +Limit (cost=0.42..28.88 rows=51 width=69) (actual time=0.072..0.187 rows=51.00 loops=1) + Buffers: shared hit=51 read=1 + -> Index Scan using idx_messages_chat_created on messages (cost=0.42..6391.22 rows=11452 width=69) (actual time=0.070..0.182 rows=51.00 loops=1) + Index Cond: ((chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) Index Searches: 1 - Buffers: shared hit=5 read=1 + Buffers: shared hit=51 read=1 Planning: Buffers: shared hit=111 -Planning Time: 1.697 ms -Execution Time: 0.153 ms +Planning Time: 2.685 ms +Execution Time: 0.214 ms ``` diff --git a/evidence/lab2_track2_restore.txt b/evidence/lab2_track2_restore.txt new file mode 100644 index 00000000..ee2deecc --- /dev/null +++ b/evidence/lab2_track2_restore.txt @@ -0,0 +1,8 @@ +Track 2 restore +dump_time_s=2.880 +dump_size_bytes=5401183 +restore_time_s=1.393 +count_original=190001 +count_restored=190001 +dist_match=true +random100_match=true diff --git a/evidence/lab2_track2_rpo.txt b/evidence/lab2_track2_rpo.txt new file mode 100644 index 00000000..169d7658 --- /dev/null +++ b/evidence/lab2_track2_rpo.txt @@ -0,0 +1,4 @@ +Track 2 RPO +marker_message_id=33458232-3904-49f4-bbff-5cdee7acddad +present_in_restore=0 +expected=0 diff --git a/evidence/lab2_track2_write_cost.json b/evidence/lab2_track2_write_cost.json index 95a2d7ee..bc33168c 100644 --- a/evidence/lab2_track2_write_cost.json +++ b/evidence/lab2_track2_write_cost.json @@ -1,20 +1,20 @@ [ { "variant": "no_index", - "insert_10000_s": 1.37, - "indexes_size": "5152 kB", - "total_size": "19 MB" + "insert_10000_s": 1.085, + "indexes_size": "6896 kB", + "total_size": "24 MB" }, { "variant": "chat_only", - "insert_10000_s": 1.093, - "indexes_size": "6648 kB", - "total_size": "21 MB" + "insert_10000_s": 0.992, + "indexes_size": "8880 kB", + "total_size": "27 MB" }, { "variant": "full", - "insert_10000_s": 0.921, - "indexes_size": "15 MB", - "total_size": "31 MB" + "insert_10000_s": 0.977, + "indexes_size": "19 MB", + "total_size": "38 MB" } ] diff --git a/student-tests/test_track2.py b/student-tests/test_track2.py index cbbfeb3d..2b9af5a5 100644 --- a/student-tests/test_track2.py +++ b/student-tests/test_track2.py @@ -25,6 +25,7 @@ import sys import time import uuid from pathlib import Path +import base64 sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) from client import Client, Failure, arguments, check, run # noqa: E402 @@ -325,14 +326,21 @@ def step_backup_restore(dsn: str): def step_rpo(dsn: str, base_url: str, ca_file): print("=== 5. RPO: запись во время pg_dump ===") c = Client(base_url, ca_file) + + # Данные и сессии готовим заранее, до дампа. alice, bob, eve, chat = c.bootstrap() + # Контроль: сессия должна быть валидна, иначе 401 — это не RPO. + me = c.call("GET", "/api/v1/users/me", 200, actor=alice, schema="User") + check(me["status"] == 200, "сессия должна быть валидна перед RPO-тестом") + marker = "rpo-" + uuid.uuid4().hex # Запускаем pg_dump out_file = EVIDENCE / "lab2_track2_rpo.dump" if out_file.exists(): out_file.unlink() + # pg_dump: тот же приём, что в шаге 4. proc = subprocess.Popen( ["docker", "run", "--rm", "--add-host=host.docker.internal:host-gateway", @@ -341,21 +349,34 @@ def step_rpo(dsn: str, base_url: str, ca_file): "pg_dump", "-Fc", "-f", "/out/lab2_track2_rpo.dump", dsn], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) - time.sleep(1.0) # гарантируем, что снапшот дампа уже снят - written = c.message(alice, chat, marker)["json"] + time.sleep(1.0) # снапшот уже снят + + # Свежий логин прямо перед записью: не зависим от TTL старой сессии. + cred = base64.b64encode( + (alice["registration"]["username"] + ":" + + alice["registration"]["password"]).encode() + ).decode() + fresh = c.call("POST", "/api/v1/auth/sessions", 201, + headers={"Authorization": "Basic " + cred}, + schema="Session")["json"] + alice_for_write = {"id": alice["id"], "user": alice["user"], "token": fresh["token"]} + + written = c.message(alice_for_write, chat, marker)["json"] code = proc.wait(timeout=300) check(code == 0, "pg_dump для RPO упал") - # restore во временный контейнер + # Временный restore-контейнер: публикуем порт 55433 на хост. restore_container = "mtusi-pg-rpo" subprocess.run(["docker", "rm", "-f", restore_container], capture_output=True) subprocess.run( ["docker", "run", "-d", "--name", restore_container, "-e", "POSTGRES_PASSWORD=restore", "-e", "POSTGRES_USER=postgres", "-e", "POSTGRES_DB=high_load_messenger", + "-p", "127.0.0.1:55433:5432", "postgres:18-alpine"], capture_output=True, text=True, ) + deadline = time.monotonic() + 60 while time.monotonic() < deadline: c2 = subprocess.run(["docker", "exec", restore_container, "pg_isready", "-U", "postgres"], @@ -363,14 +384,17 @@ def step_rpo(dsn: str, base_url: str, ca_file): if c2.returncode == 0: break time.sleep(1) + else: + raise Failure("restore container для RPO не поднялся") + # pg_restore идёт в опубликованный порт через host.docker.internal. r = subprocess.run( ["docker", "run", "--rm", "--add-host=host.docker.internal:host-gateway", - "--network", "host", "-v", f"{EVIDENCE}:/in", "postgres:18-alpine", - "pg_restore", "-d", "postgresql://postgres:restore@127.0.0.1:5432/high_load_messenger", + "pg_restore", "-d", + "postgresql://postgres:restore@host.docker.internal:55433/high_load_messenger", "--no-owner", "--no-privileges", "/in/lab2_track2_rpo.dump"], capture_output=True, text=True, timeout=600, )