import assert from "node:assert/strict" import { randomUUID } from "node:crypto" import { basename, dirname, resolve } from "node:path" import type { PoolClient, QueryConfig } from "pg" import type { PartitionWriteOwner } from "./partition-write-transaction.js" /** Explicit owned PostgreSQL acceptance. Import is inert; no production * migration, cleanup, journal deletion, native I/O or global pool replacement. */ export async function acceptPartitionWriteTransactionSql() { assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true") const url = new URL(process.env.DATABASE_URL ?? "") assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") assert(["postgres:", "postgresql:"].includes(url.protocol)) assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) const { pool } = await import("../db/index.js") const { trafficSamples } = await import("../db/schema.js") const { withPartitionWrite, PartitionWriteError, isPartitionWriteOutcomeUnknown, PARTITION_WRITER_ADVISORY_KEY: partitionKey } = await import("./partition-write-transaction.js") const { commitFlowWrite, flowWriteQuery, FLOW_WRITE_LOCK_KEY: flowKey } = await import("./traffic-flow-write-transaction.js") const { sql: drizzleSql } = await import("drizzle-orm") const { ensurePartitionFor } = await import("../db/partitions.js") assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") const serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('owned-partition-writer','127.0.0.1',9,false) RETURNING id")).rows[0].id) const sampledAt = new Date("2101-01-04T12:00:00.000Z") const query = (client: Pick, text: string, values: unknown[] = []) => client.query({ text, values, query_timeout: 8000 } as QueryConfig & { query_timeout: number }) const pidOf = async (client: Pick) => Number((await query(client, "SELECT pg_backend_pid() AS pid")).rows[0].pid) const waitFor = async (label: string, inspect: () => Promise) => { const deadline = Date.now() + 10000 while (Date.now() < deadline) { const value = await inspect(); if (value !== undefined) return value await new Promise(resolve => setTimeout(resolve, 10)) } assert.fail("Timed out observing " + label) } const waitLock = (pid: number, key: number, mode: "ShareLock" | "ExclusiveLock") => waitFor("actual waiting advisory lock", async () => { const rows = (await query(pool, "SELECT mode FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND classid=0 AND objid=$2 AND objsubid=1 AND mode=$3 AND NOT granted", [pid, key, mode])).rows return rows.length === 1 ? true : undefined }) const noRelationLock = async (pid: number) => assert.equal((await query(pool, "SELECT count(*)::int AS n FROM pg_locks l JOIN pg_class c ON c.oid=l.relation WHERE l.pid=$1 AND l.locktype='relation' AND (c.relname='traffic_samples' OR c.relname LIKE 'traffic_samples_w_%')", [pid])).rows[0].n, 0) const noFlowLock = async (pid: number) => assert.equal((await query(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND classid=0 AND objid=$2", [pid, flowKey])).rows[0].n, 0) const rowCount = async (name: string) => Number((await query(pool, "SELECT count(*)::int AS n FROM traffic_samples WHERE server_id=$1 AND interface_name=$2", [serverId, name])).rows[0].n) const write = (owner: PartitionWriteOwner, name: string) => owner.db.insert(trafficSamples).values({ serverId, interfaceName: name, sampledAt: sampledAt.toISOString(), rxBytes: 1 }) let cases = 0, waitingProofs = 0 const maintenance = await pool.connect() const maintenancePid = await pidOf(maintenance) await query(maintenance, "SET statement_timeout='12000ms'") const holder = () => { let pid = 0, lose: "BEGIN" | "COMMIT" | undefined const released: boolean[] = [] const trace: string[] = [] const connect = async () => { const client = await pool.connect(); pid = await pidOf(client) client.on("error", () => {}) return new Proxy(client, { get(target, property) { if (property === "release") return (destroy?: boolean) => { released.push(!!destroy); target.release(destroy) } if (property === "query") return async (...args: unknown[]) => { const raw = args[0], text = typeof raw === "string" ? raw : String((raw as { text: string }).text) trace.push(text) const result = await Reflect.apply(target.query, target, args) if (text === lose) { lose = undefined; throw Error("Synthetic lost ACK after actual SQL") } return result } const value = Reflect.get(target, property, target) return typeof value === "function" ? value.bind(target) : value } }) } return { connect, trace, released, pid: () => pid, lose: (boundary: "BEGIN" | "COMMIT") => { lose = boundary } } } try { await query(maintenance, "SELECT pg_advisory_lock($1::bigint)", [partitionKey]) const f = holder(), name = randomUUID(); let entered = false, captured!: PartitionWriteOwner const pending = withPartitionWrite(async owner => { entered = true; captured = owner assert.equal(Number((await owner.client.query("SELECT pg_backend_pid() AS pid")).rows[0].pid), f.pid()) const ormPid = await owner.db.execute<{ pid: number }>(drizzleSql`SELECT pg_backend_pid() AS pid`) assert.equal(Number(ormPid.rows[0].pid), f.pid()) await write(owner, name) }, { connect: f.connect, deadlineAt: Date.now() + 20000 }) void pending.catch(() => {}) try { const pid = await waitFor("ORM writer backend", async () => f.pid() || undefined) await waitLock(pid, partitionKey, "ShareLock"); waitingProofs++ assert.equal(entered, false); await noRelationLock(pid) // Harmless owned future partition DDL really succeeds while the writer // waits before its first target relation statement. No DROP/TRUNCATE. // This explicit maintenance backend already owns the session exclusive // admission. Do not present a bare PoolClient as an ordinary writer. const from = new Date(sampledAt); from.setUTCHours(0, 0, 0, 0); from.setUTCDate(from.getUTCDate() - (from.getUTCDay() || 7) + 1) const to = new Date(from); to.setUTCDate(to.getUTCDate() + 7) const leaf = "traffic_samples_w_" + from.toISOString().slice(0, 10).replaceAll("-", "") await query(maintenance, `CREATE TABLE IF NOT EXISTS public."${leaf}" PARTITION OF public.traffic_samples FOR VALUES FROM ('${from.toISOString()}') TO ('${to.toISOString()}')`) assert(/^[a-z0-9_]+$/.test(leaf)) await query(maintenance, `ALTER TABLE public."${leaf}" SET (fillfactor=90)`) assert.equal((await query(pool, "SELECT count(*)::int AS n FROM pg_locks WHERE pid=$1 AND locktype='advisory' AND objid=$2 AND mode='ExclusiveLock' AND granted", [maintenancePid, partitionKey])).rows[0].n, 1) } finally { await query(maintenance, "SELECT pg_advisory_unlock($1::bigint)", [partitionKey]); await pending } assert.equal(await rowCount(name), 1); assert.deepEqual(f.released, [false]); cases++ const beforeClosed = f.trace.length await assert.rejects(captured.client.query("SELECT 1"), PartitionWriteError) await assert.rejects(captured.db.execute(drizzleSql`SELECT 1`)) assert.equal(f.trace.length, beforeClosed); cases++ // Exercise the actual DDL helper through its live owner capability, then // its Pool entrypoint. An arbitrary checked-out client cannot claim this // capability. Both positive calls only touch this owned future leaf. const ddl = holder() await withPartitionWrite(async owner => { await ensurePartitionFor(owner.client, "traffic_samples", "week", sampledAt) }, { connect: ddl.connect }) assert.equal(ddl.trace.filter(text => text === "BEGIN").length, 1) assert.equal(ddl.trace.filter(text => text === "COMMIT").length, 1) assert.equal(ddl.trace.filter(text => text.includes("pg_advisory_xact_lock_shared")).length, 1) assert(ddl.trace.some(text => text.startsWith("CREATE TABLE"))) await ensurePartitionFor(pool, "traffic_samples", "week", sampledAt) await assert.rejects(ensurePartitionFor(maintenance, "traffic_samples", "week", sampledAt), /authenticated/) cases++ for (const exclusive of [false, true]) { // Maintenance first: flow admission waits on partition key BEFORE its // shared/exclusive flow key. Maintenance can still acquire the flow key. await query(maintenance, "SELECT pg_advisory_lock($1::bigint)", [partitionKey]) const flow = holder(), batch = randomUUID(); let entered = false const pending = commitFlowWrite({ id: batch, kind: "ingest", sourceProof: "a".repeat(64), exclusive }, async () => { entered = true; await flowWriteQuery("SELECT 1"); return { synthetic: true } }, async () => {}, flow.connect) void pending.catch(() => {}) try { const pid = await waitFor("flow backend", async () => flow.pid() || undefined) await waitLock(pid, partitionKey, "ShareLock"); waitingProofs++; await noFlowLock(pid); await noRelationLock(pid); assert.equal(entered, false) assert.equal((await query(maintenance, "SELECT pg_try_advisory_lock($1::bigint) AS acquired", [flowKey])).rows[0].acquired, true) await query(maintenance, "SELECT pg_advisory_unlock($1::bigint)", [flowKey]) } finally { await query(maintenance, "SELECT pg_advisory_unlock($1::bigint)", [partitionKey]); await pending } assert.equal(entered, true) assert(flow.trace.findIndex(text => text.includes("pg_advisory_xact_lock_shared($1::bigint)")) < flow.trace.findIndex(text => text.includes(exclusive ? "pg_try_advisory_xact_lock($1)" : "pg_advisory_xact_lock_shared($1)"))) assert.equal((await query(pool, "SELECT count(*)::int AS n FROM flow_write_receipts WHERE batch_id=$1", [batch])).rows[0].n, 1); cases++ // Flow first: a real flow transaction owns both keys; maintenance waits // on partition exclusion until that exact transaction commits. const active = holder(); let release!: () => void, enteredActive!: () => void const gate = new Promise(resolve => { release = resolve }), began = new Promise(resolve => { enteredActive = resolve }) const running = commitFlowWrite({ id: randomUUID(), kind: "ingest", sourceProof: "b".repeat(64), exclusive }, async () => { enteredActive(); await Promise.race([gate, new Promise((_resolve, reject) => { const timer = setTimeout(() => reject(Error("Owned flow gate timeout")), 10000); timer.unref() })]); return { synthetic: true } }, async () => {}, active.connect) void running.catch(() => {}) await Promise.race([began, new Promise((_resolve, reject) => { const timer = setTimeout(() => reject(Error("Flow callback barrier timeout")), 10000); timer.unref() })]) const acquire = query(maintenance, "SELECT pg_advisory_lock($1::bigint)", [partitionKey]); void acquire.catch(() => {}) try { await waitLock(maintenancePid, partitionKey, "ExclusiveLock"); waitingProofs++; release(); await running; await acquire } finally { release(); await running.catch(() => {}); await acquire.catch(() => {}); await query(maintenance, "SELECT pg_advisory_unlock($1::bigint)", [partitionKey]) } cases++ } // FK cascade acceptance touches exclusively temporary synthetic parents // and children created inside this already-admitted writer transaction. await withPartitionWrite(async owner => { await owner.client.query("CREATE TEMP TABLE partition_writer_fk_parent(id int PRIMARY KEY) ON COMMIT DROP") await owner.client.query("CREATE TEMP TABLE partition_writer_fk_child(id int PRIMARY KEY,parent_id int REFERENCES partition_writer_fk_parent(id) ON DELETE CASCADE) ON COMMIT DROP") await owner.client.query("INSERT INTO partition_writer_fk_parent VALUES(1)") await owner.client.query("INSERT INTO partition_writer_fk_child VALUES(1,1)") await owner.client.query("DELETE FROM partition_writer_fk_parent WHERE id=1") assert.equal((await owner.client.query("SELECT count(*)::int AS n FROM partition_writer_fk_child")).rows[0].n, 0) }); cases++ for (const boundary of ["BEGIN", "COMMIT"] as const) { const f = holder(), name = randomUUID(); f.lose(boundary) await assert.rejects(withPartitionWrite(async owner => { await write(owner, name) }, { connect: f.connect }), isPartitionWriteOutcomeUnknown) assert.equal(f.trace.filter(text => text === boundary).length, 1); assert(!f.trace.includes("ROLLBACK")); assert.deepEqual(f.released, [true]) assert.equal(await rowCount(name), boundary === "COMMIT" ? 1 : 0); cases++ } for (const stop of ["abort", "deadline"] as const) { const f = holder(), name = randomUUID(), controller = new AbortController(); let entered!: () => void const began = new Promise(resolve => { entered = resolve }) const pending = withPartitionWrite(async owner => { await write(owner, name); entered(); await new Promise(() => {}) }, { connect: f.connect, signal: controller.signal, deadlineAt: Date.now() + 1500 }) void pending.catch(() => {}) await Promise.race([began, pending.then(() => assert.fail("Unexpected writer completion"))]) if (stop === "abort") controller.abort() await assert.rejects(pending, (error: unknown) => error instanceof PartitionWriteError && !error.outcomeUnknown) assert.deepEqual(f.released, [true]); assert(!f.trace.includes("COMMIT")) await waitFor("destroyed transaction backend", async () => (await query(pool, "SELECT count(*)::int AS n FROM pg_stat_activity WHERE pid=$1", [f.pid()])).rows[0].n === 0 ? true : undefined) assert.equal(await rowCount(name), 0); cases++ } return { cases, observedAdvisoryWaits: waitingProofs, actualPostgresAndDrizzle: true, preRelationAdmission: true, flowBothOrdersAndExclusive: true, syntheticTemporaryFkCascade: true, nativeIo: false, productionMigrationApplied: false } } finally { // Unlock this owned maintenance backend only; no data cleanup is needed. await query(maintenance, "SELECT pg_advisory_unlock_all()").catch(() => {}); maintenance.release(true) await query(pool, "UPDATE servers SET enabled=false WHERE id=$1", [serverId]) } }