Files
MikrotikManager/backend/src/services/traffic-flow-write-transaction.ts
T

94 lines
6.6 KiB
TypeScript

import { AsyncLocalStorage } from "node:async_hooks"
import type { Pool, PoolClient, QueryResultRow } from "pg"
import { assertRuntimeDatabaseActivationCurrent, pool } from "../db/index.js"
import { bindSql } from "../db/sql-bind.js"
import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js"
import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js"
// Separate from admission 8723104: final-flush drains precede exclusive access.
export const FLOW_WRITE_LOCK_KEY = 8_723_105
export type FlowWriteKind = "ingest" | "facts" | "rebuild"
type Context = { client: PoolClient; active: boolean; partitions: Set<string>;mode:"shared"|"exclusive";authorize:()=>Promise<unknown> }
const context = new AsyncLocalStorage<Context>()
export class FlowCommitUnknown extends Error { readonly code = "FLOW_COMMIT_UNKNOWN" }
export function flowWriteContext(): Context | undefined {
const current = context.getStore()
if (current && !current.active) throw new Error("Flow write transaction context is closed")
return current
}
// Only this module resolves the owned query client; the global pool is never swapped.
export const flowWritePool = new Proxy(pool, { get(target, property, receiver) {
if (property === "query") return (async(...args:unknown[])=>{
const current=flowWriteContext()
await current?.authorize()
if(current&&!current.active)throw Error("Flow write transaction context is closed")
const query=(current?.client??target).query.bind(current?.client??target) as (...args:unknown[])=>Promise<unknown>
return query(...args)
}) as Pool["query"]
return Reflect.get(target,property,receiver)
} })
export async function flowWriteQuery<T extends QueryResultRow = QueryResultRow>(sql:string,params?:unknown[]|Record<string,unknown>) {
return flowWritePool.query<T>(bindSql(sql,params))
}
export async function flowWriteAll<T extends QueryResultRow = QueryResultRow>(sql:string,params?:unknown[]|Record<string,unknown>):Promise<T[]> {
return (await flowWriteQuery<T>(sql,params)).rows
}
export async function withFlowWriteClient<T>(client:PoolClient,work:()=>Promise<T>,mode:"shared"|"exclusive"="shared",authorize:()=>Promise<unknown>=async()=>{}):Promise<T> {
const current:Context={client,active:true,partitions:new Set(),mode,authorize}
try { return await context.run(current,work) } finally { current.active=false }
}
/** Shared xact lease covers every SQL request, including partition DDL and final
* flush. Rebuild/purge take the exclusive session lease on the same key. */
export async function commitFlowWrite<T extends Record<string,unknown>>(input:{id:string;kind:FlowWriteKind;sourceProof:string;exclusive?:boolean},work:()=>Promise<T>,authorize:()=>Promise<unknown>=async()=>{},connect:()=>Promise<PoolClient>=()=>pool.connect()):Promise<T> {
if(flowWriteContext())throw new Error("A flow write batch must own its transaction")
const client=await connect();let commitSent=false,destroy=false
try {
await authorize();await client.query("BEGIN")
await client.query("SET LOCAL lock_timeout='10s'");await client.query("SET LOCAL statement_timeout='120s'")
// Enter partition admission on THIS transaction/backend before taking the
// flow key or issuing any partition DML/DDL. A row trigger is too late.
await client.query("SELECT pg_advisory_xact_lock_shared($1::bigint)",[PARTITION_WRITER_ADVISORY_KEY])
if (databaseRestoreQuarantineEnabled()) await assertDatabaseRestoreQuarantineOpen(client)
if(input.exclusive){if((await client.query("SELECT pg_try_advisory_xact_lock($1) AS acquired",[FLOW_WRITE_LOCK_KEY])).rows[0]?.acquired!==true)throw new Error("Flow writers are busy; rebuild was not started")}
else await client.query("SELECT pg_advisory_xact_lock_shared($1)",[FLOW_WRITE_LOCK_KEY])
await client.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))",["flow-write-batch:"+input.id])
await authorize()
assertRuntimeDatabaseActivationCurrent()
const existing=(await client.query("SELECT kind,source_proof,result FROM flow_write_receipts WHERE batch_id=$1",[input.id])).rows[0]
if(existing) {
if(existing.kind!==input.kind||existing.source_proof!==input.sourceProof)throw new Error("Flow receipt source does not match owned batch")
await client.query("ROLLBACK");await authorize();return existing.result as T
}
const result=await withFlowWriteClient(client,async()=>{await authorize();return work()},input.exclusive?"exclusive":"shared",authorize)
if(Buffer.byteLength(JSON.stringify(result))>60000)throw new Error("Flow write receipt exceeds bound")
await authorize()
await client.query("INSERT INTO flow_write_receipts(batch_id,kind,source_proof,result) VALUES($1,$2,$3,$4::jsonb)",[input.id,input.kind,input.sourceProof,JSON.stringify(result)])
await authorize()
commitSent=true
const committed=await client.query("COMMIT")
if(committed.command!=="COMMIT")throw new FlowCommitUnknown("PostgreSQL did not confirm flow COMMIT")
return result
}catch(error) {
if(commitSent){destroy=true;throw new FlowCommitUnknown("Flow COMMIT was dispatched without a confirmed response; do not replay")}
try{await client.query("ROLLBACK")}catch{destroy=true}
throw error
}finally{client.release(destroy)}
}
/** Acquiring the SAME batch lock waits for an old COMMIT/rollback, even after
* quarantining its connection. Absence before this barrier is never evidence. */
export async function reconcileFlowWrite(input:{id:string;kind:FlowWriteKind;sourceProof:string},authorize:()=>Promise<unknown>=async()=>{},connect:()=>Promise<PoolClient>=()=>pool.connect()):Promise<{committed:boolean;result:Record<string,unknown>|null}> {
const client=await connect();let locked=false,destroy=false
try {
await authorize();await client.query("SET lock_timeout='10s'")
destroy=true;await client.query("SELECT pg_advisory_lock(hashtextextended($1,0))",["flow-write-batch:"+input.id]);locked=true;destroy=false
await authorize()
const row=(await client.query("SELECT kind,source_proof,result FROM flow_write_receipts WHERE batch_id=$1",[input.id])).rows[0]
if(row&&(row.kind!==input.kind||row.source_proof!==input.sourceProof))throw new Error("Flow receipt proof mismatch")
await authorize();return {committed:Boolean(row),result:row?.result??null}
}finally {
if(locked)try{const result=await client.query("SELECT pg_advisory_unlock(hashtextextended($1,0)) AS released",["flow-write-batch:"+input.id]);destroy=result.rows[0]?.released!==true}catch{destroy=true}
try{await client.query("RESET lock_timeout")}catch{destroy=true}
client.release(destroy)
}
}