nx-kv 0.45.0 → 0.45.1
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/CHANGELOG.md +16 -0
- package/package.json +1 -1
- package/src/core/db/driver.js +221 -96
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
本文件记录对外可见的变更。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/)。
|
|
4
4
|
|
|
5
|
+
## [0.45.1] - 2026-11-10 残留 -shm 打开自愈 + 打开失败错误指路
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- **Windows 上「serve 退出后 CLI 打不开库」**:库目录留下 `nxkv.db-shm`(`-wal` 常为
|
|
10
|
+
0 字节)时,新进程读写打开会报裸 `disk I/O error`(`SQLITE_IOERR_TRUNCATE` 1546 /
|
|
11
|
+
`SQLITE_IOERR_SHMMAP` 5386)。现在 `openRaw` 会识别该错误,把 shm(连同 0 字节 wal)
|
|
12
|
+
改名为 `<file>.stale-<时间戳>` **留档**,只重试一次;主库与有内容的 WAL 一个字节不动。
|
|
13
|
+
- **有活进程时不动手**:rename 本身就是活性探针——Windows 不允许改名仍被其他进程映射的
|
|
14
|
+
文件,serve / 另一条 CLI 持有时会 `EBUSY`,此时直接报清楚的错并提示改用
|
|
15
|
+
`POST /api/localtasks/claim` 或先停 serve;`-wal` 有内容时也一个字节不碰。
|
|
16
|
+
- **`unable to open database file` 带上下文**:所有打开失败统一追加库路径、errcode 与
|
|
17
|
+
处置建议(不再只有一句英文)。
|
|
18
|
+
- 单测:`tests/unit/db-stale-shm.test.mjs`(判定码 / 改名契约 / 零副作用 / 错误装饰);
|
|
19
|
+
决策记录:`docs/decisions/2026-11-10-stale-shm-self-heal.md`。
|
|
20
|
+
|
|
5
21
|
## [0.45.0] - 2026-11-10 模板 helper + 时间目标分档提示 + 未到点 done 拦截可配(#90)
|
|
6
22
|
|
|
7
23
|
### Added
|
package/package.json
CHANGED
package/src/core/db/driver.js
CHANGED
|
@@ -22,10 +22,11 @@
|
|
|
22
22
|
// 所有 REFERENCES 都不会生效,而且不报错)。这些在 openRaw 里一次设好。
|
|
23
23
|
import './quiet.js';
|
|
24
24
|
import * as nodeSqlite from 'node:sqlite';
|
|
25
|
-
import { existsSync, mkdirSync } from 'node:fs';
|
|
25
|
+
import { existsSync, mkdirSync, renameSync, statSync } from 'node:fs';
|
|
26
26
|
import { dirname } from 'node:path';
|
|
27
|
-
import { badInput } from '../errors.js';
|
|
27
|
+
import { AppError, CODES, badInput } from '../errors.js';
|
|
28
28
|
import { isBusyError, mapSqliteError } from './errors.js';
|
|
29
|
+
import { stampFileTag } from './time.js';
|
|
29
30
|
|
|
30
31
|
// v26.11 把 DatabaseSync 改名成 Database;两个都认,只在这里判断。
|
|
31
32
|
const SqliteCtor = nodeSqlite.DatabaseSync ?? nodeSqlite.Database;
|
|
@@ -109,6 +110,105 @@ function enableWal(api) {
|
|
|
109
110
|
// (WAL 一旦由任何人打开就持久生效),也不是迁移失败,所以只是放过。
|
|
110
111
|
}
|
|
111
112
|
|
|
113
|
+
// ── 残留 -shm 自愈(Windows 实测)──────────────────────────────────────
|
|
114
|
+
//
|
|
115
|
+
// 现场:上一进程没走 sqlite3_close 就退出,留下 `-shm`(`-wal` 为 0 字节);
|
|
116
|
+
// 新进程以读写方式打开时,SQLite 在恢复 wal-index 阶段要 truncate 这个 shm,
|
|
117
|
+
// 却拿到 `disk I/O error`(扩展码 1546 = SQLITE_IOERR_TRUNCATE,偶发 5386 =
|
|
118
|
+
// SQLITE_IOERR_SHMMAP)。库本身与 WAL 都是好的——把 shm 改名后立刻能打开。
|
|
119
|
+
//
|
|
120
|
+
// 策略(**只改名、不删除、只重试一次**):
|
|
121
|
+
// - shm 与 0 字节 wal 一律改名为 `<file>.stale-<时间戳>` 留档;wal 有内容时一个字节不碰
|
|
122
|
+
// (那里面可能还有未 checkpoint 的已提交事务);
|
|
123
|
+
// - **rename 本身就是活性探针**:Windows 不允许改名仍被别的进程映射的文件,
|
|
124
|
+
// 活进程(serve / 另一条 CLI)持有时会 EBUSY → 判定为「不是残留」,不插手;
|
|
125
|
+
// - 重试仍失败 → 抛出带路径 / errcode / 已改名文件的错误,而不是裸 disk I/O error。
|
|
126
|
+
const STALE_SHM_CODES = new Set([
|
|
127
|
+
1546, // SQLITE_IOERR_TRUNCATE
|
|
128
|
+
5386, // SQLITE_IOERR_SHMMAP
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
/** 这个错误是不是「残留 shm 导致的打开失败」(扩展码优先,缺失时退回消息)。 */
|
|
132
|
+
export function isStaleShmError(err) {
|
|
133
|
+
const code = Number(err && err.errcode);
|
|
134
|
+
if (STALE_SHM_CODES.has(code)) return true;
|
|
135
|
+
if (Number.isFinite(code) && code !== 0) return false;
|
|
136
|
+
return /disk I\/O error/i.test(String((err && err.message) || ''));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 当前库的 shm / wal 状态(不存在返回 null;`walBytes` 取不到时为 -1)。 */
|
|
140
|
+
function staleShmInfo(dbPath) {
|
|
141
|
+
if (!dbPath || dbPath === ':memory:') return null;
|
|
142
|
+
const shm = `${dbPath}-shm`;
|
|
143
|
+
const wal = `${dbPath}-wal`;
|
|
144
|
+
if (!existsSync(shm)) return null;
|
|
145
|
+
let walBytes = 0;
|
|
146
|
+
try {
|
|
147
|
+
walBytes = existsSync(wal) ? statSync(wal).size : 0;
|
|
148
|
+
} catch {
|
|
149
|
+
walBytes = -1;
|
|
150
|
+
}
|
|
151
|
+
return { shm, wal, walBytes };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 把残留的 `-shm`(必要时连同 0 字节 `-wal`)改名留档。
|
|
156
|
+
* @returns {string[]} 改名后的路径;没有可改的返回空数组
|
|
157
|
+
*/
|
|
158
|
+
export function quarantineStaleShm(dbPath, atMs = Date.now()) {
|
|
159
|
+
const info = staleShmInfo(dbPath);
|
|
160
|
+
if (!info) return [];
|
|
161
|
+
const tag = stampFileTag(atMs);
|
|
162
|
+
const renamed = [];
|
|
163
|
+
renameSync(info.shm, `${info.shm}.stale-${tag}`);
|
|
164
|
+
renamed.push(`${info.shm}.stale-${tag}`);
|
|
165
|
+
if (info.walBytes === 0) {
|
|
166
|
+
try {
|
|
167
|
+
renameSync(info.wal, `${info.wal}.stale-${tag}`);
|
|
168
|
+
renamed.push(`${info.wal}.stale-${tag}`);
|
|
169
|
+
} catch {
|
|
170
|
+
// 0 字节 wal 改名失败不影响重试:SQLite 会重建它。
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return renamed;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 打开失败时尝试一次自愈。
|
|
178
|
+
*
|
|
179
|
+
* **rename 即活性探针**:活进程持有 shm 时 Windows 会拒绝改名(EBUSY / EACCES),
|
|
180
|
+
* 这里把改名失败当成「不是残留、有人在用」→ 返回 null,绝不继续动刀。
|
|
181
|
+
* @returns {string[]|null} null = 不适用(不是 shm 残留 / 有活进程持有);数组 = 已改名并应重试
|
|
182
|
+
*/
|
|
183
|
+
export function healStaleShmOnce(dbPath, err) {
|
|
184
|
+
if (!isStaleShmError(err)) return null;
|
|
185
|
+
if (!staleShmInfo(dbPath)) return null;
|
|
186
|
+
try {
|
|
187
|
+
return quarantineStaleShm(dbPath);
|
|
188
|
+
} catch {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 打开失败的错误装饰:带上路径、errcode 与处置建议(不吞原始消息)。 */
|
|
194
|
+
function decorateOpenError(err, dbPath) {
|
|
195
|
+
if (err instanceof AppError) return err;
|
|
196
|
+
const code = Number(err && err.errcode);
|
|
197
|
+
const raw = String((err && err.message) || err || '未知错误');
|
|
198
|
+
const hint = /unable to open database file/i.test(raw)
|
|
199
|
+
? '目录不可写 / 文件被占用 / 盘符不存在都会报这句'
|
|
200
|
+
: '若是「另一个进程正在用这个库」,先停掉那个 serve 或改用 HTTP 领取(POST /api/localtasks/claim)';
|
|
201
|
+
const e = new AppError(
|
|
202
|
+
CODES.INTERNAL,
|
|
203
|
+
`打开库失败: ${dbPath}(errcode=${Number.isFinite(code) ? code : '?'}):${raw}\n${hint}。` +
|
|
204
|
+
'残留 -shm 只会改名留档(*.stale-<时间戳>),不会删除任何库文件。',
|
|
205
|
+
{ path: dbPath, errcode: Number.isFinite(code) ? code : null }
|
|
206
|
+
);
|
|
207
|
+
e.name = 'StoreOpenError';
|
|
208
|
+
if (Number.isFinite(code)) e.errcode = code;
|
|
209
|
+
return e;
|
|
210
|
+
}
|
|
211
|
+
|
|
112
212
|
/**
|
|
113
213
|
* 打开一个原始连接。
|
|
114
214
|
*
|
|
@@ -127,108 +227,133 @@ export function openRaw(dbPath, { readOnly = false, busyTimeoutMs = 5000 } = {})
|
|
|
127
227
|
throw badInput(`库文件不存在: ${dbPath}(只读打开不会创建它)`);
|
|
128
228
|
}
|
|
129
229
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
raw
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
230
|
+
/** 一次完整的「打开 + 连接级 PRAGMA」;失败时先关掉半成品再抛。 */
|
|
231
|
+
const openOnce = () => {
|
|
232
|
+
let raw;
|
|
233
|
+
try {
|
|
234
|
+
raw = new SqliteCtor(dbPath, readOnly ? { readOnly: true } : {});
|
|
235
|
+
} catch (err) {
|
|
236
|
+
throw mapSqliteError(err);
|
|
237
|
+
}
|
|
136
238
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const api = {
|
|
141
|
-
/** 库文件路径(测试与排障用)。 */
|
|
142
|
-
path: dbPath,
|
|
143
|
-
readOnly,
|
|
144
|
-
/** 底层 DatabaseSync(只在必须绕过封装时用:PRAGMA、VACUUM INTO)。 */
|
|
145
|
-
raw: () => raw,
|
|
146
|
-
/** 连接是否已关闭。 */
|
|
147
|
-
get closed() {
|
|
148
|
-
return closed;
|
|
149
|
-
},
|
|
150
|
-
|
|
151
|
-
/** 执行一段 SQL(可多语句,不能带参数)。DDL / PRAGMA 走它。 */
|
|
152
|
-
exec(sql) {
|
|
153
|
-
try {
|
|
154
|
-
raw.exec(sql);
|
|
155
|
-
} catch (err) {
|
|
156
|
-
throw mapSqliteError(err);
|
|
157
|
-
}
|
|
158
|
-
},
|
|
239
|
+
const stmts = new Map();
|
|
240
|
+
let closed = false;
|
|
159
241
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
stmts.set(sql, st);
|
|
171
|
-
return st;
|
|
172
|
-
},
|
|
242
|
+
const api = {
|
|
243
|
+
/** 库文件路径(测试与排障用)。 */
|
|
244
|
+
path: dbPath,
|
|
245
|
+
readOnly,
|
|
246
|
+
/** 底层 DatabaseSync(只在必须绕过封装时用:PRAGMA、VACUUM INTO)。 */
|
|
247
|
+
raw: () => raw,
|
|
248
|
+
/** 连接是否已关闭。 */
|
|
249
|
+
get closed() {
|
|
250
|
+
return closed;
|
|
251
|
+
},
|
|
173
252
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
253
|
+
/** 执行一段 SQL(可多语句,不能带参数)。DDL / PRAGMA 走它。 */
|
|
254
|
+
exec(sql) {
|
|
255
|
+
try {
|
|
256
|
+
raw.exec(sql);
|
|
257
|
+
} catch (err) {
|
|
258
|
+
throw mapSqliteError(err);
|
|
259
|
+
}
|
|
260
|
+
},
|
|
182
261
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
262
|
+
/** 预编译(带缓存)。返回 StatementSync 的薄封装。 */
|
|
263
|
+
prepare(sql) {
|
|
264
|
+
let st = stmts.get(sql);
|
|
265
|
+
if (st) return st;
|
|
266
|
+
try {
|
|
267
|
+
st = raw.prepare(sql);
|
|
268
|
+
} catch (err) {
|
|
269
|
+
throw mapSqliteError(err);
|
|
270
|
+
}
|
|
271
|
+
if (stmts.size >= STMT_CACHE_MAX) stmts.clear();
|
|
272
|
+
stmts.set(sql, st);
|
|
273
|
+
return st;
|
|
274
|
+
},
|
|
191
275
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
276
|
+
/** 取一行;没有行返回 `null`(不是 undefined:`null` 在 JSON 里才看得见)。 */
|
|
277
|
+
get(sql, params) {
|
|
278
|
+
try {
|
|
279
|
+
return plain(api.prepare(sql).get(...argsOf(params)));
|
|
280
|
+
} catch (err) {
|
|
281
|
+
throw mapSqliteError(err);
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
/** 取全部行(每行都是普通对象)。 */
|
|
286
|
+
all(sql, params) {
|
|
287
|
+
try {
|
|
288
|
+
return api.prepare(sql).all(...argsOf(params)).map(plain);
|
|
289
|
+
} catch (err) {
|
|
290
|
+
throw mapSqliteError(err);
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
/** 执行写语句 → `{ changes, lastInsertRowid }`(数字,不是 bigint)。 */
|
|
295
|
+
run(sql, params) {
|
|
296
|
+
try {
|
|
297
|
+
const r = api.prepare(sql).run(...argsOf(params));
|
|
298
|
+
return { changes: Number(r.changes), lastInsertRowid: Number(r.lastInsertRowid) };
|
|
299
|
+
} catch (err) {
|
|
300
|
+
throw mapSqliteError(err);
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
|
|
304
|
+
/** 取一个标量(第一列第一行);没有行返回 `null`。 */
|
|
305
|
+
scalar(sql, params) {
|
|
306
|
+
const row = api.get(sql, params);
|
|
307
|
+
if (!row) return null;
|
|
308
|
+
const keys = Object.keys(row);
|
|
309
|
+
return keys.length ? row[keys[0]] : null;
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
/** 关连接(幂等)。**测试删库前必须先关**:Windows 上连接开着会 EBUSY。 */
|
|
313
|
+
close() {
|
|
314
|
+
if (closed) return;
|
|
315
|
+
closed = true;
|
|
316
|
+
stmts.clear();
|
|
317
|
+
try {
|
|
318
|
+
raw.close();
|
|
319
|
+
} catch (err) {
|
|
320
|
+
throw mapSqliteError(err);
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
// ── 连接级 PRAGMA:每个连接都要设,且必须在任何业务语句之前 ──
|
|
326
|
+
//
|
|
327
|
+
// foreign_keys 默认关:漏了它,所有 REFERENCES 静默失效(删仓库不级联、
|
|
328
|
+
// 依赖能指向不存在的任务),而且**不报错**。
|
|
329
|
+
const waitMs = Number.isFinite(Number(busyTimeoutMs)) ? Math.max(0, Number(busyTimeoutMs)) : 5000;
|
|
330
|
+
try {
|
|
331
|
+
api.exec('PRAGMA busy_timeout = ' + waitMs);
|
|
332
|
+
api.exec('PRAGMA foreign_keys = ON');
|
|
333
|
+
api.exec('PRAGMA synchronous = NORMAL');
|
|
334
|
+
if (!readOnly) enableWal(api);
|
|
335
|
+
} catch (err) {
|
|
336
|
+
// 关键:先把本进程的映射关掉,否则 Windows 不允许后面改名 shm。
|
|
215
337
|
try {
|
|
216
338
|
raw.close();
|
|
217
|
-
} catch
|
|
218
|
-
|
|
339
|
+
} catch {
|
|
340
|
+
/* 半成品连接关不掉也继续:下面按原始错误处理 */
|
|
219
341
|
}
|
|
220
|
-
|
|
342
|
+
throw mapSqliteError(err);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return api;
|
|
221
346
|
};
|
|
222
347
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
348
|
+
try {
|
|
349
|
+
return openOnce();
|
|
350
|
+
} catch (err) {
|
|
351
|
+
const renamed = readOnly ? null : healStaleShmOnce(dbPath, err);
|
|
352
|
+
if (!renamed || renamed.length === 0) throw decorateOpenError(err, dbPath);
|
|
353
|
+
try {
|
|
354
|
+
return openOnce();
|
|
355
|
+
} catch (err2) {
|
|
356
|
+
throw decorateOpenError(err2, dbPath);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
234
359
|
}
|