101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""lab5 failure drills: broker restart durability and bounded retry.
|
|
|
|
A) Stop the worker, accept an upload, restart the broker (persistent volume),
|
|
start the worker: the accepted job still becomes ready.
|
|
B) Accept an upload while the worker is down, stop S3, start the worker:
|
|
transient failures exhaust MAX_JOB_ATTEMPTS and the attachment ends as
|
|
failed with a safe error_code. S3 is restored afterwards.
|
|
"""
|
|
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, arguments, check, run # noqa: E402
|
|
|
|
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
|
|
|
|
|
def compose(*args):
|
|
subprocess.run(COMPOSE + list(args), cwd=ROOT, check=True,
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
|
|
def wait_status(c, actor, att, wanted, seconds=120):
|
|
deadline = time.monotonic() + seconds
|
|
while time.monotonic() < deadline:
|
|
row = c.call("GET", "/api/v1/attachments/" + att["id"], actor=actor, schema="Attachment")["json"]
|
|
if row["status"] in wanted:
|
|
return row
|
|
time.sleep(0.5)
|
|
raise AssertionError("attachment did not reach " + repr(wanted))
|
|
|
|
|
|
def main():
|
|
args = arguments("lab5 failure drills")
|
|
c = Client(args.base_url, args.ca_file, timeout=10)
|
|
alice, bob, _eve, chat = c.bootstrap()
|
|
|
|
print("A. broker restart keeps accepted work")
|
|
compose("stop", "worker")
|
|
try:
|
|
att, _content = c.attachment(alice, chat)
|
|
compose("restart", "broker")
|
|
deadline = time.monotonic() + 90
|
|
while True:
|
|
try:
|
|
c.call("GET", "/health/ready", schema="Health")
|
|
break
|
|
except Exception:
|
|
check(time.monotonic() < deadline, "API did not recover after broker restart")
|
|
time.sleep(0.5)
|
|
compose("up", "-d", "--no-deps", "worker")
|
|
started = time.monotonic()
|
|
ready = c.wait_ready(bob, att, wait_seconds=90)
|
|
print(json.dumps({
|
|
"scenario": "broker_restart",
|
|
"attachment_id": att["id"],
|
|
"status": ready["status"],
|
|
"seconds_to_ready": round(time.monotonic() - started, 3),
|
|
}, ensure_ascii=False))
|
|
check(ready["status"] == "ready", "job lost after broker restart")
|
|
finally:
|
|
compose("up", "-d", "--no-deps", "worker")
|
|
|
|
print("B. bounded retry ends in a safe failed state")
|
|
compose("stop", "worker")
|
|
try:
|
|
att, _content = c.attachment(alice, chat)
|
|
compose("stop", "s3")
|
|
compose("up", "-d", "--no-deps", "worker")
|
|
failed = wait_status(c, bob, att, {"failed"}, seconds=180)
|
|
print(json.dumps({
|
|
"scenario": "retry_exhausted",
|
|
"attachment_id": att["id"],
|
|
"status": failed["status"],
|
|
"error_code": failed["error_code"],
|
|
}, ensure_ascii=False))
|
|
check(failed["error_code"] in ("processing_failed", "invalid_image"), "unsafe error_code")
|
|
c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", 409, actor=bob)
|
|
finally:
|
|
compose("up", "-d", "s3")
|
|
compose("up", "-d", "--no-deps", "worker")
|
|
deadline = time.monotonic() + 60
|
|
while True:
|
|
try:
|
|
c.call("GET", "/health/ready", schema="Health")
|
|
break
|
|
except Exception:
|
|
check(time.monotonic() < deadline, "S3 did not recover")
|
|
time.sleep(0.5)
|
|
print("PASS: broker durability and bounded retry demonstrated.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|