diff --git a/infra/github-backup/bin/drain_queue.py b/infra/github-backup/bin/drain_queue.py index c635f80afb..6e5de0ed94 100644 --- a/infra/github-backup/bin/drain_queue.py +++ b/infra/github-backup/bin/drain_queue.py @@ -54,6 +54,16 @@ # invocation at 1,000 KV operations — acks are sent in batches well # under that ceiling so a multi-page backlog can always drain. ACK_BATCH_SIZE = 500 +# Pagination bounds for queue_events (#6572, design #6622 Phase 2): a +# Worker page stream that always answers list_complete=false with a +# fresh, never-repeated cursor otherwise loops forever, accumulating +# events and never acking. Pages carry 500 events and the largest +# observed backlog is ~5.9k events (~12 pages), so these bounds sit +# roughly 8x above any legitimate drain; a stream that crosses either +# one is a stuck external system, and the poll fails closed (nothing +# from it is acked) with an error naming the bound it hit. +MAX_QUEUE_PAGES = 100 +MAX_QUEUE_EVENTS = 50_000 def worker_req(path, payload=None): @@ -91,23 +101,36 @@ def queue_events(queue_poll_path): Older Worker responses without pagination metadata remain a one-page response. An incomplete/malformed cursor fails closed before any event - from this poll can be acknowledged. + from this poll can be acknowledged, as does a page stream that crosses + MAX_QUEUE_PAGES / MAX_QUEUE_EVENTS without completing (#6572). """ events = [] cursor = None seen_cursors = set() + pages = 0 while True: path = queue_poll_path if cursor is not None: sep = "&" if "?" in path else "?" path += sep + urllib.parse.urlencode({"cursor": cursor}) page = worker_req(path) + pages += 1 page_events = page.get("events", []) if not isinstance(page_events, list): raise RuntimeError("Worker queue response events must be a list") events.extend(page_events) if page.get("list_complete", True): return events + if pages >= MAX_QUEUE_PAGES: + raise RuntimeError( + f"Worker queue pagination exceeded MAX_QUEUE_PAGES " + f"({MAX_QUEUE_PAGES}) without list_complete" + ) + if len(events) > MAX_QUEUE_EVENTS: + raise RuntimeError( + f"Worker queue pagination exceeded MAX_QUEUE_EVENTS " + f"({MAX_QUEUE_EVENTS}) accumulated events without list_complete" + ) next_cursor = page.get("cursor") if not isinstance(next_cursor, str) or not next_cursor: raise RuntimeError("Worker queue response is incomplete but has no cursor") diff --git a/infra/github-backup/tests/test_drain_queue.py b/infra/github-backup/tests/test_drain_queue.py index 80da9cd603..13fed4deb5 100644 --- a/infra/github-backup/tests/test_drain_queue.py +++ b/infra/github-backup/tests/test_drain_queue.py @@ -439,4 +439,95 @@ def check(name, cond): summary = json.loads(buf.getvalue().strip().splitlines()[-1]) check("after-malformed: drop counted in summary", summary.get("dropped_foreign") == 1) + +# 17. a page stream that never completes — every page incomplete with a +# fresh, never-repeated cursor (#6572) — must raise at the named +# pagination bound instead of looping forever, and a Harness-level +# run over the bound acks nothing. Oracle: design #6622's contract — +# complete, bounded, or say so; the error names the bound it hit. +_calls = {"n": 0} + + +def _novel_cursor_req(path, payload=None): + _calls["n"] += 1 + return { + "events": [ev(f"novel-{_calls['n']}", "owner/repo")], + "cursor": f"cursor-{_calls['n']}", + "list_complete": False, + } + + +_saved_req = drain_queue.worker_req +drain_queue.worker_req = _novel_cursor_req +try: + try: + drain_queue.queue_events("/queue") + raised = None + except RuntimeError as e: + raised = str(e) +finally: + drain_queue.worker_req = _saved_req +check("page bound: novel-cursor stream raises", raised is not None) +check( + "page bound: error names MAX_QUEUE_PAGES", + raised is not None and "MAX_QUEUE_PAGES" in raised, +) +check( + "page bound: stopped exactly at the bound", + _calls["n"] == drain_queue.MAX_QUEUE_PAGES, +) + +# the accumulated-events bound trips on few-but-huge incomplete pages +_huge_calls = {"n": 0} + + +def _huge_page_req(path, payload=None): + _huge_calls["n"] += 1 + return { + "events": [ev(f"huge-{_huge_calls['n']}-{i}", "owner/repo") for i in range(30_000)], + "cursor": f"huge-cursor-{_huge_calls['n']}", + "list_complete": False, + } + + +drain_queue.worker_req = _huge_page_req +try: + try: + drain_queue.queue_events("/queue") + raised = None + except RuntimeError as e: + raised = str(e) +finally: + drain_queue.worker_req = _saved_req +check("event bound: huge-page stream raises", raised is not None) +check( + "event bound: error names MAX_QUEUE_EVENTS", + raised is not None and "MAX_QUEUE_EVENTS" in raised, +) +check("event bound: tripped on the second page", _huge_calls["n"] == 2) + +# a stream that completes exactly at the page bound still returns +_edge_calls = {"n": 0} + + +def _edge_req(path, payload=None): + _edge_calls["n"] += 1 + last = _edge_calls["n"] >= drain_queue.MAX_QUEUE_PAGES + return { + "events": [ev(f"edge-{_edge_calls['n']}", "owner/repo")], + "cursor": None if last else f"edge-cursor-{_edge_calls['n']}", + "list_complete": last, + } + + +drain_queue.worker_req = _edge_req +try: + got = drain_queue.queue_events("/queue") +finally: + drain_queue.worker_req = _saved_req +check( + "page bound: completion at the bound returns every event", + len(got) == drain_queue.MAX_QUEUE_PAGES, +) + print(f"\n{sum(1 for _, ok in PASS if ok)}/{len(PASS)} checks passed")