Skip to content

Commit 135b85c

Browse files
committed
fix(server): run windowed snapshots on the SQLite reader
1 parent 066c8fc commit 135b85c

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as NodeSqlite from "node:sqlite";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
13
import { assert, it } from "@effect/vitest";
24
import {
35
EnvironmentId,
@@ -28,10 +30,14 @@ import {
2830
} from "@t3tools/contracts";
2931
import * as DateTime from "effect/DateTime";
3032
import * as Effect from "effect/Effect";
33+
import * as Exit from "effect/Exit";
34+
import * as FileSystem from "effect/FileSystem";
3135
import * as Layer from "effect/Layer";
36+
import * as Path from "effect/Path";
3237
import * as Schema from "effect/Schema";
3338
import * as Tracer from "effect/Tracer";
3439
import * as SqlClient from "effect/sql/SqlClient";
40+
import * as Statement from "effect/sql/Statement";
3541
import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness";
3642

3743
import * as SqlitePersistence from "../persistence/Sqlite.ts";
@@ -184,6 +190,60 @@ const addRolledBackRecoveryCandidate = Effect.fn("addRolledBackRecoveryCandidate
184190
return threadId;
185191
});
186192

193+
it.live("windowed snapshots return committed data while the writer is held", () =>
194+
Effect.gen(function* () {
195+
const fs = yield* FileSystem.FileSystem;
196+
const path = yield* Path.Path;
197+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-snapshot-reader-" });
198+
const connections: Array<unknown> = [];
199+
const tracer = Tracer.make({
200+
span(options) {
201+
const span = new Tracer.NativeSpan(options);
202+
const end = span.end.bind(span);
203+
span.end = (endTime, exit) => {
204+
end(endTime, exit);
205+
if (span.name === "sql.transaction") {
206+
connections.push(span.attributes.get("db.connection"));
207+
}
208+
};
209+
return span;
210+
},
211+
});
212+
yield* Effect.gen(function* () {
213+
const store = yield* ProjectionStore.ProjectionStoreV2;
214+
const threadId = yield* addRolledBackRecoveryCandidate("snapshot-reader");
215+
const sql = yield* SqlClient.SqlClient;
216+
yield* sql`PRAGMA busy_timeout = 0`;
217+
const writer = yield* Effect.acquireRelease(
218+
Effect.sync(() => new NodeSqlite.DatabaseSync(path.join(directory, "state.sqlite"))),
219+
(connection) => Effect.sync(() => connection.close()),
220+
);
221+
yield* Effect.sync(() => writer.exec("BEGIN IMMEDIATE"));
222+
const snapshot = yield* store
223+
.getThreadSnapshotWindow(threadId, { rowLimit: 76 })
224+
.pipe(
225+
Effect.withTracer(tracer),
226+
Effect.provideService(Statement.SpanPropagationEnabled, true),
227+
Effect.exit,
228+
);
229+
yield* Effect.sync(() => writer.exec("ROLLBACK"));
230+
assert(
231+
Exit.isSuccess(snapshot),
232+
"snapshot must return while another connection holds the writer",
233+
);
234+
assert.equal(snapshot.value.projection.thread.id, threadId);
235+
assert.equal(snapshot.value.projection.thread.title, "Rolled-back recovery candidate");
236+
assert.deepEqual(connections, ["reader"]);
237+
}).pipe(
238+
Effect.provide(
239+
ProjectionStore.layer.pipe(
240+
Layer.provideMerge(SqlitePersistence.layerFromPath(path.join(directory, "state.sqlite"))),
241+
),
242+
),
243+
);
244+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
245+
);
246+
187247
it.effect.each([
188248
["memory", ProjectionStore.layerMemory],
189249
["SQL", layerTest],

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
12
import { applyThreadPullRequestLinkUpdate } from "@t3tools/shared/threadPullRequests";
23
import { projectTurnItemForWire } from "./WireProjection.ts";
34
import * as Stream from "effect/Stream";
@@ -5622,6 +5623,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56225623
}),
56235624
)
56245625
.pipe(
5626+
NodeSqliteClient.readOnly,
56255627
Effect.mapError((cause) =>
56265628
isProjectionStoreThreadNotFoundError(cause) || isProjectionStoreReadError(cause)
56275629
? cause

0 commit comments

Comments
 (0)