@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 430 lines 19.5 KB
Raw
1 import { MssqlClient } from "@effect/sql-mssql"
2 import { assert, it } from "@effect/vitest"
3 import { Deferred, Effect, Exit, Fiber, Latch, Layer, Pool, Schedule, Schema, Stream } from "effect"
4 import * as PersistedCacheTest from "effect-test/persistence/PersistedCacheTest"
5 import * as PersistedQueueSqlTest from "effect-test/persistence/PersistedQueueSqlTest"
6 import * as PersistedQueueTest from "effect-test/persistence/PersistedQueueTest"
7 import { PersistedQueue, Persistence } from "effect/persistence"
8 import * as Reactivity from "effect/reactivity/Reactivity"
9 import { SqlClient, SqlError, Statement } from "effect/sql"
10 import { TestClock } from "effect/testing"
11 import * as Tedious from "tedious"
12 import { MssqlContainer } from "./utils.ts"
13
14 PersistedCacheTest.suite(
15 "sql-mssql-multi",
16 Persistence.layerSqlMultiTable.pipe(Layer.provide(MssqlContainer.layerClient))
17 )
18
19 PersistedCacheTest.suite(
20 "sql-mssql-single",
21 Persistence.layerSql.pipe(Layer.provide(MssqlContainer.layerClient))
22 )
23
24 // Allow extra time for MSSQL-backed layer setup and teardown under CI load.
25 // Keep the queue test bodies at their 30-second timeout.
26 PersistedQueueTest.suiteWith(
27 "sql-mssql",
28 PersistedQueue.layerStoreSql().pipe(Layer.provide(MssqlContainer.layerClient)),
29 it,
30 { hookTimeout: "90 seconds" }
31 )
32
33 // Records deadlock graphs in a dedicated session, since system_health can
34 // buffer events for minutes
35 const captureDeadlocks = Effect.gen(function*() {
36 const sql = yield* SqlClient.SqlClient
37 const name = `effect_deadlocks_${crypto.randomUUID().replaceAll("-", "")}`
38 const session = sql(name)
39 const drop = sql`IF EXISTS (SELECT 1 FROM sys.server_event_sessions WHERE name = ${name})
40 DROP EVENT SESSION ${session} ON SERVER`
41 // only runs if the test fails before reading
42 yield* Effect.acquireRelease(
43 sql`CREATE EVENT SESSION ${session} ON SERVER
44 ADD EVENT sqlserver.xml_deadlock_report
45 ADD TARGET package0.ring_buffer
46 WITH (MAX_DISPATCH_LATENCY = 1 SECONDS);
47 ALTER EVENT SESSION ${session} ON SERVER STATE = START`,
48 () => Effect.ignore(drop)
49 )
50 return Effect.gen(function*() {
51 // wait out the session's dispatch latency
52 yield* Effect.sleep("2 seconds")
53 const rows = yield* sql<{ readonly graph: string }>`
54 SELECT CAST(x.e.query('.') AS NVARCHAR(MAX)) AS graph
55 FROM sys.dm_xe_sessions s
56 INNER JOIN sys.dm_xe_session_targets t ON t.event_session_address = s.address
57 CROSS APPLY (SELECT CAST(t.target_data AS XML) AS data) d
58 CROSS APPLY d.data.nodes('RingBufferTarget/event[@name="xml_deadlock_report"]') AS x(e)
59 WHERE s.name = ${name} AND t.target_name = N'ring_buffer'
60 `
61 yield* drop
62 return rows.map(({ graph }) => graph)
63 })
64 })
65
66 // e.g. "UPDATE vs UPDATE on PK X, idx_queue_take S"
67 const summarizeDeadlock = (graph: string) => {
68 const transactions = Array.from(graph.matchAll(/transactionname="([^"]*)"/g), ([, name]) => name)
69 const locks = Array.from(
70 graph.matchAll(/<keylock [^>]*indexname="([^"]*)"[^>]*mode="([^"]*)"/g),
71 ([, index, mode]) => `${index.startsWith("PK__") ? "PK" : index} ${mode}`
72 )
73 return `${transactions.join(" vs ")} on ${Array.from(new Set(locks)).join(", ")}`
74 }
75
76 // Declares statement parameters as variables for plain batches
77 const declareParameter = (value: unknown, i: number): string => {
78 const name = `@${i + 1}`
79 if (typeof value === "string") return `DECLARE ${name} NVARCHAR(MAX) = N'${value.replaceAll("'", "''")}';`
80 if (typeof value === "number") return `DECLARE ${name} ${Number.isInteger(value) ? "BIGINT" : "FLOAT"} = ${value};`
81 if (typeof value === "bigint") return `DECLARE ${name} BIGINT = ${value};`
82 if (typeof value === "boolean") return `DECLARE ${name} BIT = ${value ? 1 : 0};`
83 if (value instanceof Date) return `DECLARE ${name} DATETIME2 = CONVERT(DATETIME2, '${value.toISOString()}', 127);`
84 if (value === null || value === undefined) return `DECLARE ${name} NVARCHAR(MAX) = NULL;`
85 throw new Error(`unsupported parameter ${String(value)}`)
86 }
87
88 // A third-party mssql-dialect client whose sessions run with XACT_ABORT ON.
89 // With rawBatch, statements run as plain batches. Interruption waits for the
90 // request to settle.
91 const makeXactAbortClient = Effect.fnUntraced(function*(
92 database: string,
93 options?: { readonly rawBatch?: boolean; readonly size?: number }
94 ) {
95 const container = yield* MssqlContainer
96 const fail = (cause: unknown) =>
97 Effect.fail(new SqlError.SqlError({ reason: new SqlError.UnknownError({ cause, operation: "execute" }) }))
98 const connect = Effect.acquireRelease(
99 Effect.callback<Tedious.Connection, SqlError.SqlError>((resume) => {
100 const conn = new Tedious.Connection({
101 server: container.getHost(),
102 authentication: {
103 type: "default",
104 options: { userName: container.getUsername(), password: container.getPassword() }
105 },
106 options: {
107 port: container.getPort(),
108 database,
109 encrypt: !("Deno" in globalThis),
110 trustServerCertificate: true,
111 abortTransactionOnError: true,
112 rowCollectionOnRequestCompletion: true,
113 useColumnNames: false
114 }
115 })
116 conn.connect((cause) => resume(cause ? fail(cause) : Effect.succeed(conn)))
117 }),
118 (conn) => Effect.sync(() => conn.close())
119 )
120 const pool = yield* Pool.make({ acquire: connect, size: options?.size ?? 4 })
121
122 const run = (conn: Tedious.Connection, sql: string, params: ReadonlyArray<unknown>, rowsAsArray: boolean) =>
123 Effect.callback<ReadonlyArray<any>, SqlError.SqlError>((resume) => {
124 let settled = false
125 let onCancelled: (() => void) | undefined
126 const rawBatch = options?.rawBatch === true
127 const text = rawBatch ? params.map(declareParameter).join("\n") + "\n" + sql : sql
128 const request = new Tedious.Request(text, (
129 cause,
130 _rowCount,
131 rows: Array<Array<{ readonly metadata: { readonly colName: string }; readonly value: unknown }>>
132 ) => {
133 settled = true
134 if (onCancelled) return onCancelled()
135 resume(
136 cause ? fail(cause) : Effect.succeed(rows.map((row) =>
137 rowsAsArray
138 ? row.map((column) => column.value)
139 : Object.fromEntries(row.map((column) => [column.metadata.colName, column.value]))
140 ))
141 )
142 })
143 if (!rawBatch) {
144 params.forEach((value, i) => {
145 request.addParameter(`${i + 1}`, MssqlClient.defaultParameterTypes[Statement.primitiveKind(value)], value)
146 })
147 }
148 // transaction control runs as a plain batch, so it outlives the statement
149 if (rawBatch || params.length === 0) {
150 conn.execSqlBatch(request)
151 } else {
152 conn.execSql(request)
153 }
154 return Effect.callback<void>((cancelled) => {
155 if (settled) return cancelled(Effect.void)
156 onCancelled = () => cancelled(Effect.void)
157 conn.cancel()
158 })
159 })
160
161 return yield* SqlClient.make({
162 driver: "test-xact-abort",
163 compiler: MssqlClient.makeCompiler(),
164 spanAttributes: [],
165 beginTransaction: "BEGIN TRANSACTION",
166 commit: "COMMIT TRANSACTION",
167 rollback: "ROLLBACK TRANSACTION",
168 savepoint: (name) => `SAVE TRANSACTION ${name}`,
169 rollbackSavepoint: (name) => `ROLLBACK TRANSACTION ${name}`,
170 acquirer: Effect.map(Pool.get(pool), (conn) => ({
171 execute: (sql, params, transformRows) =>
172 Effect.map(run(conn, sql, params, false), (rows) => transformRows ? transformRows(rows) : rows),
173 executeRaw: (sql, params) => run(conn, sql, params, false),
174 executeStream: () => Stream.die("executeStream not implemented"),
175 executeValues: (sql, params) => run(conn, sql, params, true),
176 executeValuesUnprepared: (sql, params) => run(conn, sql, params, true),
177 executeUnprepared: (sql, params, transformRows) =>
178 Effect.map(run(conn, sql, params, false), (rows) => transformRows ? transformRows(rows) : rows)
179 }))
180 }).pipe(Effect.provide(Reactivity.layer))
181 })
182
183 it.layer(MssqlContainer.layerClient, { timeout: "90 seconds" })((it) => {
184 PersistedQueueSqlTest.suiteWith(it)
185
186 it.effect("PersistedQueue offers, claims and settlements do not deadlock", () =>
187 Effect.gen(function*() {
188 const tableName = "effect_queue_mssql_deadlocks"
189 const readDeadlocks = yield* captureDeadlocks
190 const makeQueue = Effect.gen(function*() {
191 const store = yield* PersistedQueue.makeStoreSql({ tableName, pollInterval: "10 millis" })
192 const factory = yield* PersistedQueue.makeFactory.pipe(
193 Effect.provideService(PersistedQueue.PersistedQueueStore, store)
194 )
195 return yield* factory.make({ name: tableName, schema: Schema.Number, retrySchedule: Schedule.spaced(0) })
196 })
197 // two stores poll the same table, so claims race each other as well as
198 // offers and settlements
199 const queues = [yield* makeQueue, yield* makeQueue]
200
201 const total = 120
202 const deliveries = new Map<number, number>()
203 const succeeded = new Set<number>()
204 const completed = Latch.makeUnsafe()
205 for (const queue of queues) {
206 const worker = queue.take((n) =>
207 Effect.suspend(() => {
208 const count = (deliveries.get(n) ?? 0) + 1
209 deliveries.set(n, count)
210 // every third element fails its first delivery, so claims also
211 // race retries
212 if (n % 3 === 0 && count === 1) {
213 return Effect.fail("transient")
214 }
215 succeeded.add(n)
216 if (succeeded.size === total) {
217 completed.openUnsafe()
218 }
219 return Effect.void
220 })
221 ).pipe(Effect.ignore, Effect.forever)
222 for (let i = 0; i < 3; i++) {
223 yield* Effect.forkScoped(worker)
224 }
225 }
226
227 const offered = yield* Effect.forEach(
228 Array.from({ length: total }, (_, n) => n),
229 (n) => queues[n % 2].offer(n),
230 { concurrency: 10, discard: true }
231 ).pipe(Effect.exit)
232 if (Exit.isSuccess(offered)) {
233 yield* completed.await.pipe(Effect.timeoutOption("15 seconds"))
234 }
235
236 // a lock resource names the table as objectname="<database>.<schema>.<table>"
237 const deadlocks = (yield* readDeadlocks).filter((graph) => graph.includes(`.${tableName}"`))
238 assert.deepStrictEqual(deadlocks.map(summarizeDeadlock), [], "deadlocks")
239 assert.isTrue(Exit.isSuccess(offered), String(offered))
240 assert.strictEqual(succeeded.size, total)
241 for (const [n, count] of deliveries) {
242 assert.strictEqual(count, n % 3 === 0 ? 2 : 1, `deliveries for element ${n}`)
243 }
244 }).pipe(TestClock.withLive), { timeout: 30000 })
245 })
246
247 // These run one at a time on a server of their own: concurrent table creation
248 // can deadlock on the system catalog, which is not what they test.
249 it.layer(MssqlContainer.layerClient, { concurrent: false, timeout: "90 seconds" })(
250 "PersistedQueue SQL Server duplicates",
251 (it) => {
252 it.effect(
253 "PersistedQueue skips an offer racing an uncommitted duplicate under XACT_ABORT",
254 () =>
255 Effect.gen(function*() {
256 const master = yield* SqlClient.SqlClient
257 const database = "effect_queue_xact_abort"
258 // With read committed snapshot, the Azure SQL Database default, a plain
259 // read does not wait for another transaction's uncommitted offer.
260 yield* master`IF DB_ID(${database}) IS NULL CREATE DATABASE ${master(database)}`
261 yield* master`ALTER DATABASE ${master(database)} SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE`
262 const sql = yield* makeXactAbortClient(database)
263
264 const tableName = "effect_queue_xact_abort"
265 const store = yield* PersistedQueue.makeStoreSql({ tableName }).pipe(
266 Effect.provideService(SqlClient.SqlClient, sql)
267 )
268 const factory = yield* PersistedQueue.makeFactory.pipe(
269 Effect.provideService(PersistedQueue.PersistedQueueStore, store)
270 )
271 const queue = yield* factory.make({ name: tableName, schema: Schema.String })
272
273 const offered = Latch.makeUnsafe()
274 const first = yield* Effect.gen(function*() {
275 yield* queue.offer("first", { id: "job" })
276 yield* offered.open
277 // commit only once the duplicate offer is waiting on this row
278 yield* Effect.sleep("1 second")
279 }).pipe(sql.withTransaction, Effect.forkScoped)
280 yield* offered.await
281
282 const second = yield* Effect.gen(function*() {
283 const [{ xactAbort }] = yield* sql<{ readonly xactAbort: number }>`
284 SELECT @@OPTIONS & 16384 AS xactAbort
285 `
286 assert.strictEqual(xactAbort, 16384, "XACT_ABORT is on for the duplicate offer")
287 yield* queue.offer("duplicate", { id: "job" })
288 yield* queue.offer("other", { id: "other" })
289 }).pipe(sql.withTransaction, Effect.exit)
290 yield* Fiber.join(first)
291
292 assert.isTrue(Exit.isSuccess(second), String(second))
293 const rows = yield* sql<{ readonly id: string; readonly element: string }>`
294 SELECT id, element FROM ${sql(tableName)} ORDER BY id
295 `
296 assert.deepStrictEqual(rows, [
297 { id: "job", element: JSON.stringify("first") },
298 { id: "other", element: JSON.stringify("other") }
299 ])
300 }).pipe(TestClock.withLive),
301 { timeout: 30000 }
302 )
303
304 it.effect(
305 "PersistedQueue skips a duplicate an older release's offer inserts while the offer waits",
306 () =>
307 Effect.gen(function*() {
308 const sql = yield* SqlClient.SqlClient
309 const xa = yield* makeXactAbortClient((yield* MssqlContainer).getDatabase())
310 const tableName = "effect_queue_mixed_release"
311 const queue = yield* Effect.gen(function*() {
312 const store = yield* PersistedQueue.makeStoreSql({ tableName })
313 const factory = yield* PersistedQueue.makeFactory.pipe(
314 Effect.provideService(PersistedQueue.PersistedQueueStore, store)
315 )
316 return yield* factory.make({ name: tableName, schema: Schema.String })
317 }).pipe(Effect.provideService(SqlClient.SqlClient, xa))
318 // the SQL Server offer of releases before the MERGE was replaced, which
319 // keeps writing during a rolling upgrade
320 const legacyOffer = (id: string, element: string) =>
321 sql`
322 MERGE ${sql(tableName)} WITH (HOLDLOCK) AS target
323 USING (SELECT ${id} AS id, ${tableName} AS queue_name) AS source
324 ON target.id = source.id AND target.queue_name = source.queue_name
325 WHEN NOT MATCHED THEN
326 INSERT (id, queue_name, element, state, attempts, visible_at, created_at, updated_at)
327 VALUES (source.id, source.queue_name, ${JSON.stringify(element)}, 'pending', 0,
328 SYSDATETIME(), SYSDATETIME(), SYSDATETIME());
329 `
330
331 const legacyLocked = yield* Deferred.make<number>()
332 const offerBlocked = Latch.makeUnsafe()
333 const legacy = yield* Effect.gen(function*() {
334 yield* legacyOffer("earlier", "earlier")
335 const [{ spid }] = yield* sql<{ readonly spid: number }>`SELECT @@SPID AS spid`
336 yield* Deferred.succeed(legacyLocked, spid)
337 yield* offerBlocked.await
338 yield* legacyOffer("job", "legacy")
339 }).pipe(sql.withTransaction, Effect.forkScoped)
340 const spid = yield* Deferred.await(legacyLocked)
341
342 const offer = yield* Effect.gen(function*() {
343 const [{ xactAbort }] = yield* xa<{ readonly xactAbort: number }>`
344 SELECT @@OPTIONS & 16384 AS xactAbort
345 `
346 assert.strictEqual(xactAbort, 16384, "XACT_ABORT is on for the offer")
347 yield* queue.offer("new", { id: "job" })
348 yield* queue.offer("other", { id: "other" })
349 }).pipe(xa.withTransaction, Effect.exit, Effect.forkScoped)
350
351 // the older release inserts the same id only once the offer is waiting
352 // on its range locks
353 yield* sql<{ readonly blocked: number }>`
354 SELECT COUNT(*) AS blocked FROM sys.dm_exec_requests WHERE blocking_session_id = ${spid}
355 `.pipe(
356 Effect.flatMap(([{ blocked }]) => blocked > 0 ? Effect.void : Effect.fail("not blocked yet")),
357 Effect.retry({ schedule: Schedule.spaced("50 millis"), times: 100 }),
358 Effect.orDie
359 )
360 yield* offerBlocked.open
361 yield* Fiber.join(legacy)
362
363 const exit = yield* Fiber.join(offer)
364 assert.isTrue(Exit.isSuccess(exit), String(exit))
365 const rows = yield* sql<{ readonly id: string; readonly element: string }>`
366 SELECT id, element FROM ${sql(tableName)} ORDER BY id
367 `
368 assert.deepStrictEqual(rows, [
369 { id: "earlier", element: JSON.stringify("earlier") },
370 { id: "job", element: JSON.stringify("legacy") },
371 { id: "other", element: JSON.stringify("other") }
372 ])
373 }).pipe(TestClock.withLive),
374 { timeout: 30000 }
375 )
376
377 it.effect(
378 "PersistedQueue keeps a raw batch session's XACT_ABORT when an offer is cancelled",
379 () =>
380 Effect.gen(function*() {
381 const sql = yield* SqlClient.SqlClient
382 // a single connection, so the cancelled offer's session is the one
383 // the next statement gets
384 const raw = yield* makeXactAbortClient((yield* MssqlContainer).getDatabase(), { rawBatch: true, size: 1 })
385 const tableName = "effect_queue_raw_cancel"
386 const makeQueue = (client: SqlClient.SqlClient) =>
387 Effect.gen(function*() {
388 const store = yield* PersistedQueue.makeStoreSql({ tableName })
389 const factory = yield* PersistedQueue.makeFactory.pipe(
390 Effect.provideService(PersistedQueue.PersistedQueueStore, store)
391 )
392 return yield* factory.make({ name: tableName, schema: Schema.String })
393 }).pipe(Effect.provideService(SqlClient.SqlClient, client))
394 const queue = yield* makeQueue(sql)
395 const rawQueue = yield* makeQueue(raw)
396
397 const holding = yield* Deferred.make<number>()
398 const release = Latch.makeUnsafe()
399 const holder = yield* Effect.gen(function*() {
400 yield* queue.offer("first", { id: "job" })
401 const [{ spid }] = yield* sql<{ readonly spid: number }>`SELECT @@SPID AS spid`
402 yield* Deferred.succeed(holding, spid)
403 yield* release.await
404 }).pipe(sql.withTransaction, Effect.forkScoped)
405 const holderSpid = yield* Deferred.await(holding)
406
407 const offer = yield* Effect.forkScoped(rawQueue.offer("duplicate", { id: "job" }))
408 // cancel only once the offer waits on the uncommitted duplicate
409 const session = yield* sql<{ readonly session: number }>`
410 SELECT session_id AS session FROM sys.dm_exec_requests WHERE blocking_session_id = ${holderSpid}
411 `.pipe(
412 Effect.flatMap((rows) =>
413 rows.length > 0 ? Effect.succeed(rows[0].session) : Effect.fail("not blocked yet")
414 ),
415 Effect.retry({ schedule: Schedule.spaced("50 millis"), times: 100 }),
416 Effect.orDie
417 )
418 yield* Fiber.interrupt(offer)
419 yield* release.open
420 yield* Fiber.join(holder)
421
422 const [state] = yield* raw<{ readonly spid: number; readonly tranCount: number; readonly xactAbort: number }>`
423 SELECT @@SPID AS spid, @@TRANCOUNT AS tranCount, @@OPTIONS & 16384 AS xactAbort
424 `
425 assert.deepStrictEqual(state, { spid: session, tranCount: 0, xactAbort: 16384 })
426 }).pipe(TestClock.withLive),
427 { timeout: 30000 }
428 )
429 }
430 )