Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

fix(agent): add SQLite fallback and realtime pruning to agent flush

luoleic138d6076159
diff --git a/apps/collector/src/modules/app/agent-flush-fallback.test.ts b/apps/collector/src/modules/app/agent-flush-fallback.test.ts
new file mode 100644
index 0000000..29575a1
--- /dev/null
+++ b/apps/collector/src/modules/app/agent-flush-fallback.test.ts
@@ -0,0 +1,122 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import type { FastifyInstance } from "fastify";
+import { createApp } from "./app.js";
+import { createTestDatabase } from "../../__tests__/helpers.js";
+import { realtimeStore } from "../realtime/realtime.store.js";
+import type { StatsDatabase } from "../db/db.js";
+
+// Fake ClickHouse writer that is reported enabled+healthy (so the flush path
+// skips SQLite writes under CH_ONLY_MODE) but whose traffic write always
+// fails. This reproduces the exact condition of the agent CH_ONLY data-loss
+// bug: buffer is cleared, SQLite was skipped, and ClickHouse rejects — the
+// snapshot must be persisted to SQLite as a durable fallback.
+// Hoisted so the vi.mock factory (which is itself hoisted above imports) can
+// reference it safely.
+const { failingWriter } = vi.hoisted(() => ({
+ failingWriter: {
+ isEnabled: () => true,
+ isHealthy: () => true,
+ writeTrafficBatch: () => Promise.reject(new Error("ClickHouse unreachable")),
+ writeCountryBatch: () => Promise.resolve(),
+ },
+}));
+
+vi.mock("../clickhouse/clickhouse.writer.js", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("../clickhouse/clickhouse.writer.js")>();
+ return {
+ ...actual,
+ getClickHouseWriter: () => failingWriter,
+ };
+});
+
+describe("Agent flush ClickHouse-failure fallback (C1)", () => {
+ let db: StatsDatabase;
+ let cleanup: () => void;
+ let backendId: number;
+ let app: FastifyInstance;
+ const AGENT_TOKEN = "secret-agent-token";
+ const originalChOnly = process.env.CH_ONLY_MODE;
+
+ beforeEach(async () => {
+ // CH_ONLY_MODE + a "healthy" writer makes flush() skip SQLite writes,
+ // setting sqliteSkipped=true — the precondition for the durable fallback.
+ process.env.CH_ONLY_MODE = "1";
+
+ ({ db, cleanup } = createTestDatabase());
+ backendId = db.createBackend({
+ name: "agent-backend",
+ url: "agent://test",
+ token: AGENT_TOKEN,
+ type: "clash",
+ });
+ db.setActiveBackend(backendId);
+
+ app = await createApp({
+ port: 0,
+ db,
+ realtimeStore,
+ logger: false,
+ autoListen: false,
+ });
+ });
+
+ afterEach(async () => {
+ if (app) {
+ await app.close();
+ }
+ realtimeStore.clearBackend(backendId);
+ cleanup();
+ if (originalChOnly === undefined) delete process.env.CH_ONLY_MODE;
+ else process.env.CH_ONLY_MODE = originalChOnly;
+ });
+
+ it("persists traffic to SQLite when the ClickHouse write fails after SQLite was skipped", async () => {
+ const response = await app.inject({
+ method: "POST",
+ url: "/api/agent/report",
+ headers: { authorization: `Bearer ${AGENT_TOKEN}` },
+ payload: {
+ backendId,
+ agentId: "test-agent",
+ protocolVersion: 1,
+ requestId: "req-fallback-1",
+ updates: [
+ {
+ domain: "example.com",
+ ip: "1.2.3.4",
+ chains: ["ProxyA", "RuleA"],
+ rule: "DOMAIN-SUFFIX",
+ rulePayload: "example.com",
+ upload: 1000,
+ download: 2000,
+ connections: 1,
+ sourceIP: "192.168.1.10",
+ timestampMs: Date.now(),
+ },
+ ],
+ },
+ });
+ expect(response.statusCode).toBe(200);
+ expect(response.json()).toMatchObject({ success: true, accepted: 1 });
+
+ // Sanity: SQLite was skipped on ingest, so nothing is persisted yet.
+ expect(db.getDomainStats(backendId, 10).length).toBe(0);
+
+ // Closing the app runs the onClose hook, which flushes pending agent
+ // buffers. The ClickHouse write fails, so the fallback must write to SQLite.
+ await app.close();
+ app = undefined as unknown as FastifyInstance;
+
+ const domains = db.getDomainStats(backendId, 10);
+ expect(domains.length).toBe(1);
+ expect(domains[0].domain).toBe("example.com");
+ expect(domains[0].totalUpload).toBe(1000);
+ expect(domains[0].totalDownload).toBe(2000);
+
+ // The realtime store must be retained for the UI (not cleared) since the
+ // fallback path re-marks the write as OK, which clears it — verify data is
+ // durably in SQLite regardless.
+ const ips = db.getIPStats(backendId, 10);
+ expect(ips.some((r) => r.ip === "1.2.3.4")).toBe(true);
+ });
+});
diff --git a/apps/collector/src/modules/app/app.ts b/apps/collector/src/modules/app/app.ts
index 3e2a31a..82a29ca 100644
--- a/apps/collector/src/modules/app/app.ts
+++ b/apps/collector/src/modules/app/app.ts
@@ -171,6 +171,31 @@ export async function createApp(options: AppOptions) {
}
}
+ // Durable fallback: SQLite writes were skipped because ClickHouse was
+ // considered healthy, but the ClickHouse write failed. Persist the
+ // snapshot to SQLite so the data is not silently lost (buffer is already
+ // cleared by flush()). Mirrors gateway/surge collectors. See C1.
+ if (
+ stats.hasTrafficUpdates &&
+ stats.trafficOk &&
+ stats.sqliteSkipped &&
+ (!trafficDetailOk || !trafficAggOk)
+ ) {
+ try {
+ db.batchUpdateTrafficStats(backendId, stats.updates, false);
+ trafficDetailOk = true;
+ trafficAggOk = true;
+ console.warn(
+ `[Agent:${backendId}] ClickHouse traffic write failed; persisted ${stats.updates.length} updates to SQLite as fallback`,
+ );
+ } catch (fallbackErr) {
+ console.error(
+ `[Agent:${backendId}] SQLite fallback for traffic also failed; retaining realtime store`,
+ fallbackErr,
+ );
+ }
+ }
+
if (stats.hasTrafficUpdates && stats.trafficOk) {
if (trafficDetailOk && trafficAggOk) {
realtimeStore.clearTraffic(backendId);
@@ -188,8 +213,33 @@ export async function createApp(options: AppOptions) {
if (stats.pendingCountryWrite) {
try {
await stats.pendingCountryWrite;
- } catch {
+ } catch (err) {
countryWriteOk = false;
+ console.warn(
+ `[Agent:${backendId}] ClickHouse country write failed, keeping realtime country store`,
+ err,
+ );
+ }
+ }
+
+ // Durable fallback for country stats: same rationale as traffic above. See C1.
+ if (
+ stats.hasCountryUpdates &&
+ stats.countryOk &&
+ stats.sqliteSkipped &&
+ !countryWriteOk
+ ) {
+ try {
+ db.batchUpdateCountryStats(backendId, stats.countryUpdates);
+ countryWriteOk = true;
+ console.warn(
+ `[Agent:${backendId}] ClickHouse country write failed; persisted to SQLite as fallback`,
+ );
+ } catch (fallbackErr) {
+ console.error(
+ `[Agent:${backendId}] SQLite fallback for country also failed; retaining realtime store`,
+ fallbackErr,
+ );
}
}
@@ -199,6 +249,13 @@ export async function createApp(options: AppOptions) {
} finally {
agentIsFlushing.set(backendId, false);
}
+
+ // Periodic memory bounds check on realtime store. Without this the
+ // per-backend realtime maps grow unbounded while ClickHouse writes keep
+ // failing (the buffer clears each flush but the realtime store is retained
+ // for the UI), OOM-ing long-lived router/agent deployments. Mirrors the
+ // gateway/surge collectors. See C2.
+ realtimeStore.pruneIfNeeded(backendId);
};
const agentFlushIntervalId = setInterval(() => {