641 lines
25 KiB
TypeScript
641 lines
25 KiB
TypeScript
import { flowReadBucketSnapshot } from "./traffic-flow-read-policy.js"
|
|
import {flowIngestQualitySchema} from "@mmapp/contracts/traffic-flow"
|
|
import { flowLifecycleBoundary, type FlowLifecycleAuthorization } from "./traffic-flow-lifecycle-fence.js"
|
|
import { flowCounter } from "./traffic-flow-observed-values.js"
|
|
import { readFlowSourceLimits } from "./traffic-flow-source-limits.js"
|
|
import { deleteTrafficFlowData, executeTrafficFlowPurge, type FlowPurgeOptions } from "./traffic-flow-purge-execution.js"
|
|
import { executeGuardedApplicationWrite } from "./config-mutation-policy.js"
|
|
import { withTrafficFlowPurgeLock } from "../db/operation-lock.js"
|
|
import { Worker } from "node:worker_threads"
|
|
import { FlowWorkerLifecycle } from "./traffic-flow-worker-lifecycle.js"
|
|
import { requestExistingWorkerDrain } from "./traffic-flow-drain-protocol.js"
|
|
import { withFlowWriterExclusion } from "./traffic-flow-writer-exclusion.js"
|
|
import { db, pool } from "../db/index.js"
|
|
import { dropExpiredPartitions } from "../db/partitions.js"
|
|
import { servers } from "../db/schema.js"
|
|
import type { FlowPurgeDto, FlowStatsDto } from "@mmapp/contracts/traffic-flow"
|
|
import type { ParsedFlowInput } from "./traffic-flow-parse.js"
|
|
import type { CollectorHeartbeat, ExporterMapPayload, MainToWorker, WorkerToMain } from "./traffic-flow-collector-ipc.js"
|
|
import {
|
|
applyRingSnapshot,
|
|
markFlowRingSnapshotUnavailable,
|
|
configureEngine,
|
|
flushPending,
|
|
strictDrainEngine,
|
|
getEngineStats,
|
|
getRingMbps as engineGetRingMbps,
|
|
ingestParsedFlowsForServerForTests as engineIngestForServer,
|
|
lastFlushUsedTransactionForTests as engineLastFlushTx,
|
|
listLiveFlowRows as engineListLive,
|
|
peekPendingFlows,
|
|
queueParsedFlows,
|
|
resetEngineForTests,
|
|
resolveServerId,
|
|
rollFlowRings,
|
|
setExporterResolveCtx,
|
|
type PendingFlowRow,
|
|
} from "./traffic-flow-engine.js"
|
|
import {
|
|
getTrafficFlowSettingsRow,
|
|
listHostPeers,
|
|
invalidateTrafficFlowSettingsCache,
|
|
} from "./traffic-flow-settings.js"
|
|
import { projectFlowTalkers } from "./traffic-flow-talkers-model.js"
|
|
import { resolveIfaceName } from "./traffic-flow-ifaces.js"
|
|
import { canonicalIp } from "./traffic-flow-ip.js"
|
|
import { getServerCatalog } from "./traffic-flow-topology.js"
|
|
|
|
export type { PendingFlowRow }
|
|
|
|
export interface FlowListenerState {
|
|
bound: boolean
|
|
address: string | null
|
|
}
|
|
|
|
export interface FlowWorkerHealth {
|
|
alive: boolean
|
|
bound: boolean
|
|
pendingSize: number
|
|
dropped: number
|
|
packetsReceived: number
|
|
}
|
|
|
|
let worker: Worker | null = null
|
|
const workerLifecycle=new FlowWorkerLifecycle()
|
|
let listenerGeneration=0
|
|
let restartTimer: ReturnType<typeof setTimeout> | null = null
|
|
let restartAttempts = 0
|
|
let lastHeartbeat: CollectorHeartbeat | null = null
|
|
let heartbeatFailure:Error|null=null
|
|
const HEARTBEAT_UNAVAILABLE="NetFlow: недостоверный снимок коллектора; наблюдения недоступны"
|
|
let state: FlowListenerState = { bound: false, address: null }
|
|
let wantListen = false
|
|
let databaseStop: Promise<void> | null = null
|
|
|
|
export function trafficFlowWorkerFileUrl(): URL {
|
|
const ts = new URL(import.meta.url).pathname.endsWith(".ts")
|
|
return new URL(
|
|
ts ? "./traffic-flow-worker-bootstrap.mjs" : "./traffic-flow-collector-worker.js",
|
|
import.meta.url,
|
|
)
|
|
}
|
|
|
|
export async function buildExporterMapPayload(): Promise<ExporterMapPayload> {
|
|
const settings = await getTrafficFlowSettingsRow()
|
|
const rows = await db.select({
|
|
id: servers.id,
|
|
host: servers.host,
|
|
mgmtTunnelIp: servers.mgmtTunnelIp,
|
|
}).from(servers)
|
|
const byTunnelIp: Array<[string, number]> = []
|
|
const hostIps: Array<[string, number]> = []
|
|
for (const row of rows) {
|
|
if (row.mgmtTunnelIp) byTunnelIp.push([row.mgmtTunnelIp, row.id])
|
|
if (/^\d{1,3}(?:\.\d{1,3}){3}$/.test(row.host)) hostIps.push([row.host, row.id])
|
|
}
|
|
return {
|
|
overlayPrefix: settings.prefix,
|
|
byTunnelIp,
|
|
peers: await listHostPeers(),
|
|
hostIps,
|
|
}
|
|
}
|
|
|
|
async function applyExporterCtxFromDb(): Promise<void> {
|
|
const payload = await buildExporterMapPayload()
|
|
setExporterResolveCtx({
|
|
overlayPrefix: payload.overlayPrefix,
|
|
byTunnelIp: new Map(payload.byTunnelIp),
|
|
peers: payload.peers,
|
|
hostIps: new Map(payload.hostIps),
|
|
})
|
|
}
|
|
|
|
function postToWorker(msg: MainToWorker): void {
|
|
worker?.postMessage(msg)
|
|
}
|
|
|
|
function applyWorkerHeartbeat(payload: CollectorHeartbeat): void {
|
|
const boundedString=(value:unknown,max:number)=>value===null||typeof value==="string"&&value.length<=max
|
|
const counter=(value:unknown)=>typeof value==="number"&&Number.isSafeInteger(value)&&value>=0
|
|
if(!payload||typeof payload!=="object"||typeof payload.bound!=="boolean"||typeof payload.workerAlive!=="boolean"
|
|
||!boundedString(payload.address,1024)||!boundedString(payload.lastExporterIp,64)
|
|
||typeof payload.lastError!=="string"||payload.lastError.length>4096
|
|
||![payload.packetsReceived,payload.pendingSize,payload.dropped,payload.rowsStored].every(counter)
|
|
||!(payload.lastDatagramAt===null||typeof payload.lastDatagramAt==="string"&&payload.lastDatagramAt.length===24&&Number.isFinite(Date.parse(payload.lastDatagramAt))&&new Date(payload.lastDatagramAt).toISOString()===payload.lastDatagramAt))throw Error("Invalid NetFlow heartbeat metadata")
|
|
const dataQuality=payload.dataQuality==null?null:flowIngestQualitySchema.parse(payload.dataQuality)
|
|
applyRingSnapshot(payload.rings,dataQuality)
|
|
lastHeartbeat = {...payload,dataQuality,rings:[]}
|
|
heartbeatFailure=null
|
|
state = { bound: payload.bound, address: payload.address }
|
|
restartAttempts = 0
|
|
}
|
|
|
|
function rejectWorkerHeartbeat(error:unknown):void {
|
|
heartbeatFailure??=error instanceof Error?error:new Error("Invalid NetFlow heartbeat")
|
|
markFlowRingSnapshotUnavailable()
|
|
state={bound:false,address:null}
|
|
if(lastHeartbeat)lastHeartbeat={...lastHeartbeat,bound:false,address:null,workerAlive:false,dataQuality:null,rings:[],lastError:HEARTBEAT_UNAVAILABLE}
|
|
}
|
|
|
|
function handleWorkerMessage(msg: WorkerToMain): void {
|
|
if (msg.type === "heartbeat") {
|
|
try { applyWorkerHeartbeat(msg.payload) }
|
|
catch(error){rejectWorkerHeartbeat(error)}
|
|
finally {
|
|
try{postToWorker({ type:"heartbeat-ack",heartbeatId:msg.heartbeatId })}
|
|
catch(error){rejectWorkerHeartbeat(error)}
|
|
}
|
|
return
|
|
}
|
|
if (msg.type === "error") {
|
|
lastHeartbeat = lastHeartbeat
|
|
? { ...lastHeartbeat, lastError: msg.payload.message, workerAlive: true }
|
|
: null
|
|
}
|
|
}
|
|
|
|
async function spawnWorker(authorize?: FlowLifecycleAuthorization): Promise<void> {
|
|
if (databaseStop || !wantListen) return
|
|
const generation=listenerGeneration
|
|
const current = () => !databaseStop && generation === listenerGeneration && wantListen
|
|
const fence = () => flowLifecycleBoundary(authorize, current)
|
|
if (!await fence()) return
|
|
if (authorize && worker) throw Error("Concurrent NetFlow worker prevents a bounded startup")
|
|
stopWorkerProcess(authorize)
|
|
if (!await fence()) return
|
|
await workerLifecycle.drain()
|
|
if (!await fence()) return
|
|
const settings = await getTrafficFlowSettingsRow()
|
|
if (!await fence()) return
|
|
configureEngine({ topN: settings.topN, retentionHours: settings.retentionHours })
|
|
const exporterMap = await buildExporterMapPayload()
|
|
if (!await fence()) return
|
|
setExporterResolveCtx({ overlayPrefix: exporterMap.overlayPrefix, byTunnelIp: new Map(exporterMap.byTunnelIp), peers: exporterMap.peers, hostIps: new Map(exporterMap.hostIps) })
|
|
// An ordinary start may retire a worker installed during an earlier await.
|
|
// A gated start refuses that concurrent pointer to preserve its write budget.
|
|
while(true){
|
|
if (!await fence()) return
|
|
// Check the pointer after the asynchronous fence, then install without an
|
|
// intervening await so concurrent starts cannot orphan another SQL writer.
|
|
if (!worker) break
|
|
if (authorize) throw Error("Concurrent NetFlow worker prevents a bounded startup")
|
|
stopWorkerProcess(authorize)
|
|
if (!await fence()) return
|
|
await workerLifecycle.drain()
|
|
if (!await fence()) return
|
|
}
|
|
let startupPending = Boolean(authorize)
|
|
// Let Node inherit supported parent options. Explicit execArgv rejects V8
|
|
// heap flags that are valid on the parent CLI; the bootstrap owns TS loading.
|
|
const w = new Worker(trafficFlowWorkerFileUrl())
|
|
w.on("message", (msg: WorkerToMain) => {if(worker===w)handleWorkerMessage(msg)})
|
|
w.on("error", (err) => {
|
|
if(worker!==w)return
|
|
state = { bound: false, address: null }
|
|
lastHeartbeat = lastHeartbeat
|
|
? { ...lastHeartbeat, workerAlive: false, lastError: err.message, bound: false }
|
|
: {
|
|
bound: false,
|
|
address: null,
|
|
packetsReceived: 0,
|
|
lastExporterIp: null,
|
|
lastError: err.message,
|
|
lastDatagramAt: null,
|
|
pendingSize: 0,
|
|
dropped: 0,
|
|
dataQuality:null,
|
|
rowsStored: 0,
|
|
workerAlive: false,
|
|
rings: [],
|
|
}
|
|
})
|
|
w.on("exit", (code) => {
|
|
if (worker !== w) return
|
|
worker = null
|
|
state = { bound: false, address: null }
|
|
if (!wantListen || startupPending) return
|
|
const delay = Math.min(30_000, 1000 * 2 ** restartAttempts)
|
|
restartAttempts += 1
|
|
restartTimer = setTimeout(() => {
|
|
if (wantListen) void spawnWorker()
|
|
}, delay)
|
|
void code
|
|
})
|
|
worker = w
|
|
const host = process.env.FLOW_LISTEN_HOST?.trim() || settings.collectorIp || "127.0.0.1"
|
|
const payload = {
|
|
listenHost: host,
|
|
listenPort: settings.flowListenPort,
|
|
topN: settings.topN,
|
|
retentionHours: settings.retentionHours,
|
|
exporterMap,
|
|
}
|
|
if (authorize) {
|
|
await requestExistingWorkerDrain(w, async () => { if (!await fence()) throw Error("NetFlow startup superseded") }, [], 30000, "authorized-start", payload)
|
|
if (!await fence() || worker !== w) throw Error("NetFlow startup superseded")
|
|
startupPending = false
|
|
} else postToWorker({ type: "start", payload })
|
|
}
|
|
|
|
function stopWorkerProcess(authorize?: FlowLifecycleAuthorization): void {
|
|
if (restartTimer) {
|
|
clearTimeout(restartTimer)
|
|
restartTimer = null
|
|
}
|
|
if (worker) {
|
|
const current=worker
|
|
// Register the acknowledged retirement before detaching the active pointer.
|
|
// The sync public stop returns immediately; starts and DB operations drain it.
|
|
void workerLifecycle.retire(current,authorize)
|
|
worker = null
|
|
}
|
|
}
|
|
|
|
export function reattachFlowSqlite(): void {
|
|
/* no-op: engine uses PostgreSQL pool */
|
|
}
|
|
|
|
export function applyHeartbeatForTests(payload: CollectorHeartbeat): void {
|
|
applyWorkerHeartbeat(payload)
|
|
}
|
|
|
|
export function simulateWorkerExitForTests(): number {
|
|
worker = null
|
|
state = { bound: false, address: null }
|
|
lastHeartbeat = lastHeartbeat ? { ...lastHeartbeat, workerAlive: false, bound: false } : null
|
|
if (!wantListen) return restartAttempts
|
|
restartAttempts += 1
|
|
return restartAttempts
|
|
}
|
|
|
|
export function setWantListenForTests(value: boolean): void {
|
|
wantListen = value
|
|
}
|
|
|
|
export function getFlowListenerState(): FlowListenerState {
|
|
return state
|
|
}
|
|
|
|
export function getFlowWorkerHealth(): FlowWorkerHealth {
|
|
const hb = lastHeartbeat
|
|
const mem = getEngineStats()
|
|
return {
|
|
alive: heartbeatFailure===null&&Boolean(worker) && (hb?.workerAlive ?? false),
|
|
bound: state.bound,
|
|
pendingSize: hb?.pendingSize ?? mem.pendingSize,
|
|
dropped: hb?.dropped ?? mem.dropped,
|
|
packetsReceived: hb?.packetsReceived ?? mem.packetsReceived,
|
|
}
|
|
}
|
|
|
|
export async function getFlowRuntimeCounters() {
|
|
const hb = lastHeartbeat
|
|
if (hb) {
|
|
return {
|
|
packetsReceived: hb.packetsReceived,
|
|
lastExporterIp: hb.lastExporterIp ?? null,
|
|
lastError: heartbeatFailure?HEARTBEAT_UNAVAILABLE:hb.lastError || null,
|
|
lastDatagramAt: hb.lastDatagramAt ?? null,
|
|
dropped: hb.dropped ?? 0,
|
|
dataQuality:hb.dataQuality??null,
|
|
}
|
|
}
|
|
const settings = await getTrafficFlowSettingsRow()
|
|
return {
|
|
packetsReceived: settings.packetsReceived,
|
|
lastExporterIp: settings.lastExporterIp ?? null,
|
|
lastError: heartbeatFailure?HEARTBEAT_UNAVAILABLE:settings.lastError || null,
|
|
lastDatagramAt: settings.lastDatagramAt ?? null,
|
|
dropped: 0,
|
|
dataQuality:null,
|
|
}
|
|
}
|
|
|
|
export async function startTrafficFlowListener(authorize?: FlowLifecycleAuthorization) {
|
|
if (databaseStop) throw new Error("NetFlow listener is paused for a database operation")
|
|
// Fence the internal stop and flush separately. The ordinary synchronous stop
|
|
// keeps its existing fire-and-forget behavior for ungated callers.
|
|
const initialGeneration = listenerGeneration
|
|
if (authorize && !await flowLifecycleBoundary(authorize, () => listenerGeneration === initialGeneration && !databaseStop)) return
|
|
if (authorize) {
|
|
listenerGeneration++
|
|
wantListen = false
|
|
stopWorkerProcess(async () => {
|
|
await flowLifecycleBoundary(authorize, () => listenerGeneration === initialGeneration + 1 && !databaseStop)
|
|
})
|
|
state = { bound: false, address: null }
|
|
} else stopTrafficFlowListener()
|
|
const generation = listenerGeneration
|
|
const current = () => generation === listenerGeneration && !databaseStop
|
|
const fence = () => flowLifecycleBoundary(authorize, current)
|
|
const authorizeCurrent = authorize ? async () => {
|
|
if (!await fence()) throw Error("NetFlow listener lifecycle changed during authorization")
|
|
} : undefined
|
|
try {
|
|
if (!await fence()) return
|
|
await workerLifecycle.drain()
|
|
if (!await fence()) return
|
|
if (authorize) {
|
|
await flushPending({ force: true }, authorizeCurrent)
|
|
if (!await fence()) return
|
|
}
|
|
const settings = await getTrafficFlowSettingsRow()
|
|
if (!await fence()) return
|
|
if (!settings.enabled) {
|
|
wantListen = false
|
|
state = { bound: false, address: null }
|
|
return
|
|
}
|
|
wantListen = true
|
|
await spawnWorker(authorizeCurrent)
|
|
} catch (error) {
|
|
// A rejected/unknown transition never schedules an automatic replay.
|
|
if (generation === listenerGeneration) wantListen = false
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export function stopTrafficFlowListener() {
|
|
listenerGeneration++
|
|
wantListen = false
|
|
stopWorkerProcess()
|
|
void flushPending({ force: true }).catch(() => { /* ignore */ })
|
|
state = { bound: false, address: null }
|
|
}
|
|
|
|
export async function stopTrafficFlowListenerForDatabaseOperation(): Promise<void> {
|
|
if (databaseStop) return databaseStop
|
|
wantListen = false
|
|
listenerGeneration++
|
|
if (restartTimer) clearTimeout(restartTimer)
|
|
restartTimer = null
|
|
stopWorkerProcess()
|
|
databaseStop = (async () => {
|
|
await workerLifecycle.drain()
|
|
await flushPending({ force: true, prune: false })
|
|
state = { bound: false, address: null }
|
|
lastHeartbeat = null;heartbeatFailure=null
|
|
})()
|
|
// Keep a failed stop latched: restart must not kill a worker whose writes are unconfirmed.
|
|
await databaseStop
|
|
}
|
|
|
|
export async function restartTrafficFlowListenerAfterDatabaseOperation(): Promise<void> {
|
|
if (databaseStop) await databaseStop
|
|
const settings = await getTrafficFlowSettingsRow()
|
|
databaseStop = null
|
|
if (!settings.enabled) {
|
|
wantListen = false
|
|
state = { bound: false, address: null }
|
|
return
|
|
}
|
|
await startTrafficFlowListener()
|
|
}
|
|
|
|
export async function refreshFlowExporterMap(): Promise<void> {
|
|
await applyExporterCtxFromDb()
|
|
postToWorker({ type: "updateExporterMap", payload: await buildExporterMapPayload() })
|
|
}
|
|
|
|
export function getRingMbps(serverId: number, iface = "__all__") {
|
|
return engineGetRingMbps(serverId, iface)
|
|
}
|
|
|
|
function mergeInto(map: Map<string, PendingFlowRow>, row: PendingFlowRow): void {
|
|
const key = `${row.serverId}|${row.bucketAt}|${row.src}|${row.dst}|${row.proto}|${row.srcPort}|${row.dstPort}|${row.inIface}`
|
|
const prev = map.get(key)
|
|
if (prev) {
|
|
prev.bytes += row.bytes
|
|
prev.packets += row.packets
|
|
if (row.outIface && !prev.outIface) prev.outIface = row.outIface
|
|
if (row.nextHop && !prev.nextHop) prev.nextHop = row.nextHop
|
|
if (row.natSrc && !prev.natSrc) prev.natSrc = row.natSrc
|
|
if (row.natDst && !prev.natDst) prev.natDst = row.natDst
|
|
if (row.natSrcPort && !prev.natSrcPort) prev.natSrcPort = row.natSrcPort
|
|
if (row.natDstPort && !prev.natDstPort) prev.natDstPort = row.natDstPort
|
|
if (row.flowStartMs && (!prev.flowStartMs || row.flowStartMs < prev.flowStartMs)) prev.flowStartMs = row.flowStartMs
|
|
if (row.flowEndMs > (prev.flowEndMs ?? 0)) prev.flowEndMs = row.flowEndMs
|
|
return
|
|
}
|
|
map.set(key, { ...row })
|
|
}
|
|
|
|
/** Top-N разговоров на (server_id, bucket_at), как prune persist — не глобальный ORDER BY bytes. */
|
|
export function capFlowRowsPerServerBucket<T extends { serverId: number; bucketAt: string; bytes: number }>(
|
|
rows: T[],
|
|
keep: number,
|
|
): T[] {
|
|
const cap = Math.max(20, keep)
|
|
const groups = new Map<string, T[]>()
|
|
for (const row of rows) {
|
|
const k = `${row.serverId}\0${row.bucketAt}`
|
|
const list = groups.get(k)
|
|
if (list) list.push(row)
|
|
else groups.set(k, [row])
|
|
}
|
|
const out: T[] = []
|
|
for (const list of groups.values()) {
|
|
list.sort((a, b) => b.bytes - a.bytes)
|
|
out.push(...list.slice(0, cap))
|
|
}
|
|
return out
|
|
}
|
|
|
|
function isoBucketAt(v: unknown): string {
|
|
if (v instanceof Date) return v.toISOString()
|
|
return String(v ?? "")
|
|
}
|
|
|
|
export async function listLiveFlowRows(sinceIso: string): Promise<PendingFlowRow[]> {
|
|
if (worker && lastHeartbeat?.workerAlive) {
|
|
return await listStoredFlowRows(sinceIso)
|
|
}
|
|
return engineListLive(sinceIso)
|
|
}
|
|
|
|
interface StoredBucketRow {
|
|
server_id: number
|
|
bucket_at: string | Date
|
|
src: string
|
|
dst: string
|
|
proto: number
|
|
src_port: number
|
|
dst_port: number
|
|
bytes: number
|
|
packets: number
|
|
in_iface: string
|
|
out_iface: string | null
|
|
next_hop: string | null
|
|
flow_start_ms: number | null
|
|
flow_end_ms: number | null
|
|
nat_src: string | null
|
|
nat_dst: string | null
|
|
nat_src_port: number | null
|
|
nat_dst_port: number | null
|
|
}
|
|
|
|
export async function listStoredFlowRows(sinceIso: string, serverId?: number, options?: { maxRows?: number; untilIso?: string }): Promise<PendingFlowRow[]> {
|
|
if (options?.maxRows !== undefined && (!Number.isSafeInteger(options.maxRows) || options.maxRows < 1 || options.maxRows > 100000)) throw new Error("Некорректный предел flow rows")
|
|
if (options?.untilIso && !Number.isFinite(Date.parse(options.untilIso))) throw new Error("Некорректная граница flow window")
|
|
const settings = options?.maxRows === undefined ? await getTrafficFlowSettingsRow() : await readFlowSourceLimits()
|
|
const keep = Math.max(20, settings.topN)
|
|
const parameters: Array<string | number> = serverId === undefined ? [sinceIso, keep] : [sinceIso, keep, serverId]
|
|
const untilParameter = options?.untilIso ? parameters.push(options.untilIso) : undefined
|
|
if (options?.maxRows !== undefined) parameters.push(options.maxRows + 1)
|
|
const stored = await flowReadBucketSnapshot<StoredBucketRow>(`
|
|
SELECT
|
|
server_id, bucket_at, COALESCE(host(src), '') AS src, COALESCE(host(dst), '') AS dst, proto,
|
|
src_port, dst_port, bytes, packets, in_iface, out_iface,
|
|
COALESCE(host(next_hop), '') AS next_hop, flow_start_ms, flow_end_ms,
|
|
COALESCE(host(nat_src), '') AS nat_src, COALESCE(host(nat_dst), '') AS nat_dst, nat_src_port, nat_dst_port
|
|
FROM (
|
|
SELECT fb.*,
|
|
ROW_NUMBER() OVER (
|
|
PARTITION BY server_id, bucket_at ORDER BY bytes DESC
|
|
) AS rn
|
|
FROM flow_buckets fb
|
|
WHERE bucket_at >= $1 ${serverId === undefined ? "" : "AND server_id=$3"} ${untilParameter ? `AND bucket_at <= $${untilParameter}` : ""}
|
|
) ranked
|
|
WHERE rn <= $2
|
|
${options?.maxRows !== undefined ? `LIMIT $${parameters.length}` : ""}
|
|
`, parameters)
|
|
if (options?.maxRows !== undefined && stored.length > options.maxRows) throw new Error("Превышен предел flow rows; сократите окно или число серверов")
|
|
const merged = new Map<string, PendingFlowRow>()
|
|
for (const r of stored) {
|
|
mergeInto(merged, {
|
|
serverId: Number(r.server_id),
|
|
bucketAt: isoBucketAt(r.bucket_at),
|
|
src: canonicalIp(r.src),
|
|
dst: canonicalIp(r.dst),
|
|
proto: Number(r.proto) || 0,
|
|
srcPort: Number(r.src_port) || 0,
|
|
dstPort: Number(r.dst_port) || 0,
|
|
bytes: flowCounter(r.bytes),
|
|
packets: flowCounter(r.packets),
|
|
inIface: r.in_iface ?? "",
|
|
outIface: r.out_iface ?? "",
|
|
nextHop: canonicalIp(r.next_hop ?? ""),
|
|
flowStartMs: Number(r.flow_start_ms) || 0,
|
|
flowEndMs: Number(r.flow_end_ms) || 0,
|
|
natSrc: canonicalIp(r.nat_src ?? ""),
|
|
natDst: canonicalIp(r.nat_dst ?? ""),
|
|
natSrcPort: Number(r.nat_src_port) || 0,
|
|
natDstPort: Number(r.nat_dst_port) || 0,
|
|
})
|
|
}
|
|
if (!worker) {
|
|
for (const p of peekPendingFlows({serverId,sinceIso,untilIso:options?.untilIso})) {
|
|
if (p.bucketAt < sinceIso || options?.untilIso && p.bucketAt > options.untilIso || serverId !== undefined && p.serverId !== serverId) continue
|
|
flowCounter(p.bytes); flowCounter(p.packets)
|
|
mergeInto(merged, p)
|
|
}
|
|
}
|
|
const result = capFlowRowsPerServerBucket([...merged.values()], keep)
|
|
if (options?.maxRows !== undefined && result.length > options.maxRows) throw new Error("Превышен предел flow rows с pending; сократите окно или число серверов")
|
|
return result
|
|
}
|
|
|
|
export async function listFlowRowsForWindow(minutes: number, serverId?: number, options?: { maxRows?: number; untilIso?: string }): Promise<PendingFlowRow[]> {
|
|
const sinceIso = new Date(Date.now() - minutes * 60_000).toISOString()
|
|
// History remains available while the collector starts or is stopped; pending rows are merged below.
|
|
return await listStoredFlowRows(sinceIso, serverId, options)
|
|
}
|
|
|
|
export async function listFlowTalkers(minutes = 5): Promise<FlowStatsDto> {
|
|
const settings = await getTrafficFlowSettingsRow()
|
|
const runtime = await getFlowRuntimeCounters()
|
|
const rows = await listFlowRowsForWindow(minutes, undefined, { untilIso: new Date().toISOString() })
|
|
const catalog = await getServerCatalog()
|
|
const nameById = new Map([...catalog.byId].map(([id, server]) => [id, server.name]))
|
|
const projection = projectFlowTalkers(rows, minutes, settings.topN, nameById, resolveIfaceName)
|
|
return {
|
|
exportersOnline: projection.observedExporters,
|
|
bytesPerMin: projection.bytesPerMin,
|
|
uniqueSrc: projection.uniqueSrc,
|
|
uniqueDst: projection.uniqueDst,
|
|
topProto: projection.topProto,
|
|
talkers: projection.talkers,
|
|
dataQuality:runtime.dataQuality,
|
|
lastExporterIp: runtime.lastExporterIp,
|
|
lastError: runtime.lastError,
|
|
packetsReceived: runtime.packetsReceived,
|
|
lastDatagramAt: runtime.lastDatagramAt,
|
|
listenerBound: state.bound,
|
|
listenerAddress: state.address,
|
|
}
|
|
}
|
|
|
|
export async function ingestParsedFlowsForTests(exporterIp: string, flows: ParsedFlowInput[]) {
|
|
await applyExporterCtxFromDb()
|
|
const serverId = resolveServerId(exporterIp)
|
|
if (serverId == null) return
|
|
queueParsedFlows(serverId, flows)
|
|
rollFlowRings()
|
|
await flushPending({ force: true })
|
|
}
|
|
|
|
export function ingestParsedFlowsForServerForTests(serverId: number, flows: ParsedFlowInput[]) {
|
|
engineIngestForServer(serverId, flows)
|
|
}
|
|
|
|
export function resetFlowRingsForTests() {
|
|
resetEngineForTests()
|
|
lastHeartbeat = null;heartbeatFailure=null
|
|
wantListen = false
|
|
restartAttempts = 0
|
|
}
|
|
|
|
export function lastFlushUsedTransactionForTests(): boolean {
|
|
return engineLastFlushTx()
|
|
}
|
|
|
|
export async function flushPendingForTests(): Promise<void> {
|
|
await flushPending({ force: true })
|
|
}
|
|
|
|
/** Existing isolates only: no listener start, stop or restart is hidden here. */
|
|
export async function drainExistingTrafficFlowWriters(authorize:()=>Promise<unknown>,serverIds:readonly number[]) {
|
|
await authorize()
|
|
if(databaseStop)throw Error("NetFlow database lifecycle operation is active")
|
|
const current=worker,generation=listenerGeneration
|
|
const validate=async()=>{
|
|
await authorize()
|
|
if(databaseStop||worker!==current||listenerGeneration!==generation)throw Error("Existing collector lifecycle changed during strict drain")
|
|
}
|
|
const scope=[...serverIds]
|
|
const main=await strictDrainEngine(validate,scope)
|
|
await validate()
|
|
const existingWorker=await requestExistingWorkerDrain(current,validate,scope)
|
|
await validate()
|
|
return {main,existingWorker}
|
|
}
|
|
|
|
const PURGE_LOCK_KEY = 8_723_103
|
|
|
|
/** Удаляет данные одним transaction; ключи и пиры не меняются. */
|
|
export async function purgeTrafficFlowStore(options: FlowPurgeOptions = {}): Promise<FlowPurgeDto> {
|
|
return executeGuardedApplicationWrite({kind:"netflow-purge",id:"global"},"all", () =>
|
|
withTrafficFlowPurgeLock(() => pool.connect(), PURGE_LOCK_KEY, () =>
|
|
// Existing rebuild holds 8723104. Share its exclusion without changing its
|
|
// public behavior; a running rebuild refuses purge before worker shutdown.
|
|
withTrafficFlowPurgeLock(() => pool.connect(), 8_723_104, () => executeTrafficFlowPurge({
|
|
stop: async () => {
|
|
await options.beforeDrain?.()
|
|
await stopTrafficFlowListenerForDatabaseOperation()
|
|
resetEngineForTests()
|
|
lastHeartbeat = null;heartbeatFailure=null
|
|
state = { bound: false, address: null }
|
|
},
|
|
deleteData: () => deleteTrafficFlowData(() => pool.connect(), options.beforeDelete),
|
|
withDataFence: work => withFlowWriterExclusion(work),
|
|
invalidate: invalidateTrafficFlowSettingsCache,
|
|
maintain: async () => { await options.beforeMaintenance?.(); await dropExpiredPartitions(pool,undefined,options.beforePartitionDrop) },
|
|
restart: async () => { await options.beforeRestart?.(); await restartTrafficFlowListenerAfterDatabaseOperation() },
|
|
}))))
|
|
}
|
|
|
|
export { peekPendingFlows }
|
|
export { setPendingCapForTests } from "./traffic-flow-engine.js"
|
|
export { maybeRefreshIfaces, setRefreshIfacesForTests } from "./traffic-flow-ifaces.js"
|