Skip to content

Commit c360209

Browse files
fix(server): pace only scheduled history compaction
1 parent 3dac4ab commit c360209

6 files changed

Lines changed: 33 additions & 31 deletions

File tree

‎apps/server/src/orchestration-v2/FoundationPersistence.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ it.effect("discovers on the reader worker and deletes on the writer", () =>
330330
}),
331331
);
332332
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
333-
const result = yield* maintenance.compactEventStore;
333+
const result = yield* maintenance.compactEventStore();
334334
assert.equal(result.deletedEventCount, 148);
335335
assert.equal(result.deletedReceiptCount, 100);
336336
assert.deepEqual(
@@ -494,7 +494,7 @@ it.effect("keeps other database work runnable while discovering compaction candi
494494
}).pipe(Effect.forkScoped),
495495
);
496496

497-
const summary = yield* maintenance.compactEventStore.pipe(
497+
const summary = yield* maintenance.compactEventStore().pipe(
498498
Effect.withTracer(tracer),
499499
Effect.provideService(Scheduler.MaxOpsBeforeYield, Number.POSITIVE_INFINITY),
500500
Effect.tap(() =>
@@ -1124,7 +1124,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
11241124
ON CONFLICT(command_id) DO NOTHING
11251125
`;
11261126

1127-
const summary = yield* maintenance.compactEventStore;
1127+
const summary = yield* maintenance.compactEventStore();
11281128
// Superseded state spans several discovery pages. Both turn-item updates
11291129
// stay, and so do the first message and node events (replay order). The
11301130
// rename stays too: visits are read state and only supersede each other.

‎apps/server/src/orchestration-v2/ProjectionMaintenance.ts‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ThreadId } from "@t3tools/contracts";
22
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3-
import * as Clock from "effect/Clock";
43
import * as Context from "effect/Context";
54
import * as DateTime from "effect/DateTime";
65
import * as Effect from "effect/Effect";
@@ -33,7 +32,7 @@ export class ProjectionMaintenanceError extends Schema.TaggedError<ProjectionMai
3332
export interface ProjectionMaintenanceV2Shape {
3433
readonly verify: Effect.Effect<ProjectionVerificationV2, ProjectionMaintenanceError>;
3534
readonly rebuild: Effect.Effect<ProjectionVerificationV2, ProjectionMaintenanceError>;
36-
readonly compactEventStore: Effect.Effect<
35+
readonly compactEventStore: (betweenPages?: Effect.Effect<void>) => Effect.Effect<
3736
{
3837
readonly deletedEventCount: number;
3938
readonly deletedReceiptCount: number;
@@ -231,7 +230,6 @@ export const layer: Layer.Layer<
231230
// Bound each reader request and writer delete, including when the reader
232231
// worker is unavailable and discovery falls back to the writer.
233232
const COMPACTION_PAGE_SIZE = 50;
234-
const COMPACTION_SLICE_NANOS = 25_000_000n;
235233

236234
/**
237235
* Scan newest first to retain the newest state for each entity. Keyed
@@ -243,17 +241,9 @@ export const layer: Layer.Layer<
243241
* still scan the entire history when superseded events are sparse.
244242
* turn-item.updated stays intact because replay assigns positions on first write.
245243
*/
246-
const compactEventStore = Effect.gen(function* () {
247-
let sliceStarted = yield* Clock.monotonicTimeNanos;
248-
const pause = Effect.gen(function* () {
249-
const elapsed = (yield* Clock.monotonicTimeNanos) - sliceStarted;
250-
if (elapsed < COMPACTION_SLICE_NANOS) return;
251-
// Spend at most 10% of wall time compacting. A slow page can exceed
252-
// the slice budget; repay its entire elapsed time before the next page.
253-
// Keep both cursors and all retention state in this fiber across rests.
254-
yield* Effect.sleep((Number(elapsed) / 1_000_000) * 9);
255-
sliceStarted = yield* Clock.monotonicTimeNanos;
256-
});
244+
const compactEventStore = Effect.fnUntraced(function* (
245+
betweenPages: Effect.Effect<void> = Effect.void,
246+
) {
257247
const bounds = yield* sql<{
258248
readonly event_sequence: number;
259249
readonly receipt_row_id: number;
@@ -311,10 +301,11 @@ export const layer: Layer.Layer<
311301
FROM orchestration_events AS event
312302
WHERE event.sequence <= ${throughSequence}
313303
ORDER BY event.sequence DESC
314-
LIMIT ${COMPACTION_PAGE_SIZE}
304+
LIMIT ${COMPACTION_PAGE_SIZE + 1}
315305
`.pipe(NodeSqliteClient.readOnly);
306+
const page = rows.slice(0, COMPACTION_PAGE_SIZE);
316307
const obsolete: number[] = [];
317-
for (const row of rows) {
308+
for (const row of page) {
318309
if (row.imported_legacy_thread === 1) {
319310
obsolete.push(row.sequence);
320311
} else if (row.application_event_version === 2) {
@@ -362,9 +353,9 @@ export const layer: Layer.Layer<
362353
deletedEventCount += obsolete.length;
363354
yield* Effect.yieldNow;
364355
}
365-
throughSequence = (rows.at(-1)?.sequence ?? 1) - 1;
366-
yield* pause;
367-
if (rows.length < COMPACTION_PAGE_SIZE) break;
356+
throughSequence =
357+
rows.length <= COMPACTION_PAGE_SIZE ? 0 : (page.at(-1)?.sequence ?? 1) - 1;
358+
if (throughSequence > 0 || throughReceiptRowId > 0) yield* betweenPages;
368359
}
369360

370361
while (throughReceiptRowId > 0) {
@@ -402,7 +393,7 @@ export const layer: Layer.Layer<
402393
}
403394
throughReceiptRowId = (page.at(-1)?.row_id ?? 1) - 1;
404395
if (rows.length <= COMPACTION_PAGE_SIZE) break;
405-
yield* pause;
396+
yield* betweenPages;
406397
}
407398

408399
const freelistRows = yield* sql<{ readonly freelist_count: number }>`PRAGMA freelist_count`;
@@ -411,12 +402,12 @@ export const layer: Layer.Layer<
411402
(freelistRows[0]?.freelist_count ?? 0) * (pageSizeRows[0]?.page_size ?? 0);
412403

413404
return { deletedEventCount, deletedReceiptCount, reclaimableBytes };
414-
});
405+
}, mapError("compact event store"));
415406

