lab6: OTel collector, Prometheus/Loki/Tempo, Grafana dashboard
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
#!/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)
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Demonstrate the lab5 acceptance scenario without hiding infrastructure steps.
|
||||
|
||||
Worker stopped -> upload returns 202 queued -> message references the chat
|
||||
attachment -> API replicas restarted -> worker started -> the same job
|
||||
reaches ready with a contract-compliant JPEG thumbnail.
|
||||
|
||||
The script prints only synthetic IDs and timings. It never prints tokens.
|
||||
"""
|
||||
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 main():
|
||||
args = arguments("lab5 worker-outage demonstration")
|
||||
c = Client(args.base_url, args.ca_file, timeout=10)
|
||||
alice, bob, _eve, chat = c.bootstrap()
|
||||
|
||||
print("1. stop worker")
|
||||
compose("stop", "worker")
|
||||
try:
|
||||
print("2. upload with worker down")
|
||||
att, content = c.attachment(alice, chat)
|
||||
check(att["status"] == "queued", "queued expected after upload")
|
||||
meta = c.call("GET", "/api/v1/attachments/" + att["id"], actor=bob, schema="Attachment")["json"]
|
||||
check(meta["status"] in ("queued", "processing"), "still unprocessed while worker is down")
|
||||
pending = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", 409, actor=bob)
|
||||
check(pending["status"] == 409, "content must be 409 while queued")
|
||||
|
||||
print("3. attach the queued attachment to a message")
|
||||
msg = c.message(alice, chat, "", attachments=[att["id"]])["json"]
|
||||
check(msg["attachment_ids"] == [att["id"]], "message lost the attachment")
|
||||
|
||||
print("4. restart API replicas (accepted work must survive)")
|
||||
compose("restart", "api1", "api2")
|
||||
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")
|
||||
time.sleep(0.5)
|
||||
|
||||
print("5. start worker")
|
||||
compose("up", "-d", "--no-deps", "worker")
|
||||
started = time.monotonic()
|
||||
ready = c.wait_ready(bob, att, wait_seconds=90)
|
||||
elapsed = round(time.monotonic() - started, 3)
|
||||
check(ready["status"] == "ready", "job did not reach ready")
|
||||
variants = [v for v in ready["variants"] if v["name"] == "thumbnail"]
|
||||
check(len(variants) == 1, "expected exactly one thumbnail")
|
||||
v = variants[0]
|
||||
check((v["width"], v["height"]) == (256, 160), "thumbnail dimensions differ")
|
||||
thumb = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content?variant=thumbnail", actor=bob)
|
||||
check(thumb["headers"].get_content_type() == "image/jpeg", "thumbnail must be JPEG")
|
||||
from image_probe import jpeg_size
|
||||
check(jpeg_size(thumb["bytes"]) == (256, 160), "actual JPEG size differs")
|
||||
|
||||
print(json.dumps({
|
||||
"attachment_id": att["id"],
|
||||
"message_id": msg["id"],
|
||||
"status_after_worker_restart": ready["status"],
|
||||
"thumbnail": {"width": v["width"], "height": v["height"],
|
||||
"size_bytes": v["size_bytes"], "sha256": v["sha256"]},
|
||||
"seconds_from_worker_start_to_ready": elapsed,
|
||||
}, ensure_ascii=False))
|
||||
print("PASS: queued job completed after API and worker restart.")
|
||||
finally:
|
||||
compose("up", "-d", "--no-deps", "worker")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Executable → Regular
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
docker compose --env-file .env restart postgres s3 broker api1 api2 worker lb
|
||||
@@ -0,0 +1,2 @@
|
||||
@echo off
|
||||
"C:\Program Files\Git\bin\bash.exe" "%~dp0restart.sh"
|
||||
Executable → Regular
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
: "${TARGET_INSTANCE_ID:?}"
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
api-1) SVC=api1 ;;
|
||||
api-2) SVC=api2 ;;
|
||||
*) echo "unknown instance alias: $TARGET_INSTANCE_ID" >&2; exit 1 ;;
|
||||
esac
|
||||
docker compose --env-file .env stop "$SVC"
|
||||
@@ -0,0 +1,2 @@
|
||||
@echo off
|
||||
"C:\Program Files\Git\bin\bash.exe" "%~dp0stop-one.sh"
|
||||
Reference in New Issue
Block a user