Skip to content

Commit 65a6ca7

Browse files
d-csTrigger.dev RepoOps
authored andcommitted
feat(run-store): report outflow metrics, back off while the archive fails, and cover a stalled outflow worker (unwired)
Mono-RevId: 99b64aa36db3748f0c513b6faec28b7a88006412
1 parent 6ed4499 commit 65a6ca7

11 files changed

Lines changed: 1989 additions & 35 deletions

‎internal-packages/run-store/src/outflowArchive.test.ts‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import {
1111
type ArchiveCopyRecord,
1212
type ArchiveSetUpsert,
1313
} from "./outflowArchive.js";
14+
import { startArchiveProxy } from "./testFixtures/archiveProxy.js";
1415
import {
1516
ARCHIVE_STAND_IN_REFUSED_TASK,
1617
archiveStandInUpsert,
18+
createArchiveRole,
1719
createArchiveStandIn,
1820
readArchiveRow,
1921
} from "./testFixtures/archiveStandIn.js";
@@ -631,4 +633,99 @@ describe("outflow archive copy", () => {
631633
}
632634
}
633635
);
636+
637+
// Catches: a ping that fails while the archive answers, or that waits past the write timeout while
638+
// it does not.
639+
heteroRunOpsPostgresTest(
640+
"answers a ping while the archive is up and rejects within the write timeout while it is down",
641+
async ({ uri17 }) => {
642+
const proxy = await startArchiveProxy(uri17);
643+
const writer = new OutflowArchiveWriter({
644+
connectionString: proxy.connectionString,
645+
poolSize: 1,
646+
writeTimeoutMs: 300,
647+
reclaimIdleMs: 60_000,
648+
upsert: archiveStandInUpsert,
649+
});
650+
try {
651+
await writer.ping();
652+
proxy.setUp(false);
653+
// The first ping may take the pooled connection the outage ended; the second opens a new
654+
// one that gets no answer.
655+
for (let i = 0; i < 2; i++) {
656+
const started = performance.now();
657+
await expect(writer.ping()).rejects.toThrow();
658+
expect(performance.now() - started).toBeLessThan(300 + 200);
659+
}
660+
proxy.setUp(true);
661+
await writer.ping();
662+
expect(proxy.accepts.some((accept) => !accept.up)).toBe(true);
663+
} finally {
664+
await writer.close();
665+
await proxy.close();
666+
}
667+
}
668+
);
669+
670+
// Catches: a ping that waits on a connection that stops answering after it was established, which
671+
// only the ping's own deadline ends (the pool's connect timeout covers connecting only).
672+
heteroRunOpsPostgresTest(
673+
"rejects a ping within the write timeout when an established connection goes silent",
674+
async ({ uri17 }) => {
675+
const proxy = await startArchiveProxy(uri17);
676+
const writer = new OutflowArchiveWriter({
677+
connectionString: proxy.connectionString,
678+
poolSize: 1,
679+
writeTimeoutMs: 300,
680+
reclaimIdleMs: 60_000,
681+
upsert: archiveStandInUpsert,
682+
});
683+
try {
684+
// The pool keeps this connection, and the next ping is sent on it.
685+
await writer.ping();
686+
proxy.setSilent(true);
687+
const outcome = await Promise.race([
688+
writer.ping().then(
689+
() => "answered",
690+
() => "rejected"
691+
),
692+
new Promise((resolve) => setTimeout(() => resolve("pending"), 300 + 200)),
693+
]);
694+
expect(outcome).toBe("rejected");
695+
expect(proxy.accepts).toHaveLength(1);
696+
proxy.setSilent(false);
697+
await writer.ping();
698+
} finally {
699+
// The proxy first: a ping still waiting holds its connection, and closing the pool waits for it.
700+
await proxy.close();
701+
await writer.close();
702+
}
703+
}
704+
);
705+
706+
// Catches: a ping that answers on a session that cannot write, so a failing worker resumes claims
707+
// while every upsert is still rejected.
708+
heteroRunOpsPostgresTest(
709+
"rejects a ping on a read-only session and answers once the archive is writable",
710+
async ({ uri17 }) => {
711+
const role = await createArchiveRole(uri17);
712+
await role.setReadOnly(true);
713+
const writer = new OutflowArchiveWriter({
714+
connectionString: role.connectionString,
715+
poolSize: 1,
716+
writeTimeoutMs: 10_000,
717+
reclaimIdleMs: 60_000,
718+
upsert: archiveStandInUpsert,
719+
});
720+
try {
721+
await expect(writer.ping()).rejects.toThrow(/read-only/);
722+
// The rejected ping destroyed its session, so the next opens one in the new mode.
723+
await role.setReadOnly(false);
724+
await writer.ping();
725+
} finally {
726+
await writer.close();
727+
await role.drop();
728+
}
729+
}
730+
);
634731
});

‎internal-packages/run-store/src/outflowArchive.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,52 @@ export class OutflowArchiveWriter {
172172
}
173173
}
174174

175+
/**
176+
* Asks a connection from the pool whether its session can write. Resolves when the archive answers
177+
* that it can; rejects when the session is read-only, on any error, or once `writeTimeoutMs` has
178+
* passed, and then destroys the connection it used.
179+
*/
180+
async ping(): Promise<void> {
181+
if (this.#closed) throw new Error("OutflowArchiveWriter is closed");
182+
let timedOut = false;
183+
let connection: PoolClient | undefined;
184+
const onConnectionError = (error: Error) =>
185+
this.#logger.error("Outflow archive connection lost during a ping", { error });
186+
let timer: ReturnType<typeof setTimeout> | undefined;
187+
const deadline = new Promise<never>((_, reject) => {
188+
timer = setTimeout(() => {
189+
timedOut = true;
190+
reject(new Error(`Outflow archive ping timed out after ${this.#writeTimeoutMs}ms`));
191+
}, this.#writeTimeoutMs);
192+
});
193+
const work = (async () => {
194+
const client = await this.#pool.connect();
195+
// A connection handed over after the timeout is destroyed unused.
196+
if (timedOut) return client.release(new Error("Outflow archive ping timed out"));
197+
connection = client;
198+
client.on("error", onConnectionError);
199+
// A read-only session (a standby, or `default_transaction_read_only`) answers reads but
200+
// rejects every upsert.
201+
const { rows } = await client.query("SHOW transaction_read_only");
202+
if (rows[0]?.transaction_read_only !== "off") {
203+
throw new Error("Outflow archive session is read-only");
204+
}
205+
})();
206+
// A late rejection of an abandoned ping is ignored.
207+
work.catch(() => {});
208+
try {
209+
await Promise.race([work, deadline]);
210+
connection?.removeListener("error", onConnectionError);
211+
connection?.release();
212+
} catch (error) {
213+
connection?.removeListener("error", onConnectionError);
214+
connection?.release(error instanceof Error ? error : new Error(String(error)));
215+
throw error;
216+
} finally {
217+
clearTimeout(timer);
218+
}
219+
}
220+
175221
async close(): Promise<void> {
176222
this.#closed = true;
177223
await this.#pool.end();

0 commit comments

Comments
 (0)