|
1 | 1 | import { assert, it } from "@effect/vitest"; |
2 | | -import { EventId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import { |
| 3 | + EventId, |
| 4 | + ProjectId, |
| 5 | + ProviderInstanceId, |
| 6 | + ThreadId, |
| 7 | + type ModelSelection, |
| 8 | +} from "@t3tools/contracts"; |
3 | 9 | import * as Cause from "effect/Cause"; |
4 | 10 | import * as Clock from "effect/Clock"; |
5 | 11 | import * as DateTime from "effect/DateTime"; |
6 | 12 | import * as Deferred from "effect/Deferred"; |
7 | 13 | import * as Effect from "effect/Effect"; |
8 | 14 | import * as Exit from "effect/Exit"; |
9 | | -import * as Result from "effect/Result"; |
10 | 15 | import * as Fiber from "effect/Fiber"; |
11 | 16 | import * as FileSystem from "effect/FileSystem"; |
12 | 17 | import * as Layer from "effect/Layer"; |
| 18 | +import * as Option from "effect/Option"; |
13 | 19 | import * as Path from "effect/Path"; |
14 | 20 | import * as SqlClient from "effect/sql/SqlClient"; |
| 21 | +import type { SqlError } from "effect/sql/SqlError"; |
15 | 22 | import * as Statement from "effect/sql/Statement"; |
16 | 23 | import * as Tracer from "effect/Tracer"; |
17 | 24 | import * as TestClock from "effect/testing/TestClock"; |
@@ -39,6 +46,19 @@ const FIXED_EPOCH = "2026-01-01T00:00:00.000Z"; |
39 | 46 | /** How long a blocked read is given before it is called blocked. */ |
40 | 47 | const BLOCKED_TIMEOUT = "3 seconds"; |
41 | 48 |
|
| 49 | +/** |
| 50 | + * Print a measurement to the test output. |
| 51 | + * |
| 52 | + * `Effect.log` on its own is swallowed by the test runner, and the effect |
| 53 | + * language service rejects `console.log` inside Effect code. The measurement is |
| 54 | + * the point of the test, so it has to be readable in the output. |
| 55 | + */ |
| 56 | +const printMeasurement = (message: string) => |
| 57 | + Effect.sync(() => { |
| 58 | + // @effect-diagnostics-next-line globalConsoleInEffect:off - the measurement is the test's output, and the test runner captures the console. |
| 59 | + console.log(message); |
| 60 | + }); |
| 61 | + |
42 | 62 | /** |
43 | 63 | * The scope the writer's row is written under. |
44 | 64 | * |
@@ -64,7 +84,11 @@ const HELD_WRITE_SCOPE = "lockwait-held-write"; |
64 | 84 | */ |
65 | 85 | const withWriteLockHeld = <A, E, R>( |
66 | 86 | effect: Effect.Effect<A, E, R>, |
67 | | -): Effect.Effect<{ readonly value: A; readonly heldForMs: number }, E, R | SqlClient.SqlClient> => |
| 87 | +): Effect.Effect< |
| 88 | + { readonly value: A; readonly heldForMs: number; readonly uncommittedRows: number }, |
| 89 | + E | SqlError, |
| 90 | + R | SqlClient.SqlClient |
| 91 | +> => |
68 | 92 | Effect.gen(function* () { |
69 | 93 | const sql = yield* SqlClient.SqlClient; |
70 | 94 | const lockHeld = yield* Deferred.make<void>(); |
@@ -73,19 +97,26 @@ const withWriteLockHeld = <A, E, R>( |
73 | 97 | .withTransaction( |
74 | 98 | Effect.gen(function* () { |
75 | 99 | // A real write, with the table's own columns. A row that does not |
76 | | - // insert is not write load. |
| 100 | + // insert is not write load. The id is fixed because the database is |
| 101 | + // created for this test alone, so it is unique in it. |
77 | 102 | yield* sql`INSERT INTO orchestration_v2_projection_checkpoints ( |
78 | 103 | checkpoint_id, thread_id, scope_id, run_id, node_id, ordinal_within_scope, |
79 | 104 | status, captured_at, payload_json |
80 | 105 | ) VALUES ( |
81 | | - ${crypto.randomUUID()}, ${"lockwait-thread"}, ${HELD_WRITE_SCOPE}, ${"run"}, ${"node"}, |
| 106 | + ${"lockwait-checkpoint"}, ${"lockwait-thread"}, ${HELD_WRITE_SCOPE}, ${"run"}, ${"node"}, |
82 | 107 | 1, 'captured', ${FIXED_EPOCH}, '{}' |
83 | 108 | )`; |
84 | 109 | yield* Deferred.succeed(lockHeld, undefined); |
85 | 110 | yield* Deferred.await(release); |
86 | 111 | }), |
87 | 112 | ) |
88 | | - .pipe(Effect.forkChild({ startImmediately: true })); |
| 113 | + .pipe( |
| 114 | + // If the write fails, the lock is never held and the read would be |
| 115 | + // measuring nothing. Release the reader instead of leaving it to time |
| 116 | + // out, and let the writer's own exit report the failure below. |
| 117 | + Effect.tapError(() => Deferred.succeed(lockHeld, undefined)), |
| 118 | + Effect.forkChild({ startImmediately: true }), |
| 119 | + ); |
89 | 120 | yield* Deferred.await(lockHeld); |
90 | 121 | // The write lock is held from here until the effect finishes. |
91 | 122 | const started = yield* Clock.currentTimeMillis; |
@@ -100,6 +131,8 @@ const withWriteLockHeld = <A, E, R>( |
100 | 131 | WHERE scope_id = ${HELD_WRITE_SCOPE} |
101 | 132 | `.pipe(NodeSqliteClient.readOnly); |
102 | 133 | yield* Deferred.succeed(release, undefined); |
| 134 | + // Joining the writer propagates its failure, so a row that did not insert |
| 135 | + // fails the test rather than passing as write load. |
103 | 136 | yield* Fiber.join(writer); |
104 | 137 | return { value, heldForMs, uncommittedRows: uncommittedRows[0]?.count ?? -1 }; |
105 | 138 | }); |
@@ -157,7 +190,7 @@ const measureReadUnderWriteLock = Effect.fn("measureReadUnderWriteLock")(functio |
157 | 190 | span.end = (endTime, exit) => { |
158 | 191 | end(endTime, exit); |
159 | 192 | const connection = span.attributes.get("db.connection"); |
160 | | - if (connection !== undefined) { |
| 193 | + if (typeof connection === "string") { |
161 | 194 | acquired.push({ |
162 | 195 | connection, |
163 | 196 | lockWaitMs: Number(span.attributes.get("db.lock_wait_ms") ?? 0), |
@@ -204,7 +237,7 @@ const measureReadOnWriterConnection = Effect.fn("measureReadOnWriterConnection") |
204 | 237 | span.end = (endTime, exit) => { |
205 | 238 | end(endTime, exit); |
206 | 239 | const connection = span.attributes.get("db.connection"); |
207 | | - if (connection !== undefined) { |
| 240 | + if (typeof connection === "string") { |
208 | 241 | acquired.push({ |
209 | 242 | connection, |
210 | 243 | lockWaitMs: Number(span.attributes.get("db.lock_wait_ms") ?? 0), |
@@ -248,8 +281,7 @@ it.effect("the projection read completes while a writer holds the write lock", ( |
248 | 281 | const storeLayer = Layer.mergeAll( |
249 | 282 | ProjectionStore.layer.pipe(Layer.provideMerge(database)), |
250 | 283 | database, |
251 | | - NodeServices.layer, |
252 | | - ); |
| 284 | + ).pipe(Layer.provideMerge(NodeServices.layer)); |
253 | 285 |
|
254 | 286 | const threadId = ThreadId.make("thread:lockwait:reader"); |
255 | 287 | const { acquired, heldForMs, uncommittedRows } = yield* Effect.gen(function* () { |
@@ -276,7 +308,7 @@ it.effect("the projection read completes while a writer holds the write lock", ( |
276 | 308 | // uncommitted, and so invisible to the reader, when the read finished. |
277 | 309 | assert.isAtLeast(heldForMs, 0); |
278 | 310 | assert.equal(uncommittedRows, 0); |
279 | | - console.log( |
| 311 | + yield* printMeasurement( |
280 | 312 | `MEASURE read-on-reader: connection=${acquired[0]?.connection} lock_wait_ms=${acquired[0]?.lockWaitMs} lock_held_for_ms=${heldForMs} uncommitted_rows_visible=${uncommittedRows}`, |
281 | 313 | ); |
282 | 314 | }).pipe(Effect.provide(NodeServices.layer)), |
@@ -291,8 +323,7 @@ it.effect("the same read on the writer connection is blocked by the write lock", |
291 | 323 | const storeLayer = Layer.mergeAll( |
292 | 324 | ProjectionStore.layer.pipe(Layer.provideMerge(database)), |
293 | 325 | database, |
294 | | - NodeServices.layer, |
295 | | - ); |
| 326 | + ).pipe(Layer.provideMerge(NodeServices.layer)); |
296 | 327 |
|
297 | 328 | const threadId = ThreadId.make("thread:lockwait:writer"); |
298 | 329 | const { acquired, heldForMs, value } = yield* Effect.gen(function* () { |
@@ -309,14 +340,14 @@ it.effect("the same read on the writer connection is blocked by the write lock", |
309 | 340 | assert.equal(exit._tag, "Failure"); |
310 | 341 | // A read that failed for some other reason would not have waited for the |
311 | 342 | // bound, so the failure has to BE the timeout. |
312 | | - const found = Cause.findError(exit.cause); |
313 | | - assert.isTrue(Result.isSuccess(found), "expected a typed failure, got " + String(found)); |
314 | | - const error = found.success; |
| 343 | + const error = Exit.isFailure(exit) |
| 344 | + ? Cause.findErrorOption(exit.cause).pipe(Option.getOrElse(() => undefined)) |
| 345 | + : undefined; |
315 | 346 | assert.isTrue(Cause.isTimeoutError(error), "expected a timeout, got " + String(error)); |
316 | 347 | // It waited for the bound, with the lock held the whole time. |
317 | 348 | assert.isAtLeast(heldForMs, 2_000); |
318 | 349 | assert.deepEqual(acquired, []); |
319 | | - console.log( |
| 350 | + yield* printMeasurement( |
320 | 351 | `MEASURE read-on-writer: outcome=TimeoutError lock_held_for_ms=${heldForMs} connections_acquired=${acquired.length}`, |
321 | 352 | ); |
322 | 353 | }).pipe(Effect.provide(NodeServices.layer)), |
|