@jskit-ai/database-runtime 0.1.250 → 0.1.252
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/server/databaseSetup.js +2 -1
- package/src/shared/index.js +2 -0
- package/src/shared/repositoryOptions.js +43 -2
- package/src/shared/transactionManager.js +3 -2
- package/test/entrypoints.boundary.test.js +2 -0
- package/test/repositoryOptions.test.js +59 -1
- package/test/runtimeCore.test.js +22 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/database-runtime",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.252",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -67,6 +67,6 @@
|
|
|
67
67
|
}
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
|
-
"@jskit-ai/kernel": "0.2.
|
|
70
|
+
"@jskit-ai/kernel": "0.2.3"
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import { createKnexMigrationConfigFromApp } from "./knexMigrationConfig.js";
|
|
3
3
|
import { loadKnexFactory } from "./knexFactory.js";
|
|
4
|
+
import { runInTransaction } from "../shared/repositoryOptions.js";
|
|
4
5
|
|
|
5
6
|
function normalizeMigrationResult(result) {
|
|
6
7
|
const [batchValue, migrationValues] = Array.isArray(result) ? result : [];
|
|
@@ -27,7 +28,7 @@ async function runDatabaseSetup({ knex, seed, appRoot = process.cwd(), environme
|
|
|
27
28
|
throw new TypeError("runDatabaseSetup requires Knex transaction support for seeding.");
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
await knex
|
|
31
|
+
await runInTransaction(knex, async (transaction) => {
|
|
31
32
|
await seed(Object.freeze({
|
|
32
33
|
knex: transaction,
|
|
33
34
|
appRoot: path.resolve(appRoot),
|
package/src/shared/index.js
CHANGED
|
@@ -151,7 +151,41 @@ function toDbJson(value) {
|
|
|
151
151
|
return JSON.stringify(value);
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
|
|
154
|
+
// True when the error reports a transaction the database aborted, and rolled
|
|
155
|
+
// back in full, as a deadlock victim. A wrapper that reports its transaction
|
|
156
|
+
// committed (json-rest-api's transactionOutcome) never is one.
|
|
157
|
+
function isDeadlockError(error) {
|
|
158
|
+
const seen = new Set();
|
|
159
|
+
while (error && typeof error === "object" && !seen.has(error)) {
|
|
160
|
+
seen.add(error);
|
|
161
|
+
if (error.transactionOutcome === "committed") {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
const code = String(error.code || "").toUpperCase();
|
|
165
|
+
if (code === "ER_LOCK_DEADLOCK" || code === "40P01" || Number(error.errno) === 1213) {
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
error = error.cause;
|
|
169
|
+
}
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Runs a whole transaction again when the database chose it as a deadlock
|
|
174
|
+
// victim, so `run` must be safe to repeat: no external effects inside it.
|
|
175
|
+
async function retryDeadlocks(run, { retries = 3 } = {}) {
|
|
176
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
177
|
+
try {
|
|
178
|
+
return await run();
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (attempt > retries || !isDeadlockError(error)) {
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
await new Promise((resolve) => setTimeout(resolve, Math.random() * 20 * 2 ** attempt));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function runInTransaction(knex, callback, options = {}) {
|
|
155
189
|
if (typeof callback !== "function") {
|
|
156
190
|
throw new TypeError("runInTransaction requires callback.");
|
|
157
191
|
}
|
|
@@ -162,7 +196,12 @@ async function runInTransaction(knex, callback) {
|
|
|
162
196
|
if (typeof knex.transaction !== "function") {
|
|
163
197
|
return callback(knex);
|
|
164
198
|
}
|
|
165
|
-
|
|
199
|
+
// Inside a transaction this opens a savepoint; a deadlock aborts the
|
|
200
|
+
// outermost transaction, and only that one retries.
|
|
201
|
+
if (knex.isTransaction) {
|
|
202
|
+
return knex.transaction(callback);
|
|
203
|
+
}
|
|
204
|
+
return retryDeadlocks(() => knex.transaction(callback), options);
|
|
166
205
|
}
|
|
167
206
|
|
|
168
207
|
function createWithTransaction(knex) {
|
|
@@ -218,6 +257,8 @@ export {
|
|
|
218
257
|
resolveInsertedRecordId,
|
|
219
258
|
parseJsonValue,
|
|
220
259
|
toDbJson,
|
|
260
|
+
isDeadlockError,
|
|
261
|
+
retryDeadlocks,
|
|
221
262
|
runInTransaction,
|
|
222
263
|
createWithTransaction,
|
|
223
264
|
lockScopedRecordId
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { normalizeObject } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
|
+
import { retryDeadlocks } from "./repositoryOptions.js";
|
|
2
3
|
|
|
3
4
|
class TransactionManager {
|
|
4
5
|
constructor({ knex } = {}) {
|
|
@@ -18,9 +19,9 @@ class TransactionManager {
|
|
|
18
19
|
return work(trx);
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
return this.knex.transaction(async (nextTrx) => {
|
|
22
|
+
return retryDeadlocks(() => this.knex.transaction(async (nextTrx) => {
|
|
22
23
|
return work(nextTrx);
|
|
23
|
-
});
|
|
24
|
+
}));
|
|
24
25
|
}
|
|
25
26
|
}
|
|
26
27
|
|
|
@@ -28,6 +28,8 @@ test("server provider module exports one declarative provider", () => {
|
|
|
28
28
|
test("shared entrypoint exports shared database utilities only", () => {
|
|
29
29
|
assert.equal(typeof sharedApi.createWithTransaction, "function");
|
|
30
30
|
assert.equal(typeof sharedApi.isDuplicateEntryError, "function");
|
|
31
|
+
assert.equal(typeof sharedApi.isDeadlockError, "function");
|
|
32
|
+
assert.equal(typeof sharedApi.retryDeadlocks, "function");
|
|
31
33
|
assert.equal(typeof sharedApi.toDatabaseDateTimeUtc, "function");
|
|
32
34
|
assert.equal(typeof sharedApi.DatabaseProvider, "undefined");
|
|
33
35
|
for (const removed of ["normalizeText", "retentionTestables", "createTransactionManager", "createRepositoryScope", "BaseRepository"]) {
|
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import test from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import {
|
|
4
|
+
isDeadlockError,
|
|
4
5
|
lockScopedRecordId,
|
|
5
6
|
normalizeDbRecordId,
|
|
6
7
|
parseJsonValue,
|
|
7
|
-
resolveInsertedRecordId
|
|
8
|
+
resolveInsertedRecordId,
|
|
9
|
+
runInTransaction
|
|
8
10
|
} from "../src/shared/repositoryOptions.js";
|
|
9
11
|
|
|
12
|
+
function deadlock() {
|
|
13
|
+
return Object.assign(new Error("Deadlock found when trying to get lock; try restarting transaction"), {
|
|
14
|
+
code: "ER_LOCK_DEADLOCK",
|
|
15
|
+
errno: 1213
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function fakeKnex({ isTransaction = false } = {}) {
|
|
20
|
+
return Object.assign(() => {}, { isTransaction, transaction: async (callback) => callback("trx") });
|
|
21
|
+
}
|
|
22
|
+
|
|
10
23
|
function fakeLockTransaction(row = null) {
|
|
11
24
|
const calls = [];
|
|
12
25
|
const query = {
|
|
@@ -77,3 +90,48 @@ test("lockScopedRecordId rejects missing transactions and unsafe identifiers", a
|
|
|
77
90
|
/safe SQL identifier/u
|
|
78
91
|
);
|
|
79
92
|
});
|
|
93
|
+
|
|
94
|
+
test("isDeadlockError recognizes deadlock victims through causes, never a committed write", () => {
|
|
95
|
+
assert.equal(isDeadlockError(deadlock()), true);
|
|
96
|
+
assert.equal(isDeadlockError({ errno: 1213 }), true);
|
|
97
|
+
assert.equal(isDeadlockError({ code: "40P01" }), true);
|
|
98
|
+
assert.equal(isDeadlockError(new Error("Write failed", { cause: deadlock() })), true);
|
|
99
|
+
assert.equal(isDeadlockError({ code: "ER_LOCK_WAIT_TIMEOUT", errno: 1205 }), false);
|
|
100
|
+
assert.equal(isDeadlockError({ code: "ER_DUP_ENTRY", errno: 1062 }), false);
|
|
101
|
+
assert.equal(isDeadlockError({ transactionOutcome: "committed", cause: deadlock() }), false);
|
|
102
|
+
const cyclic = new Error("cyclic");
|
|
103
|
+
cyclic.cause = cyclic;
|
|
104
|
+
for (const error of [null, undefined, "deadlock", cyclic]) {
|
|
105
|
+
assert.equal(isDeadlockError(error), false);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("runInTransaction runs a deadlock victim again and returns the clean run", async () => {
|
|
110
|
+
let attempts = 0;
|
|
111
|
+
const result = await runInTransaction(fakeKnex(), async (trx) => {
|
|
112
|
+
attempts += 1;
|
|
113
|
+
if (attempts < 3) {
|
|
114
|
+
throw deadlock();
|
|
115
|
+
}
|
|
116
|
+
return trx;
|
|
117
|
+
});
|
|
118
|
+
assert.equal(result, "trx");
|
|
119
|
+
assert.equal(attempts, 3);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("runInTransaction retries only deadlocks of the outermost transaction, a few times", async () => {
|
|
123
|
+
async function attemptsUntilRejected(knex, error, options) {
|
|
124
|
+
let attempts = 0;
|
|
125
|
+
await assert.rejects(runInTransaction(knex, async () => {
|
|
126
|
+
attempts += 1;
|
|
127
|
+
throw error;
|
|
128
|
+
}, options), error);
|
|
129
|
+
return attempts;
|
|
130
|
+
}
|
|
131
|
+
const failure = new Error("Validation failed");
|
|
132
|
+
assert.equal(await attemptsUntilRejected(fakeKnex(), failure), 1);
|
|
133
|
+
const victim = deadlock();
|
|
134
|
+
assert.equal(await attemptsUntilRejected(fakeKnex(), victim), 4);
|
|
135
|
+
assert.equal(await attemptsUntilRejected(fakeKnex(), victim, { retries: 0 }), 1);
|
|
136
|
+
assert.equal(await attemptsUntilRejected(fakeKnex({ isTransaction: true }), victim), 1, "A savepoint leaves the retry to its outermost transaction");
|
|
137
|
+
});
|
package/test/runtimeCore.test.js
CHANGED
|
@@ -19,6 +19,28 @@ test("transaction manager wraps callback in knex transaction", async () => {
|
|
|
19
19
|
assert.equal(result.trx.trxId, "trx-1");
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
+
test("transaction manager runs a deadlocked transaction again but never a borrowed one", async () => {
|
|
23
|
+
const manager = createTransactionManager({ knex: createKnexStub() });
|
|
24
|
+
const deadlock = Object.assign(new Error("Deadlock found when trying to get lock"), { code: "ER_LOCK_DEADLOCK" });
|
|
25
|
+
let attempts = 0;
|
|
26
|
+
const result = await manager.inTransaction(async (trx) => {
|
|
27
|
+
attempts += 1;
|
|
28
|
+
if (attempts === 1) {
|
|
29
|
+
throw deadlock;
|
|
30
|
+
}
|
|
31
|
+
return trx.trxId;
|
|
32
|
+
});
|
|
33
|
+
assert.equal(result, "trx-1");
|
|
34
|
+
assert.equal(attempts, 2);
|
|
35
|
+
|
|
36
|
+
attempts = 0;
|
|
37
|
+
await assert.rejects(manager.inTransaction(async () => {
|
|
38
|
+
attempts += 1;
|
|
39
|
+
throw deadlock;
|
|
40
|
+
}, { trx: { trxId: "outer" } }), deadlock);
|
|
41
|
+
assert.equal(attempts, 1);
|
|
42
|
+
});
|
|
43
|
+
|
|
22
44
|
test("createWithTransaction creates a reusable withTransaction function", async () => {
|
|
23
45
|
const knex = Object.assign(() => {
|
|
24
46
|
throw new Error("query execution not expected");
|