Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/cluster/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export interface ClusterOptions {
* the simulator defaults.
*/
kubeletConfiguration?: Partial<KubeletConfiguration>;
/**
* 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 {
Expand Down Expand Up @@ -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({
Expand Down
33 changes: 33 additions & 0 deletions src/cluster/etcd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
44 changes: 43 additions & 1 deletion src/cluster/etcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ interface SortKey {

interface EtcdOptions {
retainedRevisions?: number;
autoCompactionRetentionMs?: number;
}

export interface WithLockOptions {
Expand Down Expand Up @@ -399,12 +400,20 @@ class FakeState {
private readonly watchers = new Set<WatcherImpl>();
private readonly leases = new Map<string, LeaseRecord>();
private readonly clock: Clock;
private autoCompactionHandle?: number;
private revisionTimestamps = new Map<number, number>();

constructor(
private readonly ctx: context.Context,
private readonly options: Required<EtcdOptions>,
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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -841,6 +881,7 @@ class FakeState {
this.compactedRevision = Math.max(this.compactedRevision, removed.historyRevision);
}
}
this.compactHistory();
this.histories.set(key, revisions);
return record;
}
Expand Down Expand Up @@ -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;
Expand Down