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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
*
* Each on organizations of its own. The candidate read is global, so the
* rows here are queued before the epoch (negative queue times) and lead
* every batch, whatever other rows the database holds.
* every batch, whatever other rows the database holds. For the same reason
* every tick here must be the only sweep running: the harness takes the
* sweep's own clock off, and the first check reads that it is off.
*/
import { randomUUID } from 'node:crypto';

Expand All @@ -51,12 +53,39 @@ export async function checkRagWatchdogBatch(
sql: Sql,
record: Record,
): Promise<void> {
await checkNoScheduledSweep(sql, record);
await checkSettledFailures(sql, record);
await checkHeldRowLock(sql, record);
await checkFailedRotation(sql, record);
await checkErrorCodePairs(sql, record);
}

/**
* The checks below count what their own ticks did, so no other sweep may run
* beside them: one the job runner starts reads the same rows, which lead its
* batch too. The harness takes the `watchdog.rag_indexing` clock off at boot.
* Its tick in the 2-59/5 slot once queued behind the row the lock check
* holds, and failed it as soon as the check let go, before the check read
* back the row its own tick had deferred (#4113). A clock that comes back
* fails here by name, not as a row another sweep settled. pg-boss's tables
* are internal; these reads are pinned to v12 like the harness's own.
*/
async function checkNoScheduledSweep(sql: Sql, record: Record): Promise<void> {
const [clock] = await sql<{ schedules: string; ticks: string }[]>`
SELECT
(SELECT count(*) FROM pgboss.schedule
WHERE name = 'watchdog.rag_indexing')::text AS schedules,
(SELECT count(*) FROM pgboss.job
WHERE name = 'watchdog.rag_indexing'
AND state IN ('created', 'retry', 'active'))::text AS ticks
`;
record(
'rag watchdog: no scheduled sweep runs beside the ticks these checks count',
clock?.schedules === '0' && clock.ticks === '0',
`watchdog.rag_indexing schedules=${clock?.schedules} (want 0), queued or running ticks=${clock?.ticks} (want 0)`,
);
}

async function insertOrganization(
sql: Sql,
label: string,
Expand Down Expand Up @@ -278,7 +307,12 @@ async function checkHeldRowLock(sql: Sql, record: Record): Promise<void> {
const lockTaken = new Promise<void>((resolve) => {
taken = resolve;
});
let holderPid = -1;
holder = sql.begin(async (tx) => {
const [self] = await tx<{ pid: number }[]>`
SELECT pg_backend_pid() AS pid
`;
holderPid = self?.pid ?? -1;
await tx`
SELECT id FROM app.file_metadata WHERE id = ${locked?.id ?? ''}
FOR UPDATE
Expand All @@ -297,6 +331,13 @@ async function checkHeldRowLock(sql: Sql, record: Record): Promise<void> {
}),
]);
const elapsedMs = Date.now() - started;
// Nothing may wait on the row once the tick is back: a write left queued
// behind the lock lands as soon as the lock is let go, and the read below
// would race it.
const [waiting] = await sql<{ count: string }[]>`
SELECT count(*)::text AS count FROM pg_stat_activity
WHERE ${holderPid}::int = ANY(pg_blocking_pids(pid))
`;
release();
await holder;
const outcome = await sweep;
Expand All @@ -310,10 +351,11 @@ async function checkHeldRowLock(sql: Sql, record: Record): Promise<void> {
'rag watchdog: a row lock held past the settle lock timeout defers that row alone',
returned &&
elapsedMs < 10_000 &&
waiting?.count === '0' &&
statusOf('first.pdf') === 'failed' &&
statusOf('locked.pdf') === 'running' &&
statusOf('after.pdf') === 'failed',
`returned while the lock was held=${returned} after ${elapsedMs} ms (want < 10000), first=${statusOf('first.pdf')} locked=${statusOf('locked.pdf')} (want running) after=${statusOf('after.pdf')}, tick=${JSON.stringify(outcome)}`,
`returned while the lock was held=${returned} after ${elapsedMs} ms (want < 10000), still waiting on the lock=${waiting?.count} (want 0), first=${statusOf('first.pdf')} locked=${statusOf('locked.pdf')} (want running) after=${statusOf('after.pdf')}, tick=${JSON.stringify(outcome)}`,
);
} finally {
release();
Expand Down
45 changes: 31 additions & 14 deletions services/platform/backend/integration-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ import { checkWorkerDrainHandOff } from './jobs/runner.integration.ts';
import { startWorker } from './jobs/runner.ts';
import { registerSchedules } from './jobs/schedules.ts';
import { createTaskList } from './jobs/task-list.ts';
import type { TaskIdentifier } from './jobs/tasks.ts';
import {
BACKEND_SERVER_OPTIONS,
installClientErrorEnvelope,
Expand Down Expand Up @@ -58361,21 +58362,37 @@ async function main(): Promise<void> {
triggerSchedules.length === 1 && triggerSchedules[0]?.cron === '* * * * *',
`schedules=${triggerSchedules.length}, cron=${triggerSchedules[0]?.cron}`,
);
// The delivery lanes drive the real scan explicitly, including overlapping
// workers and fleets across pages. A wall-clock tick competing with those
// calls makes their per-scan counts depend on the host's minute boundary.
// Disable only that recurring clock, before starting any worker; any tick
// Lanes drive the work of these recurring jobs explicitly, and a wall-clock
// tick competing with those calls makes their outcome depend on the host's
// minute boundary:
//
// - `automation.trigger_scan`: the delivery lanes drive the real scan,
// including overlapping workers and fleets across pages, and count what
// each scan fired;
// - `watchdog.rag_indexing`: the RAG watchdog lanes drive the real sweep,
// whose candidate read is global and led by their rows. A tick in its
// slot (2-59/5) settled those rows too: it queued behind the row the
// lock lane holds and failed it as soon as the lane let go, before the
// lane read back the row its own tick had deferred (#4113).
//
// Disable only those recurring clocks, before starting any worker; any tick
// queued during schedule registration is cancelled as well.
await boss.unschedule('automation.trigger_scan');
const queuedScans = await sql<{ id: string }[]>`
SELECT id FROM pgboss.job WHERE name = 'automation.trigger_scan'
AND state IN ('created', 'retry')
`;
if (queuedScans.length > 0) {
await boss.cancel(
'automation.trigger_scan',
queuedScans.map((job) => job.id),
);
const laneDrivenSchedules: readonly TaskIdentifier[] = [
'automation.trigger_scan',
'watchdog.rag_indexing',
];
for (const name of laneDrivenSchedules) {
await boss.unschedule(name);
const queued = await sql<{ id: string }[]>`
SELECT id FROM pgboss.job WHERE name = ${name}
AND state IN ('created', 'retry')
`;
if (queued.length > 0) {
await boss.cancel(
name,
queued.map((job) => job.id),
);
}
}
setEnqueueBoss(boss);
// No itest job may ever open a real IMAP/SMTP connection.
Expand Down