Очередь и обработка изображений

This commit is contained in:
Югай Артур Рустамович
2026-11-24 19:34:09 +03:00
parent 276dbdad8a
commit 616b5d4e4e
51 changed files with 5337 additions and 45 deletions
+706
View File
@@ -0,0 +1,706 @@
import argparse
from datetime import datetime, timezone
import hashlib
import json
import os
from pathlib import Path
import platform
import secrets
import shutil
import socket
import subprocess
import sys
import time
from urllib.error import URLError
from urllib.request import ProxyHandler, build_opener
import venv
ROOT = Path(__file__).resolve().parents[1]
LOG = []
LOCAL_HTTP = build_opener(ProxyHandler({}))
def settings():
path = ROOT / ".env"
result = {}
if path.exists():
for line in path.read_text(encoding="utf-8-sig").splitlines():
key, separator, value = line.partition("=")
if separator and not key.strip().startswith("#"):
result[key.strip()] = value.strip()
return result
def environment(args):
result = os.environ.copy()
result.update(PYTHONUTF8="1", PYTHONIOENCODING="utf-8", PYTHONDONTWRITEBYTECODE="1",
COMPOSE_PROJECT_NAME=args.project, HTTP_PORT=str(args.port),
SESSION_TTL_SECONDS=str(args.ttl), LAB5_PROJECT=args.project,
LAB5_PORT=str(args.port), LAB5_TTL=str(args.ttl))
subnet = 52 if args.project.endswith('-ttl') else (54 if 'clean' in args.project else 50)
result['LAB5_APP_SUBNET'] = f'10.240.{subnet}.0/24'
result['LAB5_STORAGE_SUBNET'] = f'10.240.{subnet+1}.0/24'
for key in ("NO_PROXY", "no_proxy"):
result[key] = ",".join(filter(None, [result.get(key), "localhost", "127.0.0.1"]))
return result
def say(value):
line = str(value).replace(str(ROOT), ".")
print(line, flush=True)
LOG.append(line)
def run(args, command, quiet=False):
command = [str(item) for item in command]
if not quiet:
say("$ " + subprocess.list2cmdline(command))
with subprocess.Popen(command, cwd=ROOT, env=environment(args), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace") as process:
lines = []
for line in process.stdout:
lines.append(line)
if not quiet:
say(line.rstrip())
code = process.wait()
if code:
raise RuntimeError(f"{command[0]} exited with code {code}")
return "".join(lines)
def compose(args, *command, quiet=False):
if args.project != "mtusi-lab5" and not args.project.startswith("mtusi-lab5-"):
raise RuntimeError("Only lab5 projects are allowed")
return run(args, ["docker", "compose", "--project-name", args.project, "--env-file", ".env", *command], quiet)
def interpreter():
return ROOT / ".venv" / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
def evidence(name, value):
path = ROOT / "evidence" / name
path.parent.mkdir(exist_ok=True)
content = json.dumps(value, ensure_ascii=False, indent=2) if not isinstance(value, str) else value
path.write_text(content.rstrip() + "\n", encoding="utf-8", newline="\n")
def setup(args):
if not (ROOT / ".env").exists():
shutil.copyfile(ROOT / ".env.example", ROOT / ".env")
folder = ROOT / "secrets"
folder.mkdir(exist_ok=True)
password_file = folder / "postgres_password"
if not password_file.exists():
password_file.write_text(secrets.token_hex(32) + "\n", encoding="ascii")
password = password_file.read_text(encoding="ascii").strip()
values = {"database_url": f"postgresql://messenger:{password}@db:5432/messenger",
"cursor_key": secrets.token_hex(32)}
for name, value in values.items():
path = folder / name
if not path.exists():
path.write_text(value + "\n", encoding="ascii")
if os.name != "nt":
path.chmod(0o644)
if os.name != "nt":
folder.chmod(0o700)
password_file.chmod(0o644)
for name in ("s3_access_key_id", "s3_secret_access_key", "s3_admin_access_key_id", "s3_admin_secret_access_key"):
path = folder / name
if not path.exists():
path.write_text(secrets.token_hex(32) + "\n", encoding="ascii")
if os.name != "nt":
path.chmod(0o644)
config = folder / "s3_config.json"
if not config.exists():
def key(name):
return (folder / name).read_text(encoding="ascii").strip()
bucket = settings().get("S3_BUCKET", "messenger-lab5-attachments")
identities = [
{"name": "bootstrap", "credentials": [{"accessKey": key("s3_admin_access_key_id"),
"secretKey": key("s3_admin_secret_access_key")}], "actions": ["Admin", "Read", "Write", "List"]},
{"name": "messenger", "credentials": [{"accessKey": key("s3_access_key_id"),
"secretKey": key("s3_secret_access_key")}],
"actions": [action + ":" + bucket for action in ("Read", "Write", "List")]},
]
config.write_text(json.dumps({"identities": identities}) + "\n", encoding="ascii")
if os.name != "nt":
config.chmod(0o644)
broker_password = folder / 'broker_password'
if not broker_password.exists():
broker_password.write_text(secrets.token_hex(32), encoding='ascii')
broker_values = {'broker_url': 'amqp://messenger:' + broker_password.read_text().strip() + '@broker:5672/messenger',
'rabbitmq.conf': 'default_user = messenger\ndefault_pass = ' + broker_password.read_text().strip() + '\ndefault_vhost = messenger\n'}
for name, value in broker_values.items():
path = folder / name
if not path.exists():
path.write_text(value + '\n', encoding='ascii')
if os.name != 'nt':
path.chmod(0o644)
if not interpreter().exists():
venv.EnvBuilder(with_pip=True).create(ROOT / ".venv")
lockfiles = [ROOT / "requirements.lock", ROOT / "requirements-validation.lock"]
fingerprint = hashlib.sha256(b"".join(path.read_bytes() for path in lockfiles)).hexdigest()
marker = ROOT / ".venv/.lab-dependencies"
if not marker.exists() or marker.read_text(encoding="ascii") != fingerprint:
run(args, [interpreter(), "-X", "utf8", "-m", "pip", "install", "--disable-pip-version-check",
"--no-input", "--progress-bar", "off", "--quiet", "-r", lockfiles[0], "-r", lockfiles[1]])
marker.write_text(fingerprint, encoding="ascii")
say("Environment ready; existing settings and secrets preserved")
def base_url(args):
return f"http://127.0.0.1:{args.port}"
def wait_storage(args, timeout=90):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
container = compose(args, "ps", "--all", "--quiet", "s3-storage", quiet=True).strip()
if container:
status = run(args, ["docker", "inspect", "--format", "{{.State.Health.Status}}", container], quiet=True).strip()
if status == "healthy":
return
time.sleep(0.5)
raise RuntimeError("S3 storage readiness timeout")
def wait_ready(args, timeout=90):
wait_storage(args, timeout)
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
with LOCAL_HTTP.open(base_url(args) + "/health/ready", timeout=4) as response:
if response.status == 200 and json.load(response) == {"status": "ok"}:
return
except (URLError, OSError, ValueError):
pass
time.sleep(0.3)
raise RuntimeError("API readiness timeout")
def build(args):
compose(args, "config", "--quiet")
compose(args, "build", "api1")
compose(args, "run", "--rm", "--no-deps", "lb", "haproxy", "-c", "-f",
"/usr/local/etc/haproxy/haproxy.cfg")
def start(args):
current = compose(args, "ps", "--all", "--quiet", "lb", quiet=True).strip()
if not current:
with socket.socket() as probe:
try:
probe.bind(("127.0.0.1", args.port))
except OSError:
raise RuntimeError(f"Port {args.port} is busy; stop its container or use --port") from None
compose(args, "up", "-d", "--no-build", "db", "migrate", "s3-storage", "s3", "bootstrap", "api1", "api2", "cleanup", "broker", "worker", "lb")
wait_ready(args)
wait_replicas(args)
say("Two API replicas ready through LB: " + base_url(args))
def states(args):
services = ("api1", "api2", "db", "lb", "s3", "s3-storage", "cleanup", "broker", "worker")
containers = compose(args, "ps", "--all", "--quiet", *services, quiet=True).split()
if len(set(containers)) != len(services):
raise RuntimeError("Missing runtime container")
result = {}
for data in json.loads(run(args, ["docker", "inspect", *containers], quiet=True)):
labels = data["Config"].get("Labels", {})
service = labels.get("com.docker.compose.service")
if labels.get("com.docker.compose.project") != args.project or service not in services or service in result:
raise RuntimeError("Unexpected container ownership")
result[service] = {
"id": data["Id"], "image": data["Image"], "started_at": data["State"]["StartedAt"],
"running": data["State"]["Running"], "user": data["Config"]["User"],
"volumes": [mount["Name"] for mount in data["Mounts"] if mount["Type"] == "volume"],
}
return result
def restart(args):
before = states(args)
if not all(item["running"] for item in before.values()):
raise RuntimeError("All runtime containers must be running before restart")
compose(args, "restart", "--timeout", "10", "db", "s3-storage", "s3", "api1", "api2", "cleanup", "broker", "worker", "lb")
wait_storage(args)
compose(args, "run", "--rm", "--no-deps", "bootstrap")
after = states(args)
for service in before:
if before[service]["started_at"] == after[service]["started_at"]:
raise RuntimeError("Container was not restarted: " + service)
if before[service]["volumes"] != after[service]["volumes"]:
raise RuntimeError("Volume changed during restart")
evidence("restart.json", {"project": args.project, "before": before, "after": after})
say("PASS: both APIs, PostgreSQL, S3 and LB restarted, volumes preserved")
def contract_tests(args):
run(args, [interpreter(), "-X", "utf8", "scripts/check_contract.py"])
run(args, [interpreter(), "-X", "utf8", "scripts/validate_openapi.py"])
compose(args, "config", "--quiet")
def api_tests(args):
wait_ready(args)
run(args, [interpreter(), "-X", "utf8", "tests/smoke.py", "--base-url", base_url(args)])
os.environ["BASE_URL"] = base_url(args)
run(args, [interpreter(), "-X", "utf8", "-m", "unittest", "discover", "-s", "student-tests", "-v"])
def client(args):
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
for key in ("NO_PROXY", "no_proxy"):
os.environ[key] = environment(args)[key]
return Client(base_url(args))
def database_stats(args):
sql = """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),
'sessions',(SELECT count(*) FROM sessions),'migrations',(SELECT count(*) FROM schema_migrations),
'idempotency_keys',(SELECT count(*) FROM idempotency_keys),
'attachments',(SELECT count(*) FROM attachments),'message_attachments',(SELECT count(*) FROM message_attachments),
'upload_intents',(SELECT count(*) FROM upload_intents),
'expiry_fingerprint',(SELECT md5(COALESCE(string_agg(user_id::text || created_at::text || expires_at::text,
',' ORDER BY token_hash),'')) FROM sessions),
'passwords_are_argon2id',(SELECT bool_and(password_hash LIKE '$argon2id$%') FROM users),
'session_hashes_only',(SELECT bool_and(token_hash ~ '^[0-9a-f]{64}$') FROM sessions))"""
result = compose(args, "exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger",
"-At", "-c", sql, quiet=True)
return json.loads(result)
def restart_check(args):
from urllib.parse import urlencode
c = client(args)
alice, bob, eve, chat = c.bootstrap()
path = "/api/v1/chats/" + chat["id"] + "/messages"
replay_key = secrets.token_hex(16)
original = c.message(alice, chat, "first", key=replay_key)["json"]
last = c.message(bob, chat, "second")["json"]
page = c.call("GET", path + "?limit=1", actor=alice, schema="MessagePage")["json"]
revoked = c.user("revoked")
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=revoked)
hook = ROOT / "scripts" / ("restart.cmd" if os.name == "nt" else "restart.sh")
run(args, [interpreter(), "-X", "utf8", "tests/resilience.py", "--mode", "persistence",
"--action-script", hook, "--base-url", base_url(args)])
continued = c.call("GET", path + "?" + urlencode({"cursor": page["next_cursor"]}),
actor=alice, schema="MessagePage")["json"]
if continued != {"items": [last], "next_cursor": None}:
raise RuntimeError("Old cursor did not survive restart")
c.call("GET", "/api/v1/users/me", 401, actor=revoked)
c.call("GET", "/api/v1/chats/" + chat["id"], 404, actor=eve)
before = database_stats(args)
compose(args, "run", "--rm", "--no-deps", "migrate")
compose(args, "run", "--rm", "--no-deps", "migrate")
after = database_stats(args)
stable_before = {key: value for key, value in before.items() if key != "upload_intents"}
stable_after = {key: value for key, value in after.items() if key != "upload_intents"}
if stable_before != stable_after or after["migrations"] != 4:
raise RuntimeError("Migration replay changed existing data")
if not after["passwords_are_argon2id"] or not after["session_hashes_only"]:
raise RuntimeError("Unexpected credential representation in DB")
repeated = c.message(alice, chat, "first", key=replay_key)["json"]
if repeated != original:
raise RuntimeError("Idempotency result was not preserved")
result = {"idempotency_after_restart": "PASS", "public_persistence": "PASS", "cursor_after_restart": "PASS",
"logout_after_restart": "PASS", "migration_replay": "PASS", "database": after}
evidence("persistence.json", result)
say("PASS: old session/cursor, durable logout and repeatable migrations")
return result
def ttl_check(args):
test = argparse.Namespace(project=args.project + "-ttl", port=args.test_port, ttl=2,
test_port=args.test_port, action="start")
try:
start(test)
c = client(test)
actor = c.user("expiry")
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
before = database_stats(test)["expiry_fingerprint"]
compose(test, "restart", "--timeout", "3", "api1", "api2")
wait_ready(test)
after = database_stats(test)["expiry_fingerprint"]
if before != after:
raise RuntimeError("Session expiry changed during restart")
time.sleep(2.2)
c.call("GET", "/api/v1/users/me", 401, actor=actor)
registration = actor["registration"]
import base64
basic = base64.b64encode((registration["username"] + ":" + registration["password"]).encode()).decode()
c.call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": "Basic " + basic}, schema="Session")
say("PASS: TTL=2, expired token rejected after API restart; fresh login works")
result = {"ttl_seconds": 2, "expiry_unchanged_on_restart": True, "before_restart": 200,
"after_expiry_and_restart": 401, "new_login": 201}
evidence("expiry.json", result)
return result
finally:
compose(test, "down", "--timeout", "10")
def dependency_check(args):
from urllib.error import HTTPError
from urllib.request import Request
c = client(args)
actor = c.user('dependencies')
result = {}
for service in ('db', 's3', 'broker'):
try:
compose(args, 'stop', '--timeout', '10', service)
c.call('GET', '/health/live', schema='Health')
outage_response(args, '/health/ready')
deadline = time.monotonic() + 20
while True:
try:
with LOCAL_HTTP.open(Request(base_url(args) + '/api/v1/users/me',
headers={'Authorization': 'Bearer ' + actor['token']}), timeout=5) as response:
if response.status != 200:
raise RuntimeError('Unexpected dependency response')
except HTTPError as error:
if error.code != 503:
raise RuntimeError('Unexpected dependency status') from None
error.close()
break
if time.monotonic() >= deadline:
raise RuntimeError('LB did not withdraw unready replicas')
time.sleep(0.3)
outage_response(args, '/api/v1/users/me', actor['token'])
finally:
compose(args, 'start', service)
wait_ready(args)
c.call('GET', '/api/v1/users/me', actor=actor, schema='User')
result[service] = {'live': 200, 'ready': 503, 'business': 503, 'recovered': 200}
say('PASS: ' + service + ' outage and recovery')
return result
def verify(args):
summary = {"date_utc": datetime.now(timezone.utc).isoformat(), "platform": platform.platform(),
"source_commit": run(args, ["git", "rev-parse", "HEAD"], quiet=True).strip(),
"worktree_dirty_at_start": bool(run(args, ["git", "status", "--porcelain"], quiet=True).strip()),
"project": args.project, "status": "FAIL"}
try:
setup(args)
contract_tests(args)
build(args)
start(args)
api_tests(args)
summary["queue"] = queue_check(args)
summary["retry"] = retry_check(args)
summary["persistence"] = restart_check(args)
summary["failover"] = failover_check(args)
summary["attachments_failover"] = attachment_failover_check(args)
summary["expiry"] = ttl_check(args)
summary["dependency"] = dependency_check(args)
run(args, [interpreter(), "-X", "utf8", "tests/smoke.py", "--base-url", base_url(args)])
summary["containers"] = states(args)
summary["docker"] = run(args, ["docker", "version", "--format", "{{.Server.Version}}"], quiet=True).strip()
summary["resources"] = json.loads(run(
args, ["docker", "info", "--format", '{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}'], quiet=True))
summary["status"] = "PASS"
say("PASS: full lab5 verification")
finally:
evidence("verification.txt", "\n".join(LOG))
evidence("summary.json", summary)
def instance_map(args):
current = states(args)
result = {}
for service in ("api1", "api2"):
item = current[service]
if not item["running"]:
continue
code = (
"import urllib.request; "
"o=urllib.request.build_opener(urllib.request.ProxyHandler({})); "
"r=o.open('http://127.0.0.1:8080/health/live',timeout=1); "
"print(r.headers['X-Instance-Id'])"
)
alias = run(args, ["docker", "exec", item["id"], "python", "-c", code], quiet=True).strip()
import re
if not re.fullmatch(r"[0-9a-f]{32}", alias):
raise RuntimeError("Invalid API instance alias")
result[service] = {"instance_id": alias, "container_id": item["id"]}
return result
def wait_replicas(args):
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
try:
mapping = instance_map(args)
if len(mapping) == 2:
c = client(args)
seen = {c.call("GET", "/health/ready", schema="Health")["headers"]["X-Instance-Id"]
for _ in range(8)}
if seen == {item["instance_id"] for item in mapping.values()}:
return mapping
except (RuntimeError, OSError, AssertionError):
pass
time.sleep(0.3)
raise RuntimeError("Two healthy API replicas were not observed through LB")
def restore(args):
compose(args, "start", "api1", "api2")
wait_ready(args)
return wait_replicas(args)
def stop_one(args):
target = os.environ.get("TARGET_INSTANCE_ID", "")
before = instance_map(args)
matches = [(name, item) for name, item in before.items() if item["instance_id"] == target]
if len(before) != 2 or len(matches) != 1:
raise RuntimeError("Target must match exactly one of two running API replicas")
service, item = matches[0]
started = time.monotonic()
run(args, ["docker", "stop", "--timeout", "3", item["container_id"]])
after = states(args)
if after[service]["running"] or not after["api1" if service == "api2" else "api2"]["running"]:
raise RuntimeError("Unexpected API state after stop")
elapsed = time.monotonic() - started
if elapsed >= 10:
raise RuntimeError("Stopping the selected API exceeded 10 seconds")
evidence("stop-one.json", {"service": service, "target_instance": target, "before": before,
"after": after, "stop_seconds": round(elapsed, 3)})
say("Stopped only " + service)
def failover_check(args):
wait_replicas(args)
hook = ROOT / "scripts" / ("stop-one.cmd" if os.name == "nt" else "stop-one.sh")
try:
output = run(args, [interpreter(), "-X", "utf8", "tests/resilience.py", "--mode", "failover",
"--action-script", hook, "--base-url", base_url(args)])
first = json.loads((ROOT / "evidence/stop-one.json").read_text(encoding="utf-8"))
first["metrics"] = next(json.loads(line) for line in output.splitlines()
if line.startswith("{") and "first_recovery_seconds" in line)
if first["after"][first["service"]]["running"]:
raise RuntimeError("Public hook did not leave the target stopped")
finally:
restore(args)
other_service = "api2" if first["service"] == "api1" else "api1"
mapping = instance_map(args)
target = mapping[other_service]["instance_id"]
c = client(args)
c.timeout = 2
alice, bob, eve, chat = c.bootstrap()
for _ in range(10):
replay_key = secrets.token_hex(16)
replay_text = "before-failure-" + replay_key
response = c.message(alice, chat, replay_text, key=replay_key)
if response["headers"]["X-Instance-Id"] == target:
original = response["json"]
break
else:
raise RuntimeError("Second API was not reached through LB")
env = environment(args)
env["TARGET_INSTANCE_ID"] = target
started = time.monotonic()
process = subprocess.Popen([str(hook)], cwd=ROOT, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
from client import Failure
from resilience import history
confirmations, after_instances = [], []
failures = 0
recovered = None
key = secrets.token_hex(16)
text = "during-failure-" + key
try:
while time.monotonic() - started < 15:
code = process.poll()
if code not in (None, 0):
raise RuntimeError("Second stop hook failed")
if code is None and time.monotonic() - started >= 10:
raise RuntimeError("Second stop hook exceeded 10 seconds")
try:
response = c.message(alice, chat, text, key=key)
confirmations.append(response["json"])
if code == 0:
if recovered is None:
recovered = time.monotonic() - started
after_instances.append(response["headers"]["X-Instance-Id"])
key = secrets.token_hex(16)
text = "during-failure-" + key
except Failure:
failures += 1
time.sleep(0.1)
if process.poll() != 0 or recovered is None or recovered > 10:
raise RuntimeError("Second API failure did not recover within 10 seconds")
if len(after_instances) < 5 or target in after_instances[-5:]:
raise RuntimeError("Stopped API still serves requests")
replayed = c.message(alice, chat, replay_text, key=replay_key)
if replayed["json"] != original or replayed["headers"]["X-Instance-Id"] == target:
raise RuntimeError("Message replay through the survivor changed the result")
messages = history(c, bob, chat)
ids = [item["id"] for item in messages]
texts = [item["text"] for item in messages]
if len(ids) != len(set(ids)) or len(texts) != len(set(texts)):
raise RuntimeError("Duplicates appeared during failover")
if any(item not in messages for item in [original, *confirmations]):
raise RuntimeError("A confirmed message was lost")
second = json.loads((ROOT / "evidence/stop-one.json").read_text(encoding="utf-8"))
if second["service"] != other_service:
raise RuntimeError("Wrong second API was stopped")
second["metrics"] = {"successful_writes": len(confirmations), "failed_attempts": failures,
"first_recovery_seconds": round(recovered, 3),
"replay_on_survivor": "PASS"}
result = {"public": first, "second_replica": second, "status": "PASS"}
evidence("failover.json", result)
say("PASS: both physical API replicas stopped separately; no lost messages or duplicates")
return result
finally:
if process.poll() is None:
process.terminate()
process.wait(timeout=5)
restore(args)
def outage_response(args, path, token=None):
from urllib.error import HTTPError
from urllib.request import Request
from client import shape
headers = {"Authorization": "Bearer " + token} if token else {}
try:
response = LOCAL_HTTP.open(Request(base_url(args) + path, headers=headers), timeout=10)
except HTTPError as error:
response = error
with response:
payload = json.load(response)
if response.status != 503 or not response.headers.get("X-Request-Id"):
raise RuntimeError("Unexpected dependency failure response")
shape(payload, "Error")
if payload["error"]["request_id"] != response.headers["X-Request-Id"]:
raise RuntimeError("LB error request ID mismatch")
def attachment_failover_check(args):
import hashlib
results = []
raw = (ROOT / "tests/fixtures/sample.png").read_bytes()
try:
for service in ("api1", "api2"):
mapping = wait_replicas(args)
c = client(args)
alice, bob, eve, chat = c.bootstrap()
target = mapping[service]["instance_id"]
for _ in range(8):
uploaded = c.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=failover.png", 202,
actor=alice, raw=raw, headers={"Content-Type": "image/png"}, schema="Attachment",
)
if uploaded["headers"]["X-Instance-Id"] == target:
break
else:
raise RuntimeError("Selected upload replica was not observed")
attachment = uploaded["json"]
c.wait_ready(alice, attachment)
linked = c.message(alice, chat, "", attachments=[attachment["id"]])["json"]
hook = ROOT / "scripts" / ("stop-one.cmd" if os.name == "nt" else "stop-one.sh")
previous = os.environ.get("TARGET_INSTANCE_ID")
os.environ["TARGET_INSTANCE_ID"] = target
started = time.monotonic()
try:
run(args, [str(hook)])
finally:
if previous is None:
os.environ.pop("TARGET_INSTANCE_ID", None)
else:
os.environ["TARGET_INSTANCE_ID"] = previous
from client import Client
reads = Client(base_url(args), timeout=2)
errors = 0
while True:
try:
downloaded = reads.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=bob)
history_response = reads.call(
"GET", "/api/v1/chats/" + chat["id"] + "/messages", actor=bob, schema="MessagePage",
)
break
except (AssertionError, OSError) as error:
if isinstance(error, AssertionError) and not any(
text in str(error) for text in ("received 502", "received 503", "connection/TLS failure")
):
raise
errors += 1
if time.monotonic() - started >= 10:
raise RuntimeError("File and history did not recover within 10 seconds") from None
time.sleep(0.1)
recovered = time.monotonic() - started
if downloaded["bytes"] != raw or hashlib.sha256(raw).hexdigest() != attachment["sha256"]:
raise RuntimeError("Original file changed after API failure")
if downloaded["headers"]["X-Instance-Id"] == target or recovered > 10:
raise RuntimeError("Upload replica was not replaced")
history = history_response["json"]["items"]
if history_response["headers"]["X-Instance-Id"] == target:
raise RuntimeError("Stopped API returned history")
if history != [linked]:
raise RuntimeError("Attached message changed during failover")
results.append({"stopped_service": service, "upload_instance": target,
"download_instance": downloaded["headers"]["X-Instance-Id"],
"recovery_seconds": round(recovered, 3), "transitional_errors": errors,
"same_original": True, "message_preserved": True})
restore(args)
result = {"status": "PASS", "cases": results}
evidence("attachment-failover.json", result)
say("PASS: stopped each uploader; other API downloaded the same original")
return result
finally:
restore(args)
def queue_check(args):
run(args, [interpreter(), '-X', 'utf8', 'scripts/pipeline_checks.py', 'queue'])
return json.loads((ROOT / 'evidence/queue.json').read_text(encoding='utf-8'))
def retry_check(args):
run(args, [interpreter(), '-X', 'utf8', 'scripts/pipeline_checks.py', 'retry'])
return json.loads((ROOT / 'evidence/retry.json').read_text(encoding='utf-8'))
def jobs_failed(args):
query = "SELECT a.id,a.error_code,j.attempt FROM attachments a JOIN attachment_jobs j ON a.id=j.attachment_id WHERE a.status='failed' ORDER BY a.updated_at DESC LIMIT 100"
say(compose(args, 'exec', '-T', 'db', 'psql', '-U', 'messenger', '-d', 'messenger', '-c', query, quiet=True))
def main():
saved = settings()
parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["setup", "build", "start", "test", "restart", "stop-one",
"restart-check", "failover-check", "restore", "stop", "verify", "queue-check", "retry-check", "worker-stop", "worker-start", "jobs-failed"])
parser.add_argument("--project", default=os.environ.get("LAB5_PROJECT", saved.get("COMPOSE_PROJECT_NAME", "mtusi-lab5")))
parser.add_argument("--port", type=int, default=int(os.environ.get("LAB5_PORT", saved.get("HTTP_PORT", "8080"))))
parser.add_argument("--ttl", type=int, default=int(os.environ.get("LAB5_TTL", saved.get("SESSION_TTL_SECONDS", "3600"))))
parser.add_argument("--test-port", type=int, default=18087)
args = parser.parse_args()
if not 1 <= args.port <= 65535 or not 1 <= args.test_port <= 65534 or not 1 <= args.ttl <= 86400:
parser.error("Invalid port or session TTL")
if args.action in ("setup", "build", "start", "test", "restart-check", "failover-check", "queue-check", "retry-check"):
setup(args)
actions = {"setup": lambda item: None, "build": build, "start": start,
"test": lambda item: (contract_tests(item), api_tests(item)),
"restart": restart, "restart-check": restart_check, "verify": verify,
"queue-check": queue_check, "retry-check": retry_check, "jobs-failed": jobs_failed,
"worker-stop": lambda item: compose(item, "stop", "--timeout", "3", "worker"),
"worker-start": lambda item: compose(item, "start", "worker"),
"stop-one": stop_one, "restore": restore, "failover-check": failover_check,
"stop": lambda item: compose(item, "stop", "--timeout", "10", "lb", "api1", "api2", "cleanup", "worker", "broker", "s3", "s3-storage", "db")}
actions[args.action](args)
if __name__ == "__main__":
try:
main()
except (RuntimeError, OSError, ValueError) as error:
print("FAIL: " + str(error), file=sys.stderr)
raise SystemExit(1)
+202
View File
@@ -0,0 +1,202 @@
import hashlib
import io
import json
import time
import os
from pathlib import Path
import sys
from types import SimpleNamespace
from uuid import uuid4
from PIL import Image
def check(args, lab, retry=False):
c = lab.client(args)
actor, bob, eve, chat = c.bootstrap()
def sql(statement):
return lab.compose(args, 'exec', '-T', 'db', 'psql', '-U', 'messenger', '-d', 'messenger',
'-v', 'ON_ERROR_STOP=1', '-At', '-c', statement, quiet=True).strip()
def wait(predicate, seconds=60):
end = time.monotonic() + seconds
while time.monotonic() < end:
if predicate():
return
time.sleep(0.2)
raise RuntimeError('Pipeline test condition timed out')
def ready(att):
done = c.wait_ready(actor, att)
path = '/api/v1/attachments/' + att['id'] + '/content'
raw = c.call('GET', path, actor=bob)['bytes']
if hashlib.sha256(raw).hexdigest() != att['sha256']:
raise RuntimeError('Original checksum changed')
thumb = c.call('GET', path + '?variant=thumbnail', actor=bob)['bytes']
with Image.open(io.BytesIO(thumb)) as image:
if image.format != 'JPEG' or image.size != (256, 160):
raise RuntimeError('Thumbnail format or size mismatch')
if hashlib.sha256(thumb).hexdigest() != done['variants'][0]['sha256']:
raise RuntimeError('Thumbnail checksum mismatch')
return done
def duplicate(att):
code = ('from src.storage import PostgresStore; from src.broker import publish,envelope; '
's=PostgresStore(); s.open()\n'
'with s.connection() as c: j=c.execute("SELECT * FROM attachment_jobs WHERE attachment_id=%s",'
'("' + att['id'] + '",)).fetchone()\n'
'publish(envelope(j)); publish(envelope(j)); s.close()')
lab.compose(args, 'exec', '-T', 'api1', 'python', '-c', code, quiet=True)
def drained():
lines = lab.compose(args, 'exec', '-T', 'broker', 'rabbitmqctl', '-q',
'list_queues', '-p', 'messenger', 'name', 'messages', quiet=True)
return any(line.split() == ['attachment.process.v1', '0'] for line in lines.splitlines())
wait(drained, 30)
now = c.call('GET', '/api/v1/attachments/' + att['id'], actor=actor, schema='Attachment')['json']
if now != att:
raise RuntimeError('Duplicate delivery changed ready metadata')
lab.compose(args, 'stop', '--timeout', '3', 'worker')
try:
attachment, original = c.attachment(actor, chat)
path = '/api/v1/attachments/' + attachment['id']
if not retry:
from jsonschema import Draft202012Validator, FormatChecker
job_row = json.loads(sql("SELECT row_to_json(j) FROM attachment_jobs j WHERE attachment_id='" + attachment['id'] + "'"))
carrier = {'schema_version': 1, 'job_id': job_row['id'], 'attachment_id': attachment['id'],
'type': 'attachment.process.v1', 'pipeline_version': 1, 'attempt': 1,
'created_at': job_row['created_at']}
schema = json.loads((lab.ROOT / 'contracts/jobs.schema.json').read_text())
Draft202012Validator(schema, format_checker=FormatChecker()).validate(carrier)
anonymous = (
"import os,boto3; from botocore import UNSIGNED; from botocore.config import Config; "
"from botocore.exceptions import ClientError\n"
"c=boto3.client('s3',endpoint_url=os.environ['S3_ENDPOINT'],config=Config(signature_version=UNSIGNED,proxies={}))\n"
"try:\n r=c.get_object(Bucket=os.environ['S3_BUCKET'],Key='system/readiness'); r['Body'].close(); status=200\n"
"except ClientError as e:\n status=e.response['ResponseMetadata']['HTTPStatusCode']\n"
"assert status==403; print(status)"
)
if lab.compose(args, 'exec', '-T', 'api1', 'python', '-c', anonymous, quiet=True).strip() != '403':
raise RuntimeError('Anonymous S3 access allowed')
linked = c.message(actor, chat, '', attachments=[attachment['id']])['json']
for variant in ('original', 'thumbnail'):
c.call('GET', path + '/content?variant=' + variant, 409, actor=bob)
c.call('GET', path, 404, actor=eve)
c.message(bob, chat, 'worker is stopped')
before = lab.states(args)
lab.compose(args, 'restart', '--timeout', '10', 'api1', 'api2', 'broker')
lab.wait_ready(args)
after = lab.states(args)
for name in ('api1', 'api2', 'broker'):
if before[name]['started_at'] == after[name]['started_at'] or before[name]['volumes'] != after[name]['volumes']:
raise RuntimeError('Queue restart evidence mismatch')
queued = c.call('GET', path, actor=actor, schema='Attachment')['json']
if queued['status'] != 'queued':
raise RuntimeError('Stopped worker unexpectedly processed the upload')
lab.compose(args, 'start', 'worker')
done = ready(attachment)
duplicate(done)
c.call('GET', path + '/content?variant=crop', 404, actor=bob)
history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=bob, schema='MessagePage')['json']['items']
if linked not in history:
raise RuntimeError('Queued attachment message was lost')
result = {'status': 'PASS', 'upload_with_stopped_worker': 202, 'pending_content': 409,
'same_attachment_after_api_broker_restart': True, 'original_and_thumbnail': 'PASS',
'duplicate_noop': 'PASS', 'job_schema': 'PASS', 'anonymous_s3': 403, 'before': before, 'after': after}
lab.evidence('queue.json', result)
lab.say('PASS: accepted upload survived API/broker restart; thumbnail and duplicate delivery checked')
return result
job = json.loads(sql("SELECT row_to_json(j) FROM attachment_jobs j WHERE attachment_id='" + attachment['id'] + "'"))
sql("UPDATE attachment_jobs SET staging_key='missing-test/" + attachment['id'] + "' WHERE id='" + job['id'] + "'")
lab.compose(args, 'start', 'worker')
wait(lambda: int(sql("SELECT attempt FROM attachment_jobs WHERE id='" + job['id'] + "'")) >= 1)
sql("UPDATE attachment_jobs SET staging_key='" + job['staging_key'] + "' WHERE id='" + job['id'] + "'")
ready(attachment)
attempts = int(sql("SELECT attempt FROM attachment_jobs WHERE id='" + job['id'] + "'"))
if attempts < 2:
raise RuntimeError('Transient error did not cause a retry')
lab.compose(args, 'stop', '--timeout', '3', 'worker')
failed, _ = c.attachment(actor, chat)
failed_job = json.loads(sql("SELECT row_to_json(j) FROM attachment_jobs j WHERE attachment_id='" + failed['id'] + "'"))
sql("UPDATE attachment_jobs SET staging_key='missing-test/" + failed['id'] + "' WHERE id='" + failed_job['id'] + "'")
lab.compose(args, 'start', 'worker')
wait(lambda: sql("SELECT status FROM attachments WHERE id='" + failed['id'] + "'") == 'failed', 75)
state = c.call('GET', '/api/v1/attachments/' + failed['id'], actor=actor, schema='Attachment')['json']
count = int(sql("SELECT attempt FROM attachment_jobs WHERE id='" + failed_job['id'] + "'"))
if count != 5 or state['error_code'] != 'retry_exhausted':
raise RuntimeError('Retry budget was not enforced')
c.call('GET', '/api/v1/attachments/' + failed['id'] + '/content', 409, actor=actor)
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=actor,
body={'text': '', 'attachment_ids': [failed['id']]}, headers={'Idempotency-Key': uuid4().hex})
sql("UPDATE attachment_jobs SET staging_key='" + failed_job['staging_key'] + "' WHERE id='" + failed_job['id'] + "'")
lab.compose(args, 'stop', '--timeout', '3', 'worker')
corrupt, _ = c.attachment(actor, chat)
stage = sql("SELECT staging_key FROM attachment_jobs WHERE attachment_id='" + corrupt['id'] + "'")
code = ("from src.objects import Objects; from io import BytesIO; o=Objects(); "
"o.put(" + repr(stage) + ",BytesIO(b'broken'),6,'image/png'); o.close()")
lab.compose(args, 'exec', '-T', 'api1', 'python', '-c', code, quiet=True)
lab.compose(args, 'start', 'worker')
wait(lambda: sql("SELECT status FROM attachments WHERE id='" + corrupt['id'] + "'") == 'failed')
if sql("SELECT error_code FROM attachments WHERE id='" + corrupt['id'] + "'") != 'invalid_image':
raise RuntimeError('Permanent image error was not classified')
if sql("SELECT attempt FROM attachment_jobs WHERE attachment_id='" + corrupt['id'] + "'") != '1':
raise RuntimeError('Permanent error retried')
lab.compose(args, 'stop', '--timeout', '3', 'worker')
crash, _ = c.attachment(actor, chat)
function = 'test_pause_' + uuid4().hex
sql("CREATE FUNCTION " + function + "() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN PERFORM pg_sleep(30); RETURN NEW; END $$; "
"CREATE TRIGGER " + function + " BEFORE INSERT ON attachment_variants FOR EACH ROW "
"WHEN (NEW.attachment_id='" + crash['id'] + "'::uuid) EXECUTE FUNCTION " + function + "()")
try:
lab.compose(args, 'start', 'worker')
wait(lambda: sql("SELECT count(*) FROM pg_stat_activity WHERE wait_event='PgSleep'") != '0')
if sql("SELECT status FROM attachments WHERE id='" + crash['id'] + "'") != 'processing':
raise RuntimeError('Processing state not observed')
lab.compose(args, 'stop', '--timeout', '3', 'worker')
finally:
sql('DROP TRIGGER IF EXISTS ' + function + ' ON attachment_variants; DROP FUNCTION IF EXISTS ' + function + '()')
lab.compose(args, 'start', 'worker')
ready(crash)
if sql("SELECT count(*) FROM attachment_variants WHERE attachment_id='" + crash['id'] + "'") != '1':
raise RuntimeError('Worker restart duplicated variants')
lab.compose(args, 'stop', '--timeout', '3', 'worker')
pending, _ = c.attachment(actor, chat)
sql("UPDATE attachment_jobs SET created_at=clock_timestamp()-INTERVAL '25 hours' "
"WHERE attachment_id='" + pending['id'] + "'; UPDATE attachments SET "
"updated_at=clock_timestamp()-INTERVAL '25 hours' WHERE id='" + failed['id'] + "'")
cleanup = ('from src.storage import PostgresStore; from src.objects import Objects; '
'from src.cleanup import clean_once; s=PostgresStore(); s.open(); o=Objects(); '
'clean_once(s,o); o.close(); s.close()')
lab.compose(args, 'exec', '-T', 'cleanup', 'python', '-c', cleanup, quiet=True)
if sql("SELECT status || ':' || staging_deleted FROM attachments WHERE id='" + pending['id'] + "'") != 'queued:false':
raise RuntimeError('Cleanup removed accepted waiting work')
if sql("SELECT staging_deleted FROM attachments WHERE id='" + failed['id'] + "'") != 't':
raise RuntimeError('Expired failed staging was not cleaned')
if sql("SELECT staging_deleted FROM attachments WHERE id='" + corrupt['id'] + "'") != 'f':
raise RuntimeError('Fresh failed staging was removed early')
ready(attachment)
lab.compose(args, 'start', 'worker')
ready(pending)
result = {'status': 'PASS', 'transient_attempts': attempts, 'max_attempts': count,
'failed_content': 409, 'failed_link': 409, 'processing_observed': True,
'permanent_error_attempts': 1, 'worker_stopped_before_result_commit': True,
'staging_retention_and_pending_protection': True, 'redelivery_same_result': True}
lab.evidence('retry.json', result)
lab.say('PASS: retry recovery, final failed and real worker interruption')
return result
finally:
lab.compose(args, 'start', 'worker')
lab.wait_ready(args)
if __name__ == '__main__':
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from scripts import lab
args = SimpleNamespace(project=os.environ['LAB5_PROJECT'], port=int(os.environ['LAB5_PORT']),
ttl=int(os.environ['LAB5_TTL']), test_port=18087)
check(args, lab, retry=sys.argv[1] == 'retry')
+3
View File
@@ -0,0 +1,3 @@
@echo off
python "%~dp0lab.py" restart
exit /b %errorlevel%
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
TASK_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec python3 "$TASK_DIR/lab.py" restart
+3
View File
@@ -0,0 +1,3 @@
@echo off
python "%~dp0lab.py" stop-one
exit /b %errorlevel%
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?missing TARGET_INSTANCE_ID}"
TASK_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec python3 "$TASK_DIR/lab.py" stop-one