"""Real two-store failures, dry-run, locking, grace period, retry and traversal cost.""" from concurrent.futures import ThreadPoolExecutor import hashlib import json from pathlib import Path import sys import time import uuid sys.path.insert(0,str(Path(__file__).resolve().parents[1])) sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests')) from client import Client,check from src.storage import storage from src.objects import client,BUCKET,object_lock from src.reconcile import reconcile def keys(s3): return {o['Key'] for p in s3.get_paginator('list_objects_v2').paginate(Bucket=BUCKET,Prefix='originals/') for o in p.get('Contents',[])} def main(): storage.pool.open(wait=True) s3=client(); c=Client('http://lb:8080',timeout=30) alice,bob,eve,chat=c.bootstrap() att,data=c.attachment(alice,chat) message=c.message(alice,chat,'used',attachments=[att['id']])['json'] with storage.pool.connection() as conn: key=conn.execute('SELECT object_key FROM attachments WHERE id=%s',(att['id'],)).fetchone()['object_key'] prefix='originals/research-'+uuid.uuid4().hex+'/' orphan=prefix+'old'; locked=prefix+'locked'; fresh=prefix+'fresh' for name in (orphan,locked): s3.put_object(Bucket=BUCKET,Key=name,Body=b'orphan') time.sleep(6) s3.put_object(Bucket=BUCKET,Key=fresh,Body=b'fresh') s3.delete_object(Bucket=BUCKET,Key=key) # metadata and used message survive missing bytes with storage.pool.connection() as conn: object_lock(conn,locked) before=keys(s3) with storage.pool.connection() as snapshot_conn: metadata_before=snapshot_conn.execute('SELECT id,state,updated_at FROM attachments ORDER BY id').fetchall() dry=reconcile(False,5,page_size=7) check(keys(s3)==before,'dry-run deleted an object') with storage.pool.connection() as snapshot_conn: check(snapshot_conn.execute('SELECT id,state,updated_at FROM attachments ORDER BY id').fetchall()==metadata_before,'dry-run changed metadata snapshot') with storage.pool.connection() as other: check(other.execute('SELECT state FROM attachments WHERE id=%s',(att['id'],)).fetchone()['state']=='ready','dry-run changed metadata') check(dry['counts'].get('locked_skipped',0)>=1,'in-flight lock ignored') s3.put_object(Bucket=BUCKET,Key=fresh,Body=b'fresh') applied=reconcile(True,5,page_size=7) remaining=keys(s3) check(orphan not in remaining,'old orphan not removed') check(locked in remaining and fresh in remaining,'fresh or locked object deleted') with storage.pool.connection() as conn: check(conn.execute('SELECT state FROM attachments WHERE id=%s',(att['id'],)).fetchone()['state']=='missing','missing not marked') check(conn.execute('SELECT count(*) AS n FROM message_attachments WHERE message_id=%s',(message['id'],)).fetchone()['n']==1,'used metadata removed') c.call('GET','/api/v1/attachments/'+att['id'],503,actor=bob) repeated=reconcile(True,5,page_size=7) check(repeated['counts'].get('metadata_marked_missing',0)==0,'not idempotent') s3.put_object(Bucket=BUCKET,Key=key,Body=b'corrupt',ContentType='image/png') corrupt=reconcile(True,5,page_size=7) check(corrupt['counts'].get('corrupt_objects',0)>=1,'corruption not found') c.call('GET','/api/v1/attachments/'+att['id'],503,actor=bob) s3.put_object(Bucket=BUCKET,Key=key,Body=data,ContentType='image/png') restored=reconcile(True,5,page_size=7) check(restored['counts'].get('metadata_restored',0)==1,'verified object not restored') check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)['bytes']==data,'restore changed bytes') # Two repeated concurrent workers must not delete referenced rows/objects. with ThreadPoolExecutor(max_workers=2) as pool: concurrent=list(pool.map(lambda _:reconcile(True,5,page_size=11),range(2))) check(key in keys(s3),'used object removed') check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)['bytes']==data,'concurrent reconciliation damaged data') # Fixed-seed count sweep, 3 dry runs per point, same image/data and resources. cost=[]; created=[] for count in (0,50,200): while len(created)