Prober: the claim's random starting points drawn in code
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:
1 parent
18f5d27413
commit
5ab5399b43
1 file changed
+6
-5
@@ -126,17 +126,18 @@ export class Prober {
|
|||||||
for (const kind of KINDS) {
|
for (const kind of KINDS) {
|
||||||
const want = coordinatorConfig.probe.batch - this.queue.length
|
const want = coordinatorConfig.probe.batch - this.queue.length
|
||||||
if (want <= 0) return
|
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(
|
const { rows } = await db.rawQuery(
|
||||||
`UPDATE probe_candidates SET status = 'taken' WHERE id IN (
|
`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
|
SELECT pc.id FROM unnest(?::bigint[]) AS o(start)
|
||||||
CROSS JOIN LATERAL generate_series(1, ?) AS s
|
|
||||||
CROSS JOIN LATERAL (
|
CROSS JOIN LATERAL (
|
||||||
SELECT pc.id FROM probe_candidates pc JOIN releases r ON r.id = pc.release_id
|
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
|
WHERE pc.status = 'waiting' AND pc.kind = ? AND r.settled_at IS NOT NULL AND pc.id >= o.start
|
||||||
AND pc.id >= b.lo + floor(random() * (b.hi - b.lo + 1))::bigint + s * 0
|
|
||||||
ORDER BY pc.id LIMIT ? FOR UPDATE OF pc SKIP LOCKED) pc)
|
ORDER BY pc.id LIMIT ? FOR UPDATE OF pc SKIP LOCKED) pc)
|
||||||
RETURNING id, release_id, kind, message_id`,
|
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 })
|
for (const r of rows) this.queue.push({ id: Number(r.id), releaseId: Number(r.release_id), kind: r.kind, messageId: r.message_id })
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user