Skip to content

Commit b0493ba

Browse files
committed
fix(server): run windowed snapshots on the SQLite reader
1 parent 89424d9 commit b0493ba

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, vi } from "@effect/vitest";
24
import {
35
EnvironmentId,
@@ -26,7 +28,10 @@ import {
2628
} from "@t3tools/contracts";
2729
import * as DateTime from "effect/DateTime";
2830
import * as Effect from "effect/Effect";
31+
import * as Exit from "effect/Exit";
32+
import * as FileSystem from "effect/FileSystem";
2933
import * as Layer from "effect/Layer";
34+
import * as Path from "effect/Path";
3035
import * as Schema from "effect/Schema";
3136
import * as Tracer from "effect/Tracer";
3237
import * as SqlClient from "effect/sql/SqlClient";
@@ -183,6 +188,61 @@ const addRolledBackRecoveryCandidate = Effect.fn("addRolledBackRecoveryCandidate
183188
return threadId;
184189
});
185190

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

0 commit comments

Comments
 (0)