Prober: the claim's random starting points drawn in code
Test and deploy / test (push) Successful in 54s
Test and deploy / deploy (push) Successful in 1m21s

Postgres evaluated random() once for all 20 slices: every claim still
came from one place in the queue (214 of 214 taken from a.b.zogg).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daan MeijerandClaude Opus 5.5 committed 2026-10-11 16:00:23 +02:00
1 parent 18f5d27413
commit 5ab5399b43
1 file changed
+6 -5
+6 -5
View File
@@ -126,17 +126,18 @@ export class Prober {
for (const kind of KINDS) {
const want = coordinatorConfig.probe.batch - this.queue.length
if (want <= 0) return
// The starting points chosen here: in the query Postgres drew random() once for all slices.
const [{ lo, hi }] = (await db.rawQuery(`SELECT min(id) AS lo, max(id) AS hi FROM probe_candidates`)).rows
const starts = Array.from({ length: SLICES }, () => Number(lo) + Math.floor(Math.random() * (Number(hi) - Number(lo) + 1)))
const { rows } = await db.rawQuery(
`UPDATE probe_candidates SET status = 'taken' WHERE id IN (
SELECT pc.id FROM (SELECT min(id) AS lo, max(id) AS hi FROM probe_candidates) b
CROSS JOIN LATERAL generate_series(1, ?) AS s
SELECT pc.id FROM unnest(?::bigint[]) AS o(start)
CROSS JOIN LATERAL (
SELECT pc.id FROM probe_candidates pc JOIN releases r ON r.id = pc.release_id
WHERE pc.status = 'waiting' AND pc.kind = ? AND r.settled_at IS NOT NULL
AND pc.id >= b.lo + floor(random() * (b.hi - b.lo + 1))::bigint + s * 0
WHERE pc.status = 'waiting' AND pc.kind = ? AND r.settled_at IS NOT NULL AND pc.id >= o.start
ORDER BY pc.id LIMIT ? FOR UPDATE OF pc SKIP LOCKED) pc)
RETURNING id, release_id, kind, message_id`,
[SLICES, kind, Math.ceil(want / SLICES)]
[starts, kind, Math.ceil(want / SLICES)]
)
for (const r of rows) this.queue.push({ id: Number(r.id), releaseId: Number(r.release_id), kind: r.kind, messageId: r.message_id })
}