forked from mtusi2026/mtusi2026lab2
156 lines
5.9 KiB
Python
156 lines
5.9 KiB
Python
"""Трек 5, вариант 2: план запроса и восстановление.
|
||
|
||
Сравнивает два индекса сообщений:
|
||
* idx_messages_chat_created_id (chat_id, created_at, id) — row‑comparison курсор
|
||
* idx_messages_chat_id_created_at (chat_id, created_at) — простой диапазон по created_at
|
||
|
||
Измеряет план (EXPLAIN ANALYZE + BUFFERS), время вставки и выполняет
|
||
backup/restore через pg_dump/pg_restore.
|
||
|
||
Запуск (стек должен быть поднят):
|
||
|
||
python student-tests/experiment_query_restore.py
|
||
"""
|
||
import base64
|
||
import os
|
||
import subprocess
|
||
import time
|
||
from datetime import datetime
|
||
|
||
from _harness import BASE_URL, call
|
||
|
||
EVIDENCE = os.path.join(os.path.dirname(__file__), "..", "evidence")
|
||
os.makedirs(EVIDENCE, exist_ok=True)
|
||
OUTFILE = os.path.join(EVIDENCE, "experiment_query_restore.txt")
|
||
|
||
HEADER = "=" * 70
|
||
lines = []
|
||
|
||
|
||
def log(text):
|
||
lines.append(text)
|
||
print(text)
|
||
|
||
|
||
def psql(sql, tuples=False):
|
||
cmd = [
|
||
"docker", "compose", "exec", "-T", "postgres", "psql",
|
||
"-U", "messenger", "-d", "messenger",
|
||
]
|
||
if tuples:
|
||
cmd += ["-tA"]
|
||
cmd += ["-c", sql]
|
||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||
if result.returncode != 0:
|
||
raise RuntimeError(f"psql error: {result.stderr[:500]}")
|
||
return result.stdout
|
||
def register_user_and_chat():
|
||
status, payload, _ = call("POST", "/api/v1/users", body={
|
||
"username": "exp_restore_user",
|
||
"display_name": "EXP_RESTORE",
|
||
"password": "ExpRestorePass123!",
|
||
})
|
||
if status == 201:
|
||
uid = payload["id"]
|
||
else:
|
||
cred = base64.b64encode(b"exp_restore_user:ExpRestorePass123!").decode()
|
||
status, payload, _ = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred})
|
||
uid = payload["user"]["id"]
|
||
cred = base64.b64encode(b"exp_restore_user:ExpRestorePass123!").decode()
|
||
status, payload, _ = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred})
|
||
token = payload["token"]
|
||
auth = {"Authorization": "Bearer " + token}
|
||
status, payload, _ = call("POST", "/api/v1/chats", body={"title": "exp_restore_chat", "member_ids": [uid]}, headers=auth)
|
||
assert status == 201, f"chat creation failed: {status} {payload}"
|
||
return uid, payload["id"], auth
|
||
|
||
|
||
def bulk_insert_messages(chat_id, user_id, count, offset=0):
|
||
sql = f"""
|
||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||
SELECT gen_random_uuid(),
|
||
'{chat_id}'::uuid,
|
||
'{user_id}'::uuid,
|
||
'exp-' || g,
|
||
now() - (g || ' seconds')::interval - ('{offset}' || ' seconds')::interval
|
||
FROM generate_series({offset + 1}, {offset + count}) AS g;
|
||
"""
|
||
start = time.monotonic()
|
||
out = psql(sql)
|
||
elapsed = time.monotonic() - start
|
||
log(f"Bulk insert of {count} messages took {elapsed:.3f}s (last stdout: {out.strip()[:80]})")
|
||
return elapsed
|
||
|
||
|
||
def explain(chat_id, cursor_ts, cursor_id, index_name=None):
|
||
if index_name == "idx_messages_chat_created_id":
|
||
where = f"AND (created_at, id) > ('{cursor_ts}'::timestamptz, '{cursor_id}'::uuid)"
|
||
else:
|
||
where = f"AND created_at > '{cursor_ts}'::timestamptz"
|
||
plan = f"""
|
||
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
||
SELECT id FROM messages
|
||
WHERE chat_id = '{chat_id}'::uuid {where}
|
||
ORDER BY created_at ASC, id ASC
|
||
LIMIT 51
|
||
"""
|
||
return psql(plan)
|
||
|
||
|
||
def backup_restore():
|
||
log(HEADER)
|
||
log("Backup via pg_dump (custom format) and restore into a fresh DB")
|
||
log(HEADER)
|
||
dump_path = "/tmp/messenger_exp_backup.dump"
|
||
subprocess.run(["docker", "compose", "exec", "-T", "postgres", "pg_dump", "-Fc", "-f", dump_path, "messenger"], check=True)
|
||
log(f"pg_dump created at {dump_path}")
|
||
subprocess.run(["docker", "compose", "exec", "-T", "postgres", "createdb", "-U", "messenger", "messenger_restore"], check=False)
|
||
subprocess.run(["docker", "compose", "exec", "-T", "postgres", "pg_restore", "-d", "messenger_restore", dump_path], check=True)
|
||
cnt1 = psql("SELECT count(*) FROM messages", tuples=True).strip()
|
||
cnt2 = psql("SELECT count(*) FROM messenger_restore.messages", tuples=True).strip()
|
||
log(f"messages before restore: {cnt1}, after restore: {cnt2}")
|
||
sample = psql("SELECT id, chat_id, sender_id, text, created_at FROM messages ORDER BY created_at DESC LIMIT 1", tuples=True)
|
||
log(f"sample before restore: {sample.strip()}")
|
||
sample2 = psql("SELECT id, chat_id, sender_id, text, created_at FROM messenger_restore.messages ORDER BY created_at DESC LIMIT 1", tuples=True)
|
||
log(f"sample after restore: {sample2.strip()}")
|
||
|
||
|
||
def main():
|
||
log(HEADER)
|
||
log("Experiment: query plan and restore (track 5, option 2)")
|
||
log(HEADER)
|
||
|
||
uid, chat_id, auth = register_user_and_chat()
|
||
|
||
bulk_insert_messages(chat_id, uid, 90000)
|
||
status, payload, _ = call("POST", "/api/v1/chats", body={"title": "exp_restore_chat2", "member_ids": [uid]}, headers=auth)
|
||
chat2_id = payload["id"]
|
||
bulk_insert_messages(chat2_id, uid, 10000)
|
||
|
||
cursor_row = psql(f"""
|
||
SELECT id::text, created_at::text FROM messages
|
||
WHERE chat_id = '{chat_id}'::uuid
|
||
ORDER BY created_at ASC, id ASC
|
||
LIMIT 1 OFFSET 99950
|
||
""", tuples=True).strip()
|
||
cursor_id, cursor_ts_raw = cursor_row.split("|")
|
||
cursor_ts = datetime.fromisoformat(cursor_ts_raw).isoformat()
|
||
|
||
log("EXPLAIN ANALYZE for idx_messages_chat_created_id")
|
||
plan_a = explain(chat_id, cursor_ts, cursor_id, index_name="idx_messages_chat_created_id")
|
||
log(plan_a)
|
||
|
||
log("EXPLAIN ANALYZE for idx_messages_chat_id_created_at")
|
||
plan_b = explain(chat_id, cursor_ts, cursor_id, index_name="idx_messages_chat_id_created_at")
|
||
log(plan_b)
|
||
|
||
backup_restore()
|
||
|
||
with open(OUTFILE, "w", encoding="utf-8") as f:
|
||
f.write("\n".join(lines))
|
||
log(f"\nResults written to {OUTFILE}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|