Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eventlog-mssql-inline-indexes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix `SqlEventLogServerEncrypted` on SQL Server stalling for seconds when two instances create the same tables at once.
18 changes: 9 additions & 9 deletions packages/effect/src/eventlog/SqlEventLogServerEncrypted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,21 @@ export const makeStorage = (options?: {
})
// Two instances can open the same table at once, and PostgreSQL and SQL
// Server can fail the loser of a concurrent create, so creation retries.
// SQL Server declares its indexes inline, as concurrent CREATE INDEX
// statements can deadlock.
const createTable = (name: string, columns: string, indexes: ReadonlyArray<string> = []) =>
sql.onDialectOrElse({
mysql: () => {
const definitions = [columns, ...indexes.map((index) => `INDEX (${index})`)].join(", ")
return Effect.asVoid(sql`CREATE TABLE IF NOT EXISTS ${sql(name)} (${sql.literal(definitions)})`)
},
mssql: () =>
sql`IF OBJECT_ID(${name}, N'U') IS NULL CREATE TABLE ${sql(name)} (${sql.literal(columns)})`.pipe(
Effect.andThen(Effect.forEach(indexes, (index, i) => {
const indexName = `${name}_i${i}`
return sql`
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = ${indexName} AND object_id = OBJECT_ID(${name}))
CREATE INDEX ${sql(indexName)} ON ${sql(name)} (${sql.literal(index)})`
}, { discard: true }))
),
mssql: () => {
const definitions = sql.csv([
sql.literal(columns),
...indexes.map((index, i) => sql`INDEX ${sql(`${name}_i${i}`)} NONCLUSTERED (${sql.literal(index)})`)
])
return Effect.asVoid(sql`IF OBJECT_ID(${name}, N'U') IS NULL CREATE TABLE ${sql(name)} (${definitions})`)
},
orElse: () =>
sql`CREATE TABLE IF NOT EXISTS ${sql(name)} (${sql.literal(columns)})`.pipe(
Effect.andThen(Effect.forEach(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Layer } from "effect"
import { assert, it } from "@effect/vitest"
import { Deferred, Effect, Layer } from "effect"
import * as CompactionTest from "effect-test/eventlog/EventLogServerEncryptedCompactionTest"
import * as EncryptedTest from "effect-test/eventlog/EventLogServerEncryptedTest"
import * as SqlEventLogServerEncryptedStorageTest from "effect-test/eventlog/SqlEventLogServerEncryptedStorageTest"
import * as Digest from "effect/crypto/Digest"
import * as EventLogEncryption from "effect/eventlog/EventLogEncryption"
import * as SqlEventLogServerEncrypted from "effect/eventlog/SqlEventLogServerEncrypted"
import { Reactivity } from "effect/reactivity"
import { SqlClient } from "effect/sql"
import { MssqlContainer } from "./utils.ts"

EncryptedTest.suite(
Expand All @@ -24,3 +26,50 @@ CompactionTest.suite(
)

SqlEventLogServerEncryptedStorageTest.compactionSuite("mssql", MssqlContainer.layerClient)

// Succeeds once a request waits on a lock that session `spid` holds. Other
// sessions don't count, as SQL Server's background tasks block each other.
const blockedBy = Effect.fnUntraced(function*(spid: number) {
const sql = yield* SqlClient.SqlClient
while (true) {
const [row] = yield* sql<{ n: number }>`
SELECT COUNT(*) AS n FROM sys.dm_exec_requests WHERE blocking_session_id = ${spid}`
if (row.n > 0) return "waited"
yield* Effect.sleep(20)
}
})

it.layer(Layer.mergeAll(Digest.layerWebCrypto, MssqlContainer.layerClient), {
timeout: "60 seconds",
excludeTestServices: true
})(
"SqlEventLogServerEncrypted tables (mssql)",
(it) => {
it.effect("opens a store while another instance is creating an index", () =>
Effect.gen(function*() {
const sql = yield* SqlClient.SqlClient
const prefix = "indexing"
const stores = `${prefix}_compaction_stores`
yield* sql`CREATE TABLE ${sql(stores)} (scope NVARCHAR(36) PRIMARY KEY, next_sweep_at BIGINT NULL)`
// Hold a separate index creation open, as an older instance would.
const indexing = yield* Deferred.make<number>()
yield* Effect.gen(function*() {
yield* sql`CREATE INDEX ${sql(`${stores}_i0`)} ON ${sql(stores)} (next_sweep_at)`
const [row] = yield* sql<{ spid: number }>`SELECT @@SPID AS spid`
yield* Deferred.succeed(indexing, row.spid)
return yield* Effect.never
}).pipe(sql.withTransaction, Effect.forkScoped)
const spid = yield* Deferred.await(indexing)

const outcome = yield* Effect.race(
SqlEventLogServerEncrypted.makeStorage({ entryTablePrefix: prefix }).pipe(
Effect.as("opened"),
Effect.scoped
),
blockedBy(spid)
)

assert.strictEqual(outcome, "opened")
}))
}
)
Loading