From 0f7b8083f4a0d7dfcb9fbf4fea76d3264d15467d Mon Sep 17 00:00:00 2001 From: Tanmay-008 Date: Sun, 16 Aug 2026 08:40:19 +0530 Subject: [PATCH] fix: unbounded in-memory growth in fake etcd --- src/cluster/cluster.ts | 10 ++++++++- src/cluster/etcd.test.ts | 33 ++++++++++++++++++++++++++++++ src/cluster/etcd.ts | 44 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/cluster/cluster.ts b/src/cluster/cluster.ts index c4bd9c9..62bdae2 100644 --- a/src/cluster/cluster.ts +++ b/src/cluster/cluster.ts @@ -101,6 +101,12 @@ export interface ClusterOptions { * the simulator defaults. */ kubeletConfiguration?: Partial; + /** + * Periodic auto-compaction retention in milliseconds for the simulated etcd. + * History older than this is eligible for compaction. Set to `0` to disable. + * Defaults to 5 minutes (300,000ms). + */ + autoCompactionRetentionMs?: number; } export type { @@ -153,7 +159,9 @@ export class Cluster extends EventEmitter { this, ); this.cancelContext = cancelContext; - this.etcd = new Etcd(this.ctx); + this.etcd = new Etcd(this.ctx, { + autoCompactionRetentionMs: options.autoCompactionRetentionMs ?? 5 * 60 * 1000, + }); this.serviceCIDR = options.serviceCIDR; this.nodePortRange = options.nodePortRange ?? DEFAULT_NODE_PORT_RANGE; this.kubeConfig = new k8s.KubeConfig({ diff --git a/src/cluster/etcd.test.ts b/src/cluster/etcd.test.ts index 5b72514..4c3e345 100644 --- a/src/cluster/etcd.test.ts +++ b/src/cluster/etcd.test.ts @@ -1756,3 +1756,36 @@ etcd.describe("etcd", ({ createEtcd }) => { }); }); }); + +import * as context from "../go/context.js"; +import { withClock } from "../clock-context.js"; +import { Clock } from "../clock.js"; + +describe("Fake etcd auto-compaction", () => { + it("inline compaction trims global history when per-key history overflows", async () => { + const clock = new Clock(); + const ctx = withClock(context.background(), clock); + const etcd = new Etcd(ctx, { retainedRevisions: 2 }); + for (let i = 0; i < 5; i++) { + await etcd.put("key").value(`v${i}`); + } + await expect(etcd.get("key").revision(1).string()).rejects.toThrow(/compacted/); + etcd.close(); + }); + + it("periodic auto-compaction cleans history after retention period", async () => { + const clock = new Clock(); + const ctx = withClock(context.background(), clock); + const etcd = new Etcd(ctx, { autoCompactionRetentionMs: 1000 }); + + await etcd.put("a").value("1"); // rev 1 + await etcd.put("b").value("2"); // rev 2 + + clock.step(1500); + + await etcd.put("c").value("3"); // rev 3, triggers auto-compact check + + await expect(etcd.get("a").revision(1).string()).rejects.toThrow(/compacted/); + etcd.close(); + }); +}); diff --git a/src/cluster/etcd.ts b/src/cluster/etcd.ts index 5355f7a..04fb366 100644 --- a/src/cluster/etcd.ts +++ b/src/cluster/etcd.ts @@ -245,6 +245,7 @@ interface SortKey { interface EtcdOptions { retainedRevisions?: number; + autoCompactionRetentionMs?: number; } export interface WithLockOptions { @@ -399,12 +400,20 @@ class FakeState { private readonly watchers = new Set(); private readonly leases = new Map(); private readonly clock: Clock; + private autoCompactionHandle?: number; + private revisionTimestamps = new Map(); constructor( private readonly ctx: context.Context, - private readonly options: Required, + private readonly options: EtcdOptions & { retainedRevisions: number }, ) { this.clock = getClock(ctx); + if (this.options.autoCompactionRetentionMs !== undefined) { + const intervalMs = this.options.autoCompactionRetentionMs; + this.autoCompactionHandle = this.clock.setInterval(() => { + this.autoCompact(intervalMs); + }, intervalMs); + } } public range(namespace: string, options: RangeOptions): RangeResponse { @@ -590,6 +599,9 @@ class FakeState { public close(): void { this.watchers.clear(); + if (this.autoCompactionHandle !== undefined) { + this.clock.clearInterval(this.autoCompactionHandle); + } for (const lease of this.leases.values()) { this.clock.clearTimeout(lease.expireHandle); this.clock.clearInterval(lease.keepAliveHandle); @@ -701,9 +713,37 @@ class FakeState { private bumpRevision(): number { this.revision += 1; + if (this.options.autoCompactionRetentionMs !== undefined) { + this.revisionTimestamps.set(this.revision, this.clock.nowMs()); + } return this.revision; } + private compactHistory(): void { + for (const [historyRevision] of this.history.entries()) { + if (historyRevision > this.compactedRevision) { + break; + } + this.history.delete(historyRevision); + } + } + + private autoCompact(retentionMs: number): void { + const cutoff = this.clock.nowMs() - retentionMs; + let compactRevision = 0; + for (const [rev, timestamp] of this.revisionTimestamps.entries()) { + if (timestamp > cutoff) {break;} + compactRevision = rev; + } + if (compactRevision > 0) { + this.compact(compactRevision); + for (const [rev] of this.revisionTimestamps.entries()) { + if (rev > compactRevision) {break;} + this.revisionTimestamps.delete(rev); + } + } + } + private collectDeleted(namespace: string, options: DeleteOptions): StoredValue[] { this.assertValidRange(options.key, options.rangeEnd); const deleted: StoredValue[] = []; @@ -841,6 +881,7 @@ class FakeState { this.compactedRevision = Math.max(this.compactedRevision, removed.historyRevision); } } + this.compactHistory(); this.histories.set(key, revisions); return record; } @@ -1120,6 +1161,7 @@ export class Etcd extends Namespace { constructor(ctx: context.Context, options?: EtcdOptions) { const state = new FakeState(ctx, { retainedRevisions: Math.max(1, options?.retainedRevisions ?? 3), + autoCompactionRetentionMs: options?.autoCompactionRetentionMs, }); super(state, ""); this.stateRef = state;