416407
return ProjectionMaintenanceV2.of({
417408
verify: mapError("verify")(verify),
418409
rebuild: mapError("rebuild")(rebuild),
419-
compactEventStore: mapError("compact event store")(compactEventStore),
410+
compactEventStore,
420411
});
421412
}),
422413
);

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5455,7 +5455,7 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events",
54555455
});
54565456
}
54575457
const latest = yield* orchestrator.getThreadShell(threadId);
5458-
yield* maintenance.compactEventStore;
5458+
yield* maintenance.compactEventStore();
54595459
const linkEvents = yield* sql<{ command_id: string }>`
54605460
SELECT command_id FROM orchestration_events
54615461
WHERE stream_id = ${threadId} AND event_type = 'thread.pull-request-link-updated'
@@ -5497,7 +5497,7 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events",
54975497
visitedAt,
54985498
});
54995499
}
5500-
yield* maintenance.compactEventStore;
5500+
yield* maintenance.compactEventStore();
55015501
assert.deepEqual(
55025502
(yield* threadEvents()).map((row) => row.event_type),
55035503
["thread.metadata-updated", "thread.visited"],
@@ -5508,7 +5508,7 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events",
55085508
threadId,
55095509
title: "Renamed after visit",
55105510
});
5511-
yield* maintenance.compactEventStore;
5511+
yield* maintenance.compactEventStore();
55125512
assert.deepEqual(
55135513
(yield* threadEvents()).map((row) => row.event_type),
55145514
["thread.metadata-updated"],

‎apps/server/src/persistence/DatabaseMaintenance.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ const make = Effect.gen(function* () {
185185
if (!(yield* maintenance.verify).valid) {
186186
return yield* new DatabaseProjectionsInvalidError({ path: databasePath });
187187
}
188-
const summary = yield* maintenance.compactEventStore;
188+
const summary = yield* maintenance.compactEventStore();
189189
if (!(yield* maintenance.verify).valid) {
190190
return yield* new DatabaseProjectionsInvalidError({ path: databasePath });
191191
}

‎apps/server/src/persistence/DatabaseStorage.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DatabaseStorageStatus } from "@t3tools/contracts";
2+
import * as Clock from "effect/Clock";
23
import * as Context from "effect/Context";
34
import * as Effect from "effect/Effect";
45
import * as Equal from "effect/Equal";
@@ -89,7 +90,17 @@ const make = Effect.gen(function* () {
8990
Effect.catch((cause) => Effect.logWarning("Database space reclamation failed", { cause })),
9091
);
9192

92-
const compact = maintenance.compactEventStore.pipe(
93+
const compact = Effect.gen(function* () {
94+
let sliceStarted = yield* Clock.monotonicTimeNanos;
95+
const betweenPages = Effect.gen(function* () {
96+
const elapsed = (yield* Clock.monotonicTimeNanos) - sliceStarted;
97+
if (elapsed < 25_000_000n) return;
98+
// Keep background maintenance at 10% duty, including pages that exceed the budget.
99+
yield* Effect.sleep((Number(elapsed) / 1_000_000) * 9);
100+
sliceStarted = yield* Clock.monotonicTimeNanos;
101+
});
102+
return yield* maintenance.compactEventStore(betweenPages);
103+
}).pipe(
93104
Effect.tap((result) =>
94105
result.deletedEventCount > 0 || result.deletedReceiptCount > 0
95106
? Effect.logInfo("Compacted orchestration history", result)

‎apps/server/src/ws.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ it.effect("thread resume across compacted history keeps message order and final
414414
`;
415415
}
416416
// Only the event between a's first and newest is deleted.
417-
assert.equal((yield* maintenance.compactEventStore).deletedEventCount, 1);
417+
assert.equal((yield* maintenance.compactEventStore()).deletedEventCount, 1);
418418

419419
const items = yield* subscribeOrchestrationV2Thread({
420420
threadId,

0 commit comments

Comments
 (0)