#!/usr/bin/env python3 """Трек 2: план keyset-запроса на неравномерной истории и backup в новый volume.""" import json import re import statistics import subprocess import time import uuid from pathlib import Path from httpapi import bearer, call from sql import ROOT, compose, psql, psql_file EVIDENCE = ROOT / "evidence" BACKUPS = ROOT / "backups" RESTORE_NAME = "mtusi-lab2-restore" RESTORE_VOLUME = "mtusi-lab2-restore-data" INSERT_ROWS = 20000 REPEATS = 3 def log(text): print(text, flush=True) def check(cond, message): if not cond: raise SystemExit("FAIL: " + message) def postgres_image(): text = (ROOT / "compose.yaml").read_text(encoding="utf-8") match = re.search(r"image:\s*(postgres:\S+)", text) check(match is not None, "postgres image tag") return match.group(1) def median(values): return statistics.median(values) def execution_ms(plan): found = re.findall(r"Execution Time:\s+([0-9.]+)\s+ms", plan) check(found, "no execution time in plan") return float(found[-1]) def planning_ms(plan): found = re.findall(r"Planning Time:\s+([0-9.]+)\s+ms", plan) return float(found[-1]) if found else None def first_buffers(plan): found = re.findall(r"Buffers:.*", plan) return found[0].strip() if found else "" def run_insert(label): sql = """ EXPLAIN (ANALYZE, BUFFERS) INSERT INTO track2_write (id, chat_id, sender_id, body, created_at) SELECT gen_random_uuid(), '00000000-0000-4000-8000-000000000001'::uuid, '00000000-0000-4000-8000-000000000002'::uuid, 'w', timestamptz '2025-01-01 00:00:00+00' + (n || ' microseconds')::interval FROM generate_series(1, {rows}) AS n; """.format(rows=INSERT_ROWS) samples = [] psql("TRUNCATE track2_write;") psql_file(sql) for _ in range(REPEATS): psql("TRUNCATE track2_write;") samples.append(execution_ms(psql_file(sql))) result = {"label": label, "rows": INSERT_ROWS, "repeats": samples, "median_ms": median(samples)} log("{} median {:.1f} ms {}".format(label, result["median_ms"], ["{:.1f}".format(x) for x in samples])) return result def psql_on(container, sql): proc = subprocess.run( ["docker", "exec", "-i", container, "psql", "-U", "messenger", "-d", "messenger", "-v", "ON_ERROR_STOP=1", "-q", "-t", "-A"], input=sql.encode("utf-8"), stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) if proc.returncode != 0: raise RuntimeError(proc.stderr.decode("utf-8", "replace")[-500:]) return proc.stdout.decode("utf-8").strip() def main(): EVIDENCE.mkdir(exist_ok=True) BACKUPS.mkdir(exist_ok=True) source = (ROOT / "src" / "store.py").read_text(encoding="utf-8") check("AND (created_at, id) > (%s, %s)" in source, "app query changed") password = "track2-" + uuid.uuid4().hex username = "track2_sender" psql( """ DELETE FROM messages WHERE sender_id IN (SELECT id FROM users WHERE username = 'track2_sender') OR chat_id IN (SELECT id FROM chats WHERE title LIKE 'track2:%'); DELETE FROM chat_members WHERE user_id IN (SELECT id FROM users WHERE username = 'track2_sender') OR chat_id IN (SELECT id FROM chats WHERE title LIKE 'track2:%'); DELETE FROM chats WHERE title LIKE 'track2:%'; DELETE FROM users WHERE username = 'track2_sender'; DROP TABLE IF EXISTS track2_write; """ ) status, _, user = call( "POST", "/api/v1/users", {"username": username, "display_name": "Трек 2", "password": password}, ) check(status == 201, "register track2 {}".format(status)) status, _, session = call( "POST", "/api/v1/auth/sessions", headers=__import__("httpapi").basic(username, password), ) check(status == 201, "login track2") user_id = user["id"] specs = [("track2:big", 70000, 0), ("track2:mid", 20000, 1)] for index in range(10): specs.append(("track2:m{:02d}".format(index), 1000, 2 + index)) for index in range(100): specs.append(("track2:s{:02d}".format(index), 10, 12 + index)) check(sum(item[1] for item in specs) == 101000, "distribution") chat_rows = [] for title, _count, _idx in specs: chat_rows.append( "('{}', '{}', timestamptz '2024-06-01 00:00:00+00')".format(str(uuid.uuid4()), title) ) psql("INSERT INTO chats (id, title, created_at) VALUES\n" + ",\n".join(chat_rows) + ";") psql( """ INSERT INTO chat_members (chat_id, user_id, ordinal) SELECT id, '{}'::uuid, 0 FROM chats WHERE title LIKE 'track2:%'; """.format(user_id) ) statements = ["BEGIN;"] for title, count, index in specs: statements.append( """ INSERT INTO messages (id, chat_id, sender_id, body, created_at) SELECT gen_random_uuid(), c.id, '{user}'::uuid, 't2-' || n::text, timestamptz '2024-06-01 00:00:00+00' + ((n * 1000 + {index}) || ' microseconds')::interval FROM chats c CROSS JOIN generate_series(1, {count}) AS n WHERE c.title = '{title}'; """.format(user=user_id, index=index, count=count, title=title) ) statements.append("COMMIT;") log("loading 101000 messages") started = time.perf_counter() psql("\n".join(statements), timeout=300) load_seconds = time.perf_counter() - started psql("ANALYZE messages; ANALYZE chats; ANALYZE chat_members;") total = int(psql("SELECT count(*) FROM messages WHERE chat_id IN (SELECT id FROM chats WHERE title LIKE 'track2:%');")) check(total == 101000, "loaded {}".format(total)) log("loaded in {:.1f}s".format(load_seconds)) big_id = psql("SELECT id FROM chats WHERE title = 'track2:big';") small_id = psql("SELECT id FROM chats WHERE title = 'track2:s00';") cursor = psql( """ SELECT created_at::text || '|' || id::text FROM messages WHERE chat_id = '{chat}' AND body = 't2-5'; """.format(chat=big_id) ) created_at, message_id = cursor.split("|", 1) check(re.fullmatch(r"[0-9a-f-]{36}", message_id), "cursor id") def keyset_sql(): return """ EXPLAIN (ANALYZE, BUFFERS) SELECT id, chat_id, sender_id, body AS text, created_at FROM messages WHERE chat_id = '{chat}'::uuid AND (created_at, id) > (timestamptz '{ts}', '{mid}'::uuid) ORDER BY created_at, id LIMIT 51; """.format(chat=big_id, ts=created_at, mid=message_id) psql_file(keyset_sql()) good_plan = psql_file(keyset_sql()) (EVIDENCE / "explain_history_index.txt").write_text(good_plan, encoding="utf-8") check("messages_history_idx" in good_plan, "history index was not used") first_plan = psql_file( """ EXPLAIN (ANALYZE, BUFFERS) SELECT id, chat_id, sender_id, body AS text, created_at FROM messages WHERE chat_id = '{chat}'::uuid ORDER BY created_at, id LIMIT 51; """.format(chat=big_id) ) (EVIDENCE / "explain_history_first_page.txt").write_text(first_plan, encoding="utf-8") offset_plan = psql_file( """ EXPLAIN (ANALYZE, BUFFERS) SELECT id, chat_id, sender_id, body AS text, created_at FROM messages WHERE chat_id = '{chat}'::uuid ORDER BY created_at, id OFFSET 5000 LIMIT 50; """.format(chat=big_id) ) (EVIDENCE / "explain_offset.txt").write_text(offset_plan, encoding="utf-8") weak_script = """ BEGIN; DROP INDEX messages_history_idx; CREATE INDEX messages_created_idx ON messages (created_at); ANALYZE messages; {explain} ROLLBACK; """.format(explain=keyset_sql()) weak_plan = psql_file(weak_script) (EVIDENCE / "explain_created_at_index.txt").write_text(weak_plan, encoding="utf-8") still = psql("SELECT indexdef FROM pg_indexes WHERE indexname = 'messages_history_idx';") check("chat_id, created_at, id" in still, "history index missing after rollback") check(psql("SELECT count(*) FROM pg_indexes WHERE indexname = 'messages_created_idx';") == "0", "weak index left behind") status, _, page = call( "GET", "/api/v1/chats/" + big_id + "/messages?limit=2", headers=bearer(session["token"]), ) check(status == 200 and len(page["items"]) == 2, "api page") sql_first = psql( """ SELECT body FROM messages WHERE chat_id = '{chat}' ORDER BY created_at, id LIMIT 2; """.format(chat=big_id) ).splitlines() check([item["text"] for item in page["items"]] == sql_first, "api order != sql") log("write cost") psql( """ CREATE TABLE track2_write ( id uuid PRIMARY KEY, chat_id uuid NOT NULL, sender_id uuid NOT NULL, body text NOT NULL, created_at timestamptz NOT NULL ); """ ) try: pk_only = run_insert("pk_only") psql("CREATE INDEX track2_write_hist ON track2_write (chat_id, created_at, id);") with_hist = run_insert("pk_and_history") psql("CREATE INDEX track2_write_body ON track2_write (chat_id, body);") with_body = run_insert("pk_history_and_body") finally: psql("DROP TABLE IF EXISTS track2_write;") log("backup") dump_started = time.perf_counter() dump = compose( "exec", "-T", "postgres", "pg_dump", "-U", "messenger", "-d", "messenger", "-Fc", "--no-owner", "--no-acl", timeout=300, ) dump_seconds = time.perf_counter() - dump_started dump_path = BACKUPS / "track2.dump" dump_path.write_bytes(dump.stdout) log("dump {:.2f}s, {} bytes".format(dump_seconds, len(dump.stdout))) image = postgres_image() db_password = (ROOT / "secrets" / "postgres_password").read_text(encoding="utf-8").strip() subprocess.run(["docker", "rm", "-f", RESTORE_NAME], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False) subprocess.run(["docker", "volume", "rm", RESTORE_VOLUME], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False) subprocess.run(["docker", "volume", "create", RESTORE_VOLUME], check=True, stdout=subprocess.PIPE) started_container = subprocess.run( [ "docker", "run", "-d", "--name", RESTORE_NAME, "--network", "mtusi-lab2-net", "-e", "POSTGRES_USER=messenger", "-e", "POSTGRES_PASSWORD=" + db_password, "-e", "POSTGRES_DB=messenger", "-v", RESTORE_VOLUME + ":/var/lib/postgresql/data", image, ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) check(started_container.returncode == 0, "restore container") try: deadline = time.time() + 60 while time.time() < deadline: probe = subprocess.run( ["docker", "exec", RESTORE_NAME, "pg_isready", "-U", "messenger", "-d", "messenger"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False, ) if probe.returncode == 0: break time.sleep(0.5) else: raise SystemExit("FAIL: restore postgres not ready") restore_started = time.perf_counter() restored = subprocess.run( [ "docker", "exec", "-i", RESTORE_NAME, "pg_restore", "-U", "messenger", "-d", "messenger", "--no-owner", "--no-acl", "--exit-on-error", ], input=dump.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) restore_seconds = time.perf_counter() - restore_started if restored.returncode != 0: err = restored.stderr.decode("utf-8", "replace") err = err.replace(db_password, "***") raise SystemExit("FAIL: pg_restore\n" + err[-800:]) log("restore {:.2f}s".format(restore_seconds)) def counts(sql): return { "messages": int(sql("SELECT count(*) FROM messages;")), "users": int(sql("SELECT count(*) FROM users;")), "chats": int(sql("SELECT count(*) FROM chats;")), "members": int(sql("SELECT count(*) FROM chat_members;")), "big": int(sql("SELECT count(*) FROM messages WHERE chat_id = '{}'::uuid;".format(big_id))), "small": int(sql("SELECT count(*) FROM messages WHERE chat_id = '{}'::uuid;".format(small_id))), "small_bodies": sql( """ SELECT string_agg(body, ',' ORDER BY created_at, id) FROM messages WHERE chat_id = '{}'::uuid; """.format(small_id) ), "markers": sql( """ SELECT string_agg(body, ',' ORDER BY created_at, id) FROM messages WHERE chat_id = '{}'::uuid AND body IN ('t2-1', 't2-500', 't2-70000'); """.format(big_id) ), } src_counts = counts(psql) dst_counts = counts(lambda sql: psql_on(RESTORE_NAME, sql)) check(src_counts == dst_counts, "restore mismatch {} {}".format(src_counts, dst_counts)) log("counts match {}".format({k: src_counts[k] for k in ("messages", "users", "chats", "members", "big", "small")})) finally: subprocess.run(["docker", "rm", "-f", RESTORE_NAME], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False) subprocess.run(["docker", "volume", "rm", RESTORE_VOLUME], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False) if dump_path.exists(): dump_path.unlink() settings = psql( """ SELECT name || '=' || setting FROM pg_settings WHERE name IN ('fsync', 'synchronous_commit', 'full_page_writes', 'wal_level') ORDER BY name; """ ) version = psql("SELECT version();") size = psql("SELECT pg_size_pretty(pg_total_relation_size('messages'));") info = subprocess.run( ["docker", "info", "--format", "{{.NCPU}}|{{.MemTotal}}|{{.OperatingSystem}}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, text=True, ) summary = { "loaded_messages": total, "load_seconds": round(load_seconds, 2), "distribution": { "big": 70000, "mid": 20000, "medium_10x1000": 10000, "small_100x10": 1000, }, "messages_relation_size": size, "history_index_deep": { "execution_ms": execution_ms(good_plan), "planning_ms": planning_ms(good_plan), "buffers": first_buffers(good_plan), }, "history_index_first_page": { "execution_ms": execution_ms(first_plan), "buffers": first_buffers(first_plan), }, "offset_5000": { "execution_ms": execution_ms(offset_plan), "buffers": first_buffers(offset_plan), }, "created_at_index_deep": { "execution_ms": execution_ms(weak_plan), "buffers": first_buffers(weak_plan), }, "write_cost": [pk_only, with_hist, with_body], "dump_seconds": round(dump_seconds, 3), "restore_seconds": round(restore_seconds, 3), "counts": src_counts, "postgres": version, "durability": settings.splitlines(), "docker_info": info.stdout.strip(), "api_first_page_matches_sql": True, } (EVIDENCE / "track2_summary.json").write_text( json.dumps(summary, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) log("PASS track2") if __name__ == "__main__": main()