forked from mtusi2026/mtusi2026lab6
lab 6 observability
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Lab5 level-4 failure scenarios against a RUNNING compose stack (stdlib only; run from the repo root).
|
||||
|
||||
python3 student-tests/level4_failures.py all # or: worker_down | commit_publish_gap | crash_after_s3 | replay | s3_outage
|
||||
|
||||
Each scenario drives `docker compose` (never `down -v`: volumes stay) and prints PASS/FAIL with the observed facts.
|
||||
"""
|
||||
import base64, json, os, subprocess, sys, time, urllib.error, urllib.request, uuid
|
||||
from pathlib import Path
|
||||
|
||||
BASE = os.getenv("BASE_URL", "http://localhost:8080")
|
||||
PNG = (Path(__file__).resolve().parents[1] / "tests/fixtures/sample.png").read_bytes()
|
||||
DC = ["docker", "compose", "--env-file", ".env"]
|
||||
|
||||
|
||||
def sh(*args, env=None, check=True):
|
||||
r = subprocess.run(list(args), capture_output=True, text=True, env={**os.environ, **(env or {})})
|
||||
if check and r.returncode:
|
||||
raise SystemExit(f"command failed: {' '.join(args)}\n{r.stderr[-500:]}")
|
||||
return r.stdout.strip()
|
||||
|
||||
|
||||
def sql(query):
|
||||
return sh(*DC, "exec", "-T", "postgres", "sh", "-c", f'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At -c "{query}"')
|
||||
|
||||
|
||||
def http(method, path, token=None, body=None, headers=None, raw=None):
|
||||
h = dict(headers or {})
|
||||
if token:
|
||||
h["Authorization"] = "Bearer " + token
|
||||
data = raw if raw is not None else (json.dumps(body).encode() if body is not None else None)
|
||||
if body is not None:
|
||||
h["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(BASE + path, data=data, method=method, headers=h)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
payload = r.read()
|
||||
return r.status, (json.loads(payload) if payload and "json" in r.headers.get("Content-Type", "") else payload)
|
||||
except urllib.error.HTTPError as e:
|
||||
payload = e.read()
|
||||
try:
|
||||
return e.code, json.loads(payload)
|
||||
except ValueError:
|
||||
return e.code, payload
|
||||
|
||||
|
||||
def setup():
|
||||
name = "u" + uuid.uuid4().hex[:10]
|
||||
pw = "correct-horse-battery"
|
||||
st, user = http("POST", "/api/v1/users", body={"username": name, "display_name": "Tester", "password": pw})
|
||||
assert st == 201, (st, user)
|
||||
cred = base64.b64encode(f"{name}:{pw}".encode()).decode()
|
||||
st, sess = http("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred})
|
||||
token = sess["token"]
|
||||
st, chat = http("POST", "/api/v1/chats", token, {"title": "lab5", "member_ids": [user["id"]]})
|
||||
return token, chat["id"]
|
||||
|
||||
|
||||
def upload(token, chat):
|
||||
st, att = http("POST", f"/api/v1/chats/{chat}/attachments?filename=sample.png", token, raw=PNG, headers={"Content-Type": "image/png"})
|
||||
assert st == 202 and att["status"] == "queued", (st, att)
|
||||
return att["id"]
|
||||
|
||||
|
||||
def wait(token, aid, want, timeout=90):
|
||||
end, last = time.time() + timeout, None
|
||||
while time.time() < end:
|
||||
st, att = http("GET", f"/api/v1/attachments/{aid}", token)
|
||||
last = att
|
||||
if st == 200 and att["status"] == want:
|
||||
return att
|
||||
time.sleep(1)
|
||||
raise AssertionError(f"attachment {aid} did not reach {want}; last={last}")
|
||||
|
||||
|
||||
def wait_ready_api(timeout=90):
|
||||
end = time.time() + timeout
|
||||
while time.time() < end:
|
||||
if http("GET", "/health/ready")[0] == 200:
|
||||
return
|
||||
time.sleep(1)
|
||||
raise AssertionError("API not ready")
|
||||
|
||||
|
||||
def check_variants(att):
|
||||
v = {x["name"]: x for x in att["variants"]}
|
||||
assert (v["thumbnail"]["width"], v["thumbnail"]["height"], v["thumbnail"]["content_type"]) == (256, 160, "image/jpeg"), v["thumbnail"]
|
||||
assert (v["crop"]["width"], v["crop"]["height"]) == (128, 128), v["crop"]
|
||||
assert v["optimized"]["content_type"] == "image/webp"
|
||||
|
||||
|
||||
def events(aid):
|
||||
return sql(f"SELECT string_agg(event, ',' ORDER BY id) FROM job_events e JOIN attachment_jobs j USING (job_id) WHERE j.attachment_id='{aid}'")
|
||||
|
||||
|
||||
def worker_down():
|
||||
token, chat = setup()
|
||||
sh(*DC, "stop", "worker")
|
||||
aid = upload(token, chat)
|
||||
st, _ = http("GET", f"/api/v1/attachments/{aid}/content", token)
|
||||
assert st == 409, st # content of a queued attachment
|
||||
st, msg = http("POST", f"/api/v1/chats/{chat}/messages", token, {"text": "hi", "attachment_ids": [aid]}, {"Idempotency-Key": "k1"})
|
||||
assert st == 201, (st, msg) # other features keep working; queued attachment can be attached
|
||||
sh(*DC, "restart", "api"); wait_ready_api()
|
||||
assert http("GET", f"/api/v1/attachments/{aid}", token)[1]["status"] == "queued" # accepted work survived the API restart
|
||||
sh(*DC, "up", "-d", "worker")
|
||||
att = wait(token, aid, "ready"); check_variants(att)
|
||||
print("PASS worker_down:", aid, "events:", events(aid))
|
||||
|
||||
|
||||
def commit_publish_gap():
|
||||
token, chat = setup()
|
||||
sh(*DC, "up", "-d", "dispatcher", env={"FAULT_DISPATCHER_EXIT_BEFORE_PUBLISH": "1"}, check=True)
|
||||
sh(*DC, "exec", "-T", "dispatcher", "rm", "-f", "/tmp/dispatcher_fault_done", check=False)
|
||||
sh(*DC, "restart", "dispatcher") # fresh container process: the fault fires once, on the first outbox batch
|
||||
time.sleep(3)
|
||||
aid = upload(token, chat) # COMMIT happens; the dispatcher dies before publishing
|
||||
att = wait(token, aid, "ready", 90); check_variants(att)
|
||||
sh(*DC, "up", "-d", "dispatcher") # back to the normal configuration (.env: fault off)
|
||||
print("PASS commit_publish_gap:", aid, "events:", events(aid))
|
||||
|
||||
|
||||
def crash_after_s3():
|
||||
token, chat = setup()
|
||||
env = {"FAULT_WORKER_EXIT_AFTER_S3": "1", "JOB_TIMEOUT_SECONDS": "10", "LEASE_GRACE_SECONDS": "5"}
|
||||
sh(*DC, "up", "-d", "worker", env=env)
|
||||
time.sleep(5)
|
||||
aid = upload(token, chat)
|
||||
att = wait(token, aid, "ready", 150); check_variants(att)
|
||||
n = sql(f"SELECT count(*) FROM attachment_variants WHERE attachment_id='{aid}'")
|
||||
assert n == "3", n # the retry completed the SAME result, no second set of variants
|
||||
sh(*DC, "up", "-d", "worker") # restore normal worker config
|
||||
print("PASS crash_after_s3:", aid, "events:", events(aid))
|
||||
|
||||
|
||||
def replay():
|
||||
token, chat = setup()
|
||||
sh(*DC, "stop", "worker")
|
||||
aid = upload(token, chat)
|
||||
good = sql(f"SELECT sha256 FROM attachments WHERE id='{aid}'")
|
||||
sql(f"UPDATE attachments SET sha256=repeat('0',64) WHERE id='{aid}'") # simulate corrupted/tampered staging bytes
|
||||
sh(*DC, "up", "-d", "worker")
|
||||
att = wait(token, aid, "failed", 60)
|
||||
assert att["error_code"] == "integrity_error", att # permanent error: failed at once, no retries
|
||||
st, _ = http("POST", f"/api/v1/chats/{chat}/messages", token, {"text": "", "attachment_ids": [aid]}, {"Idempotency-Key": "k2"})
|
||||
assert st == 409, st
|
||||
sql(f"UPDATE attachments SET sha256='{good}' WHERE id='{aid}'") # operator fixes the cause
|
||||
job = sql(f"SELECT job_id FROM attachment_jobs WHERE attachment_id='{aid}'")
|
||||
print(sh(*DC, "exec", "-T", "worker", "python", "scripts/replay_failed.py", "--job-id", job, "--reason", "student-test"))
|
||||
att = wait(token, aid, "ready", 60); check_variants(att)
|
||||
again = sh(*DC, "exec", "-T", "worker", "python", "scripts/replay_failed.py", "--job-id", job, check=False)
|
||||
assert "REFUSED" in again, again # replay of a successful job is refused
|
||||
print("PASS replay:", aid, "events:", events(aid))
|
||||
|
||||
|
||||
def s3_outage():
|
||||
token, chat = setup()
|
||||
sh(*DC, "stop", "worker")
|
||||
aid = upload(token, chat)
|
||||
sh(*DC, "stop", "s3")
|
||||
sh(*DC, "up", "-d", "--no-deps", "worker")
|
||||
time.sleep(15)
|
||||
ev = events(aid)
|
||||
assert "retry_scheduled" in ev, ev # temporary error => backoff with jitter, not failed
|
||||
sh(*DC, "start", "s3"); wait_ready_api()
|
||||
att = wait(token, aid, "ready", 120); check_variants(att)
|
||||
print("PASS s3_outage:", aid, "events:", events(aid))
|
||||
|
||||
|
||||
SCENARIOS = {"worker_down": worker_down, "commit_publish_gap": commit_publish_gap, "crash_after_s3": crash_after_s3, "replay": replay, "s3_outage": s3_outage}
|
||||
|
||||
if __name__ == "__main__":
|
||||
which = sys.argv[1] if len(sys.argv) > 1 else "all"
|
||||
for name, fn in SCENARIOS.items():
|
||||
if which in ("all", name):
|
||||
fn()
|
||||
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Lab6 level-4 observability scenarios against a RUNNING compose stack (stdlib only, run from the repo root).
|
||||
|
||||
python3 student-tests/level4_observability.py trace_chain | queue_lag | db_slow | telemetry_down | all
|
||||
Redirect to evidence: ... all | tee evidence/level4-observability.txt (no tokens are printed)
|
||||
"""
|
||||
import json, subprocess, sys, threading, time, urllib.parse
|
||||
sys.path.insert(0, __import__("pathlib").Path(__file__).parent.as_posix())
|
||||
from level4_failures import DC, http, setup, upload, wait, sh, sql
|
||||
|
||||
def prom(query):
|
||||
out = sh(*DC, "exec", "-T", "prometheus", "wget", "-qO-", "http://localhost:9090/api/v1/query?query=" + urllib.parse.quote(query))
|
||||
return json.loads(out)["data"]["result"]
|
||||
|
||||
def firing(name):
|
||||
return any(r["metric"].get("alertname") == name and r["metric"].get("alertstate") == "firing" for r in prom("ALERTS"))
|
||||
|
||||
def wait_alert(name, want, timeout):
|
||||
end = time.time() + timeout
|
||||
while time.time() < end:
|
||||
if firing(name) == want:
|
||||
return round(timeout - (end - time.time()))
|
||||
time.sleep(5)
|
||||
raise AssertionError(f"alert {name} did not become {'firing' if want else 'resolved'} in {timeout}s")
|
||||
|
||||
def grafana_get(url):
|
||||
return sh(*DC, "exec", "-T", "grafana", "wget", "-qO-", url)
|
||||
|
||||
def trace_chain():
|
||||
token, chat = setup(); aid = upload(token, chat); wait(token, aid, "ready")
|
||||
time.sleep(10) # batch export + ingestion
|
||||
q = urllib.parse.quote(f'{{service_name="messenger-api"}} | event="attachment_accepted" | attachment_id="{aid}"')
|
||||
res = json.loads(grafana_get(f"http://loki:3100/loki/api/v1/query_range?query={q}"))["data"]["result"]
|
||||
tid = res[0]["stream"]["trace_id"]
|
||||
tr = json.loads(grafana_get(f"http://tempo:3200/api/traces/{tid}"))
|
||||
spans = {}
|
||||
for b in tr["batches"]:
|
||||
svc = [a["value"]["stringValue"] for a in b["resource"]["attributes"] if a["key"] == "service.name"][0]
|
||||
for ss in b["scopeSpans"]:
|
||||
for s in ss["spans"]:
|
||||
spans[s["spanId"]] = (svc, s["name"], s.get("kind"), s.get("parentSpanId", ""))
|
||||
kinds = {(v[0], v[2]) for v in spans.values()}
|
||||
assert ("messenger-api", "SPAN_KIND_SERVER") in kinds and ("messenger-dispatcher", "SPAN_KIND_PRODUCER") in kinds and ("messenger-worker", "SPAN_KIND_CONSUMER") in kinds, kinds
|
||||
consumer = next(v for v in spans.values() if v[2] == "SPAN_KIND_CONSUMER")
|
||||
assert spans[consumer[3]][2] == "SPAN_KIND_PRODUCER", "consumer must be a child of the producer span"
|
||||
print("PASS trace_chain: trace", tid, "spans:", sorted({f"{v[0]}:{v[1]}" for v in spans.values()}))
|
||||
|
||||
def queue_lag(): # incident 1: worker stopped -> oldest job age grows -> QueueLagGrowing firing -> worker back -> resolved
|
||||
token, chat = setup(); sh(*DC, "stop", "worker"); aid = upload(token, chat)
|
||||
t = wait_alert("QueueLagGrowing", True, 240); age = prom("max(messenger_queue_oldest_job_age_seconds)")[0]["value"][1]
|
||||
print(f"queue_lag: FIRING after ~{t}s, oldest_job_age={float(age):.0f}s, depth={prom('max(messenger_queue_depth)')[0]['value'][1]}")
|
||||
sh(*DC, "up", "-d", "worker"); wait(token, aid, "ready", 120)
|
||||
print(f"PASS queue_lag: ready after worker restart; RESOLVED after ~{wait_alert('QueueLagGrowing', False, 240)}s")
|
||||
|
||||
def db_slow(): # incident 2: ACCESS EXCLUSIVE lock on messages -> INSERT waits until statement_timeout (5s) -> 503
|
||||
token, chat = setup(); stop = threading.Event(); codes = []
|
||||
def load():
|
||||
i = 0
|
||||
while not stop.is_set():
|
||||
i += 1
|
||||
codes.append(http("POST", f"/api/v1/chats/{chat}/messages", token, {"text": "x"}, {"Idempotency-Key": f"k{threading.get_ident()}-{i}-{time.time()}"})[0])
|
||||
lock = subprocess.Popen([*DC, "exec", "-T", "postgres", "sh", "-c", 'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "BEGIN; LOCK TABLE messages IN ACCESS EXCLUSIVE MODE; SELECT pg_sleep(150); COMMIT;"'], stdout=subprocess.DEVNULL)
|
||||
ts = [threading.Thread(target=load) for _ in range(4)]; [t.start() for t in ts]
|
||||
t = wait_alert("HighApiErrorRate", True, 240)
|
||||
print(f"db_slow: FIRING after ~{t}s; 5xx so far: {codes.count(503)} of {len(codes)}")
|
||||
sql("select pg_terminate_backend(pid) from pg_stat_activity where query like '%pg_sleep(150)%' and pid<>pg_backend_pid()")
|
||||
lock.wait(); print(f"PASS db_slow: lock released; RESOLVED after ~{wait_alert('HighApiErrorRate', False, 300)}s")
|
||||
stop.set(); [t.join() for t in ts]
|
||||
|
||||
def telemetry_down(): # scenario 3: stop all telemetry stores, API keeps working, Collector RAM stays bounded
|
||||
token, chat = setup(); sh(*DC, "stop", "prometheus", "loki", "tempo")
|
||||
bad = 0; end = time.time() + 90
|
||||
while time.time() < end:
|
||||
bad += http("POST", f"/api/v1/chats/{chat}/messages", token, {"text": "x"}, {"Idempotency-Key": f"t{time.time()}"})[0] != 201
|
||||
aid = upload(token, chat); wait(token, aid, "ready", 90)
|
||||
mem = sh("docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", sh(*DC, "ps", "-q", "otel-collector"))
|
||||
assert bad == 0, bad
|
||||
sh(*DC, "up", "-d", "prometheus", "loki", "tempo"); time.sleep(60)
|
||||
print(f"PASS telemetry_down: 0 failed writes with all telemetry stores down; collector memory {mem} (limit 256MiB); stores restarted")
|
||||
|
||||
if __name__ == "__main__":
|
||||
names = {"trace_chain": trace_chain, "queue_lag": queue_lag, "db_slow": db_slow, "telemetry_down": telemetry_down}
|
||||
for n in (names if sys.argv[1] == "all" else [sys.argv[1]]):
|
||||
names[n]()
|
||||
Reference in New Issue
Block a user