Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Poll for the schema lock so concurrent migrations do not deadlock (#2401)

Anurag Goel38081205e0ae
diff --git a/docs/upgrading-runtime.md b/docs/upgrading-runtime.md
index 4f1052f5c..d6aa3233d 100644
--- a/docs/upgrading-runtime.md
+++ b/docs/upgrading-runtime.md
@@ -52,7 +52,7 @@ after an interrupted startup. It walks every session at roughly 30 sessions per
second, so a few thousand sessions take a couple of minutes; above that, run
`node src/migrate-main.ts` with the release's configuration before starting the
servers so readiness checks do not time out. Instances that start meanwhile wait for the copy on
-the migration lock, so a database or role `statement_timeout` must allow that wait.
+the migration lock.
Rolling back the runtime restores code and configuration, not database contents.
Older binaries do not display cron history written only to the new journal; those
diff --git a/src/persistence/pg-pool.ts b/src/persistence/pg-pool.ts
index c199ce0d1..8a7f68603 100644
--- a/src/persistence/pg-pool.ts
+++ b/src/persistence/pg-pool.ts
@@ -1,7 +1,7 @@
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import type { Pool, PoolClient } from "pg";
-import { createKeyedQueue } from "../util/async.ts";
+import { createKeyedQueue, sleep } from "../util/async.ts";
import { errMessage, swallowAs } from "../util/errors.ts";
export type { Pool, PoolClient };
@@ -214,11 +214,23 @@ export function definePgMigration(
};
}
+const SCHEMA_LOCK_POLL_MS = 50;
+
+async function lockSchema(client: PoolClient, name: string): Promise<void> {
+ for (;;) {
+ const result = await client.query<{ locked: boolean }>("SELECT pg_try_advisory_lock(hashtext($1)) AS locked", [
+ name,
+ ]);
+ if (result.rows[0]?.locked) return;
+ await sleep(SCHEMA_LOCK_POLL_MS);
+ }
+}
+
export async function applyPgMigrations(pool: Pool, migrations: readonly PgMigration[]): Promise<void> {
if (!migrations.length) return;
const client = await pool.connect();
try {
- await client.query("SELECT pg_advisory_lock(hashtext('qm:schema-migrations'))");
+ await lockSchema(client, "qm:schema-migrations");
await client.query(
`CREATE TABLE IF NOT EXISTS ${PG_MIGRATIONS_TABLE}(
id TEXT PRIMARY KEY,
@@ -377,7 +389,7 @@ async function applyPgMaintenance(pool: Pool, maintenance: readonly PgMigration[
if (!maintenance.length) return;
const client = await pool.connect();
try {
- await client.query("SELECT pg_advisory_lock(hashtext('qm:schema-maintenance'))");
+ await lockSchema(client, "qm:schema-maintenance");
for (const operation of maintenance) {
await client.query("BEGIN");
try {
diff --git a/test/postgres-migration-startup.test.ts b/test/postgres-migration-startup.test.ts
index 9b6757f39..ee679cd46 100644
--- a/test/postgres-migration-startup.test.ts
+++ b/test/postgres-migration-startup.test.ts
@@ -49,7 +49,7 @@ test("slow lazy migrations queue with dynamic and bulk migrations before pool ac
});
});
-test("migration entrypoint exits after cold and repeat startup and rejects checksum drift", { skip }, async () => {
+test("concurrent cold and repeat migration entrypoints exit and reject checksum drift", { skip }, async () => {
await withDatabase(async (url, admin) => {
const run = () =>
exec(process.execPath, ["src/migrate-main.ts"], {
@@ -65,8 +65,8 @@ test("migration entrypoint exits after cold and repeat startup and rejects check
CONNECTOR_SECRET_KEY: "test-connector-secret-0123456789abcdef",
},
});
- for (let attempt = 0; attempt < 2; attempt++) {
- const result = await run();
+ const cold = await Promise.all([run(), run()]);
+ for (const result of [...cold, await run()]) {
assert.match(result.stdout, /\[qm:migrate\] database migrations applied/);
}
const applied = await admin.query(`SELECT id FROM ${PG_MIGRATIONS_TABLE} ORDER BY id`);