Skip to content
TypeScript

fix(server): provider logs of finished threads obey retention limits (#23274)

Victor Vogele2d9d9a7bb96
diff --git a/apps/server/src/provider/EventNdjsonLogger.test.ts b/apps/server/src/provider/EventNdjsonLogger.test.ts
index af94ede2e58..c04150dce8d 100644
--- a/apps/server/src/provider/EventNdjsonLogger.test.ts
+++ b/apps/server/src/provider/EventNdjsonLogger.test.ts
@@ -420,6 +420,45 @@ it.layer(NodeServices.layer)("EventNdjsonLogger", (it) => {
}),
);
+ it.effect("retires a thread log once a retention interval passes without writes", () =>
+ Effect.gen(function* () {
+ const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-provider-log-"));
+ const basePath = NodePath.join(tempDir, "events.log");
+ const finishedPath = ownedLogPath(basePath, "finished");
+ const activePath = ownedLogPath(basePath, "active");
+
+ try {
+ const store = yield* makeEventNdjsonLogStore(basePath, {
+ batchWindowMs: 0,
+ });
+ const logger = store.logger("native");
+
+ yield* logger.write({ id: "finished-first" }, ThreadId.make("finished"));
+ yield* TestClock.adjust("5 minutes");
+ yield* logger.write({ id: "active-first" }, ThreadId.make("active"));
+ assert.equal(NodeFS.existsSync(finishedPath), true);
+ NodeFS.truncateSync(finishedPath, 512 * 1024 * 1024 + 1);
+
+ yield* TestClock.adjust("5 minutes");
+ yield* logger.write({ id: "active-second" }, ThreadId.make("active"));
+ assert.equal(NodeFS.existsSync(finishedPath), false);
+ assert.equal(NodeFS.existsSync(activePath), true);
+
+ yield* logger.write({ id: "finished-again" }, ThreadId.make("finished"));
+ yield* store.close();
+ assert.deepEqual(
+ NodeFS.readFileSync(finishedPath, "utf8")
+ .trim()
+ .split("\n")
+ .map((line) => parseLogLine(line).payload),
+ ['{"id":"finished-again"}'],
+ );
+ } finally {
+ NodeFS.rmSync(tempDir, { recursive: true, force: true });
+ }
+ }),
+ );
+
it.effect("attributes batches that were written before a later chunk fails", () =>
Effect.gen(function* () {
const records: ReadonlyArray<PendingRecord> = [
diff --git a/apps/server/src/provider/EventNdjsonLogger.ts b/apps/server/src/provider/EventNdjsonLogger.ts
index 00956a1bcff..0e8c9043dcf 100644
--- a/apps/server/src/provider/EventNdjsonLogger.ts
+++ b/apps/server/src/provider/EventNdjsonLogger.ts
@@ -114,6 +114,7 @@ interface StoreState {
readonly pending: Array<PendingRecord>;
readonly pendingBytes: number;
readonly sinks: ReadonlyMap<string, AsyncFileSink>;
+ readonly writtenSinceRetention: ReadonlySet<string>;
readonly flushScheduled: boolean;
readonly closed: boolean;
readonly lastRetentionAt: number;
@@ -483,6 +484,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
}
const sinks = new Map(input.state.sinks);
+ const writtenSegments = new Set(input.state.writtenSinceRetention);
const failures: Array<FileOperationFailure> = [];
const attributionByStream = new Map<
EventNdjsonStream,
@@ -508,6 +510,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
sink = created.success;
sinks.set(threadSegment, sink);
}
+ writtenSegments.add(threadSegment);
const written = yield* writeBatchedMessages(
sink,
@@ -534,6 +537,13 @@ const drainPending = Effect.fnUntraced(function* (input: {
const retentionDue =
input.now - input.state.lastRetentionAt >= input.options.retentionCheckIntervalMs;
+ if (retentionDue) {
+ // A sink idle for a whole retention interval reopens from disk on its next
+ // write, so retention may remove its files without leaving a stale size.
+ for (const threadSegment of sinks.keys()) {
+ if (!writtenSegments.has(threadSegment)) sinks.delete(threadSegment);
+ }
+ }
const retention = retentionDue
? yield* Effect.promise(() =>
enforceRetention({
@@ -563,6 +573,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
pending: [],
pendingBytes: 0,
sinks,
+ writtenSinceRetention: retentionDue ? new Set() : writtenSegments,
flushScheduled: input.timerFired ? false : input.state.flushScheduled,
closed: input.close,
lastRetentionAt: retentionDue ? input.now : input.state.lastRetentionAt,
@@ -629,6 +640,7 @@ export const makeEventNdjsonLogStore = Effect.fnUntraced(function* (
pending: [],
pendingBytes: 0,
sinks: new Map(),
+ writtenSinceRetention: new Set(),
flushScheduled: false,
closed: false,
lastRetentionAt: initializedAt,