forked from mtusi2026/mtusi2026lab2
Complete lab
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
"""Run on the host: research measurements and a real Redis restart through HTTP."""
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client
|
||||
|
||||
|
||||
def command(*args):
|
||||
return subprocess.check_output(list(args), cwd=ROOT, text=True, encoding='utf-8', timeout=180)
|
||||
|
||||
|
||||
def cache_state(chat_id, clear=False):
|
||||
code = """import json, sys
|
||||
from uuid import UUID
|
||||
from src.auth import sessions
|
||||
keys = list(sessions.client.scan_iter(match='history:' + str(UUID(sys.argv[1])) + ':*'))
|
||||
print(json.dumps({'keys': len(keys)}))
|
||||
if sys.argv[2] == 'clear' and keys:
|
||||
sessions.client.delete(*keys)
|
||||
"""
|
||||
return json.loads(command('docker', 'compose', 'exec', '-T', 'api', 'python', '-c', code,
|
||||
chat_id, 'clear' if clear else 'inspect'))
|
||||
|
||||
|
||||
def restart_check():
|
||||
client = Client('http://127.0.0.1:8080')
|
||||
actor = client.user('restartcache')
|
||||
chat = client.call('POST', '/api/v1/chats', 201, actor=actor,
|
||||
body={'title': 'Cache restart', 'member_ids': [actor['id']]}, schema='Chat')['json']
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
def read():
|
||||
return client.call('GET', path, actor=actor, schema='MessagePage')['json']
|
||||
first = client.message(actor, chat, 'before Redis restart')['json']
|
||||
assert read()['items'] == [first]
|
||||
cached_before = cache_state(chat['id'])['keys']
|
||||
assert cached_before > 0
|
||||
second = client.message(actor, chat, 'committed after cache fill')['json']
|
||||
redis_id = command('docker', 'compose', 'ps', '-q', 'redis').strip()
|
||||
before_start = command('docker', 'inspect', '--format', '{{.State.StartedAt}}', redis_id).strip()
|
||||
started = time.monotonic()
|
||||
command('docker', 'compose', 'restart', 'redis')
|
||||
deadline = time.monotonic() + 60
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
client.call('GET', '/health/ready')
|
||||
break
|
||||
except AssertionError:
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
raise AssertionError('Redis recovery timed out')
|
||||
after_start = command('docker', 'inspect', '--format', '{{.State.StartedAt}}', redis_id).strip()
|
||||
assert before_start != after_start
|
||||
assert read()['items'] == [first, second]
|
||||
recovery = round(time.monotonic() - started, 3)
|
||||
cache_state(chat['id'], clear=True)
|
||||
assert cache_state(chat['id'])['keys'] == 0
|
||||
assert read()['items'] == [first, second]
|
||||
assert cache_state(chat['id'])['keys'] > 0
|
||||
third = client.message(actor, chat, 'after Redis recovery')['json']
|
||||
assert read()['items'] == [first, second, third]
|
||||
client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor)
|
||||
client.call('GET', path, 401, actor=actor)
|
||||
return {'status': 'PASS', 'same_session': True, 'confirmed_messages': 3,
|
||||
'cold_cache_rebuilt': True, 'read_after_write': True, 'logout_still_401': True,
|
||||
'recovery_seconds': recovery, 'redis_start_before_utc': before_start,
|
||||
'redis_start_after_utc': after_start}
|
||||
|
||||
|
||||
def main():
|
||||
evidence = ROOT / 'evidence'
|
||||
evidence.mkdir(exist_ok=True)
|
||||
result = json.loads(command('docker', 'compose', 'exec', '-T', 'api',
|
||||
'python', '-m', 'scripts.cache_experiment'))
|
||||
(evidence / 'cache-experiment.json').write_text(json.dumps(result, indent=2) + '\n', encoding='utf-8')
|
||||
result = restart_check()
|
||||
(evidence / 'cache-restart.json').write_text(json.dumps(result, indent=2) + '\n', encoding='utf-8')
|
||||
ids = command('docker', 'compose', 'ps', '-q').split()
|
||||
snapshot = command('docker', 'stats', '--no-stream', '--format', '{{.Name}} CPU={{.CPUPerc}} RAM={{.MemUsage}}', *ids)
|
||||
(evidence / 'cache-resources.txt').write_text('Post-experiment snapshot (not peak):\n' + snapshot, encoding='utf-8')
|
||||
print('PASS: stale race reproduced before protection and prevented after; SQL/latency measured.')
|
||||
print('PASS: Redis restart, same session, read-after-write, cold cache rebuild, logout.')
|
||||
print('Evidence: cache-experiment.json, cache-restart.json, cache-resources.txt')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user