#!/usr/bin/env python3 """Track 2: skewed data, real planner, read/write cost, pg_dump/restore. Isolated research/restore volumes; the API database is never altered. """ import hashlib import json import os from pathlib import Path import platform import re import statistics import subprocess import time import uuid ROOT=Path(__file__).resolve().parents[1] COMPOSE=['docker','compose','--env-file','.env'] DBNAME='messenger' def command(args,data=None): p=subprocess.run(args,cwd=ROOT,input=data,stdout=subprocess.PIPE,stderr=subprocess.PIPE) if p.returncode:raise RuntimeError('Experiment command failed: '+args[0]+' '+p.stderr.decode()[-1000:]) return p.stdout def sql(query,service='research-db',database=None): return command(COMPOSE+['exec','-T',service,'psql','-X','-q','-U','messenger','-d',database or DBNAME,'-At','-v','ON_ERROR_STOP=1'],query.encode()).decode().strip() def explain(query,service='research-db'): return json.loads(sql('EXPLAIN (ANALYZE,BUFFERS,WAL,FORMAT JSON) '+query,service))[0] def pgbench(script,clients=1,transactions=100): command(COMPOSE+['exec','-T','research-db','sh','-c','cat > /tmp/lab2-bench.sql'],script.encode()) raw=command(COMPOSE+['exec','-T','research-db','pgbench','-U','messenger','-d',DBNAME,'-n','-M','prepared','-c',str(clients),'-j',str(clients),'-t',str(transactions),'-f','/tmp/lab2-bench.sql']).decode() failure=re.search(r'number of failed transactions:\s*(\d+)',raw) assert failure is None or int(failure[1])==0 return {'raw':raw,'latency_avg_ms':float(re.search(r'latency average = ([\d.]+)',raw)[1]),'tps':float(re.search(r'tps = ([\d.]+)',raw)[1]),'errors':int(failure[1]) if failure else 0} def signature(service): q="""SELECT json_build_object('users',(SELECT count(*) FROM users),'chats',(SELECT count(*) FROM chats),'members',(SELECT count(*) FROM chat_members),'messages',(SELECT count(*) FROM messages),'fingerprint',(SELECT md5(string_agg(id::text||chat_id::text||sender_id::text||text||created_at::text,'' ORDER BY id)) FROM messages),'samples',(SELECT json_agg(m) FROM (SELECT id,chat_id,sender_id,text,created_at FROM messages ORDER BY id LIMIT 5) m))""" return json.loads(sql(q,service)) def metrics(): raw=command(COMPOSE+['exec','-T','research-db','sh','-c','cat /sys/fs/cgroup/memory.current /sys/fs/cgroup/cpu.stat']).decode().splitlines() return int(raw[0]),int(next(line.split()[1] for line in raw if line.startswith('usage_usec '))) def main(): # A fresh pair of databases per run inside dedicated services; previous # experiment databases/volumes are retained, not destroyed. command(COMPOSE+['--profile','research','up','-d','--wait','research-db','restore-db']) # Create a new database name per run: never DROP or overwrite old data. global DBNAME DBNAME='experiment_'+uuid.uuid4().hex[:12] for service in ['research-db','restore-db']:sql('CREATE DATABASE '+DBNAME,service,database='messenger') sql((ROOT/'internal/database/migrations/001_initial.sql').read_text()) sizes=[80000,15000,4900,100] # Synthetic fixtures use SQL for fast deterministic seeding; read/write # measurements are SQL, not claimed to be full HTTP measurements. sql("INSERT INTO users(id,username,display_name,password_hash) VALUES('00000000-0000-4000-8000-000000000001','research','Research','synthetic-no-login')") chat_ids=[] for j,n in enumerate(sizes,1): cid=f'00000000-0000-4000-8000-{j:012d}';chat_ids.append(cid) sql(f"INSERT INTO chats(id,title) VALUES('{cid}','Chat {n}'); INSERT INTO chat_members VALUES('{cid}','00000000-0000-4000-8000-000000000001'); INSERT INTO messages SELECT md5('{j}:'||i::text)::uuid,'{cid}'::uuid,'00000000-0000-4000-8000-000000000001'::uuid,'synthetic-'||i||repeat('x',128),'2026-01-01T00:00:00Z'::timestamptz+i*interval '1 microsecond' FROM generate_series(1,{n}) i;") assert sql('SELECT count(*) FROM messages')=='100000' sql('VACUUM ANALYZE messages') result={'date_utc':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),'database':DBNAME, 'method':{'sizes':sizes,'messages':100000,'page_size':51,'repeats':3,'clients':[1,4], 'profile':'warm SQL keyset GET at 90% and first page; pgbench prepared; 100 transactions/client; 20 warmup transactions', 'write_profile':'1000-row INSERT plus real COMMIT, 3 repeats, pgbench single client on separate write_probe table', 'postgres_limits':{'cpu':2,'memory_mib':512},'host':platform.platform(), 'clock':'SQL planner execution vs pgbench full transaction latency; excludes HTTP/auth/JSON', 'note':'Closed-loop; warm cache; one batch write transaction, not a maximum-capacity result'}, 'versions':{'postgres':sql('SELECT version()'),'durability':sql("SELECT json_build_object('fsync',current_setting('fsync'),'synchronous_commit',current_setting('synchronous_commit'),'full_page_writes',current_setting('full_page_writes'))")}, 'variants':[]} variants=[('chat_only','(chat_id)'),('composite','(chat_id,created_at,id)'),('covering','(chat_id,created_at,id) INCLUDE(sender_id,text)')] for name,index in variants: print('Measuring '+name,flush=True) measured_start=time.monotonic() memory_before,cpu_before=metrics() sql('DROP INDEX IF EXISTS messages_history_idx; DROP INDEX IF EXISTS experiment_history_idx; CREATE INDEX experiment_history_idx ON messages '+index+'; ANALYZE messages;') trial={'name':name,'index':index,'index_bytes':int(sql("SELECT pg_relation_size('experiment_history_idx')")),'reads':[],'writes':[]} for n,cid in zip(sizes,chat_ids): for position,fraction in [('first',0),('deep',.9)]: offset=int(n*fraction) query=f"SELECT id::text,chat_id::text,sender_id::text,text,created_at FROM messages WHERE chat_id='{cid}'::uuid AND (created_at,id)>('2026-01-01T00:00:00Z'::timestamptz+{offset}*interval '1 microsecond','00000000-0000-0000-0000-000000000000'::uuid) ORDER BY messages.created_at,messages.id LIMIT 51" plans=[explain(query) for _ in range(3)] # EXPLAIN actual executes the exact API row tuple boundary SQL. for clients in [1,4]: pgbench(query+';\n',clients,20) runs=[pgbench(query+';\n',clients,100) for _ in range(3)] trial['reads'].append({'chat_messages':n,'position':position,'clients':clients,'plans':plans if clients==1 else [],'runs':runs, 'median_execution_ms':statistics.median(p['Execution Time'] for p in plans), 'median_latency_ms':statistics.median(r['latency_avg_ms'] for r in runs), 'median_tps':statistics.median(r['tps'] for r in runs),'errors':sum(r['errors'] for r in runs)}) # Independent identical heap and PK: only the secondary index differs. # Counts in messages stay fixed; write_probe is not part of the backup. sql("CREATE TABLE write_probe(id uuid PRIMARY KEY,chat_id uuid NOT NULL,sender_id uuid NOT NULL,text text NOT NULL,created_at timestamptz NOT NULL); CREATE INDEX write_probe_idx ON write_probe "+index) for repeat in range(3): sql('TRUNCATE write_probe') query="INSERT INTO write_probe SELECT md5('write:'||i)::uuid,'00000000-0000-4000-8000-000000000001'::uuid,'00000000-0000-4000-8000-000000000001'::uuid,'synthetic-'||i||repeat('x',128),'2026-01-01'::timestamptz+i*interval '1 microsecond' FROM generate_series(1,1000) i" plan=explain(query);sql('TRUNCATE write_probe') run=pgbench('BEGIN;\n'+query+';\nCOMMIT;\n',1,1) trial['writes'].append({'repeat':repeat+1,'plan':plan,'transaction':run,'table_bytes':int(sql("SELECT pg_total_relation_size('write_probe')"))}) sql('DROP TABLE write_probe') memory_after,cpu_after=metrics() elapsed=time.monotonic()-measured_start trial['resources']={'measurement_seconds':round(elapsed,3),'cpu_percent':round((cpu_after-cpu_before)/1e6/elapsed*100,3),'memory_before_bytes':memory_before,'memory_after_bytes':memory_after,'note':'cgroup memory includes shared buffers/page cache, not API RSS; CPU window includes setup and SQL trials'} result['variants'].append(trial) # Return to chosen production schema before backup. sql('DROP INDEX experiment_history_idx; CREATE INDEX messages_history_idx ON messages(chat_id,created_at,id); ANALYZE messages') before=signature('research-db') # Binary custom dump is temporary container-external bytes, never committed. start=time.monotonic();dump=command(COMPOSE+['exec','-T','research-db','pg_dump','-U','messenger','-d',DBNAME,'-Fc','--no-owner','--no-acl']);backup_seconds=time.monotonic()-start start=time.monotonic();command(COMPOSE+['exec','-T','restore-db','pg_restore','-U','messenger','-d',DBNAME,'--no-owner','--no-acl','--exit-on-error','--single-transaction'],dump);restore_seconds=time.monotonic()-start sql('ANALYZE','restore-db');after=signature('restore-db');assert before==after # Prove the backup cutoff using one acknowledged, post-backup INSERT. sql("INSERT INTO messages VALUES('ffffffff-ffff-4fff-8fff-ffffffffffff','00000000-0000-4000-8000-000000000001','00000000-0000-4000-8000-000000000001','after-backup-marker',clock_timestamp())") assert sql("SELECT count(*) FROM messages WHERE id='ffffffff-ffff-4fff-8fff-ffffffffffff'",'restore-db')=='0' result['backup_restore']={'backup_seconds':round(backup_seconds,3),'restore_seconds':round(restore_seconds,3),'dump_bytes':len(dump),'dump_sha256':hashlib.sha256(dump).hexdigest(),'before':before,'after':after,'equal':True,'source_after_backup_messages':100001,'restored_messages':100000,'post_backup_marker_present':False,'new_storage':'restore-db, separate named restore_data volume, new empty database', 'rpo':'snapshot cutoff; demonstrated 1 later committed message absent, no WAL archive/PITR','rto':'restore only; excludes incident detection/provisioning/API switchover'} result['result']='PASS' evidence=ROOT/'evidence';(evidence/'experiment.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n') lines=['# SQL-эксперимент: результаты','', '100 000 сообщений, чаты 80 000 / 15 000 / 4900 / 100. Медианы 3 повторов; warm SQL, без HTTP.','', '| Индекс | Чат | Страница | Клиенты | latency, ms | TPS | EXPLAIN, ms | Ошибки |','|---|---:|---|---:|---:|---:|---:|---:|'] for v in result['variants']: for r in v['reads']:lines.append(f"| {v['name']} | {r['chat_messages']} | {r['position']} | {r['clients']} | {r['median_latency_ms']:.3f} | {r['median_tps']:.1f} | {r['median_execution_ms']:.3f} | {r['errors']} |") lines+=['','| Индекс | Размер индекса, KiB | INSERT 1000 + COMMIT, ms |','|---|---:|---:|'] for v in result['variants']:lines.append(f"| {v['name']} | {v['index_bytes']/1024:.1f} | {statistics.median(w['transaction']['latency_avg_ms'] for w in v['writes']):.3f} |") lines+=['',f"Backup: {backup_seconds:.3f}s; restore: {restore_seconds:.3f}s; {len(dump)} bytes; 100000 сообщений, полный fingerprint и 5 выбранных записей совпали. После backup добавлено 1 сообщение: восстановленная БД его не содержит."] (evidence/'experiment-summary.md').write_text('\n'.join(lines)+'\n') print('PASS: SQL comparison and restore of 100000 messages',flush=True) if __name__=='__main__':main()