Files
mtusi2026lab2/research/worker.py
T

157 lines
9.4 KiB
Python

"""Controlled SQL measurements on isolated databases, never the application DB."""
import datetime as dt
import hashlib
import json
import math
import os
from pathlib import Path
import statistics
import sys
import time
import uuid
import psycopg
from psycopg import sql
from psycopg.conninfo import make_conninfo
BASE=Path(os.environ['DATABASE_URL_FILE']).read_text().strip()
SIZES=[80000,15000,4000,1000]
USER=uuid.UUID('11111111-1111-4111-8111-111111111111')
START=dt.datetime(2026,1,1,tzinfo=dt.timezone.utc)
def stats(values):
ordered=sorted(values)
def pct(p):
return round(ordered[math.ceil(p*len(ordered))-1],4)
return {'p50':pct(.5),'p95':pct(.95),'p99':pct(.99),'mean':round(statistics.mean(values),4)}
def fingerprint(connection):
counts={table:connection.execute(sql.SQL('SELECT count(*) FROM {}').format(sql.Identifier(table))).fetchone()[0]
for table in ('users','chats','chat_members','messages','sessions','schema_migrations','app_settings')}
digest=hashlib.sha256()
with connection.cursor(name='fingerprint') as cursor:
cursor.execute('SELECT id,chat_id,sender_id,text,created_at FROM messages ORDER BY id')
for row in cursor:
digest.update((json.dumps(row,default=str,separators=(',',':'))+'\n').encode())
samples=[]
for offset in (1,500,80000,80001,95000,99000,100000):
row=connection.execute('SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE id=md5(%s)::uuid',('research-message-'+str(offset),)).fetchone()
samples.append([str(v) if isinstance(v,(uuid.UUID,dt.datetime)) else v for v in row])
return {'counts':counts,'all_messages_sha256':digest.hexdigest(),'selected_messages':samples}
def benchmark():
prefix=os.environ['RESEARCH_PREFIX']
report={'postgres_version':None,'dataset_messages':sum(SIZES),'chat_sizes':SIZES,'read_repeats':30,'warmup_queries':3,
'write_batches':10,'write_batch_size':1000,'limits':{'postgres_cpus':1,'postgres_memory_mib':512,'client_cpus':1,'client_memory_mib':512},
'variants':[]}
databases=[]
for variant in ('chat_only','keyset','covering'):
name=prefix+'_'+variant
with psycopg.connect(BASE,autocommit=True) as admin:
admin.execute(sql.SQL('CREATE DATABASE {}').format(sql.Identifier(name)))
databases.append(name)
dsn=make_conninfo(BASE,dbname=name)
with psycopg.connect(dsn,autocommit=True) as c:
report['postgres_version']=c.execute('SHOW server_version').fetchone()[0]
migration=(Path('/app/migrations/001_initial.sql')).read_text()
c.execute('CREATE TABLE schema_migrations(version text PRIMARY KEY,sha256 text NOT NULL,applied_at timestamptz NOT NULL DEFAULT clock_timestamp())')
c.execute(migration)
c.execute('INSERT INTO schema_migrations(version,sha256) VALUES (%s,%s)',('001_initial.sql',hashlib.sha256(migration.encode()).hexdigest()))
c.execute("INSERT INTO app_settings(name,value) VALUES ('cursor_key',%s)",('00'*32,))
if variant=='chat_only':
c.execute('DROP INDEX messages_chat_created_id_idx')
c.execute('CREATE INDEX messages_chat_only_idx ON messages(chat_id)')
elif variant=='covering':
c.execute('DROP INDEX messages_chat_created_id_idx')
c.execute('CREATE INDEX messages_chat_covering_idx ON messages(chat_id,created_at,id) INCLUDE (sender_id,text)')
c.execute('INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s)',(USER,'research_user','Synthetic fixture','research-only-not-a-real-password'))
c.execute("INSERT INTO sessions(token_hash,user_id,expires_at) VALUES (%s,%s,clock_timestamp()+interval '1 hour')",(hashlib.sha256(b'synthetic-not-a-session-token').digest(),USER))
offset=0
seeded=time.perf_counter()
for index,size in enumerate(SIZES,1):
cid=uuid.UUID(int=index)
c.execute('INSERT INTO chats(id,title) VALUES (%s,%s)',(cid,'Synthetic chat '+str(index)))
c.execute('INSERT INTO chat_members(chat_id,user_id) VALUES (%s,%s)',(cid,USER))
c.execute("INSERT INTO messages(id,chat_id,sender_id,text,created_at) SELECT md5('research-message-'||g)::uuid,%s,%s,repeat('x',100)||':'||g,%s::timestamptz+g*interval '1 microsecond' FROM generate_series(%s,%s) g",(cid,USER,START,offset+1,offset+size))
offset+=size
seed_s=time.perf_counter()-seeded
c.execute('VACUUM (ANALYZE) messages')
writes=[]
for batch in range(10):
started=time.perf_counter()
with c.transaction():
plan=c.execute("EXPLAIN (ANALYZE, BUFFERS, WAL, FORMAT JSON) INSERT INTO messages(id,chat_id,sender_id,text,created_at) SELECT md5(%s||g)::uuid,%s,%s,'write-'||repeat('x',100)||':'||g,clock_timestamp()+g*interval '1 microsecond' FROM generate_series(1,1000) g",('write-'+str(batch)+'-',uuid.UUID(int=1),USER)).fetchone()[0][0]
writes.append({'commit_wall_ms':round((time.perf_counter()-started)*1000,4),'execution_ms':plan['Execution Time'],
'wal_bytes':plan['Plan'].get('WAL Bytes',0),'shared_dirtied_blocks':plan['Plan'].get('Shared Dirtied Blocks',0)})
c.execute("DELETE FROM messages WHERE text LIKE 'write-%'")
c.execute('VACUUM (ANALYZE) messages')
row={'name':variant,'database':name,'seed_seconds':round(seed_s,4),
'table_bytes':c.execute("SELECT pg_table_size('messages')").fetchone()[0],
'indexes_bytes':c.execute("SELECT pg_indexes_size('messages')").fetchone()[0],
'write_runs':writes,'write_commit_ms':stats([w['commit_wall_ms'] for w in writes]),'reads':[]}
offset=0
for index,size in enumerate(SIZES,1):
cid=uuid.UUID(int=index)
cursor_at=offset+size-100
cursor_time=START+dt.timedelta(microseconds=cursor_at)
cursor_id=uuid.UUID(hashlib.md5(('research-message-'+str(cursor_at)).encode()).hexdigest())
for kind in ('keyset','offset'):
if kind=='keyset':
query='SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s AND (created_at,id)>(%s,%s) ORDER BY created_at,id LIMIT 50'
parameters=(cid,cursor_time,cursor_id)
else:
query='SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s ORDER BY created_at,id OFFSET %s LIMIT 50'
parameters=(cid,size-100)
for _ in range(3):
assert len(c.execute(query,parameters).fetchall())==50
timings=[]
total_started=time.perf_counter()
for _ in range(30):
started=time.perf_counter()
result=c.execute(query,parameters).fetchall()
assert len(result)==50
timings.append((time.perf_counter()-started)*1000)
total_elapsed=time.perf_counter()-total_started
plan=c.execute('EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) '+query,parameters).fetchone()[0][0]
row['reads'].append({'chat_size':size,'query':kind,'latency_ms':stats(timings),'raw_latency_ms':[round(v,4) for v in timings],
'successful_queries':30,'failed_queries':0,'observed_sequential_qps':round(30/total_elapsed,3),'explain':plan})
offset+=size
report['variants'].append(row)
report['databases']=databases
report['backup_source']=prefix+'_keyset'
# Keep the production-index database immutable until the dump starts.
with psycopg.connect(make_conninfo(BASE,dbname=report['backup_source'])) as connection:
report['before_backup']=fingerprint(connection)
print(json.dumps(report,ensure_ascii=False,default=str))
def verify():
dsn=make_conninfo(BASE,host=os.environ['RESTORE_HOST'],dbname='messenger')
with psycopg.connect(dsn) as connection:
report=fingerprint(connection)
orphan=connection.execute('SELECT count(*) FROM messages m LEFT JOIN chat_members cm ON (cm.chat_id,cm.user_id)=(m.chat_id,m.sender_id) WHERE cm.user_id IS NULL').fetchone()[0]
report['orphan_messages']=orphan
print(json.dumps(report,ensure_ascii=False))
def canary():
name=os.environ['RESEARCH_SOURCE']
with psycopg.connect(make_conninfo(BASE,dbname=name)) as connection:
connection.execute("INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES (%s,%s,%s,'AFTER_BACKUP_CANARY',clock_timestamp())",(uuid.uuid4(),uuid.UUID(int=1),USER))
count=connection.execute('SELECT count(*) FROM messages').fetchone()[0]
print(json.dumps({'source_messages_after_dump':count,'expected_messages_missing_from_snapshot':1}))
def cleanup():
prefix=os.environ['RESEARCH_PREFIX']
with psycopg.connect(BASE,autocommit=True) as admin:
for variant in ('chat_only','keyset','covering'):
admin.execute(sql.SQL('DROP DATABASE IF EXISTS {} WITH (FORCE)').format(sql.Identifier(prefix+'_'+variant)))
print(json.dumps({'isolated_databases_removed':True}))
{'benchmark':benchmark,'verify':verify,'canary':canary,'cleanup':cleanup}[os.environ['RESEARCH_ACTION']]()