Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions infra/cargo-target-janitor/bin/cargo_target_janitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,33 @@ def queue_claims(client) -> set[int]:

Paginated (#6618): a single page capped at 1000 rows silently
dropped every claimed task past row 1000 from the never-delete
set. Pages are followed with offset until a short page arrives;
needing more than MAX_CLAIM_PAGES pages fails closed.
set. Pages are followed until a short page arrives; needing more
than MAX_CLAIM_PAGES pages fails closed.

Keyset pagination on id (#6790): pages are ordered by id and each
next page asks for id greater than the largest id seen. Offset
paging over this LIVE set was not stable — PostgREST re-evaluates
offset against the current filtered set on every request, so a
task leaving the claimed set mid-read shifted every later row one
position forward and the next offset page skipped a still-claimed
task, dropping it from the never-delete set. Rows leaving behind
the keyset cursor cannot shift rows ahead of it; rows ahead are
read exactly once, in order.
"""
module, key = client
rows = []
offset = 0
after = 0
for _ in range(MAX_CLAIM_PAGES):
query = (
"tasks?select=id,queue,status,payload&"
f"queue=eq.{QUEUE}&status=eq.claimed"
f"&limit={CLAIMS_PAGE}&offset={offset}"
f"&id=gt.{after}&order=id.asc&limit={CLAIMS_PAGE}"
)
page = module.http("GET", query, key) or []
rows.extend(page)
if len(page) < CLAIMS_PAGE:
break
offset += CLAIMS_PAGE
after = max(int(row["id"]) for row in page)
else:
raise SafetyError(
f"claimed-task read exceeded {MAX_CLAIM_PAGES} pages "
Expand Down
29 changes: 29 additions & 0 deletions infra/cargo-target-janitor/tests/test_cargo_target_janitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ def http(self, method, query, key):
for x in re.search(r"id=in\.\%28([0-9,]+)\%29", query).group(1).split(",")
}
return [r for r in self.rows if r["id"] in ids][:limit]
if "id=gt." in query:
if self.always_full:
return self.rows[:limit]
after = int(re.search(r"id=gt\.(\d+)", query).group(1))
keyset = sorted(
(r for r in self.rows if r["id"] > after), key=lambda r: r["id"]
)
return keyset[:limit]
offset = int(re.search(r"offset=(\d+)", query).group(1))
if self.always_full:
return self.rows[:limit]
Expand All @@ -258,6 +266,27 @@ def test_queue_claims_at_cap_boundary(self):
stub = StubQueueModule([claimed_row(i) for i in range(1, 1001)])
self.assertEqual(janitor.queue_claims((stub, "key")), set(range(1, 1001)))

def test_queue_claims_survives_churn_behind_the_cursor(self):
"""#6790: a task leaving the claimed set mid-read must not
shift a still-claimed task out of the never-delete set."""
rows = [claimed_row(i) for i in range(1, 1501)]

class ChurnStub(StubQueueModule):
def http(self, method, query, key):
page = super().http(method, query, key)
if len(self.queries) == 1:
# Task 1 completes after page 1 is served — the
# ordinary churn of a live claimed set.
self.rows = [r for r in self.rows if r["id"] != 1]
return page

stub = ChurnStub(rows)
got = janitor.queue_claims((stub, "key"))
# Task 1001 is the row offset paging skipped in the filing's
# probe; the keyset read returns every claimed id.
self.assertEqual(got, set(range(1, 1501)))
self.assertTrue(all("offset=" not in q for q in stub.queries))

def test_queue_claims_fails_closed_when_pages_never_end(self):
stub = StubQueueModule(
[claimed_row(i) for i in range(1, 1001)], always_full=True
Expand Down
Loading