Document pipeline evidence and load profile (U5)

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-02 14:59:32 +03:00
parent ab88c5ba1b
commit 3386e46c39
8 changed files with 357 additions and 21 deletions
+59
View File
@@ -0,0 +1,59 @@
"""Run in a one-shot publisher container to inject publish-before-confirm gap."""
import json
import os
from pathlib import Path
from uuid import UUID, uuid4
import pika
from src.db import pool
from src.publisher import QUEUE, job_payload, publish_batch
attachment_id = UUID(os.environ["FAULT_ATTACHMENT_ID"])
url = Path(os.environ["BROKER_URL_FILE"]).read_text(encoding="utf-8").strip()
broker = pika.BlockingConnection(pika.URLParameters(url))
try:
channel = broker.channel()
channel.queue_declare(queue=QUEUE, passive=True)
channel.confirm_delivery()
token = uuid4()
with pool().connection() as conn:
row = conn.execute(
"SELECT o.id,o.job_id,o.attempt,j.attachment_id,j.pipeline_version,j.created_at "
"FROM attachment_outbox o JOIN attachment_jobs j ON j.id=o.job_id "
"WHERE j.attachment_id=%s AND j.pipeline_version=1 AND o.attempt=1",
(attachment_id,),
).fetchone()
assert row is not None
claimed = conn.execute(
"UPDATE attachment_outbox SET claim_token=%s,"
"claim_expires_at=clock_timestamp()+interval '30 seconds' "
"WHERE id=%s AND claim_token IS NULL AND published_at IS NULL RETURNING id",
(token, row["id"]),
).fetchone()
assert claimed is not None
body = json.dumps(job_payload(row), separators=(",", ":")).encode()
channel.basic_publish(
exchange="", routing_key=QUEUE, mandatory=True, body=body,
properties=pika.BasicProperties(content_type="application/json", delivery_mode=2),
)
# Simulate process death after broker confirm, before PostgreSQL confirm.
with pool().connection() as conn:
conn.execute(
"UPDATE attachment_outbox SET claim_expires_at=clock_timestamp()-interval '1 second' "
"WHERE id=%s AND claim_token=%s AND published_at IS NULL",
(row["id"], token),
)
published = publish_batch(channel)
with pool().connection() as conn:
outbox = conn.execute(
"SELECT published_at IS NOT NULL AS confirmed FROM attachment_outbox "
"WHERE id=%s", (row["id"],),
).fetchone()
count = channel.queue_declare(queue=QUEUE, passive=True).method.message_count
assert published >= 1 and outbox["confirmed"] and count >= 2
print(json.dumps({"confirmed_after_republish": True, "queued_copies_before_worker": count}))
finally:
broker.close()
+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""Show that broker confirm without outbox confirmation safely republishes."""
import subprocess
import sys
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
def compose(*args, input_data=None):
return subprocess.run(
["docker", "compose", "--env-file", ".env", *args], cwd=ROOT,
input=input_data, check=True,
)
def main():
args = arguments("Lab5 publish/confirm gap")
client = Client(args.base_url, args.ca_file)
compose("stop", "worker", "publisher")
attachment = None
try:
alice, bob, _eve, chat = client.bootstrap()
attachment, _original = client.attachment(alice, chat)
check(attachment["status"] == "queued", "upload was not queued")
source = (ROOT / "student-tests/publish_gap_inner.py").read_bytes()
compose(
"run", "--rm", "--no-deps", "-T",
"-e", f"FAULT_ATTACHMENT_ID={attachment['id']}", "publisher", "python", "-",
input_data=source,
)
finally:
compose("up", "-d", "publisher", "worker")
ready = client.wait_ready(bob, attachment)
check(ready["status"] == "ready", "republished job did not finish")
check(len(ready["variants"]) == 3, "duplicate delivery changed variant set")
print("PASS: confirmed broker copy republished after lost DB confirm; worker reached ready once")
if __name__ == "__main__":
run(main)