Files
MikrotikManager/backend/src/services/server-poll-production.ts
T

284 lines
32 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { carryPollError } from "./server-poll-error-provenance.js"
import { projectServerPollApprovalContext } from "./server-poll-approval-context.js"
import { assertServerPollSettingsEpochCatalog, SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL, SERVER_POLL_SETTINGS_TABLES } from "./server-poll-settings-epoch.js"
import { SERVER_POLL_WALL_BUDGET_MS } from "./server-poll-limits.js"
import { AsyncLocalStorage } from "node:async_hooks"
import type { QueryConfig } from "pg"
import type { ServerPartitionTransaction } from "./server-partition-maintenance-lease.js"
import type { ServerPollBoundaryCoordinator } from "./server-poll-boundary.js"
import { PARTITION_SPECS } from "../db/partitions.js"
import { createServerPollExecution,type ServerPollDependencies,type ServerPollGrant,type ServerPollRequest } from "./server-poll-execution.js"
import { ServerPollProblem,pollProblem,validateServerPollContext,planServerPollMaintenance,type ServerPollContext,type PollRow,type ServerPollAck } from "./server-poll-execution-model.js"
export interface ServerPollSqlConnection {
query(text:string,values:unknown[],options:{signal?:AbortSignal;timeoutMs:number;statementName?:string}):Promise<{rows:Record<string,unknown>[];rowCount?:number|null;command?:string}>
release(unsafe?:boolean):void
on?(event:"error",listener:(error:Error)=>void):unknown
removeListener?(event:"error",listener:(error:Error)=>void):unknown
}
export interface ServerPollProductionIo {
connect(options:{signal?:AbortSignal;deadlineAt:number}):Promise<ServerPollSqlConnection>
/** A maintenance lease may bind every transaction (and nested context read)
* to its one SQL backend. No global pool replacement is permitted. */
transaction?:ServerPartitionTransaction
applicationLock:<T>(work:()=>Promise<T>)=>Promise<T>
serverLocks:<T>(ids:number[],work:()=>Promise<T>)=>Promise<T>
native:ServerPollDependencies["native"]
invalidateCache:()=>Promise<void>
}
export interface ServerPollProductionInput {
/** Fixed private composition switch. Public storage sessions always enable v3. */
approvalProtocol?:"configuration-epoch-v3"
boundary?:ServerPollBoundaryCoordinator
request:ServerPollRequest
authorize:ServerPollDependencies["authorize"]
/** REQUIRED durable integration. No process-memory journal fallback exists. */
journal:Pick<ServerPollDependencies,"intent"|"acknowledge"|"claim"|"complete">
/** REQUIRED: quiesce/drain ALL affected partition writers across replicas and
* keep their exclusion held until work settles. Config locks alone do not
* fence scheduler/NetFlow writers. No default/no-op implementation exists. */
maintenanceLease:<T>(work:()=>Promise<T>,options:{signal?:AbortSignal;deadlineAt:number;serverIds:number[]})=>Promise<T>
io?:ServerPollProductionIo
proof?:ServerPollDependencies["proof"]
now?:()=>number
}
const MAX_BYTES=1048576
const parents=PARTITION_SPECS.map(spec=>spec.parent)
const partitionKeys:Record<string,string>=Object.fromEntries(parents.map(parent=>[parent,["flow_buckets","flow_minute_stats","flow_minute_dims","flow_hour_facts"].includes(parent)?"RANGE (bucket_at)":["flow_daily_dims","flow_daily_facts"].includes(parent)?"RANGE (day)":parent==="server_snapshots"?"RANGE (polled_at)":"RANGE (sampled_at)"]))
const factStorage={fillfactor:"70",autovacuum_vacuum_scale_factor:"0.05",autovacuum_vacuum_cost_limit:"2000"}
const settingsTables={traffic:"traffic_settings",uptime:"uptime_settings",internetPath:"internet_path_settings"}as const
const dates=new Set(["created_at","updated_at","polled_at","last_collected_at"])
const bounded=(value:unknown,limit=MAX_BYTES)=>{try{return Buffer.byteLength(JSON.stringify(value))<=limit}catch{return false}}
function record(value:unknown):value is Record<string,unknown>{return !!value&&typeof value==="object"&&!Array.isArray(value)&&Object.getPrototypeOf(value)===Object.prototype}
function rawRow(value:unknown):PollRow{
if(typeof value!=="string"||Buffer.byteLength(value)>MAX_BYTES)pollProblem("STATE_UNAVAILABLE","Отсутствует bounded SQL row server poll")
let raw:unknown
try{raw=JSON.parse(value,(_key,item:unknown)=>{if(typeof item==="number"&&(!Number.isFinite(item)||Number.isInteger(item)&&!Number.isSafeInteger(item)))throw Error();return item})}catch{pollProblem("STATE_UNAVAILABLE","Непредставимая SQL row server poll")}
if(!record(raw))pollProblem("STATE_UNAVAILABLE","Повреждена SQL row server poll")
const result:PollRow={}
for(const [key,item]of Object.entries(raw)){
const name=key.replace(/_([a-z])/g,(_match,letter:string)=>letter.toUpperCase())
if(Object.hasOwn(result,name))pollProblem("STATE_UNAVAILABLE","Неоднозначное имя SQL поля server poll")
let normalized=item
if(dates.has(key)&&item!==null){if(typeof item!=="string"||!Number.isFinite(Date.parse(item)))pollProblem("STATE_UNAVAILABLE","Не подтверждено время SQL row server poll");normalized=new Date(item).toISOString()}
result[name]=normalized as PollRow[string]
}
return result
}
function requireRows(rows:Record<string,unknown>[],limit:number):void{if(!Array.isArray(rows)||rows.length>limit)pollProblem("STATE_UNAVAILABLE","Превышен bounded SQL каталог server poll")}
function partitionRow(raw:Record<string,unknown>):PollRow{
if(typeof raw.parent!=="string"||!parents.includes(raw.parent)||typeof raw.name!=="string"||typeof raw.namespace!=="string"||raw.namespace!=="public"||typeof raw.bound!=="string"||!Array.isArray(raw.reloptions)||raw.reloptions.some(option=>typeof option!=="string")||typeof raw.catalog_json!=="string")pollProblem("STATE_UNAVAILABLE","Неполный pg partition каталог server poll")
const storage:Record<string,string>={}
for(const option of raw.reloptions as string[]){const equal=option.indexOf("=");if(equal<=0||Object.hasOwn(storage,option.slice(0,equal)))pollProblem("STATE_UNAVAILABLE","Повреждены reloptions partition");storage[option.slice(0,equal)]=option.slice(equal+1)}
const match=raw.bound.match(/^FOR VALUES FROM \('([^']+)'\) TO \('([^']+)'\)$/)
const parsed=(text:string)=>{const value=Date.parse(text);if(!Number.isFinite(value))pollProblem("STATE_UNAVAILABLE","Неподдерживаемые bounds partition");return new Date(value).toISOString()}
return {namespace:raw.namespace,parent:raw.parent,name:raw.name,bound:raw.bound,from:match?parsed(match[1]):null,to:match?parsed(match[2]):null,storage,catalog:rawRow(raw.catalog_json)}
}
const partitionSql=`SELECT p.relname AS parent,c.relname AS name,n.nspname AS namespace,pg_get_expr(c.relpartbound,c.oid,true) AS bound,
COALESCE(c.reloptions,ARRAY[]::text[]) AS reloptions,
CASE WHEN octet_length(to_jsonb(c)::text)<=16384 AND SUM(octet_length(to_jsonb(c)::text)) OVER (ORDER BY c.oid)<=1048576 THEN (to_jsonb(c)-ARRAY['reloptions','relpages','reltuples','relallvisible','relallfrozen','relfrozenxid','relminmxid'])::text END AS catalog_json
FROM pg_inherits i JOIN pg_class c ON c.oid=i.inhrelid JOIN pg_class p ON p.oid=i.inhparent
JOIN pg_namespace pn ON pn.oid=p.relnamespace JOIN pg_namespace n ON n.oid=c.relnamespace
WHERE pn.nspname='public' AND p.relname=ANY($1::text[])`
const jsonSelect=(alias:string)=>`CASE WHEN octet_length(to_jsonb(${alias})::text)<=1048576 THEN to_jsonb(${alias})::text END AS raw_json`
/** Enforce the aggregate transfer budget IN SQL. A per-row CASE alone could
* otherwise transfer 2,001 individually valid one-MiB raw snapshots. The first
* over-budget row remains present as NULL and is rejected, never truncated. */
const boundedRowsSql=(table:string,where:string,order:string,limit:number)=>`WITH poll_rows AS (
SELECT ${jsonSelect("s")},octet_length(to_jsonb(s)::text) AS row_bytes,row_number() OVER (ORDER BY ${order}) AS row_order
FROM ${table} s ${where} ORDER BY ${order} LIMIT ${limit}
) SELECT row_order,CASE WHEN SUM(row_bytes) OVER (ORDER BY row_order)<=1048576 THEN raw_json END AS raw_json FROM poll_rows ORDER BY row_order`
const parentSql="SELECT p.relname AS name,n.nspname AS namespace,p.relkind,p.oid::text AS oid,p.relowner::text AS owner,CASE WHEN octet_length(pg_get_partkeydef(p.oid))<=256 THEN pg_get_partkeydef(p.oid) END AS partition_key FROM pg_class p JOIN pg_namespace n ON n.oid=p.relnamespace WHERE n.nspname='public' AND p.relname=ANY($2::text[]) ORDER BY p.oid LIMIT 13"
const contextSources=["servers","traffic","uptime","internetPath","parents","partitions","snapshots","protections","settingsEpoch"]as const
type ContextSource=typeof contextSources[number]
const tagged=(source:ContextSource,sql:string)=>`SELECT '${source}'::text AS source,q.row_order,q.raw_json FROM (${sql}) q`
/** One snapshot statement after metadata authorization. Each source keeps its
* original overflow sentinel and ordering; the outer budget bounds transfer
* across all sources, including catalog envelopes. The transport envelope may
* escape nested catalog JSON, so its cap is 8 MiB; each original source and the
* decoded complete context retain their original 1 MiB cap. NULL is never filtered. */
const contextBundleSql=(epoch:boolean)=>`/* poll-context:bundle */ WITH bundle AS MATERIALIZED (
${[
tagged("servers",boundedRowsSql("servers","WHERE id=ANY($1::int[])","s.id",1001)),
...Object.entries(settingsTables).map(([key,table])=>tagged(key as ContextSource,boundedRowsSql(table,"WHERE id=1","s.id",2))),
tagged("parents",`SELECT row_number() OVER(ORDER BY p.oid::bigint) AS row_order,to_jsonb(p)::text AS raw_json FROM (${parentSql}) p`),
tagged("partitions",`SELECT row_number() OVER(ORDER BY (p.catalog_json::jsonb->>'oid')::bigint) AS row_order,to_jsonb(p)::text AS raw_json FROM (${partitionSql.replace("$1::text[]","$2::text[]")} ORDER BY c.oid LIMIT 1001) p`),
tagged("snapshots",boundedRowsSql("server_snapshots","WHERE server_id=$3","s.id,s.polled_at",2001)),
tagged("protections",boundedRowsSql("mcp_protections","","s.id",1001)),
...(epoch?[tagged("settingsEpoch",`SELECT row_number() OVER(ORDER BY g.table_name,g.trigger_name) AS row_order,to_jsonb(g)::text AS raw_json FROM (${SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL.replace("$1::text[]","$4::text[]")}) g`)]:[]),
].join("\nUNION ALL\n")}
), metered AS (
SELECT source,row_order,raw_json,SUM(octet_length(raw_json)) OVER(ORDER BY source,row_order) AS transfer_bytes FROM bundle
) SELECT source,row_order::text AS row_order,CASE WHEN transfer_bytes<=8388608 THEN raw_json END AS raw_json FROM metered ORDER BY source,metered.row_order`
function contextBundle(rows:Record<string,unknown>[]):Record<ContextSource,Record<string,unknown>[]> {
requireRows(rows,5028)
const groups=Object.fromEntries(contextSources.map(source=>[source,[]]))as unknown as Record<ContextSource,Record<string,unknown>[]>
const positions=new Set<string>()
for(const row of rows){
if(Object.keys(row).sort().join(",")!=="raw_json,row_order,source"||!contextSources.includes(row.source as ContextSource)
||typeof row.row_order!=="string"||!/^[1-9]\d{0,4}$/.test(row.row_order)||typeof row.raw_json!=="string"||Buffer.byteLength(row.raw_json)>MAX_BYTES)pollProblem("STATE_UNAVAILABLE","Повреждён bounded context bundle poll")
const key=row.source+":"+row.row_order
if(positions.has(key))pollProblem("STATE_UNAVAILABLE","Неоднозначный порядок context bundle poll")
positions.add(key);groups[row.source as ContextSource].push(row)
}
for(const source of contextSources){groups[source].sort((a,b)=>Number(a.row_order)-Number(b.row_order));if(groups[source].some((row,index)=>Number(row.row_order)!==index+1))pollProblem("STATE_UNAVAILABLE","Неполный порядок context bundle poll")}
return groups
}
function catalogEnvelope(value:unknown):Record<string,unknown>{
if(typeof value!=="string"||Buffer.byteLength(value)>MAX_BYTES)pollProblem("STATE_UNAVAILABLE","Отсутствует bounded catalog envelope poll")
let parsed:unknown;try{parsed=JSON.parse(value)}catch{pollProblem("STATE_UNAVAILABLE","Повреждён catalog envelope poll")}
if(!record(parsed))pollProblem("STATE_UNAVAILABLE","Повреждён catalog envelope poll")
return parsed
}
const ident=(value:string)=>{if(!/^[a-z_][a-z\d_]{0,99}$/.test(value))pollProblem("INVALID_INPUT","Недопустимый SQL identifier poll");return '"'+value+'"'}
/** Imports alone do not connect, query, acquire locks, invalidate a cache or
* create clients. Native GET uses the shared last-moment request hook. */
export function createServerPollProductionIo():ServerPollProductionIo{
return {
connect:async options=>{
options.signal?.throwIfAborted();const {pool}=await import("../db/index.js")
let settled=false
const pending=pool.connect()
let timer:ReturnType<typeof setTimeout>|undefined,abort:(()=>void)|undefined
const client=await Promise.race([pending,new Promise<never>((_resolve,reject)=>{timer=setTimeout(()=>reject(Error("SQL acquisition deadline")),Math.max(1,options.deadlineAt-Date.now()));abort=()=>reject(Error("SQL acquisition aborted"));options.signal?.addEventListener("abort",abort,{once:true})})]).then(client=>{settled=true;return client}).finally(async()=>{if(timer)clearTimeout(timer);if(abort)options.signal?.removeEventListener("abort",abort);if(!settled)await pending.then(client=>client.release(true),()=>{})})
let released=false,unsafe=false
const release=(destroy=false)=>{if(released)return;released=true;client.release(destroy||unsafe)}
return {query:async(text,values,queryOptions)=>{
queryOptions.signal?.throwIfAborted();if(released)pollProblem("STATE_UNAVAILABLE","SQL connection poll закрыт")
const cancel=()=>{unsafe=true;release(true)};queryOptions.signal?.addEventListener("abort",cancel,{once:true})
if(queryOptions.signal?.aborted){cancel();pollProblem("ABORTED","SQL server poll отменён до dispatch")}
try{const request:QueryConfig&{query_timeout:number}={text,values,name:queryOptions.statementName,query_timeout:queryOptions.timeoutMs};return await client.query<Record<string,unknown>>(request)}catch(error){unsafe=true;throw error}finally{queryOptions.signal?.removeEventListener("abort",cancel)}
},release,on:(event,listener)=>client.on(event,listener),removeListener:(event,listener)=>client.removeListener(event,listener)}
},
applicationLock:async work=>(await import("./config-mutation-lock.js")).withApplicationMutationLock(work),
serverLocks:async(ids,work)=>(await import("./config-mutation-lock.js")).withConfigMutationLock(ids,work),
native:async(read,server,options)=>{
const {MikrotikClient}=await import("./mikrotik.js"),path={identity:"/system/identity",resource:"/system/resource",interfaces:"/interface",addresses:"/ip/address"}[read]
return MikrotikClient.fromServer(server as unknown as Parameters<typeof MikrotikClient.fromServer>[0]).get(path,Math.max(1,Math.min(15000,options.deadlineAt-Date.now())),options.signal,MAX_BYTES,options.beforeDispatch)
},
invalidateCache:async()=>{(await import("./traffic-flow-topology.js")).invalidateFlowCatalogCache()},
}
}
/** Bound to one private durable operation. The returned dependencies cannot be
* used for an unrelated request or outside their owned live lock/lease. */
export function createServerPollProductionDependencies(input:ServerPollProductionInput):ServerPollDependencies{
if(!input.request||!Number.isSafeInteger(input.request.serverId)||input.request.serverId<=0||!/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i.test(input.request.receiptId)||!input.journal||!["intent","acknowledge","claim","complete"].every(key=>typeof input.journal[key as keyof typeof input.journal]==="function")||typeof input.maintenanceLease!=="function"||typeof input.authorize!=="function")pollProblem("INVALID_INPUT","Нужны bound request, durable journal и global writer lease server poll")
const io=input.io??createServerPollProductionIo(),now=input.now??Date.now
const proof=input.proof??(async(value:unknown)=>{const security=await import("../modules/mcp/security.js");return security.privateSecretFingerprint(security.fingerprint(value),"server-poll-production-v1")})
const held=new AsyncLocalStorage<{active:boolean;ids:number[];authorityProof:string;context?:ServerPollContext}>()
const bound=(request:ServerPollRequest)=>{if(request.serverId!==input.request.serverId||request.receiptId!==input.request.receiptId)pollProblem("STATE_CONFLICT","Request не принадлежит server poll adapter")}
const check=async(ids:number[],options:{signal?:AbortSignal;deadlineAt:number},expected?:string):Promise<ServerPollGrant>=>{
options.signal?.throwIfAborted();if(now()>=options.deadlineAt)pollProblem("ABORTED","Истёк deadline server poll SQL")
const grant=await input.authorize(input.request,[...new Set([input.request.serverId,...ids])],options.signal)
if(!grant.global||!grant.serverIds.includes(input.request.serverId)||ids.some(id=>!grant.serverIds.includes(id))||!/^[a-f0-9]{64}$/.test(grant.authorityProof)||expected&&grant.authorityProof!==expected)pollProblem("PERMISSION_DENIED","Допуск global server poll изменился")
options.signal?.throwIfAborted();if(now()>=options.deadlineAt)pollProblem("ABORTED","Истёк deadline server poll SQL");return grant
}
const transaction=async<T>(readOnly:boolean,fence:()=>Promise<void>,options:{signal?:AbortSignal;deadlineAt:number},work:(query:(sql:string,values?:unknown[])=>Promise<{rows:Record<string,unknown>[];rowCount?:number|null}>)=>Promise<T>):Promise<T>=>{
if(io.transaction)return io.transaction(readOnly,fence,options,work)
await fence();let connection:ServerPollSqlConnection;try{connection=await io.connect(options)}catch(error){throw carryPollError(new ServerPollProblem("STATE_UNAVAILABLE","SQL connection server poll недоступен"),error,"poll.connect")};let unsafe=false,committed=false
const query=async(sql:string,values:unknown[]=[])=>{await fence();const timeoutMs=Math.max(1,Math.min(10000,options.deadlineAt-now()));let result:{rows:Record<string,unknown>[];rowCount?:number|null};try{result=await connection.query(sql,values,{signal:options.signal,timeoutMs})}catch(error){unsafe=true;throw carryPollError(Error("SQL boundary unavailable"),error,"poll.sql")};await fence();return result}
try{await query(readOnly?"BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY":"BEGIN");await query("SET LOCAL TIME ZONE 'UTC'");await query(`SET LOCAL statement_timeout='${Math.max(1,Math.min(10000,options.deadlineAt-now()))}ms'`);const result=await work(query);await query("COMMIT");committed=true;return result}
finally{if(!committed)unsafe=true;connection.release(unsafe)}
}
const metadataIds=async(query:(sql:string,values?:unknown[])=>Promise<{rows:Record<string,unknown>[]}>)=>{
const {rows}=await query("SELECT id FROM servers ORDER BY id LIMIT 1001");requireRows(rows,1000)
const ids=rows.map(row=>Number(row.id));if(ids.some(id=>!Number.isSafeInteger(id)||id<=0)||new Set(ids).size!==ids.length)pollProblem("STATE_UNAVAILABLE","Повреждён metadata scope server poll");return ids
}
const approvalEnabled=input.approvalProtocol==="configuration-epoch-v3",approvalContexts=new WeakSet<ServerPollContext>()
const readContext:ServerPollDependencies["readContext"]=async(request,beforeRead,permitted,options)=>{
bound(request);const initial=await check([request.serverId],options)
const own=input.boundary?.fence(permitted,initial.authorityProof,options)
const fence=own?input.boundary!.combine([beforeRead,own],permitted,options)??(async()=>{await beforeRead();await check([request.serverId],options,initial.authorityProof)}):(async()=>{await beforeRead();await check([request.serverId],options,initial.authorityProof)})
const context=await transaction(true,fence,options,async query=>{
const ids=await metadataIds(query)
if(ids.some(id=>!permitted.includes(id)))pollProblem("PERMISSION_DENIED","Global metadata содержит сервер вне разрешённой области")
await check(ids,options,initial.authorityProof)
const bundle=contextBundle((await query(contextBundleSql(approvalEnabled),approvalEnabled?[ids,parents,request.serverId,[...SERVER_POLL_SETTINGS_TABLES]]:[ids,parents,request.serverId])).rows)
const serverRows=bundle.servers;requireRows(serverRows,1000)
const servers=serverRows.map(row=>rawRow(row.raw_json));if(servers.length!==ids.length||servers.some((row,index)=>row.id!==ids[index]))pollProblem("STATE_CONFLICT","Полный каталог серверов изменился")
if(servers.some(row=>typeof row.host!=="string"||!row.host.trim()||!Number.isSafeInteger(row.port)||Number(row.port)<1||Number(row.port)>65535||typeof row.username!=="string"||typeof row.password!=="string"||typeof row.useSsl!=="boolean"||typeof row.verifySsl!=="boolean"||typeof row.enabled!=="boolean"))pollProblem("STATE_UNAVAILABLE","Неполные endpoint credentials server poll")
const settings={}as ServerPollContext["settings"]
for(const key of Object.keys(settingsTables)){const rows=bundle[key as keyof typeof settingsTables];requireRows(rows,1);settings[key as keyof typeof settings]=rows.length?rawRow(rows[0].raw_json):null}
const parentRows=bundle.parents.map(row=>catalogEnvelope(row.raw_json))
if(parentRows.length!==parents.length||new Set(parentRows.map(row=>row.name)).size!==parents.length||parentRows.some(row=>!parents.includes(String(row.name))||row.namespace!=="public"||row.relkind!=="p"||typeof row.partition_key!=="string"||row.partition_key.replaceAll('"',"").replace(/\s+/g," ").trim()!==partitionKeys[String(row.name)]))pollProblem("STATE_UNAVAILABLE","Неполный или несовместимый каталог partition parents")
const childRows=bundle.partitions.map(row=>catalogEnvelope(row.raw_json));requireRows(childRows,1000)
const snapshotsRaw=bundle.snapshots;requireRows(snapshotsRaw,2000)
const protectionsRaw=bundle.protections;requireRows(protectionsRaw,1000)
const captured:ServerPollContext&{partitionParents:PollRow[];settingsEpochGuard?:PollRow[]}={servers,settings,partitions:childRows.map(partitionRow),snapshots:snapshotsRaw.map(row=>rawRow(row.raw_json)),protections:protectionsRaw.map(row=>rawRow(row.raw_json)),partitionParents:parentRows as PollRow[]}
if(approvalEnabled){const guard=bundle.settingsEpoch.map(row=>catalogEnvelope(row.raw_json));assertServerPollSettingsEpochCatalog(guard);captured.settingsEpochGuard=guard as PollRow[]}
validateServerPollContext(captured,request.serverId)
if(!bounded(captured))pollProblem("STATE_UNAVAILABLE","Полный private context poll превышает 1 МиБ")
for(const effect of planServerPollMaintenance(captured,new Date(now()).toISOString()))if(effect.kind==="partition.ensure"){const old=captured.partitions.find(row=>row.name===effect.name);if(old&&(old.parent!==effect.parent||old.from!==effect.from||old.to!==effect.to))pollProblem("STATE_UNAVAILABLE","Существующая partition имеет другие границы")}
await check(ids,options,initial.authorityProof);if(approvalEnabled)approvalContexts.add(captured);return captured
})
await fence();const lease=held.getStore();if(lease?.active){if(lease.authorityProof!==initial.authorityProof||JSON.stringify(lease.ids)!==JSON.stringify(context.servers.map(row=>Number(row.id))))pollProblem("STATE_CONFLICT","Global lock scope server poll изменился");lease.context=structuredClone(context)}
return context
}
const requireLease=(options:{signal?:AbortSignal;deadlineAt:number})=>{const lease=held.getStore();if(!lease?.active)pollProblem("PERMISSION_DENIED","Нет живой global maintenance lease poll");const live=()=>{if(!lease.active)pollProblem("PERMISSION_DENIED","Global lease poll закрыта")};return {lease,fence:input.boundary?.fence(lease.ids,lease.authorityProof,options,live)??(async()=>{live();await check(lease.ids,options,lease.authorityProof)})}}
const effect:ServerPollDependencies["effect"]=async(effect,options)=>{
if(options.receiptId!==input.request.receiptId)pollProblem("STATE_CONFLICT","Publication poll принадлежит другому receipt")
const {lease,fence}=requireLease(options)
await fence()
if(effect.kind==="flow-cache.invalidate"){if(effect.serverId!==input.request.serverId)pollProblem("STATE_CONFLICT","Cache scope poll изменился");await options.beforeDispatch();await fence();await io.invalidateCache();await fence();return {}}
let dispatched=false
try{return await transaction(false,fence,options,async query=>{
// query owns a fresh authority check immediately before the SQL send.
// beforeDispatch persists intent; no authority decision crosses that I/O.
const dispatch=async(sql:string,values:unknown[]=[])=>{if(dispatched)pollProblem("STATE_CONFLICT","Повторная SQL publication poll запрещена");await options.beforeDispatch();dispatched=true;return query(sql,values)}
let ack:ServerPollAck={}
if(effect.kind==="server.name"){
if(effect.serverId!==input.request.serverId)pollProblem("STATE_CONFLICT","Изменён server name scope")
const result=await dispatch(`UPDATE servers s SET name=$2,updated_at=$3 WHERE id=$1 RETURNING ${jsonSelect("s")}`,[effect.serverId,effect.name,effect.updatedAt])
if(result.rows.length!==1){pollProblem("VERIFICATION_FAILED","Не подтверждено обновление имени poll")}
const row=rawRow(result.rows[0].raw_json);if(row.id!==effect.serverId||row.name!==effect.name||row.updatedAt!==effect.updatedAt)pollProblem("VERIFICATION_FAILED","Не совпадает имя server poll")
}else if(effect.kind==="snapshot.insert"){
if(effect.snapshot.serverId!==input.request.serverId||!lease.context||!bounded({...lease.context,snapshots:[...lease.context.snapshots,effect.snapshot]},MAX_BYTES-16384))pollProblem("STATE_UNAVAILABLE","Snapshot превысит проверяемый private budget poll")
const fields=["serverId","polledAt","status","latencyMs","rosVersion","boardName","uptime","cpuLoad","freeMemory","totalMemory","identityName","rawInterfaces","rawIpAddresses"]
if(Object.keys(effect.snapshot).some(key=>!fields.includes(key)))pollProblem("INVALID_INPUT","Неизвестное поле snapshot poll")
const columns=fields.map(key=>key.replace(/[A-Z]/g,letter=>"_"+letter.toLowerCase())),values=fields.map(key=>["rawInterfaces","rawIpAddresses"].includes(key)?JSON.stringify(effect.snapshot[key]??null):effect.snapshot[key]??null)
const result=await dispatch(`INSERT INTO server_snapshots AS s (${columns.join(",")}) VALUES (${fields.map((key,index)=>"$"+(index+1)+(["rawInterfaces","rawIpAddresses"].includes(key)?"::jsonb":"")).join(",")}) RETURNING ${jsonSelect("s")}`,values)
if(result.rows.length!==1)pollProblem("VERIFICATION_FAILED","Не подтверждён INSERT snapshot poll");ack={row:rawRow(result.rows[0].raw_json)}
}else if(effect.kind==="snapshot.raw.clear"){
if(effect.serverId!==input.request.serverId||effect.ids.length>2000||effect.ids.some(id=>!Number.isSafeInteger(Number(id))||Number(id)<=0))pollProblem("INVALID_INPUT","Некорректный raw cleanup inventory poll")
const result=await dispatch("UPDATE server_snapshots SET raw_interfaces=NULL,raw_ip_addresses=NULL WHERE server_id=$1 AND polled_at<$2 AND id=ANY($3::bigint[]) AND (raw_interfaces IS NOT NULL OR raw_ip_addresses IS NOT NULL) RETURNING id::text AS id,server_id,polled_at::text,raw_interfaces,raw_ip_addresses",[effect.serverId,effect.beforeAt,effect.ids])
if(result.rows.length!==effect.ids.length||result.rows.some(row=>!effect.ids.some(id=>String(id)===row.id)||row.raw_interfaces!==null||row.raw_ip_addresses!==null))pollProblem("VERIFICATION_FAILED","Не подтверждена точная очистка raw snapshots")
}else{
if(!parents.includes(effect.parent)||!new RegExp("^"+effect.parent+"_[dwm]_\\d{6}(?:\\d{2})?$").test(effect.name))pollProblem("INVALID_INPUT","Недопустимая partition publication poll")
const table='"public".'+ident(effect.name)
if(effect.kind==="partition.drop")await dispatch("DROP TABLE IF EXISTS "+table)
else if(effect.kind==="partition.ensure"){
if(![effect.from,effect.to].every(value=>Number.isFinite(Date.parse(value))&&new Date(value).toISOString()===value)||effect.from>=effect.to)pollProblem("INVALID_INPUT","Некорректные partition bounds poll")
await dispatch(`CREATE TABLE IF NOT EXISTS ${table} PARTITION OF "public".${ident(effect.parent)} FOR VALUES FROM ('${effect.from}') TO ('${effect.to}')`)
}else{
if(!["flow_hour_facts","flow_daily_facts"].includes(effect.parent)||JSON.stringify(effect.settings)!==JSON.stringify(factStorage))pollProblem("INVALID_INPUT","Недопустимые fact storage settings poll")
await dispatch(`ALTER TABLE ${table} SET (fillfactor=70,autovacuum_vacuum_scale_factor=0.05,autovacuum_vacuum_cost_limit=2000)`)
}
const rows=(await query(partitionSql+" AND c.relname=$2 ORDER BY c.oid LIMIT 2",[parents,effect.name])).rows;requireRows(rows,1)
if(effect.kind==="partition.drop"){if(rows.length)pollProblem("VERIFICATION_FAILED","DROP partition poll не подтверждён")}
else{if(rows.length!==1)pollProblem("VERIFICATION_FAILED","Partition poll не подтверждена");const row=partitionRow(rows[0]);if(row.parent!==effect.parent||row.name!==effect.name||effect.kind==="partition.ensure"&&(row.from!==effect.from||row.to!==effect.to)||effect.kind==="partition.storage"&&Object.entries(factStorage).some(([key,value])=>(row.storage as Record<string,string>)[key]!==value))pollProblem("VERIFICATION_FAILED","Partition read-back poll не совпадает");ack={row}}
}
// The transaction owner checks authority again before and after COMMIT.
return ack
})}catch(error){if(dispatched)pollProblem("MUTATION_OUTCOME_UNKNOWN","SQL/DDL poll могла примениться; повтор запрещён");throw error}
}
return {
boundary:input.boundary,now,proof,...(approvalEnabled?{approvalProof:async(context:ServerPollContext)=>{if(!approvalContexts.has(context))pollProblem("STATE_UNAVAILABLE","Отсутствует подлинный SQL снимок конфигурации poll");return proof(projectServerPollApprovalContext(context,input.request.serverId))}}:{}),authorize:async(request,ids,signal)=>{bound(request);return check(ids,{signal,deadlineAt:now()+SERVER_POLL_WALL_BUDGET_MS})},readContext,
withGlobalLock:async(work,options)=>{
const initial=await check([input.request.serverId],options)
return io.applicationLock(async()=>{
const fence=async()=>{await check([input.request.serverId],options,initial.authorityProof)}
const ids=await transaction(true,fence,options,metadataIds);await check(ids,options,initial.authorityProof)
return io.serverLocks(ids,()=>input.maintenanceLease(async()=>{
await check(ids,options,initial.authorityProof);const lease={active:true,ids,authorityProof:initial.authorityProof}
try{return await held.run(lease,work)}finally{lease.active=false}
},{...options,serverIds:ids}))
})
},
native:async(read,server,options)=>{const {lease,fence}=requireLease(options);let dispatched=false;await fence();return io.native(read,server,{...options,beforeDispatch:async()=>{if(dispatched)pollProblem("STATE_CONFLICT","Повтор native dispatch poll запрещён");await fence();await options.beforeDispatch();await fence();const current=lease.context?.servers.find(row=>row.id===input.request.serverId);if(server.id!==input.request.serverId||!current||await proof(current)!==await proof(server)||dispatched)pollProblem("STATE_CONFLICT","Native endpoint/dispatch не совпадает с private source poll");dispatched=true}})},
effect,intent:input.journal.intent,acknowledge:input.journal.acknowledge,claim:input.journal.claim,complete:input.journal.complete,
}
}
export function createServerPollProductionExecution(input:ServerPollProductionInput){return createServerPollExecution(createServerPollProductionDependencies(input))}