@delali/narsil 0.3.1-next.85 → 0.3.1-next.87
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/dist/browser/chunk-3BXQPIXK.mjs +602 -0
- package/dist/browser/chunk-3ZRMZIH4.mjs +3959 -0
- package/dist/browser/chunk-BBDAYUCO.mjs +2703 -0
- package/dist/browser/chunk-FHFFWLEK.mjs +669 -0
- package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
- package/dist/browser/chunk-NQW2YXEX.mjs +346 -0
- package/dist/browser/chunk-TBMNPGFO.mjs +22910 -0
- package/dist/browser/durability-wiring-7RUN7LNS.mjs +3250 -0
- package/dist/browser/module-E25UDADM.mjs +2766 -0
- package/dist/browser/snapshot-A42DRERF.mjs +888 -0
- package/dist/browser/write-4U6Q26TJ.mjs +206 -0
- package/dist/{capabilities-CAdkp2P-.d.ts → capabilities-SJPttcgQ.d.ts} +2 -2
- package/dist/chunk-37CD3OKS.mjs +87 -0
- package/dist/chunk-AQU2QHSY.mjs +7247 -0
- package/dist/{chunk-OLXM65YH.mjs → chunk-AV4IJKD2.mjs} +1 -18
- package/dist/{chunk-4D6OH4EF.mjs → chunk-AXDAS2VA.mjs} +7 -5
- package/dist/{chunk-LS5IRM23.mjs → chunk-BVOJIYRV.mjs} +2 -25
- package/dist/chunk-C7SLA6CM.mjs +19 -0
- package/dist/{chunk-KJ7LB3TR.mjs → chunk-CVQWQVFX.mjs} +3 -22
- package/dist/chunk-DJHHF2L5.mjs +17 -0
- package/dist/chunk-FGMGUDHX.mjs +21 -0
- package/dist/chunk-IKST36MA.mjs +614 -0
- package/dist/{chunk-2453RJYK.mjs → chunk-J2BPXTD7.mjs} +167 -176
- package/dist/chunk-JFJBA3PD.mjs +522 -0
- package/dist/chunk-K4OWQ2M3.mjs +462 -0
- package/dist/chunk-L5ORED7V.mjs +25 -0
- package/dist/chunk-MKK4OZ5W.mjs +703 -0
- package/dist/chunk-MXS5HWIH.mjs +515 -0
- package/dist/chunk-P36MUFEJ.mjs +10 -0
- package/dist/chunk-PR4O7CAP.mjs +18 -0
- package/dist/{chunk-4VBG6PEH.mjs → chunk-PSUJCOSE.mjs} +3 -2
- package/dist/{chunk-ZCKPRCYI.mjs → chunk-Q5DC6WAB.mjs} +1 -1
- package/dist/{chunk-NFF5TDRG.mjs → chunk-QZN3MIJ3.mjs} +1 -16
- package/dist/chunk-R7OSDAFK.mjs +44 -0
- package/dist/chunk-RVM7AODE.mjs +73 -0
- package/dist/{chunk-XYDP7CCJ.mjs → chunk-SYRFSF6V.mjs} +6 -4
- package/dist/{chunk-SREPGC6O.mjs → chunk-T6CNO72J.mjs} +1 -1
- package/dist/chunk-T7DMUVTL.mjs +1714 -0
- package/dist/chunk-UKSUENHK.mjs +997 -0
- package/dist/{chunk-NIWDQTZB.mjs → chunk-ULRPNYAG.mjs} +1220 -2311
- package/dist/{chunk-GNYXCLJS.mjs → chunk-URGLSNKR.mjs} +4 -3
- package/dist/chunk-WQSHDZ7Z.mjs +631 -0
- package/dist/{chunk-RCSXJCI5.mjs → chunk-XBXKPWMR.mjs} +20 -2664
- package/dist/{chunk-RM6FRYEC.mjs → chunk-XSZVS4S3.mjs} +3 -2
- package/dist/chunk-Y2F66IQJ.mjs +74 -0
- package/dist/chunk-YCJLVNWR.mjs +262 -0
- package/dist/{client-CKUtkLz3.d.ts → client-BMK53IR2.d.ts} +3 -3
- package/dist/client.d.ts +8 -8
- package/dist/{config-CSDEPraV.d.ts → config-BUR-7eVQ.d.ts} +2 -2
- package/dist/{config-BuIkeM5C.d.ts → config-UJx1dyX1.d.ts} +2 -2
- package/dist/direct-executor-LD6JW57R.mjs +18 -0
- package/dist/distribution/coordinator/etcd.d.ts +2 -2
- package/dist/distribution/coordinator/in-memory.d.ts +2 -2
- package/dist/distribution.d.ts +8 -8
- package/dist/distribution.mjs +30 -15
- package/dist/durability-wiring-5S32KDDC.mjs +2797 -0
- package/dist/{engine-DMNZJAUq.d.ts → engine-9X6W5naj.d.ts} +2 -2
- package/dist/{index-DzONZUKE.d.ts → index-vCMu8OeQ.d.ts} +1 -1
- package/dist/index.browser.mjs +2 -38121
- package/dist/index.d.ts +23 -9
- package/dist/index.mjs +18 -14
- package/dist/{memory-BPKNx9Tl.d.ts → memory-CV9NAxLY.d.ts} +1 -1
- package/dist/module-WIDYIJLY.mjs +1819 -0
- package/dist/partial-words.mjs +5 -3
- package/dist/pattern.mjs +3 -2
- package/dist/persistence/durability/checkpoint-worker.d.ts +1 -1
- package/dist/persistence/durability/checkpoint-worker.mjs +23 -13
- package/dist/react.d.ts +7 -7
- package/dist/{schema-DgyDijhf.d.ts → schema-DtsgmNfU.d.ts} +1 -1
- package/dist/server.d.ts +10 -10
- package/dist/server.mjs +14 -9
- package/dist/snapshot-6BYSUPWL.mjs +469 -0
- package/dist/{tasks-DGyz586z.d.ts → tasks-BGaZJyoY.d.ts} +1 -1
- package/dist/{thread-EUUNJNKU.mjs → thread-GS65E6RM.mjs} +12 -9
- package/dist/vector/search-worker.d.ts +30 -3
- package/dist/vector/search-worker.mjs +6 -2
- package/dist/workers/entry.mjs +5 -3
- package/dist/write-HLEZTL2L.mjs +233 -0
- package/package.json +11 -7
- package/dist/chunk-67MQF5CV.mjs +0 -15198
- package/dist/direct-executor-NY7ZTZIH.mjs +0 -19
- package/dist/simd-DW5AtJ6B.d.ts +0 -41
|
@@ -0,0 +1,3250 @@
|
|
|
1
|
+
import { nodeThreadId, spawnNodeWorker } from './chunk-HOZW3PKP.mjs';
|
|
2
|
+
import { DEFAULT_SEGMENT_MAX_BYTES, DEFAULT_CHECKPOINT_INTERVAL_MS, DEFAULT_CHECKPOINT_MUTATION_THRESHOLD, DEFAULT_COMPACTION_THRESHOLD, DEFAULT_ASYNC_FLUSH_INTERVAL_MS, WRITES_STALL_ABOVE_UNCHECKPOINTED_BYTES, directoryCheckpointStore, manifestKey, decodeSegmentManifest, readListedFile, readMetadataEnvelope, adapterCheckpointStore, readSegmentManifest, reclaimUnlistedKeys, snapshotBundleKey, manifestReferencedKeys, loadSegmentedCheckpoint, checkpointLastSeqNo, writeMetadataEnvelope, metadataFromConfig, DIRECTORY_LOCK_ATTEMPTS, createSeqOwner, readCurrentManifest, DIRECTORY_LOCK_SETTLE_MS, commitCheckpointManifest, removeCheckpointGarbage, readSegmentFile, isRecord, invalidManifest, deserializePayloadV2, unpackEnvelopeBytes, checkpointPrimaryTerm, SINGLE_NODE_PRIMARY_TERM, writeCheckpointSegments, partitionContentsOf, writeLiveVectors, frameRecord, WHOLE_PARTITION_LARGEST_DOCUMENT_COUNT, WHOLE_PARTITION_MIN_CHANGED_SHARE, captureWholePartition, readCommitMarker, isId, isUint32, isSequenceNumber, readWalSegments, durableRecordMissing, growPartitionsTo, CHECKPOINT_TIMEOUT_RECOVERY_BACKOFF_MS, MAX_SEGMENTS_PER_PARTITION, VECTOR_FILE_ENVELOPE_VERSION, writeSegmentHeader, CHECKPOINT_WORKER_TIMEOUT_MS, CHECKPOINT_WORKER_IDLE_MS, SEGMENT_HEADER_SIZE, documentsOfPayloadV2, nextMarkerSlot, encodeMarkerSlot, markerSlotOffset, LOG_RECORDS_PER_CHECKPOINT, DIRECTORY_IDENTITY_CHECK_INTERVAL_MS, metadataConfigOf, buildEntry } from './chunk-BBDAYUCO.mjs';
|
|
3
|
+
import { applyDeleteEntry, applyIndexEntry } from './chunk-3BXQPIXK.mjs';
|
|
4
|
+
import { encodeDocument, fnv1a, requireValidFieldTypes, openChecksummedEnvelope, HEADER_SIZE, buildSegmentPayload, resolvePartitionInsertOptions, freezeSegmentShared, createSharedFrozenSegment } from './chunk-TBMNPGFO.mjs';
|
|
5
|
+
import { decodeVectorIndexParts, VECTOR_FILE_MAX_VECTORS, deadBytesFor, countDead, decodeVectorFilePayload, decodeVectorGraphPayload, graphPartsOfEarlierPayload } from './chunk-NQW2YXEX.mjs';
|
|
6
|
+
import { NarsilError, ErrorCodes, compareCodePoints, detectRuntime, MIN_M, resolveWorkerEntry, MAX_FILE_BYTES } from './chunk-FHFFWLEK.mjs';
|
|
7
|
+
import { decode } from '@msgpack/msgpack';
|
|
8
|
+
|
|
9
|
+
// src/persistence/durability/node-fs.browser.ts
|
|
10
|
+
function filesystemUnavailable() {
|
|
11
|
+
throw new NarsilError(
|
|
12
|
+
ErrorCodes.CONFIG_INVALID,
|
|
13
|
+
"Filesystem durability requires a Node.js runtime; use the memory or IndexedDB persistence adapters in the browser"
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
async function getFs() {
|
|
17
|
+
return filesystemUnavailable();
|
|
18
|
+
}
|
|
19
|
+
async function getPath() {
|
|
20
|
+
return filesystemUnavailable();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/persistence/durability/lock-holder.browser.ts
|
|
24
|
+
async function holderIsLive(_holder, _self, _lockPath) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
function holdDirectoryOpen(_directory) {
|
|
28
|
+
return () => void 0;
|
|
29
|
+
}
|
|
30
|
+
async function removeStaleLock(_lockPath, _asidePath, _isStale) {
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/persistence/durability/directory-lock.ts
|
|
34
|
+
var LOCK_FILE_NAME = ".narsil.lock";
|
|
35
|
+
var HARD_LINK_UNSUPPORTED_CODES = /* @__PURE__ */ new Set(["EPERM", "ENOTSUP", "EOPNOTSUPP", "ENOSYS"]);
|
|
36
|
+
var lockedDirectories = /* @__PURE__ */ new Set();
|
|
37
|
+
function errnoOf(err) {
|
|
38
|
+
return err?.code;
|
|
39
|
+
}
|
|
40
|
+
function integerOrNull(text) {
|
|
41
|
+
const value = Number.parseInt(text ?? "", 10);
|
|
42
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
43
|
+
}
|
|
44
|
+
function parseHolder(text) {
|
|
45
|
+
const [pidText, threadText, startText, nonceText] = text.split("\n");
|
|
46
|
+
const pid = integerOrNull(pidText);
|
|
47
|
+
if (pid === null || pid <= 0) return null;
|
|
48
|
+
const nonce = nonceText?.trim() ?? "";
|
|
49
|
+
return {
|
|
50
|
+
pid,
|
|
51
|
+
threadId: integerOrNull(threadText),
|
|
52
|
+
processStartMs: integerOrNull(startText),
|
|
53
|
+
nonce: nonce.length > 0 ? nonce : null
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function formatHolder(holder) {
|
|
57
|
+
return `${holder.pid}
|
|
58
|
+
${holder.threadId ?? ""}
|
|
59
|
+
${holder.processStartMs ?? ""}
|
|
60
|
+
${holder.nonce ?? ""}
|
|
61
|
+
`;
|
|
62
|
+
}
|
|
63
|
+
async function readHolder(fs, lockPath) {
|
|
64
|
+
try {
|
|
65
|
+
return parseHolder(await fs.readFile(lockPath, "utf8")) ?? "unreadable";
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (errnoOf(err) === "ENOENT") return "absent";
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function settledHolder(fs, lockPath) {
|
|
72
|
+
const first = await readHolder(fs, lockPath);
|
|
73
|
+
if (first !== "unreadable") return first;
|
|
74
|
+
await new Promise((resolve) => setTimeout(resolve, DIRECTORY_LOCK_SETTLE_MS));
|
|
75
|
+
return readHolder(fs, lockPath);
|
|
76
|
+
}
|
|
77
|
+
function heldElsewhere(root, holder, lockPath) {
|
|
78
|
+
return new NarsilError(
|
|
79
|
+
ErrorCodes.CONFIG_INVALID,
|
|
80
|
+
`The durability directory "${root}" is in use by ${holder}, and only one engine may write a write-ahead log directory. Point this engine at another directory, or shut the other engine down first. Where no engine uses the directory, delete "${lockPath}"`,
|
|
81
|
+
{ directory: root }
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
function holderLabel(holder, self) {
|
|
85
|
+
if (holder.pid !== self.pid) return `process ${holder.pid}`;
|
|
86
|
+
return holder.threadId === self.threadId ? "another copy of Narsil in this thread" : "another thread of this process";
|
|
87
|
+
}
|
|
88
|
+
async function createLockFileExclusively(fs, lockPath, content) {
|
|
89
|
+
try {
|
|
90
|
+
await fs.writeFile(lockPath, content, { flag: "wx" });
|
|
91
|
+
return true;
|
|
92
|
+
} catch (err) {
|
|
93
|
+
if (errnoOf(err) === "EEXIST") return false;
|
|
94
|
+
throw err;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async function createLockFile(fs, lockPath, draftPath, content) {
|
|
98
|
+
await fs.writeFile(draftPath, content, { flag: "w" });
|
|
99
|
+
try {
|
|
100
|
+
await fs.link(draftPath, lockPath);
|
|
101
|
+
return true;
|
|
102
|
+
} catch (err) {
|
|
103
|
+
const code = errnoOf(err);
|
|
104
|
+
if (code === "EEXIST") return false;
|
|
105
|
+
if (code !== void 0 && HARD_LINK_UNSUPPORTED_CODES.has(code)) {
|
|
106
|
+
return createLockFileExclusively(fs, lockPath, content);
|
|
107
|
+
}
|
|
108
|
+
throw err;
|
|
109
|
+
} finally {
|
|
110
|
+
await fs.unlink(draftPath).catch(() => void 0);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function acquire(fs, root, lockPath, self) {
|
|
114
|
+
const draftPath = `${lockPath}.${self.pid}.${self.threadId ?? 0}`;
|
|
115
|
+
const content = formatHolder(self);
|
|
116
|
+
for (let attempt = 0; attempt < DIRECTORY_LOCK_ATTEMPTS; attempt++) {
|
|
117
|
+
if (await createLockFile(fs, lockPath, draftPath, content)) return;
|
|
118
|
+
const holder = await settledHolder(fs, lockPath);
|
|
119
|
+
if (typeof holder === "object" && await holderIsLive()) {
|
|
120
|
+
throw heldElsewhere(root, holderLabel(holder, self), lockPath);
|
|
121
|
+
}
|
|
122
|
+
await removeStaleLock();
|
|
123
|
+
}
|
|
124
|
+
throw heldElsewhere(root, "another process", lockPath);
|
|
125
|
+
}
|
|
126
|
+
async function describeSelf() {
|
|
127
|
+
return {
|
|
128
|
+
pid: process.pid,
|
|
129
|
+
threadId: await nodeThreadId(),
|
|
130
|
+
processStartMs: Math.round(Date.now() - process.uptime() * 1e3),
|
|
131
|
+
nonce: globalThis.crypto.randomUUID()
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async function lockDirectory(root) {
|
|
135
|
+
const fs = await getFs();
|
|
136
|
+
const pathMod = await getPath();
|
|
137
|
+
const resolvedRoot = pathMod.resolve(root);
|
|
138
|
+
const lockPath = pathMod.join(resolvedRoot, LOCK_FILE_NAME);
|
|
139
|
+
const self = await describeSelf();
|
|
140
|
+
async function holds() {
|
|
141
|
+
const holder = await readHolder(fs, lockPath);
|
|
142
|
+
return typeof holder === "object" && holder.nonce === self.nonce;
|
|
143
|
+
}
|
|
144
|
+
async function removeOwnLockFile() {
|
|
145
|
+
if (await holds().catch(() => false)) await fs.unlink(lockPath).catch(() => void 0);
|
|
146
|
+
}
|
|
147
|
+
let directoryKey = null;
|
|
148
|
+
let closeDirectory = () => void 0;
|
|
149
|
+
try {
|
|
150
|
+
await fs.mkdir(resolvedRoot, { recursive: true });
|
|
151
|
+
const stat = await fs.stat(resolvedRoot, { bigint: true });
|
|
152
|
+
const key = `${stat.dev}:${stat.ino}`;
|
|
153
|
+
if (lockedDirectories.has(key)) throw heldElsewhere(root, "another engine in this process", lockPath);
|
|
154
|
+
lockedDirectories.add(key);
|
|
155
|
+
directoryKey = key;
|
|
156
|
+
await acquire(fs, root, lockPath, self);
|
|
157
|
+
closeDirectory = holdDirectoryOpen(resolvedRoot);
|
|
158
|
+
} catch (err) {
|
|
159
|
+
if (directoryKey !== null) lockedDirectories.delete(directoryKey);
|
|
160
|
+
await removeOwnLockFile();
|
|
161
|
+
if (err instanceof NarsilError) throw err;
|
|
162
|
+
throw new NarsilError(
|
|
163
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
164
|
+
`The engine cannot lock the durability directory "${root}"`,
|
|
165
|
+
{ directory: root, cause: err instanceof Error ? err.message : String(err) }
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
const heldKey = directoryKey;
|
|
169
|
+
let released = false;
|
|
170
|
+
return {
|
|
171
|
+
holds,
|
|
172
|
+
async release() {
|
|
173
|
+
if (released) return;
|
|
174
|
+
released = true;
|
|
175
|
+
lockedDirectories.delete(heldKey);
|
|
176
|
+
await removeOwnLockFile();
|
|
177
|
+
closeDirectory();
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/persistence/durability/durable-filesystem.ts
|
|
183
|
+
function validateRelativeKey(key) {
|
|
184
|
+
if (key.length === 0) {
|
|
185
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: empty");
|
|
186
|
+
}
|
|
187
|
+
if (key.includes("\0")) {
|
|
188
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: null byte detected", { key });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function wrapFsyncError(err, key) {
|
|
192
|
+
throw new NarsilError(
|
|
193
|
+
ErrorCodes.PERSISTENCE_FSYNC_FAILED,
|
|
194
|
+
`fsync failed for "${key}"; the write is not acknowledged and the node must recover from the durable log`,
|
|
195
|
+
{ key, cause: err instanceof Error ? err.message : String(err) }
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
async function openReadWriteCreating(filePath, fs) {
|
|
199
|
+
try {
|
|
200
|
+
return await fs.open(filePath, "r+");
|
|
201
|
+
} catch (err) {
|
|
202
|
+
if (err.code !== "ENOENT") {
|
|
203
|
+
throw err;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
return await fs.open(filePath, "wx+");
|
|
208
|
+
} catch (err) {
|
|
209
|
+
if (err.code !== "EEXIST") {
|
|
210
|
+
throw err;
|
|
211
|
+
}
|
|
212
|
+
return fs.open(filePath, "r+");
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
async function writeFully(handle, buffer, key) {
|
|
216
|
+
let offset = 0;
|
|
217
|
+
while (offset < buffer.length) {
|
|
218
|
+
const { bytesWritten } = await handle.write(buffer, offset, buffer.length - offset);
|
|
219
|
+
if (bytesWritten <= 0) {
|
|
220
|
+
throw new NarsilError(
|
|
221
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
222
|
+
`Write to "${key}" made no progress; the file would be truncated and the write is not acknowledged`,
|
|
223
|
+
{ key, written: offset, total: buffer.length }
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
offset += bytesWritten;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
async function fsyncDirectory(dirPath, fs) {
|
|
230
|
+
let handle = null;
|
|
231
|
+
try {
|
|
232
|
+
handle = await fs.open(dirPath, "r");
|
|
233
|
+
await handle.sync();
|
|
234
|
+
} catch (err) {
|
|
235
|
+
const code = err.code;
|
|
236
|
+
if (code === "EISDIR" || code === "EPERM" || code === "EINVAL" || code === "ENOTSUP") {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
wrapFsyncError(err, dirPath);
|
|
240
|
+
} finally {
|
|
241
|
+
if (handle !== null) {
|
|
242
|
+
await handle.close().catch(() => void 0);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function createDurableDirectory(root) {
|
|
247
|
+
if (root.trim().length === 0) {
|
|
248
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "Durable directory requires a non-empty root path");
|
|
249
|
+
}
|
|
250
|
+
async function resolve(key) {
|
|
251
|
+
validateRelativeKey(key);
|
|
252
|
+
const pathMod = await getPath();
|
|
253
|
+
const resolvedBase = pathMod.resolve(root);
|
|
254
|
+
const resolvedPath = pathMod.resolve(root, key);
|
|
255
|
+
if (resolvedPath !== resolvedBase && !resolvedPath.startsWith(resolvedBase + pathMod.sep)) {
|
|
256
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: path traversal detected", { key });
|
|
257
|
+
}
|
|
258
|
+
return { path: resolvedPath, resolvedBase, pathMod };
|
|
259
|
+
}
|
|
260
|
+
async function ensureDir(filePath, pathMod, fs) {
|
|
261
|
+
await fs.mkdir(pathMod.dirname(filePath), { recursive: true });
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
get root() {
|
|
265
|
+
return root;
|
|
266
|
+
},
|
|
267
|
+
lock() {
|
|
268
|
+
return lockDirectory(root);
|
|
269
|
+
},
|
|
270
|
+
async identity() {
|
|
271
|
+
const fs = await getFs();
|
|
272
|
+
const pathMod = await getPath();
|
|
273
|
+
try {
|
|
274
|
+
const stat = await fs.stat(pathMod.resolve(root), { bigint: true });
|
|
275
|
+
return `${stat.dev}:${stat.ino}`;
|
|
276
|
+
} catch (err) {
|
|
277
|
+
if (err.code === "ENOENT") return null;
|
|
278
|
+
throw err;
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
async appendHandle(key) {
|
|
282
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
283
|
+
const fs = await getFs();
|
|
284
|
+
await ensureDir(filePath, pathMod, fs);
|
|
285
|
+
const handle = await fs.open(filePath, "a");
|
|
286
|
+
return {
|
|
287
|
+
async append(bytes) {
|
|
288
|
+
await writeFully(handle, bytes, key);
|
|
289
|
+
},
|
|
290
|
+
async sync() {
|
|
291
|
+
try {
|
|
292
|
+
await handle.sync();
|
|
293
|
+
} catch (err) {
|
|
294
|
+
wrapFsyncError(err, key);
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
async truncate(byteLength) {
|
|
298
|
+
await handle.truncate(byteLength);
|
|
299
|
+
try {
|
|
300
|
+
await handle.sync();
|
|
301
|
+
} catch (err) {
|
|
302
|
+
wrapFsyncError(err, key);
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
async size() {
|
|
306
|
+
const stat = await handle.stat();
|
|
307
|
+
return stat.size;
|
|
308
|
+
},
|
|
309
|
+
async close() {
|
|
310
|
+
await handle.close();
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
},
|
|
314
|
+
async markerHandle(key) {
|
|
315
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
316
|
+
const fs = await getFs();
|
|
317
|
+
await ensureDir(filePath, pathMod, fs);
|
|
318
|
+
const handle = await openReadWriteCreating(filePath, fs);
|
|
319
|
+
return {
|
|
320
|
+
async read() {
|
|
321
|
+
const stat = await handle.stat();
|
|
322
|
+
if (stat.size === 0) {
|
|
323
|
+
return new Uint8Array(0);
|
|
324
|
+
}
|
|
325
|
+
const buffer = Buffer.alloc(stat.size);
|
|
326
|
+
await handle.read(buffer, 0, stat.size, 0);
|
|
327
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
328
|
+
},
|
|
329
|
+
async writeSlot(offset, slot) {
|
|
330
|
+
await handle.write(slot, 0, slot.length, offset);
|
|
331
|
+
try {
|
|
332
|
+
await handle.sync();
|
|
333
|
+
} catch (err) {
|
|
334
|
+
wrapFsyncError(err, key);
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
async close() {
|
|
338
|
+
await handle.close();
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
},
|
|
342
|
+
async syncDirectoryOf(key) {
|
|
343
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
344
|
+
const fs = await getFs();
|
|
345
|
+
await fsyncDirectory(pathMod.dirname(filePath), fs);
|
|
346
|
+
},
|
|
347
|
+
async atomicWrite(key, data) {
|
|
348
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
349
|
+
const fs = await getFs();
|
|
350
|
+
await ensureDir(filePath, pathMod, fs);
|
|
351
|
+
const dir = pathMod.dirname(filePath);
|
|
352
|
+
const tempName = `${pathMod.basename(filePath)}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
353
|
+
const tempPath = pathMod.join(dir, tempName);
|
|
354
|
+
const buffers = Array.isArray(data) ? data : [data];
|
|
355
|
+
let handle = null;
|
|
356
|
+
try {
|
|
357
|
+
handle = await fs.open(tempPath, "w");
|
|
358
|
+
for (const buffer of buffers) {
|
|
359
|
+
await writeFully(handle, buffer, tempPath);
|
|
360
|
+
}
|
|
361
|
+
try {
|
|
362
|
+
await handle.sync();
|
|
363
|
+
} catch (err) {
|
|
364
|
+
wrapFsyncError(err, tempPath);
|
|
365
|
+
}
|
|
366
|
+
await handle.close();
|
|
367
|
+
handle = null;
|
|
368
|
+
await fs.rename(tempPath, filePath);
|
|
369
|
+
await fsyncDirectory(dir, fs);
|
|
370
|
+
} catch (err) {
|
|
371
|
+
if (handle !== null) {
|
|
372
|
+
await handle.close().catch(() => void 0);
|
|
373
|
+
}
|
|
374
|
+
await fs.unlink(tempPath).catch(() => void 0);
|
|
375
|
+
throw err;
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
async read(key) {
|
|
379
|
+
const { path: filePath } = await resolve(key);
|
|
380
|
+
const fs = await getFs();
|
|
381
|
+
try {
|
|
382
|
+
const data = await fs.readFile(filePath);
|
|
383
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
384
|
+
} catch (err) {
|
|
385
|
+
if (err.code === "ENOENT") {
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Failed to read "${key}"`, {
|
|
389
|
+
key,
|
|
390
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
async readRange(key, offset, length) {
|
|
395
|
+
const { path: filePath } = await resolve(key);
|
|
396
|
+
const fs = await getFs();
|
|
397
|
+
let handle = null;
|
|
398
|
+
try {
|
|
399
|
+
handle = await fs.open(filePath, "r");
|
|
400
|
+
const bytes = new Uint8Array(length);
|
|
401
|
+
let filled = 0;
|
|
402
|
+
while (filled < length) {
|
|
403
|
+
const { bytesRead } = await handle.read(bytes, filled, length - filled, offset + filled);
|
|
404
|
+
if (bytesRead === 0) break;
|
|
405
|
+
filled += bytesRead;
|
|
406
|
+
}
|
|
407
|
+
if (filled < length) {
|
|
408
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `"${key}" ends before the bytes a read asked for`, {
|
|
409
|
+
key,
|
|
410
|
+
offset,
|
|
411
|
+
length
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
return bytes;
|
|
415
|
+
} finally {
|
|
416
|
+
await handle?.close();
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
async byteLength(key) {
|
|
420
|
+
const { path: filePath } = await resolve(key);
|
|
421
|
+
const fs = await getFs();
|
|
422
|
+
try {
|
|
423
|
+
return (await fs.stat(filePath)).size;
|
|
424
|
+
} catch (err) {
|
|
425
|
+
if (err.code === "ENOENT") {
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Failed to read the length of "${key}"`, {
|
|
429
|
+
key,
|
|
430
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
},
|
|
434
|
+
async remove(key) {
|
|
435
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
436
|
+
const fs = await getFs();
|
|
437
|
+
try {
|
|
438
|
+
await fs.unlink(filePath);
|
|
439
|
+
} catch (err) {
|
|
440
|
+
if (err.code === "ENOENT") {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_DELETE_FAILED, `Failed to delete "${key}"`, {
|
|
444
|
+
key,
|
|
445
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
await fsyncDirectory(pathMod.dirname(filePath), fs);
|
|
449
|
+
},
|
|
450
|
+
async move(from, to) {
|
|
451
|
+
const source = await resolve(from);
|
|
452
|
+
const target = await resolve(to);
|
|
453
|
+
const fs = await getFs();
|
|
454
|
+
await ensureDir(target.path, target.pathMod, fs);
|
|
455
|
+
await fs.rename(source.path, target.path);
|
|
456
|
+
await fsyncDirectory(target.pathMod.dirname(target.path), fs);
|
|
457
|
+
await fsyncDirectory(source.pathMod.dirname(source.path), fs);
|
|
458
|
+
},
|
|
459
|
+
async list(prefix) {
|
|
460
|
+
const fs = await getFs();
|
|
461
|
+
const pathMod = await getPath();
|
|
462
|
+
const resolvedBase = pathMod.resolve(root);
|
|
463
|
+
const all = await listRecursive(resolvedBase, resolvedBase, pathMod, fs);
|
|
464
|
+
return all.filter((entry) => entry.startsWith(prefix)).sort(compareCodePoints);
|
|
465
|
+
},
|
|
466
|
+
async pathOf(key) {
|
|
467
|
+
const { path: filePath } = await resolve(key);
|
|
468
|
+
return filePath;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
async function listRecursive(dir, base, pathMod, fs) {
|
|
473
|
+
const results = [];
|
|
474
|
+
let entries;
|
|
475
|
+
try {
|
|
476
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
477
|
+
} catch (err) {
|
|
478
|
+
if (err.code === "ENOENT") {
|
|
479
|
+
return results;
|
|
480
|
+
}
|
|
481
|
+
throw err;
|
|
482
|
+
}
|
|
483
|
+
for (const entry of entries) {
|
|
484
|
+
const full = pathMod.join(dir, entry.name);
|
|
485
|
+
if (entry.isDirectory()) {
|
|
486
|
+
results.push(...await listRecursive(full, base, pathMod, fs));
|
|
487
|
+
} else {
|
|
488
|
+
results.push(pathMod.relative(base, full).split(pathMod.sep).join("/"));
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return results;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// src/persistence/durability/archive-checkpoint.ts
|
|
495
|
+
var RESTORE_STAGING_PREFIX = ".restore-";
|
|
496
|
+
function createRestoreStaging(directory, indexStore, indexName) {
|
|
497
|
+
const stagingRoot = `${RESTORE_STAGING_PREFIX}${indexName}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}/`;
|
|
498
|
+
const indexRoot = `${indexName}/`;
|
|
499
|
+
const staged = (key) => {
|
|
500
|
+
if (!key.startsWith(indexRoot)) {
|
|
501
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `A restore of "${indexName}" cannot stage "${key}"`, {
|
|
502
|
+
key
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
return `${stagingRoot}${key.slice(indexRoot.length)}`;
|
|
506
|
+
};
|
|
507
|
+
const unstaged = (key) => `${indexRoot}${key.slice(stagingRoot.length)}`;
|
|
508
|
+
const stagingStore = {
|
|
509
|
+
read: (key) => directory.read(staged(key)),
|
|
510
|
+
write: (key, chunks) => directory.atomicWrite(staged(key), chunks),
|
|
511
|
+
remove: (key) => directory.remove(staged(key)),
|
|
512
|
+
list: async (prefix) => (await directory.list(staged(prefix))).map(unstaged),
|
|
513
|
+
pathOf: (key) => directory.pathOf(staged(key))
|
|
514
|
+
};
|
|
515
|
+
return {
|
|
516
|
+
store: stagingStore,
|
|
517
|
+
async commit() {
|
|
518
|
+
for (const key of await directory.list(stagingRoot)) await directory.move(key, unstaged(key));
|
|
519
|
+
return indexStore;
|
|
520
|
+
},
|
|
521
|
+
async discard() {
|
|
522
|
+
for (const key of await directory.list(stagingRoot)) await directory.remove(key);
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
async function removeAbandonedRestores(directory) {
|
|
527
|
+
for (const key of await directory.list(RESTORE_STAGING_PREFIX)) await directory.remove(key);
|
|
528
|
+
}
|
|
529
|
+
function releaser(held, indexName) {
|
|
530
|
+
let released = false;
|
|
531
|
+
return () => {
|
|
532
|
+
if (released) return;
|
|
533
|
+
released = true;
|
|
534
|
+
const count = (held.get(indexName) ?? 1) - 1;
|
|
535
|
+
if (count <= 0) held.delete(indexName);
|
|
536
|
+
else held.set(indexName, count);
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
function createArchiveCheckpointAccess(deps) {
|
|
540
|
+
const store = directoryCheckpointStore(deps.directory);
|
|
541
|
+
return {
|
|
542
|
+
async openArchiveCheckpoint(indexName) {
|
|
543
|
+
deps.held.set(indexName, (deps.held.get(indexName) ?? 0) + 1);
|
|
544
|
+
const release = releaser(deps.held, indexName);
|
|
545
|
+
try {
|
|
546
|
+
await deps.checkpoint(indexName);
|
|
547
|
+
await deps.checkpoint(indexName);
|
|
548
|
+
const manifestBytes = await deps.directory.read(manifestKey(indexName));
|
|
549
|
+
if (manifestBytes === null) {
|
|
550
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `Index "${indexName}" holds no checkpoint`, {
|
|
551
|
+
indexName
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
const decoded = await decodeSegmentManifest(manifestBytes, indexName);
|
|
555
|
+
if (decoded.kind !== "current") {
|
|
556
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `Index "${indexName}" holds an earlier layout`, {
|
|
557
|
+
indexName
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
return {
|
|
561
|
+
manifestBytes,
|
|
562
|
+
manifest: decoded.manifest,
|
|
563
|
+
readFile: (ref) => readListedFile(store, ref.key, { bytes: ref.bytes, what: "checkpoint file" }),
|
|
564
|
+
readRange: async (ref, offset, length) => {
|
|
565
|
+
const readRange = deps.directory.readRange;
|
|
566
|
+
if (readRange !== void 0) return readRange.call(deps.directory, ref.key, offset, length);
|
|
567
|
+
const whole = await readListedFile(store, ref.key, { bytes: ref.bytes, what: "checkpoint file" });
|
|
568
|
+
return whole.slice(offset, offset + length);
|
|
569
|
+
},
|
|
570
|
+
release
|
|
571
|
+
};
|
|
572
|
+
} catch (error) {
|
|
573
|
+
release();
|
|
574
|
+
throw error;
|
|
575
|
+
}
|
|
576
|
+
},
|
|
577
|
+
stageRestore(indexName) {
|
|
578
|
+
return createRestoreStaging(deps.directory, store, indexName);
|
|
579
|
+
},
|
|
580
|
+
adoptRestoredCheckpoint(indexName, checkpoint) {
|
|
581
|
+
for (const { partitionId, lastSeqNo } of checkpoint) deps.registerPartition(indexName, partitionId, lastSeqNo);
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// src/persistence/durability/checkpoint.ts
|
|
587
|
+
function walPrefix(indexName, partitionId) {
|
|
588
|
+
return `${indexName}/wal/${partitionId}/`;
|
|
589
|
+
}
|
|
590
|
+
async function truncateCoveredSegments(directory, indexName, checkpoint) {
|
|
591
|
+
for (const { partitionId, lastSeqNo } of checkpoint) {
|
|
592
|
+
const prefix = walPrefix(indexName, partitionId);
|
|
593
|
+
const markerBytes = await directory.read(`${prefix}commit`);
|
|
594
|
+
const marker = markerBytes === null ? null : readCommitMarker(markerBytes);
|
|
595
|
+
if (marker === null) {
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
const activeSegmentSeqNo = marker.state.activeSegmentSeqNo;
|
|
599
|
+
const segments = await directory.list(prefix);
|
|
600
|
+
const startSeqNos = segments.map((key) => parseSegmentStartSeqNo(key, prefix)).filter((value) => value !== null).sort((a, b) => a - b);
|
|
601
|
+
for (let i = 0; i < startSeqNos.length; i += 1) {
|
|
602
|
+
const start = startSeqNos[i];
|
|
603
|
+
if (start >= activeSegmentSeqNo) {
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
const nextStart = startSeqNos[i + 1];
|
|
607
|
+
const segmentCoversBeyondCheckpoint = nextStart === void 0 || nextStart > lastSeqNo + 1;
|
|
608
|
+
if (segmentCoversBeyondCheckpoint) {
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
await directory.remove(`${prefix}${formatSegmentStart(start)}`);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
async function reclaimWalBeyondCount(directory, indexName, partitionCount, partitions, onCloseError) {
|
|
616
|
+
for (const [partitionId, partition] of [...partitions]) {
|
|
617
|
+
if (partitionId < partitionCount) continue;
|
|
618
|
+
try {
|
|
619
|
+
await partition.walWriter.close();
|
|
620
|
+
} catch (err) {
|
|
621
|
+
onCloseError(err instanceof Error ? err : new Error(String(err)));
|
|
622
|
+
}
|
|
623
|
+
partitions.delete(partitionId);
|
|
624
|
+
}
|
|
625
|
+
const walRoot = `${indexName}/wal/`;
|
|
626
|
+
for (const key of await directory.list(walRoot)) {
|
|
627
|
+
const partitionId = walKeyPartitionId(key, walRoot);
|
|
628
|
+
if (partitionId === null || partitionId < partitionCount) continue;
|
|
629
|
+
await directory.remove(key);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
function walKeyPartitionId(key, walRoot) {
|
|
633
|
+
const tail = key.slice(walRoot.length);
|
|
634
|
+
const separator = tail.indexOf("/");
|
|
635
|
+
if (separator <= 0) {
|
|
636
|
+
return null;
|
|
637
|
+
}
|
|
638
|
+
const segment = tail.slice(0, separator);
|
|
639
|
+
if (!/^\d+$/.test(segment)) {
|
|
640
|
+
return null;
|
|
641
|
+
}
|
|
642
|
+
const value = Number.parseInt(segment, 10);
|
|
643
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
644
|
+
}
|
|
645
|
+
function parseSegmentStartSeqNo(key, prefix) {
|
|
646
|
+
if (!key.startsWith(prefix)) {
|
|
647
|
+
return null;
|
|
648
|
+
}
|
|
649
|
+
const tail = key.slice(prefix.length);
|
|
650
|
+
if (!/^\d{16}$/.test(tail)) {
|
|
651
|
+
return null;
|
|
652
|
+
}
|
|
653
|
+
const value = Number.parseInt(tail, 10);
|
|
654
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
655
|
+
}
|
|
656
|
+
function formatSegmentStart(startSeqNo) {
|
|
657
|
+
return startSeqNo.toString().padStart(16, "0");
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// src/persistence/durability/recovery.ts
|
|
661
|
+
async function listPersistedIndexes(directory) {
|
|
662
|
+
const metaKeys = await directory.list("");
|
|
663
|
+
const names = [];
|
|
664
|
+
for (const key of metaKeys) {
|
|
665
|
+
if (key.endsWith("/meta")) {
|
|
666
|
+
names.push(key.slice(0, -"/meta".length));
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
return names;
|
|
670
|
+
}
|
|
671
|
+
async function loadMetadata(directory, indexName) {
|
|
672
|
+
const bytes = await directory.read(`${indexName}/meta`);
|
|
673
|
+
if (bytes === null) {
|
|
674
|
+
return null;
|
|
675
|
+
}
|
|
676
|
+
const { metadata } = await readMetadataEnvelope(bytes);
|
|
677
|
+
return metadata;
|
|
678
|
+
}
|
|
679
|
+
async function replayWal(directory, indexName, partitionId, fromSeqNoExclusive, deps) {
|
|
680
|
+
const read = await readWalSegments(directory, indexName, partitionId);
|
|
681
|
+
if (read === null) {
|
|
682
|
+
return { highestSeqNo: fromSeqNoExclusive, highestPrimaryTerm: 0 };
|
|
683
|
+
}
|
|
684
|
+
const { marker, segments, durableEntries, activeTail, highestReadFromWal } = read;
|
|
685
|
+
if (Math.max(fromSeqNoExclusive, highestReadFromWal) < marker.state.highestDurableSeqNo) {
|
|
686
|
+
throw durableRecordMissing(indexName, partitionId, highestReadFromWal, marker);
|
|
687
|
+
}
|
|
688
|
+
let highestSeqNo = fromSeqNoExclusive;
|
|
689
|
+
let highestPrimaryTerm = 0;
|
|
690
|
+
const replay = (entries) => {
|
|
691
|
+
for (const entry of entries) {
|
|
692
|
+
if (entry.seqNo <= highestSeqNo) {
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
applyEntry(entry, deps);
|
|
696
|
+
highestSeqNo = entry.seqNo;
|
|
697
|
+
if (entry.primaryTerm > highestPrimaryTerm) {
|
|
698
|
+
highestPrimaryTerm = entry.primaryTerm;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
replay(durableEntries);
|
|
703
|
+
if (activeTail !== null) {
|
|
704
|
+
replay(activeTail.entries);
|
|
705
|
+
}
|
|
706
|
+
await deleteOrphanSegments(directory, segments, marker.state.activeSegmentSeqNo);
|
|
707
|
+
if (activeTail !== null && activeTail.cleanEnd < activeTail.segmentLength) {
|
|
708
|
+
await truncateSegmentTail(directory, activeTail.key, activeTail.cleanEnd);
|
|
709
|
+
}
|
|
710
|
+
return { highestSeqNo, highestPrimaryTerm };
|
|
711
|
+
}
|
|
712
|
+
async function deleteOrphanSegments(directory, segments, activeSegmentSeqNo) {
|
|
713
|
+
for (const { key, startSeqNo } of segments) {
|
|
714
|
+
if (startSeqNo > activeSegmentSeqNo) {
|
|
715
|
+
await directory.remove(key);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
async function truncateSegmentTail(directory, key, durableByteLength) {
|
|
720
|
+
const cleanLength = Math.max(durableByteLength, SEGMENT_HEADER_SIZE);
|
|
721
|
+
const handle = await directory.appendHandle(key);
|
|
722
|
+
try {
|
|
723
|
+
const currentSize = await handle.size();
|
|
724
|
+
if (cleanLength < currentSize) {
|
|
725
|
+
await handle.truncate(cleanLength);
|
|
726
|
+
}
|
|
727
|
+
} finally {
|
|
728
|
+
await handle.close();
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
function applyEntry(entry, deps) {
|
|
732
|
+
if (entry.operation === "DELETE") {
|
|
733
|
+
applyDeleteEntry(entry, deps.manager, deps.vectorIndexes);
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
applyIndexEntry(entry, deps.manager, deps.vectorFieldPaths, deps.vectorIndexes);
|
|
737
|
+
}
|
|
738
|
+
function snapshotCheckpointFor(checkpoint, partitionId) {
|
|
739
|
+
return checkpointLastSeqNo(checkpoint, partitionId);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// src/persistence/durability/checkpoint-capture.ts
|
|
743
|
+
function whileNoMutationApplies(partitions, capture2) {
|
|
744
|
+
return new Promise((resolve, reject) => {
|
|
745
|
+
let release = () => void 0;
|
|
746
|
+
const released = new Promise((settle) => {
|
|
747
|
+
release = settle;
|
|
748
|
+
});
|
|
749
|
+
function captureNow() {
|
|
750
|
+
try {
|
|
751
|
+
resolve(capture2());
|
|
752
|
+
} catch (error) {
|
|
753
|
+
reject(error);
|
|
754
|
+
} finally {
|
|
755
|
+
release();
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
let waiting = partitions.length;
|
|
759
|
+
if (waiting === 0) {
|
|
760
|
+
captureNow();
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
for (const partition of partitions) {
|
|
764
|
+
partition.appendChain = partition.appendChain.then(() => {
|
|
765
|
+
waiting -= 1;
|
|
766
|
+
if (waiting === 0) captureNow();
|
|
767
|
+
return released;
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
async function makeEveryAppliedMutationDurable(indexState, markFatal) {
|
|
773
|
+
for (const partition of [...indexState.partitions.values()]) {
|
|
774
|
+
await partition.appendChain;
|
|
775
|
+
if (partition.failed !== null) throw partition.failed;
|
|
776
|
+
try {
|
|
777
|
+
await partition.walWriter.commit();
|
|
778
|
+
} catch (error) {
|
|
779
|
+
partition.failed = error instanceof Error ? error : new Error(String(error));
|
|
780
|
+
markFatal(partition.failed);
|
|
781
|
+
throw partition.failed;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
function noWholePartitions() {
|
|
786
|
+
return { contents: [], captured: [] };
|
|
787
|
+
}
|
|
788
|
+
function wholePartitionsWhereMostChanged(manager, rules) {
|
|
789
|
+
const { priorCheckpoint, priorPartitionIds, everyPartition, aWorkerCanSerialise } = rules;
|
|
790
|
+
return (targets) => {
|
|
791
|
+
const whole = { contents: [], captured: [] };
|
|
792
|
+
const changed = targets.filter(
|
|
793
|
+
(target) => everyPartition || target.lastSeqNo !== snapshotCheckpointFor(priorCheckpoint, target.partitionId)
|
|
794
|
+
);
|
|
795
|
+
const mostOfEachChanged = changed.every((target) => {
|
|
796
|
+
const records = target.lastSeqNo - snapshotCheckpointFor(priorCheckpoint, target.partitionId);
|
|
797
|
+
const documents = manager.getPartition(target.partitionId).count();
|
|
798
|
+
return documents <= WHOLE_PARTITION_LARGEST_DOCUMENT_COUNT && records >= documents * WHOLE_PARTITION_MIN_CHANGED_SHARE;
|
|
799
|
+
});
|
|
800
|
+
const logMatchesMemory = targets.length === 1 && priorPartitionIds.every((partitionId) => partitionId === 0);
|
|
801
|
+
if (!everyPartition && !(mostOfEachChanged && logMatchesMemory)) return whole;
|
|
802
|
+
for (const { partitionId } of changed) {
|
|
803
|
+
const captured = aWorkerCanSerialise ? captureWholePartition(manager, partitionId) : null;
|
|
804
|
+
if (captured !== null) {
|
|
805
|
+
whole.captured.push(captured);
|
|
806
|
+
} else if (everyPartition || !aWorkerCanSerialise) {
|
|
807
|
+
whole.contents.push(partitionContentsOf(manager, partitionId));
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
return whole;
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
async function captureCheckpoint(indexState, manager, vectorIndexes, wholePartitionsOf = noWholePartitions, priorCheckpointThatLimitsTheRecords, listedVectorFields = []) {
|
|
814
|
+
for (const vectorIndex of vectorIndexes.values()) await vectorIndex.completeGraph();
|
|
815
|
+
return whileNoMutationApplies([...indexState.partitions.values()], () => {
|
|
816
|
+
const documentCount = manager.countDocuments();
|
|
817
|
+
const targets = [];
|
|
818
|
+
let recordsLeftInLog = 0;
|
|
819
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
820
|
+
const partition = indexState.partitions.get(partitionId);
|
|
821
|
+
const appliedSeqNo = partition?.appliedSeqNo ?? 0;
|
|
822
|
+
const lastSeqNo = priorCheckpointThatLimitsTheRecords === void 0 ? appliedSeqNo : Math.min(
|
|
823
|
+
appliedSeqNo,
|
|
824
|
+
snapshotCheckpointFor(priorCheckpointThatLimitsTheRecords, partitionId) + LOG_RECORDS_PER_CHECKPOINT
|
|
825
|
+
);
|
|
826
|
+
recordsLeftInLog += appliedSeqNo - lastSeqNo;
|
|
827
|
+
targets.push({
|
|
828
|
+
partitionId,
|
|
829
|
+
lastSeqNo,
|
|
830
|
+
primaryTerm: partition?.seqOwner.primaryTerm ?? SINGLE_NODE_PRIMARY_TERM
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
const vectorPlans = /* @__PURE__ */ new Map();
|
|
834
|
+
for (const [fieldPath, vectorIndex] of vectorIndexes) {
|
|
835
|
+
const listed = listedVectorFields.find((ref) => ref.fieldPath === fieldPath);
|
|
836
|
+
const listedKeys = listed === void 0 ? null : listed.files.map((file) => file.key);
|
|
837
|
+
vectorPlans.set(fieldPath, vectorIndex.planCheckpoint(listedKeys));
|
|
838
|
+
}
|
|
839
|
+
const memoryMatchesTheTargets = recordsLeftInLog === 0;
|
|
840
|
+
const wholePartitions = memoryMatchesTheTargets ? wholePartitionsOf(targets) : noWholePartitions();
|
|
841
|
+
return { targets, documentCount, vectorPlans, wholePartitions, recordsLeftInLog };
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
async function writeCapturedVectors(store, indexName, capture2, priorManifest, rewriteUnchanged) {
|
|
845
|
+
if (capture2.vectorPlans.size === 0) return { vectors: [], layouts: [], written: [] };
|
|
846
|
+
const priorVectors = priorManifest?.vectors ?? [];
|
|
847
|
+
const priorCheckpoint = priorManifest?.checkpoint ?? [];
|
|
848
|
+
const noPartitionChanged = capture2.targets.every(
|
|
849
|
+
(target) => target.lastSeqNo === snapshotCheckpointFor(priorCheckpoint, target.partitionId)
|
|
850
|
+
);
|
|
851
|
+
const everyFieldWritten = [...capture2.vectorPlans.keys()].every(
|
|
852
|
+
(fieldPath) => priorVectors.some((ref) => ref.fieldPath === fieldPath)
|
|
853
|
+
);
|
|
854
|
+
if (!rewriteUnchanged && everyFieldWritten && noPartitionChanged) {
|
|
855
|
+
return { vectors: priorVectors, layouts: [], written: [] };
|
|
856
|
+
}
|
|
857
|
+
const written = await writeLiveVectors({ store, indexName, plans: capture2.vectorPlans, priorVectors });
|
|
858
|
+
return { vectors: written.refs, layouts: written.layouts, written: written.written };
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// src/persistence/durability/checkpoint-worker-dispatch.ts
|
|
862
|
+
var workerUsable = true;
|
|
863
|
+
var pooledWorker = null;
|
|
864
|
+
var workerBusy = false;
|
|
865
|
+
var spawnedWorkerCount = 0;
|
|
866
|
+
var workerIdleMs = CHECKPOINT_WORKER_IDLE_MS;
|
|
867
|
+
var idleTimer = null;
|
|
868
|
+
async function spawnWorker() {
|
|
869
|
+
const entryPoint = resolveWorkerEntry(
|
|
870
|
+
import.meta.url,
|
|
871
|
+
/\/src\/persistence\/durability\/[^/]+$/,
|
|
872
|
+
"persistence/durability/checkpoint-worker.mjs"
|
|
873
|
+
);
|
|
874
|
+
if (entryPoint === null) return null;
|
|
875
|
+
try {
|
|
876
|
+
const worker = await spawnNodeWorker(new URL(entryPoint));
|
|
877
|
+
if (worker === null) {
|
|
878
|
+
return null;
|
|
879
|
+
}
|
|
880
|
+
if (typeof worker.unref === "function") {
|
|
881
|
+
worker.unref();
|
|
882
|
+
}
|
|
883
|
+
worker.on("error", () => discardWorker(worker));
|
|
884
|
+
worker.on("exit", () => discardWorker(worker));
|
|
885
|
+
spawnedWorkerCount += 1;
|
|
886
|
+
return worker;
|
|
887
|
+
} catch {
|
|
888
|
+
return null;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
function discardWorker(worker) {
|
|
892
|
+
if (pooledWorker === worker) {
|
|
893
|
+
pooledWorker = null;
|
|
894
|
+
}
|
|
895
|
+
try {
|
|
896
|
+
void worker.terminate();
|
|
897
|
+
} catch {
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
function unrefTimer(timer) {
|
|
901
|
+
if (typeof timer.unref === "function") {
|
|
902
|
+
timer.unref();
|
|
903
|
+
}
|
|
904
|
+
return timer;
|
|
905
|
+
}
|
|
906
|
+
function runWorker(worker, request) {
|
|
907
|
+
return new Promise((resolve) => {
|
|
908
|
+
let settled = false;
|
|
909
|
+
let timeoutId = armSilenceTimeout();
|
|
910
|
+
function armSilenceTimeout() {
|
|
911
|
+
return unrefTimer(setTimeout(() => settle({ written: null, timedOut: true }, true), CHECKPOINT_WORKER_TIMEOUT_MS));
|
|
912
|
+
}
|
|
913
|
+
const onMessage = (msg) => {
|
|
914
|
+
const response = msg;
|
|
915
|
+
if (response.type === "heartbeat") {
|
|
916
|
+
clearTimeout(timeoutId);
|
|
917
|
+
timeoutId = armSilenceTimeout();
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
if (response.type === "success") {
|
|
921
|
+
settle({ written: response.segments, timedOut: false }, false);
|
|
922
|
+
} else {
|
|
923
|
+
settle({ written: null, timedOut: false }, true);
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
const onError = () => {
|
|
927
|
+
settle({ written: null, timedOut: false }, true);
|
|
928
|
+
};
|
|
929
|
+
const onExit = () => {
|
|
930
|
+
settle({ written: null, timedOut: false }, true);
|
|
931
|
+
};
|
|
932
|
+
function settle(outcome, discard) {
|
|
933
|
+
if (settled) {
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
settled = true;
|
|
937
|
+
clearTimeout(timeoutId);
|
|
938
|
+
worker.off("message", onMessage);
|
|
939
|
+
worker.off("error", onError);
|
|
940
|
+
worker.off("exit", onExit);
|
|
941
|
+
if (discard) {
|
|
942
|
+
discardWorker(worker);
|
|
943
|
+
}
|
|
944
|
+
resolve(outcome);
|
|
945
|
+
}
|
|
946
|
+
worker.on("message", onMessage);
|
|
947
|
+
worker.on("error", onError);
|
|
948
|
+
worker.on("exit", onExit);
|
|
949
|
+
try {
|
|
950
|
+
worker.postMessage(request);
|
|
951
|
+
} catch {
|
|
952
|
+
settle({ written: null, timedOut: false }, true);
|
|
953
|
+
}
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
function delay(ms) {
|
|
957
|
+
return new Promise((resolve) => {
|
|
958
|
+
unrefTimer(setTimeout(resolve, ms));
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
function checkpointWorkerIsUsable() {
|
|
962
|
+
const runtime = detectRuntime();
|
|
963
|
+
return runtime.supportsWorkerThreads && runtime.supportsFileSystem && workerUsable;
|
|
964
|
+
}
|
|
965
|
+
async function runCheckpointOnWorker(request) {
|
|
966
|
+
const runtime = detectRuntime();
|
|
967
|
+
if (!runtime.supportsWorkerThreads || !runtime.supportsFileSystem || !workerUsable) {
|
|
968
|
+
return null;
|
|
969
|
+
}
|
|
970
|
+
if (workerBusy) {
|
|
971
|
+
return null;
|
|
972
|
+
}
|
|
973
|
+
workerBusy = true;
|
|
974
|
+
cancelIdleRetirement();
|
|
975
|
+
try {
|
|
976
|
+
if (pooledWorker === null) {
|
|
977
|
+
pooledWorker = await spawnWorker();
|
|
978
|
+
}
|
|
979
|
+
const worker = pooledWorker;
|
|
980
|
+
if (worker === null) {
|
|
981
|
+
workerUsable = false;
|
|
982
|
+
return null;
|
|
983
|
+
}
|
|
984
|
+
const outcome = await runWorker(worker, request);
|
|
985
|
+
if (outcome.written !== null) {
|
|
986
|
+
return outcome.written;
|
|
987
|
+
}
|
|
988
|
+
if (outcome.timedOut) {
|
|
989
|
+
await delay(CHECKPOINT_TIMEOUT_RECOVERY_BACKOFF_MS);
|
|
990
|
+
}
|
|
991
|
+
return null;
|
|
992
|
+
} finally {
|
|
993
|
+
workerBusy = false;
|
|
994
|
+
retireWorkerOnceIdle();
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
function cancelIdleRetirement() {
|
|
998
|
+
if (idleTimer !== null) {
|
|
999
|
+
clearTimeout(idleTimer);
|
|
1000
|
+
idleTimer = null;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
function retireWorkerOnceIdle() {
|
|
1004
|
+
cancelIdleRetirement();
|
|
1005
|
+
idleTimer = unrefTimer(
|
|
1006
|
+
setTimeout(() => {
|
|
1007
|
+
idleTimer = null;
|
|
1008
|
+
if (!workerBusy) terminateCheckpointWorker();
|
|
1009
|
+
}, workerIdleMs)
|
|
1010
|
+
);
|
|
1011
|
+
}
|
|
1012
|
+
function terminateCheckpointWorker() {
|
|
1013
|
+
cancelIdleRetirement();
|
|
1014
|
+
const worker = pooledWorker;
|
|
1015
|
+
if (worker === null) {
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
pooledWorker = null;
|
|
1019
|
+
try {
|
|
1020
|
+
void worker.terminate();
|
|
1021
|
+
} catch {
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// src/persistence/durability/checkpoint-write.ts
|
|
1026
|
+
function workerCanWriteEverySegment(input) {
|
|
1027
|
+
if (!input.canOffload || input.wholePartitions.length > 0) return false;
|
|
1028
|
+
const analysesTextInProcessOnly = input.metadata.tokenizer !== void 0 || input.metadata.stopWords !== void 0;
|
|
1029
|
+
if (!analysesTextInProcessOnly) return true;
|
|
1030
|
+
return input.targets.every(
|
|
1031
|
+
(target) => input.capturedPartitions.some((captured) => captured.partitionId === target.partitionId)
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
async function writeIndexCheckpointSegments(input) {
|
|
1035
|
+
const { directory, metadata, targets, compactionThreshold, prior, wholePartitions, capturedPartitions } = input;
|
|
1036
|
+
const offloaded = workerCanWriteEverySegment(input) ? await runCheckpointOnWorker({
|
|
1037
|
+
root: directory.root,
|
|
1038
|
+
metadata,
|
|
1039
|
+
targets,
|
|
1040
|
+
compactionThreshold,
|
|
1041
|
+
prior,
|
|
1042
|
+
capturedPartitions: [...capturedPartitions]
|
|
1043
|
+
}) : null;
|
|
1044
|
+
if (offloaded !== null) {
|
|
1045
|
+
return offloaded;
|
|
1046
|
+
}
|
|
1047
|
+
return writeCheckpointSegments({
|
|
1048
|
+
store: directoryCheckpointStore(directory),
|
|
1049
|
+
directory,
|
|
1050
|
+
metadata,
|
|
1051
|
+
targets,
|
|
1052
|
+
compactionThreshold,
|
|
1053
|
+
prior,
|
|
1054
|
+
wholePartitions,
|
|
1055
|
+
capturedPartitions
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
// src/persistence/durability/segment/vector-adoption.ts
|
|
1060
|
+
function recordWrittenVectors(vectorIndexes, written) {
|
|
1061
|
+
for (const field of written) {
|
|
1062
|
+
vectorIndexes.get(field.fieldPath)?.recordCheckpoint(field.plan, field.files);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
async function adoptVectorLayouts(store, vectorIndexes, layouts) {
|
|
1066
|
+
const pathOf = store.pathOf;
|
|
1067
|
+
if (pathOf === void 0) return;
|
|
1068
|
+
for (const layout of layouts) {
|
|
1069
|
+
const vecIndex = vectorIndexes.get(layout.fieldPath);
|
|
1070
|
+
if (vecIndex === void 0 || vecIndex.storage !== "disk") continue;
|
|
1071
|
+
const path = await pathOf.call(store, layout.key);
|
|
1072
|
+
await vecIndex.adoptDiskLayout({
|
|
1073
|
+
path,
|
|
1074
|
+
docIds: layout.docIds,
|
|
1075
|
+
dead: layout.dead,
|
|
1076
|
+
vectorsOffset: layout.vectorsOffset
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
async function garbageThatNoFieldStillReads(store, vectorIndexes, keys) {
|
|
1081
|
+
const pathOf = store.pathOf;
|
|
1082
|
+
if (pathOf === void 0) return [...keys];
|
|
1083
|
+
const read = /* @__PURE__ */ new Set();
|
|
1084
|
+
for (const vectorIndex of vectorIndexes.values()) {
|
|
1085
|
+
for (const path of vectorIndex.vectorFilesInUse()) read.add(path);
|
|
1086
|
+
}
|
|
1087
|
+
if (read.size === 0) return [...keys];
|
|
1088
|
+
const unread = [];
|
|
1089
|
+
for (const key of keys) {
|
|
1090
|
+
if (!read.has(await pathOf.call(store, key))) unread.push(key);
|
|
1091
|
+
}
|
|
1092
|
+
return unread;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
// src/persistence/durability/write-stall.ts
|
|
1096
|
+
function checkpointIsDue(indexState, mutationThreshold, stallAboveBytes = WRITES_STALL_ABOVE_UNCHECKPOINTED_BYTES) {
|
|
1097
|
+
return indexState.mutationsSinceCheckpoint >= mutationThreshold || indexState.documentBytesSinceCheckpoint >= stallAboveBytes;
|
|
1098
|
+
}
|
|
1099
|
+
async function stallWhileCheckpointsFallBehind(indexState, stallAboveBytes = WRITES_STALL_ABOVE_UNCHECKPOINTED_BYTES) {
|
|
1100
|
+
while (indexState.documentBytesSinceCheckpoint >= stallAboveBytes && indexState.checkpointInFlight !== null && !indexState.unloading) {
|
|
1101
|
+
await new Promise((resume) => {
|
|
1102
|
+
indexState.stalledWrites.push(resume);
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
function noteCheckpointCommitted(indexState, recordsBeyondTheCheckpoint) {
|
|
1107
|
+
const counted = indexState.mutationsSinceCheckpoint;
|
|
1108
|
+
const shareLeft = counted <= 0 ? 0 : Math.min(1, recordsBeyondTheCheckpoint / counted);
|
|
1109
|
+
indexState.documentBytesSinceCheckpoint = Math.round(indexState.documentBytesSinceCheckpoint * shareLeft);
|
|
1110
|
+
indexState.mutationsSinceCheckpoint = recordsBeyondTheCheckpoint;
|
|
1111
|
+
releaseStalledWrites(indexState);
|
|
1112
|
+
}
|
|
1113
|
+
function releaseStalledWrites(indexState) {
|
|
1114
|
+
const waiting = indexState.stalledWrites.splice(0);
|
|
1115
|
+
for (const resume of waiting) resume();
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
// src/persistence/durability/checkpoint-run.ts
|
|
1119
|
+
async function bothOrTheFirstFailure(first, second) {
|
|
1120
|
+
const [firstSettled, secondSettled] = await Promise.allSettled([first, second]);
|
|
1121
|
+
if (firstSettled.status === "rejected") throw firstSettled.reason;
|
|
1122
|
+
if (secondSettled.status === "rejected") throw secondSettled.reason;
|
|
1123
|
+
return [firstSettled.value, secondSettled.value];
|
|
1124
|
+
}
|
|
1125
|
+
async function runDurableCheckpoint(input) {
|
|
1126
|
+
try {
|
|
1127
|
+
let recordsLeftInLog = await writeOneBoundedCheckpoint(input);
|
|
1128
|
+
while (recordsLeftInLog > 0 && input.mayContinue()) {
|
|
1129
|
+
recordsLeftInLog = await writeOneBoundedCheckpoint(input);
|
|
1130
|
+
}
|
|
1131
|
+
} catch (err) {
|
|
1132
|
+
if (!isSystemError(err)) throw err;
|
|
1133
|
+
const error = new NarsilError(
|
|
1134
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1135
|
+
`The engine cannot write the checkpoint of index "${input.indexName}" to the durability directory "${input.directory.root}"`,
|
|
1136
|
+
{
|
|
1137
|
+
indexName: input.indexName,
|
|
1138
|
+
directory: input.directory.root,
|
|
1139
|
+
cause: err.message
|
|
1140
|
+
}
|
|
1141
|
+
);
|
|
1142
|
+
input.markFatal(error);
|
|
1143
|
+
throw error;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
function isSystemError(err) {
|
|
1147
|
+
return err instanceof Error && !(err instanceof NarsilError) && typeof err.code === "string";
|
|
1148
|
+
}
|
|
1149
|
+
async function writeOneBoundedCheckpoint(input) {
|
|
1150
|
+
const manager = input.hooks.getManager(input.indexName);
|
|
1151
|
+
if (manager === void 0) {
|
|
1152
|
+
return 0;
|
|
1153
|
+
}
|
|
1154
|
+
const metadata = input.hooks.buildMetadata(input.indexName);
|
|
1155
|
+
if (metadata === void 0) {
|
|
1156
|
+
return 0;
|
|
1157
|
+
}
|
|
1158
|
+
const vectorIndexes = input.hooks.getVectorIndexes(input.indexName);
|
|
1159
|
+
const store = directoryCheckpointStore(input.directory);
|
|
1160
|
+
const priorManifest = await readCurrentManifest(store, input.indexName);
|
|
1161
|
+
const capture2 = await captureCheckpoint(
|
|
1162
|
+
input.indexState,
|
|
1163
|
+
manager,
|
|
1164
|
+
vectorIndexes,
|
|
1165
|
+
wholePartitionsWhereMostChanged(manager, {
|
|
1166
|
+
priorCheckpoint: priorManifest?.checkpoint ?? [],
|
|
1167
|
+
priorPartitionIds: (priorManifest?.partitions ?? []).map((partition) => partition.partitionId),
|
|
1168
|
+
everyPartition: input.fromMemory,
|
|
1169
|
+
aWorkerCanSerialise: input.canOffload && checkpointWorkerIsUsable()
|
|
1170
|
+
}),
|
|
1171
|
+
input.fromMemory ? void 0 : priorManifest?.checkpoint ?? [],
|
|
1172
|
+
priorManifest?.vectors ?? []
|
|
1173
|
+
);
|
|
1174
|
+
const { targets, documentCount } = capture2;
|
|
1175
|
+
await makeEveryAppliedMutationDurable(input.indexState, input.markFatal);
|
|
1176
|
+
const [liveVectors, segments] = await bothOrTheFirstFailure(
|
|
1177
|
+
writeCapturedVectors(store, input.indexName, capture2, priorManifest, input.fromMemory),
|
|
1178
|
+
writeIndexCheckpointSegments({
|
|
1179
|
+
directory: input.directory,
|
|
1180
|
+
metadata,
|
|
1181
|
+
targets,
|
|
1182
|
+
compactionThreshold: input.compactionThreshold,
|
|
1183
|
+
canOffload: input.canOffload,
|
|
1184
|
+
prior: priorManifest,
|
|
1185
|
+
wholePartitions: capture2.wholePartitions.contents,
|
|
1186
|
+
capturedPartitions: capture2.wholePartitions.captured
|
|
1187
|
+
})
|
|
1188
|
+
);
|
|
1189
|
+
await makeEveryAppliedMutationDurable(input.indexState, input.markFatal);
|
|
1190
|
+
const written = await commitCheckpointManifest(store, metadata, segments, liveVectors.vectors);
|
|
1191
|
+
recordWrittenVectors(vectorIndexes, liveVectors.written);
|
|
1192
|
+
await adoptVectorLayouts(store, vectorIndexes, liveVectors.layouts);
|
|
1193
|
+
if (!input.holdsCheckpointFiles()) {
|
|
1194
|
+
await removeCheckpointGarbage(store, await garbageThatNoFieldStillReads(store, vectorIndexes, written.garbage));
|
|
1195
|
+
}
|
|
1196
|
+
const checkpointDocumentCount = documentCount;
|
|
1197
|
+
await input.queueMetadataWrite(input.indexName, async () => {
|
|
1198
|
+
const checkpointMetadata = input.hooks.buildMetadata(input.indexName, checkpointDocumentCount);
|
|
1199
|
+
if (checkpointMetadata === void 0) {
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
const bytes = await writeMetadataEnvelope(checkpointMetadata, { checksum: true });
|
|
1203
|
+
await input.directory.atomicWrite(`${input.indexName}/meta`, bytes);
|
|
1204
|
+
input.hooks.recordCheckpoint?.(input.indexName, checkpointDocumentCount, manager.partitionCount);
|
|
1205
|
+
});
|
|
1206
|
+
await truncateCoveredSegments(input.directory, input.indexName, targets);
|
|
1207
|
+
await reclaimWalBeyondCount(
|
|
1208
|
+
input.directory,
|
|
1209
|
+
input.indexName,
|
|
1210
|
+
manager.partitionCount,
|
|
1211
|
+
input.indexState.partitions,
|
|
1212
|
+
input.markFatal
|
|
1213
|
+
);
|
|
1214
|
+
const beyondTheCheckpoint = recordsBeyond(input.indexState, targets);
|
|
1215
|
+
noteCheckpointCommitted(input.indexState, beyondTheCheckpoint);
|
|
1216
|
+
const anotherIsDue = input.mutationThreshold > 0 && beyondTheCheckpoint >= input.mutationThreshold;
|
|
1217
|
+
return anotherIsDue ? beyondTheCheckpoint : capture2.recordsLeftInLog;
|
|
1218
|
+
}
|
|
1219
|
+
function recordsBeyond(indexState, targets) {
|
|
1220
|
+
let records = 0;
|
|
1221
|
+
for (const [partitionId, partition] of indexState.partitions) {
|
|
1222
|
+
records += Math.max(0, partition.appliedSeqNo - snapshotCheckpointFor(targets, partitionId));
|
|
1223
|
+
}
|
|
1224
|
+
return records;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// src/persistence/durability/directory-watch.ts
|
|
1228
|
+
function unrefInterval(tick, intervalMs) {
|
|
1229
|
+
const timer = setInterval(tick, intervalMs);
|
|
1230
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
1231
|
+
return timer;
|
|
1232
|
+
}
|
|
1233
|
+
function createDirectoryWatch(directory, isStopped, onLost) {
|
|
1234
|
+
let expected = null;
|
|
1235
|
+
let timer = null;
|
|
1236
|
+
let lock = null;
|
|
1237
|
+
function cannotCheck(err) {
|
|
1238
|
+
return new NarsilError(
|
|
1239
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1240
|
+
`The engine cannot check the durability directory "${directory.root}", so it cannot confirm that a write is durable`,
|
|
1241
|
+
{ directory: directory.root, cause: err instanceof Error ? err.message : String(err) }
|
|
1242
|
+
);
|
|
1243
|
+
}
|
|
1244
|
+
async function currentIdentity() {
|
|
1245
|
+
try {
|
|
1246
|
+
return await directory.identity?.() ?? null;
|
|
1247
|
+
} catch (err) {
|
|
1248
|
+
return cannotCheck(err);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
async function lockTakenOver() {
|
|
1252
|
+
if (lock === null) return null;
|
|
1253
|
+
try {
|
|
1254
|
+
if (await lock.holds()) return null;
|
|
1255
|
+
} catch (err) {
|
|
1256
|
+
return cannotCheck(err);
|
|
1257
|
+
}
|
|
1258
|
+
return new NarsilError(
|
|
1259
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1260
|
+
`The lock file of the durability directory "${directory.root}" records another engine, which may be writing the same log`,
|
|
1261
|
+
{ directory: directory.root }
|
|
1262
|
+
);
|
|
1263
|
+
}
|
|
1264
|
+
async function verify() {
|
|
1265
|
+
if (directory.identity === void 0 || isStopped()) return true;
|
|
1266
|
+
const current = await currentIdentity();
|
|
1267
|
+
if (current instanceof Error) {
|
|
1268
|
+
onLost(current);
|
|
1269
|
+
return false;
|
|
1270
|
+
}
|
|
1271
|
+
if (expected === null) {
|
|
1272
|
+
expected = current;
|
|
1273
|
+
} else if (current !== expected) {
|
|
1274
|
+
onLost(
|
|
1275
|
+
new NarsilError(
|
|
1276
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1277
|
+
`The durability directory "${directory.root}" is missing or is a different directory from the one that this instance opens at start-up, so a write to it is not durable`,
|
|
1278
|
+
{ directory: directory.root }
|
|
1279
|
+
)
|
|
1280
|
+
);
|
|
1281
|
+
return false;
|
|
1282
|
+
}
|
|
1283
|
+
const takenOver = await lockTakenOver();
|
|
1284
|
+
if (takenOver === null) return true;
|
|
1285
|
+
onLost(takenOver);
|
|
1286
|
+
return false;
|
|
1287
|
+
}
|
|
1288
|
+
function stop() {
|
|
1289
|
+
if (timer !== null) {
|
|
1290
|
+
clearInterval(timer);
|
|
1291
|
+
timer = null;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
return {
|
|
1295
|
+
async claim() {
|
|
1296
|
+
if (lock === null && directory.lock !== void 0) lock = await directory.lock();
|
|
1297
|
+
},
|
|
1298
|
+
async release() {
|
|
1299
|
+
const held = lock;
|
|
1300
|
+
lock = null;
|
|
1301
|
+
await held?.release();
|
|
1302
|
+
},
|
|
1303
|
+
verify,
|
|
1304
|
+
async verifyOnce() {
|
|
1305
|
+
if (expected === null && directory.identity !== void 0) await verify();
|
|
1306
|
+
},
|
|
1307
|
+
start() {
|
|
1308
|
+
if (directory.identity === void 0 || timer !== null || isStopped()) return;
|
|
1309
|
+
timer = unrefInterval(() => void verify(), DIRECTORY_IDENTITY_CHECK_INTERVAL_MS);
|
|
1310
|
+
},
|
|
1311
|
+
stop
|
|
1312
|
+
};
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
// src/persistence/durability/manager-state.ts
|
|
1316
|
+
async function drainIndexStateForUnload(indexState, pendingMetadataWrite, closeWalWriter) {
|
|
1317
|
+
indexState.unloading = true;
|
|
1318
|
+
try {
|
|
1319
|
+
if (indexState.checkpointInFlight !== null) await indexState.checkpointInFlight;
|
|
1320
|
+
await pendingMetadataWrite;
|
|
1321
|
+
for (const partition of indexState.partitions.values()) {
|
|
1322
|
+
await closeWalWriter(partition.walWriter.close());
|
|
1323
|
+
}
|
|
1324
|
+
} catch (error) {
|
|
1325
|
+
indexState.unloading = false;
|
|
1326
|
+
throw error;
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
// src/persistence/durability/mutation-batch.ts
|
|
1331
|
+
function toError(err) {
|
|
1332
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
1333
|
+
}
|
|
1334
|
+
function failUnlessAlreadyFailed(outcomes, count, error) {
|
|
1335
|
+
for (let i = 0; i < count; i++) {
|
|
1336
|
+
const outcome = outcomes[i];
|
|
1337
|
+
if (outcome === void 0 || outcome.ok) {
|
|
1338
|
+
outcomes[i] = { ok: false, error };
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
function failPartition(partition, deps, err) {
|
|
1343
|
+
const error = toError(err);
|
|
1344
|
+
partition.failed = error;
|
|
1345
|
+
deps.markFatal(error);
|
|
1346
|
+
return error;
|
|
1347
|
+
}
|
|
1348
|
+
async function recordBatchesByPartition(records, partitionOf, deps) {
|
|
1349
|
+
const positionsByPartition = /* @__PURE__ */ new Map();
|
|
1350
|
+
for (let i = 0; i < records.length; i++) {
|
|
1351
|
+
const partition = partitionOf(records[i]);
|
|
1352
|
+
const positions = positionsByPartition.get(partition);
|
|
1353
|
+
if (positions === void 0) {
|
|
1354
|
+
positionsByPartition.set(partition, [i]);
|
|
1355
|
+
} else {
|
|
1356
|
+
positions.push(i);
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
const outcomes = new Array(records.length);
|
|
1360
|
+
await Promise.all(
|
|
1361
|
+
[...positionsByPartition].map(async ([partition, positions]) => {
|
|
1362
|
+
const partitionOutcomes = await recordPartitionBatch(
|
|
1363
|
+
partition,
|
|
1364
|
+
positions.map((position) => records[position]),
|
|
1365
|
+
deps
|
|
1366
|
+
);
|
|
1367
|
+
for (let i = 0; i < positions.length; i++) {
|
|
1368
|
+
outcomes[positions[i]] = partitionOutcomes[i];
|
|
1369
|
+
}
|
|
1370
|
+
})
|
|
1371
|
+
);
|
|
1372
|
+
return outcomes;
|
|
1373
|
+
}
|
|
1374
|
+
async function recordPartitionBatch(partition, records, deps) {
|
|
1375
|
+
const outcomes = new Array(records.length);
|
|
1376
|
+
let appendedCount = 0;
|
|
1377
|
+
const appended = partition.appendChain.then(async () => {
|
|
1378
|
+
const fatal = deps.fatalError();
|
|
1379
|
+
if (fatal !== null) {
|
|
1380
|
+
throw fatal;
|
|
1381
|
+
}
|
|
1382
|
+
if (partition.failed !== null) {
|
|
1383
|
+
throw partition.failed;
|
|
1384
|
+
}
|
|
1385
|
+
const applied = [];
|
|
1386
|
+
for (let i = 0; i < records.length; i++) {
|
|
1387
|
+
try {
|
|
1388
|
+
await records[i].apply();
|
|
1389
|
+
applied.push(i);
|
|
1390
|
+
} catch (err) {
|
|
1391
|
+
outcomes[i] = { ok: false, error: err };
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
if (applied.length === 0) {
|
|
1395
|
+
return;
|
|
1396
|
+
}
|
|
1397
|
+
let lastSeqNo = partition.appliedSeqNo;
|
|
1398
|
+
try {
|
|
1399
|
+
const entries = [];
|
|
1400
|
+
for (const i of applied) {
|
|
1401
|
+
lastSeqNo = partition.seqOwner.next();
|
|
1402
|
+
outcomes[i] = { ok: true, seqNo: lastSeqNo };
|
|
1403
|
+
entries.push(deps.buildEntry(records[i], lastSeqNo));
|
|
1404
|
+
}
|
|
1405
|
+
await partition.walWriter.appendAll(entries);
|
|
1406
|
+
} catch (err) {
|
|
1407
|
+
throw failPartition(partition, deps, err);
|
|
1408
|
+
}
|
|
1409
|
+
partition.appliedSeqNo = lastSeqNo;
|
|
1410
|
+
appendedCount = applied.length;
|
|
1411
|
+
});
|
|
1412
|
+
partition.appendChain = appended.catch(() => void 0);
|
|
1413
|
+
try {
|
|
1414
|
+
await appended;
|
|
1415
|
+
if (deps.syncEachBatch && appendedCount > 0) {
|
|
1416
|
+
try {
|
|
1417
|
+
await partition.walWriter.commit();
|
|
1418
|
+
} catch (err) {
|
|
1419
|
+
throw failPartition(partition, deps, err);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
} catch (err) {
|
|
1423
|
+
failUnlessAlreadyFailed(outcomes, records.length, toError(err));
|
|
1424
|
+
}
|
|
1425
|
+
return outcomes;
|
|
1426
|
+
}
|
|
1427
|
+
var SNAPSHOT_BUNDLE_VERSION = 2;
|
|
1428
|
+
function invalid(message) {
|
|
1429
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Snapshot bundle ${message}`);
|
|
1430
|
+
}
|
|
1431
|
+
function vectorIndexesOf(raw) {
|
|
1432
|
+
if (raw === void 0 || raw === null) return {};
|
|
1433
|
+
if (typeof raw !== "object" || Array.isArray(raw)) invalid("vectorIndexes must be a map");
|
|
1434
|
+
const result = {};
|
|
1435
|
+
for (const [fieldPath, parts] of Object.entries(raw)) {
|
|
1436
|
+
result[fieldPath] = decodeVectorIndexParts(parts);
|
|
1437
|
+
}
|
|
1438
|
+
return result;
|
|
1439
|
+
}
|
|
1440
|
+
function checkpointOf(raw) {
|
|
1441
|
+
if (!Array.isArray(raw)) return [];
|
|
1442
|
+
const result = [];
|
|
1443
|
+
for (const entry of raw) {
|
|
1444
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
1445
|
+
const { partitionId, lastSeqNo, primaryTerm } = entry;
|
|
1446
|
+
if (typeof partitionId !== "number" || typeof lastSeqNo !== "number" || typeof primaryTerm !== "number") continue;
|
|
1447
|
+
result.push({ partitionId, lastSeqNo, primaryTerm });
|
|
1448
|
+
}
|
|
1449
|
+
return result;
|
|
1450
|
+
}
|
|
1451
|
+
async function decodeSnapshotBundle(data) {
|
|
1452
|
+
const { payloadBytes } = await unpackEnvelopeBytes(data);
|
|
1453
|
+
const raw = decode(payloadBytes);
|
|
1454
|
+
if (raw.version !== SNAPSHOT_BUNDLE_VERSION) {
|
|
1455
|
+
throw new NarsilError(
|
|
1456
|
+
ErrorCodes.ENVELOPE_VERSION_MISMATCH,
|
|
1457
|
+
`The snapshot bundle holds layout ${String(raw.version)}, which this release cannot open. Export the index with the release that wrote it and import the export`,
|
|
1458
|
+
{ version: raw.version }
|
|
1459
|
+
);
|
|
1460
|
+
}
|
|
1461
|
+
if (raw.schema === null || typeof raw.schema !== "object" || Array.isArray(raw.schema)) invalid("missing schema");
|
|
1462
|
+
requireValidFieldTypes(raw.schema);
|
|
1463
|
+
if (typeof raw.language !== "string") invalid("missing language");
|
|
1464
|
+
if (!Array.isArray(raw.partitions) || !raw.partitions.every((part) => part instanceof Uint8Array)) {
|
|
1465
|
+
invalid("missing partitions");
|
|
1466
|
+
}
|
|
1467
|
+
return {
|
|
1468
|
+
schema: raw.schema,
|
|
1469
|
+
language: raw.language,
|
|
1470
|
+
partitions: raw.partitions,
|
|
1471
|
+
vectorIndexes: vectorIndexesOf(raw.vectorIndexes),
|
|
1472
|
+
checkpoint: checkpointOf(raw.checkpoint)
|
|
1473
|
+
};
|
|
1474
|
+
}
|
|
1475
|
+
function countSnapshotBundleDocuments(bundle) {
|
|
1476
|
+
let total = 0;
|
|
1477
|
+
for (const partitionBytes of bundle.partitions)
|
|
1478
|
+
total += Object.keys(deserializePayloadV2(partitionBytes).documents).length;
|
|
1479
|
+
return total;
|
|
1480
|
+
}
|
|
1481
|
+
async function loadSnapshotBundle(bytes, deps) {
|
|
1482
|
+
const bundle = await decodeSnapshotBundle(bytes);
|
|
1483
|
+
while (deps.manager.partitionCount < bundle.partitions.length) deps.manager.addPartition();
|
|
1484
|
+
if (bundle.partitions.length > 0) deps.manager.trimPartitions(bundle.partitions.length);
|
|
1485
|
+
for (let i = 0; i < bundle.partitions.length; i += 1) {
|
|
1486
|
+
deps.manager.deserializePartition(i, deserializePayloadV2(bundle.partitions[i]));
|
|
1487
|
+
}
|
|
1488
|
+
for (const [fieldPath, parts] of Object.entries(bundle.vectorIndexes)) {
|
|
1489
|
+
const rebuildsGraph = parts.some((part) => part.graphs.some((graph) => graph.m < MIN_M));
|
|
1490
|
+
deps.vectorIndexes.get(fieldPath)?.deserialize(rebuildsGraph ? parts.map((part) => ({ ...part, graphs: [] })) : parts);
|
|
1491
|
+
}
|
|
1492
|
+
return bundle.checkpoint;
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
// src/persistence/durability/earlier-layout/manifest.ts
|
|
1496
|
+
function isKey(value) {
|
|
1497
|
+
return typeof value === "string" && value.length > 0 && !value.includes("\0");
|
|
1498
|
+
}
|
|
1499
|
+
function checkpointOf2(raw) {
|
|
1500
|
+
if (!Array.isArray(raw)) return [];
|
|
1501
|
+
const result = [];
|
|
1502
|
+
for (const entry of raw) {
|
|
1503
|
+
if (!isRecord(entry) || !isUint32(entry.partitionId)) continue;
|
|
1504
|
+
if (!isSequenceNumber(entry.lastSeqNo) || !isSequenceNumber(entry.primaryTerm)) continue;
|
|
1505
|
+
result.push({ partitionId: entry.partitionId, lastSeqNo: entry.lastSeqNo, primaryTerm: entry.primaryTerm });
|
|
1506
|
+
}
|
|
1507
|
+
return result;
|
|
1508
|
+
}
|
|
1509
|
+
function segmentsOf(raw, partitionId) {
|
|
1510
|
+
if (raw === void 0) return [];
|
|
1511
|
+
if (!Array.isArray(raw) || raw.length > MAX_SEGMENTS_PER_PARTITION) {
|
|
1512
|
+
invalidManifest("has malformed segment references", { partitionId });
|
|
1513
|
+
}
|
|
1514
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1515
|
+
return raw.map((segment) => {
|
|
1516
|
+
if (!isRecord(segment) || !isId(segment.id) || !isUint32(segment.docCount) || !isUint32(segment.tombstoneCount) || !isKey(segment.key) || seen.has(segment.id)) {
|
|
1517
|
+
invalidManifest("has an invalid segment reference", { partitionId });
|
|
1518
|
+
}
|
|
1519
|
+
seen.add(segment.id);
|
|
1520
|
+
return { id: segment.id, key: segment.key, docCount: segment.docCount, tombstoneCount: segment.tombstoneCount };
|
|
1521
|
+
});
|
|
1522
|
+
}
|
|
1523
|
+
function partitionsOf(raw) {
|
|
1524
|
+
if (!Array.isArray(raw)) invalidManifest("is missing partition entries");
|
|
1525
|
+
return raw.map((entry) => {
|
|
1526
|
+
if (!isRecord(entry) || !isUint32(entry.partitionId)) invalidManifest("has a partition without a valid id");
|
|
1527
|
+
const segments = segmentsOf(entry.segments, entry.partitionId);
|
|
1528
|
+
const nextSegmentId = entry.nextSegmentId;
|
|
1529
|
+
if (!isId(nextSegmentId) || segments.some((segment) => segment.id >= nextSegmentId)) {
|
|
1530
|
+
invalidManifest("has an invalid next segment id", { partitionId: entry.partitionId });
|
|
1531
|
+
}
|
|
1532
|
+
return { partitionId: entry.partitionId, nextSegmentId, segments };
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
function vectorFileOf(raw, fieldPath) {
|
|
1536
|
+
if (!isRecord(raw) || !isId(raw.id) || !isKey(raw.key)) invalidManifest("has an invalid vector file reference");
|
|
1537
|
+
const count = raw.count;
|
|
1538
|
+
if (!isUint32(count) || count > VECTOR_FILE_MAX_VECTORS) invalidManifest(`gives "${fieldPath}" an invalid count`);
|
|
1539
|
+
const dead = raw.dead ?? null;
|
|
1540
|
+
if (dead !== null && (!(dead instanceof Uint8Array) || dead.byteLength !== deadBytesFor(count))) {
|
|
1541
|
+
invalidManifest(`marks the dead vectors of "${fieldPath}" with the wrong number of bytes`);
|
|
1542
|
+
}
|
|
1543
|
+
return { id: raw.id, key: raw.key, count, dead };
|
|
1544
|
+
}
|
|
1545
|
+
function vectorsOf(raw) {
|
|
1546
|
+
if (raw === void 0 || raw === null) return [];
|
|
1547
|
+
if (!Array.isArray(raw)) invalidManifest("has malformed vector references");
|
|
1548
|
+
return raw.map((field) => {
|
|
1549
|
+
if (!isRecord(field) || typeof field.fieldPath !== "string") invalidManifest("has an invalid vector reference");
|
|
1550
|
+
const fieldPath = field.fieldPath;
|
|
1551
|
+
if (!isId(field.nextFileId) || !Array.isArray(field.files) || !isId(field.graphGeneration)) {
|
|
1552
|
+
invalidManifest(`gives "${fieldPath}" an invalid vector reference`);
|
|
1553
|
+
}
|
|
1554
|
+
const graphKey = field.graphKey ?? null;
|
|
1555
|
+
if (graphKey !== null && (!isKey(graphKey) || field.graphGeneration === 0)) {
|
|
1556
|
+
invalidManifest(`gives "${fieldPath}" an invalid graph key`);
|
|
1557
|
+
}
|
|
1558
|
+
return {
|
|
1559
|
+
fieldPath,
|
|
1560
|
+
nextFileId: field.nextFileId,
|
|
1561
|
+
files: field.files.map((file) => vectorFileOf(file, fieldPath)),
|
|
1562
|
+
graphGeneration: field.graphGeneration,
|
|
1563
|
+
graphKey
|
|
1564
|
+
};
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
1567
|
+
function decodeEarlierManifest(raw) {
|
|
1568
|
+
const schema = raw.schema;
|
|
1569
|
+
if (!isRecord(schema)) invalidManifest("is missing its schema");
|
|
1570
|
+
requireValidFieldTypes(schema);
|
|
1571
|
+
if (typeof raw.language !== "string") invalidManifest("is missing its language");
|
|
1572
|
+
return {
|
|
1573
|
+
schema,
|
|
1574
|
+
language: raw.language,
|
|
1575
|
+
checkpoint: checkpointOf2(raw.checkpoint),
|
|
1576
|
+
partitions: partitionsOf(raw.partitions),
|
|
1577
|
+
vectors: vectorsOf(raw.vectors)
|
|
1578
|
+
};
|
|
1579
|
+
}
|
|
1580
|
+
function earlierReferencedKeys(manifest) {
|
|
1581
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1582
|
+
for (const partition of manifest.partitions) {
|
|
1583
|
+
for (const segment of partition.segments) keys.add(segment.key);
|
|
1584
|
+
}
|
|
1585
|
+
for (const vector of manifest.vectors) {
|
|
1586
|
+
for (const file of vector.files) keys.add(file.key);
|
|
1587
|
+
if (vector.graphKey !== null) keys.add(vector.graphKey);
|
|
1588
|
+
}
|
|
1589
|
+
return keys;
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
// src/persistence/durability/earlier-layout/merge.ts
|
|
1593
|
+
function mergeTimeOrderedSegments(ordered, fallback) {
|
|
1594
|
+
const winner = resolveWinners(ordered);
|
|
1595
|
+
const first = ordered[0]?.partition;
|
|
1596
|
+
const merged = {
|
|
1597
|
+
indexName: first?.indexName ?? fallback.indexName,
|
|
1598
|
+
partitionId: first?.partitionId ?? fallback.partitionId,
|
|
1599
|
+
totalPartitions: first?.totalPartitions ?? fallback.totalPartitions,
|
|
1600
|
+
language: first?.language ?? fallback.language,
|
|
1601
|
+
schema: {},
|
|
1602
|
+
docCount: 0,
|
|
1603
|
+
avgDocLength: 0,
|
|
1604
|
+
documents: /* @__PURE__ */ Object.create(null),
|
|
1605
|
+
invertedIndex: /* @__PURE__ */ Object.create(null),
|
|
1606
|
+
fieldIndexes: {
|
|
1607
|
+
numeric: /* @__PURE__ */ Object.create(null),
|
|
1608
|
+
boolean: /* @__PURE__ */ Object.create(null),
|
|
1609
|
+
enum: /* @__PURE__ */ Object.create(null),
|
|
1610
|
+
geopoint: /* @__PURE__ */ Object.create(null)
|
|
1611
|
+
},
|
|
1612
|
+
statistics: {
|
|
1613
|
+
totalDocuments: 0,
|
|
1614
|
+
totalFieldLengths: /* @__PURE__ */ Object.create(null),
|
|
1615
|
+
averageFieldLengths: /* @__PURE__ */ Object.create(null),
|
|
1616
|
+
docFrequencies: /* @__PURE__ */ Object.create(null)
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1619
|
+
for (let segIndex = 0; segIndex < ordered.length; segIndex += 1) {
|
|
1620
|
+
const part = ordered[segIndex].partition;
|
|
1621
|
+
addSegmentSchema(merged.schema, part.schema);
|
|
1622
|
+
collectDocuments(merged, part, winner, segIndex);
|
|
1623
|
+
collectInvertedIndex(merged, part, winner, segIndex);
|
|
1624
|
+
collectFieldIndexes(merged, part, winner, segIndex);
|
|
1625
|
+
collectSurfaceForms(merged, part);
|
|
1626
|
+
}
|
|
1627
|
+
recomputeStatistics(merged);
|
|
1628
|
+
validateMergedPartition(merged);
|
|
1629
|
+
return merged;
|
|
1630
|
+
}
|
|
1631
|
+
function countLiveDocuments(ordered) {
|
|
1632
|
+
return resolveWinners(ordered).size;
|
|
1633
|
+
}
|
|
1634
|
+
function resolveWinners(ordered) {
|
|
1635
|
+
const winner = /* @__PURE__ */ new Map();
|
|
1636
|
+
for (let segIndex = 0; segIndex < ordered.length; segIndex += 1) {
|
|
1637
|
+
const segment = ordered[segIndex];
|
|
1638
|
+
for (const docId of segment.tombstones) {
|
|
1639
|
+
winner.delete(docId);
|
|
1640
|
+
}
|
|
1641
|
+
for (const docId of Object.keys(segment.partition.documents)) {
|
|
1642
|
+
winner.set(docId, segIndex);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
return winner;
|
|
1646
|
+
}
|
|
1647
|
+
function owns(winner, docId, segIndex) {
|
|
1648
|
+
return winner.get(docId) === segIndex;
|
|
1649
|
+
}
|
|
1650
|
+
function addSegmentSchema(agreed, schema) {
|
|
1651
|
+
for (const [field, type] of Object.entries(schema)) {
|
|
1652
|
+
const existing = agreed[field];
|
|
1653
|
+
if (existing !== void 0 && existing !== type) {
|
|
1654
|
+
throw new NarsilError(
|
|
1655
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1656
|
+
`Segment schemas disagree on field "${field}": "${existing}" vs "${type}"`,
|
|
1657
|
+
{ field, existing, found: type }
|
|
1658
|
+
);
|
|
1659
|
+
}
|
|
1660
|
+
agreed[field] = type;
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
function collectDocuments(merged, part, winner, segIndex) {
|
|
1664
|
+
for (const [docId, doc] of Object.entries(part.documents)) {
|
|
1665
|
+
if (owns(winner, docId, segIndex)) {
|
|
1666
|
+
merged.documents[docId] = doc;
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
function collectInvertedIndex(merged, part, winner, segIndex) {
|
|
1671
|
+
for (const [token, list] of Object.entries(part.invertedIndex)) {
|
|
1672
|
+
let target = merged.invertedIndex[token];
|
|
1673
|
+
for (const posting of list.postings) {
|
|
1674
|
+
if (!owns(winner, posting.docId, segIndex)) continue;
|
|
1675
|
+
if (target === void 0) {
|
|
1676
|
+
target = { docFrequency: 0, postings: [] };
|
|
1677
|
+
merged.invertedIndex[token] = target;
|
|
1678
|
+
}
|
|
1679
|
+
target.postings.push(posting);
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
function collectEntries(target, source, winner, segIndex) {
|
|
1684
|
+
for (const [path, entries] of Object.entries(source)) {
|
|
1685
|
+
for (const entry of entries) {
|
|
1686
|
+
if (!owns(winner, entry.docId, segIndex)) continue;
|
|
1687
|
+
let bucket = target[path];
|
|
1688
|
+
if (bucket === void 0) {
|
|
1689
|
+
bucket = [];
|
|
1690
|
+
target[path] = bucket;
|
|
1691
|
+
}
|
|
1692
|
+
bucket.push(entry);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
function collectFieldIndexes(merged, part, winner, segIndex) {
|
|
1697
|
+
collectEntries(merged.fieldIndexes.numeric, part.fieldIndexes.numeric, winner, segIndex);
|
|
1698
|
+
collectEntries(merged.fieldIndexes.geopoint, part.fieldIndexes.geopoint, winner, segIndex);
|
|
1699
|
+
for (const [path, idx] of Object.entries(part.fieldIndexes.boolean)) {
|
|
1700
|
+
for (const [docIds, flag] of [
|
|
1701
|
+
[idx.trueDocs, true],
|
|
1702
|
+
[idx.falseDocs, false]
|
|
1703
|
+
]) {
|
|
1704
|
+
for (const docId of docIds) {
|
|
1705
|
+
if (!owns(winner, docId, segIndex)) continue;
|
|
1706
|
+
let target = merged.fieldIndexes.boolean[path];
|
|
1707
|
+
if (target === void 0) {
|
|
1708
|
+
target = { trueDocs: [], falseDocs: [] };
|
|
1709
|
+
merged.fieldIndexes.boolean[path] = target;
|
|
1710
|
+
}
|
|
1711
|
+
(flag ? target.trueDocs : target.falseDocs).push(docId);
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
for (const [path, values] of Object.entries(part.fieldIndexes.enum)) {
|
|
1716
|
+
for (const [value, docIds] of Object.entries(values)) {
|
|
1717
|
+
for (const docId of docIds) {
|
|
1718
|
+
if (!owns(winner, docId, segIndex)) continue;
|
|
1719
|
+
let target = merged.fieldIndexes.enum[path];
|
|
1720
|
+
if (target === void 0) {
|
|
1721
|
+
target = /* @__PURE__ */ Object.create(null);
|
|
1722
|
+
merged.fieldIndexes.enum[path] = target;
|
|
1723
|
+
}
|
|
1724
|
+
let bucket = target[value];
|
|
1725
|
+
if (bucket === void 0) {
|
|
1726
|
+
bucket = [];
|
|
1727
|
+
target[value] = bucket;
|
|
1728
|
+
}
|
|
1729
|
+
bucket.push(docId);
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
function collectSurfaceForms(merged, part) {
|
|
1735
|
+
if (!part.surfaceForms) return;
|
|
1736
|
+
let target = merged.surfaceForms;
|
|
1737
|
+
if (!target) {
|
|
1738
|
+
target = /* @__PURE__ */ Object.create(null);
|
|
1739
|
+
merged.surfaceForms = target;
|
|
1740
|
+
}
|
|
1741
|
+
for (const [surface, value] of Object.entries(part.surfaceForms)) {
|
|
1742
|
+
const count = typeof value === "number" ? value : value[0];
|
|
1743
|
+
const token = typeof value === "number" ? surface : value[1];
|
|
1744
|
+
const existing = target[surface];
|
|
1745
|
+
if (existing === void 0) {
|
|
1746
|
+
target[surface] = value;
|
|
1747
|
+
continue;
|
|
1748
|
+
}
|
|
1749
|
+
const existingCount = typeof existing === "number" ? existing : existing[0];
|
|
1750
|
+
const total = existingCount + count;
|
|
1751
|
+
target[surface] = token === surface ? total : [total, token];
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
function recomputeStatistics(merged) {
|
|
1755
|
+
const totalFieldLengths = /* @__PURE__ */ Object.create(null);
|
|
1756
|
+
let totalDocuments = 0;
|
|
1757
|
+
for (const doc of Object.values(merged.documents)) {
|
|
1758
|
+
totalDocuments += 1;
|
|
1759
|
+
for (const [field, length] of Object.entries(doc.fieldLengths)) {
|
|
1760
|
+
totalFieldLengths[field] = (totalFieldLengths[field] ?? 0) + length;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
const docFrequencies = /* @__PURE__ */ Object.create(null);
|
|
1764
|
+
for (const [token, list] of Object.entries(merged.invertedIndex)) {
|
|
1765
|
+
const distinct = /* @__PURE__ */ new Set();
|
|
1766
|
+
for (const posting of list.postings) distinct.add(posting.docId);
|
|
1767
|
+
list.docFrequency = distinct.size;
|
|
1768
|
+
docFrequencies[token] = distinct.size;
|
|
1769
|
+
}
|
|
1770
|
+
const averageFieldLengths = /* @__PURE__ */ Object.create(null);
|
|
1771
|
+
for (const [field, length] of Object.entries(totalFieldLengths)) {
|
|
1772
|
+
averageFieldLengths[field] = totalDocuments > 0 ? length / totalDocuments : 0;
|
|
1773
|
+
}
|
|
1774
|
+
merged.statistics.totalDocuments = totalDocuments;
|
|
1775
|
+
merged.statistics.totalFieldLengths = totalFieldLengths;
|
|
1776
|
+
merged.statistics.averageFieldLengths = averageFieldLengths;
|
|
1777
|
+
merged.statistics.docFrequencies = docFrequencies;
|
|
1778
|
+
merged.docCount = totalDocuments;
|
|
1779
|
+
merged.avgDocLength = Object.values(averageFieldLengths).reduce((sum, value) => sum + value, 0);
|
|
1780
|
+
}
|
|
1781
|
+
function validateMergedPartition(merged) {
|
|
1782
|
+
for (const [token, list] of Object.entries(merged.invertedIndex)) {
|
|
1783
|
+
for (const posting of list.postings) {
|
|
1784
|
+
if (!(posting.docId in merged.documents)) {
|
|
1785
|
+
throw new NarsilError(
|
|
1786
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1787
|
+
`Merged segment posting references document "${posting.docId}" that is absent from the partition`,
|
|
1788
|
+
{ token, docId: posting.docId }
|
|
1789
|
+
);
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
function normalizeTombstones(raw) {
|
|
1795
|
+
if (raw === void 0) return [];
|
|
1796
|
+
if (!Array.isArray(raw) || !raw.every((value) => typeof value === "string")) {
|
|
1797
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, "Segment file has a malformed tombstone list");
|
|
1798
|
+
}
|
|
1799
|
+
return raw;
|
|
1800
|
+
}
|
|
1801
|
+
async function openContainer(store, key) {
|
|
1802
|
+
const bytes = await store.read(key);
|
|
1803
|
+
if (bytes === null) {
|
|
1804
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Segment file "${key}" is missing`, { key });
|
|
1805
|
+
}
|
|
1806
|
+
const { payloadBytes } = await unpackEnvelopeBytes(bytes);
|
|
1807
|
+
const raw = decode(payloadBytes);
|
|
1808
|
+
if (!(raw.payload instanceof Uint8Array)) {
|
|
1809
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, "Segment file is missing its partition payload", { key });
|
|
1810
|
+
}
|
|
1811
|
+
return { payload: raw.payload, tombstones: normalizeTombstones(raw.tombstones) };
|
|
1812
|
+
}
|
|
1813
|
+
async function readEarlierSegmentContents(store, key) {
|
|
1814
|
+
const { payload, tombstones } = await openContainer(store, key);
|
|
1815
|
+
return { partition: deserializePayloadV2(payload), tombstones };
|
|
1816
|
+
}
|
|
1817
|
+
async function readEarlierSegmentDocuments(store, key) {
|
|
1818
|
+
const { payload, tombstones } = await openContainer(store, key);
|
|
1819
|
+
const stored = documentsOfPayloadV2(payload);
|
|
1820
|
+
const documents = [];
|
|
1821
|
+
for (const [docId, document] of Object.entries(stored.documents)) {
|
|
1822
|
+
documents.push({ docId, document: document.fields });
|
|
1823
|
+
}
|
|
1824
|
+
return { schema: stored.schema, documents, tombstones };
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
// src/persistence/durability/checkpoint-count.ts
|
|
1828
|
+
var decoder = new TextDecoder();
|
|
1829
|
+
async function countCurrentLayout(store, manifest) {
|
|
1830
|
+
let total = 0;
|
|
1831
|
+
for (const partition of manifest.partitions) {
|
|
1832
|
+
const live = /* @__PURE__ */ new Set();
|
|
1833
|
+
for (const ref of partition.segments) {
|
|
1834
|
+
const contents = await readSegmentFile(store, ref);
|
|
1835
|
+
for (const docId of contents.tombstones) live.delete(docId);
|
|
1836
|
+
const ids = contents.arrays.idTable;
|
|
1837
|
+
for (let ordinal = 0; ordinal < contents.arrays.documentCount; ordinal++) {
|
|
1838
|
+
live.add(decoder.decode(ids.blob.subarray(ids.offsets[ordinal], ids.offsets[ordinal + 1])));
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
total += live.size;
|
|
1842
|
+
}
|
|
1843
|
+
return total;
|
|
1844
|
+
}
|
|
1845
|
+
async function countCheckpointDocuments(store, indexName) {
|
|
1846
|
+
const decoded = await readSegmentManifest(store, indexName);
|
|
1847
|
+
if (decoded?.kind === "current") return countCurrentLayout(store, decoded.manifest);
|
|
1848
|
+
if (decoded?.kind === "earlier") {
|
|
1849
|
+
let total = 0;
|
|
1850
|
+
for (const partition of decodeEarlierManifest(decoded.payload).partitions) {
|
|
1851
|
+
const ordered = [];
|
|
1852
|
+
for (const segment of partition.segments) ordered.push(await readEarlierSegmentContents(store, segment.key));
|
|
1853
|
+
total += countLiveDocuments(ordered);
|
|
1854
|
+
}
|
|
1855
|
+
return total;
|
|
1856
|
+
}
|
|
1857
|
+
const bundle = await store.read(snapshotBundleKey(indexName));
|
|
1858
|
+
return bundle === null ? 0 : countSnapshotBundleDocuments(await decodeSnapshotBundle(bundle));
|
|
1859
|
+
}
|
|
1860
|
+
function removeWhatTheSegmentReplaces(manager, segment) {
|
|
1861
|
+
manager.beginBatchRemove();
|
|
1862
|
+
try {
|
|
1863
|
+
for (const docId of segment.tombstones) {
|
|
1864
|
+
if (manager.has(docId)) manager.remove(docId);
|
|
1865
|
+
}
|
|
1866
|
+
for (const { docId } of segment.documents) {
|
|
1867
|
+
if (manager.has(docId)) manager.remove(docId);
|
|
1868
|
+
}
|
|
1869
|
+
} finally {
|
|
1870
|
+
manager.endBatchRemove();
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
function attachDocuments(manager, partitionId, segment) {
|
|
1874
|
+
if (segment.documents.length === 0) return;
|
|
1875
|
+
const payload = buildSegmentPayload(
|
|
1876
|
+
segment.documents,
|
|
1877
|
+
manager.config.schema,
|
|
1878
|
+
manager.language,
|
|
1879
|
+
resolvePartitionInsertOptions(manager.config, manager.analysis),
|
|
1880
|
+
manager.config.trackPositions ?? true
|
|
1881
|
+
);
|
|
1882
|
+
const documents = segment.documents.map((entry) => entry.document);
|
|
1883
|
+
const snapshot = freezeSegmentShared(payload, documents);
|
|
1884
|
+
if (snapshot === null) {
|
|
1885
|
+
manager.mergeSegment(partitionId, payload, documents);
|
|
1886
|
+
return;
|
|
1887
|
+
}
|
|
1888
|
+
manager.attachFrozenSegment(partitionId, createSharedFrozenSegment(snapshot));
|
|
1889
|
+
}
|
|
1890
|
+
async function loadPartition(store, indexName, partition, deps) {
|
|
1891
|
+
if (deps.storedTermsAreCurrent) {
|
|
1892
|
+
const agreedSchema = {};
|
|
1893
|
+
for (const ref of partition.segments) {
|
|
1894
|
+
const segment = await readEarlierSegmentDocuments(store, ref.key);
|
|
1895
|
+
addSegmentSchema(agreedSchema, segment.schema);
|
|
1896
|
+
removeWhatTheSegmentReplaces(deps.manager, segment);
|
|
1897
|
+
attachDocuments(deps.manager, partition.partitionId, segment);
|
|
1898
|
+
}
|
|
1899
|
+
return;
|
|
1900
|
+
}
|
|
1901
|
+
const ordered = [];
|
|
1902
|
+
for (const ref of partition.segments) ordered.push(await readEarlierSegmentContents(store, ref.key));
|
|
1903
|
+
const merged = mergeTimeOrderedSegments(ordered, {
|
|
1904
|
+
indexName,
|
|
1905
|
+
partitionId: partition.partitionId,
|
|
1906
|
+
totalPartitions: deps.manager.partitionCount,
|
|
1907
|
+
language: deps.manager.language.name
|
|
1908
|
+
});
|
|
1909
|
+
deps.manager.deserializePartition(partition.partitionId, merged);
|
|
1910
|
+
}
|
|
1911
|
+
async function readGraph(store, ref) {
|
|
1912
|
+
if (ref.graphKey === null) return null;
|
|
1913
|
+
const bytes = await store.read(ref.graphKey);
|
|
1914
|
+
if (bytes === null) {
|
|
1915
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The graph file "${ref.graphKey}" is missing`, {
|
|
1916
|
+
key: ref.graphKey
|
|
1917
|
+
});
|
|
1918
|
+
}
|
|
1919
|
+
const { payload } = await openChecksummedEnvelope(bytes, {
|
|
1920
|
+
envelopeFormatVersion: VECTOR_FILE_ENVELOPE_VERSION,
|
|
1921
|
+
what: `graph file "${ref.graphKey}"`,
|
|
1922
|
+
allowsCompression: true
|
|
1923
|
+
});
|
|
1924
|
+
const graph = decodeVectorGraphPayload(decode(payload));
|
|
1925
|
+
if (graph.graphs.some((numbered) => numbered.m < MIN_M)) return null;
|
|
1926
|
+
return graphPartsOfEarlierPayload(graph);
|
|
1927
|
+
}
|
|
1928
|
+
async function loadVectorField(store, ref, vectorIndex, fileRefs) {
|
|
1929
|
+
let liveVectors = 0;
|
|
1930
|
+
for (const file of ref.files) liveVectors += file.count - countDead(file.dead, file.count);
|
|
1931
|
+
const restore = vectorIndex.restoreCheckpoint({
|
|
1932
|
+
liveVectors,
|
|
1933
|
+
holdsGraph: ref.graphKey !== null,
|
|
1934
|
+
graph: await readGraph(store, ref)
|
|
1935
|
+
});
|
|
1936
|
+
for (const file of ref.files) {
|
|
1937
|
+
const bytes = await store.read(file.key);
|
|
1938
|
+
if (bytes === null) {
|
|
1939
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The vector file "${file.key}" is missing`, {
|
|
1940
|
+
key: file.key
|
|
1941
|
+
});
|
|
1942
|
+
}
|
|
1943
|
+
const { header, payload: payloadBytes } = await openChecksummedEnvelope(bytes, {
|
|
1944
|
+
envelopeFormatVersion: VECTOR_FILE_ENVELOPE_VERSION,
|
|
1945
|
+
what: `vector file "${file.key}"`,
|
|
1946
|
+
allowsCompression: false
|
|
1947
|
+
});
|
|
1948
|
+
const payload = decodeVectorFilePayload(decode(payloadBytes));
|
|
1949
|
+
if (payload.docIds.length !== file.count) {
|
|
1950
|
+
throw new NarsilError(
|
|
1951
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1952
|
+
`The vector file "${file.key}" holds ${payload.docIds.length} vectors where the manifest counts ${file.count}`,
|
|
1953
|
+
{ key: file.key }
|
|
1954
|
+
);
|
|
1955
|
+
}
|
|
1956
|
+
fileRefs.set(file.key, { key: file.key, bytes: bytes.byteLength, checksum: header.checksum });
|
|
1957
|
+
restore.addFile({
|
|
1958
|
+
id: file.id,
|
|
1959
|
+
key: file.key,
|
|
1960
|
+
payload,
|
|
1961
|
+
dead: file.dead,
|
|
1962
|
+
location: store.pathOf === void 0 ? null : {
|
|
1963
|
+
path: await store.pathOf(file.key),
|
|
1964
|
+
vectorsOffset: HEADER_SIZE + header.payloadLength - payload.vectors.byteLength
|
|
1965
|
+
}
|
|
1966
|
+
});
|
|
1967
|
+
}
|
|
1968
|
+
restore.finish();
|
|
1969
|
+
}
|
|
1970
|
+
async function loadEarlierLayout(store, indexName, manifest, deps) {
|
|
1971
|
+
let highestPartitionId = manifest.partitions.length - 1;
|
|
1972
|
+
for (const partition of manifest.partitions) {
|
|
1973
|
+
if (partition.partitionId > highestPartitionId) highestPartitionId = partition.partitionId;
|
|
1974
|
+
}
|
|
1975
|
+
growPartitionsTo(deps.manager, highestPartitionId);
|
|
1976
|
+
for (const partition of manifest.partitions) {
|
|
1977
|
+
if (partition.partitionId >= deps.manager.partitionCount) {
|
|
1978
|
+
throw new NarsilError(
|
|
1979
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
1980
|
+
`Segment manifest references partition ${partition.partitionId} beyond the partition count ${deps.manager.partitionCount}`,
|
|
1981
|
+
{ indexName, partitionId: partition.partitionId }
|
|
1982
|
+
);
|
|
1983
|
+
}
|
|
1984
|
+
await loadPartition(store, indexName, partition, deps);
|
|
1985
|
+
}
|
|
1986
|
+
const vectorFileRefs = /* @__PURE__ */ new Map();
|
|
1987
|
+
for (const vector of manifest.vectors) {
|
|
1988
|
+
const vectorIndex = deps.vectorIndexes.get(vector.fieldPath);
|
|
1989
|
+
if (vectorIndex === void 0) continue;
|
|
1990
|
+
await loadVectorField(store, vector, vectorIndex, vectorFileRefs);
|
|
1991
|
+
}
|
|
1992
|
+
return { checkpoint: manifest.checkpoint, vectorFileRefs };
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
// src/persistence/durability/earlier-layout/upgrade.ts
|
|
1996
|
+
async function loadEarlier(deps, earlier) {
|
|
1997
|
+
const loadDeps = {
|
|
1998
|
+
manager: deps.manager,
|
|
1999
|
+
vectorIndexes: deps.vectorIndexes,
|
|
2000
|
+
storedTermsAreCurrent: deps.storedTermsAreCurrent
|
|
2001
|
+
};
|
|
2002
|
+
if (earlier.kind === "bundle") {
|
|
2003
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, /* @__PURE__ */ new Set());
|
|
2004
|
+
return { checkpoint: await loadSnapshotBundle(earlier.bytes, loadDeps), fileRefs: /* @__PURE__ */ new Map() };
|
|
2005
|
+
}
|
|
2006
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, earlierReferencedKeys(earlier.manifest));
|
|
2007
|
+
const loaded = await loadEarlierLayout(deps.store, deps.indexName, earlier.manifest, loadDeps);
|
|
2008
|
+
return { checkpoint: loaded.checkpoint, fileRefs: loaded.vectorFileRefs };
|
|
2009
|
+
}
|
|
2010
|
+
function priorVectorsOf(earlier, fileRefs) {
|
|
2011
|
+
if (earlier.kind === "bundle") return [];
|
|
2012
|
+
return earlier.manifest.vectors.map((field) => ({
|
|
2013
|
+
fieldPath: field.fieldPath,
|
|
2014
|
+
nextFileId: field.nextFileId,
|
|
2015
|
+
graphGeneration: field.graphGeneration,
|
|
2016
|
+
graphParts: [],
|
|
2017
|
+
files: field.files.flatMap((file) => {
|
|
2018
|
+
const ref = fileRefs.get(file.key);
|
|
2019
|
+
return ref === void 0 ? [] : [{ ...file, bytes: ref.bytes, checksum: ref.checksum }];
|
|
2020
|
+
})
|
|
2021
|
+
}));
|
|
2022
|
+
}
|
|
2023
|
+
function priorPartitionsOf(earlier, partitionCount) {
|
|
2024
|
+
if (earlier.kind === "bundle") return [];
|
|
2025
|
+
return earlier.manifest.partitions.filter((partition) => partition.partitionId < partitionCount).map((partition) => ({ partitionId: partition.partitionId, nextSegmentId: partition.nextSegmentId, segments: [] }));
|
|
2026
|
+
}
|
|
2027
|
+
async function writeCurrentLayout(deps, earlier, targets, fileRefs) {
|
|
2028
|
+
const priorVectors = priorVectorsOf(earlier, fileRefs);
|
|
2029
|
+
const plans = /* @__PURE__ */ new Map();
|
|
2030
|
+
for (const [fieldPath, vectorIndex] of deps.vectorIndexes) {
|
|
2031
|
+
await vectorIndex.completeGraph();
|
|
2032
|
+
const listed = priorVectors.find((ref) => ref.fieldPath === fieldPath);
|
|
2033
|
+
plans.set(
|
|
2034
|
+
fieldPath,
|
|
2035
|
+
vectorIndex.planCheckpoint(listed === void 0 ? null : listed.files.map((file) => file.key), true)
|
|
2036
|
+
);
|
|
2037
|
+
}
|
|
2038
|
+
const written = await writeCheckpointSegments({
|
|
2039
|
+
store: deps.store,
|
|
2040
|
+
metadata: deps.metadata,
|
|
2041
|
+
targets,
|
|
2042
|
+
compactionThreshold: deps.compactionThreshold,
|
|
2043
|
+
prior: { checkpoint: [], partitions: priorPartitionsOf(earlier, deps.manager.partitionCount) },
|
|
2044
|
+
wholePartitions: targets.map((target) => partitionContentsOf(deps.manager, target.partitionId))
|
|
2045
|
+
});
|
|
2046
|
+
const vectors = await writeLiveVectors({
|
|
2047
|
+
store: deps.store,
|
|
2048
|
+
indexName: deps.indexName,
|
|
2049
|
+
plans,
|
|
2050
|
+
priorVectors,
|
|
2051
|
+
priorFileRefs: fileRefs
|
|
2052
|
+
});
|
|
2053
|
+
const committed = await commitCheckpointManifest(deps.store, deps.metadata, written, vectors.refs);
|
|
2054
|
+
recordWrittenVectors(deps.vectorIndexes, vectors.written);
|
|
2055
|
+
await adoptVectorLayouts(deps.store, deps.vectorIndexes, vectors.layouts);
|
|
2056
|
+
await removeCheckpointGarbage(
|
|
2057
|
+
deps.store,
|
|
2058
|
+
await garbageThatNoFieldStillReads(deps.store, deps.vectorIndexes, committed.garbage)
|
|
2059
|
+
);
|
|
2060
|
+
}
|
|
2061
|
+
async function upgradeEarlierLayout(deps, earlier) {
|
|
2062
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "started", layout: earlier.kind });
|
|
2063
|
+
try {
|
|
2064
|
+
const loaded = await loadEarlier(deps, earlier);
|
|
2065
|
+
const targets = await deps.replay(loaded.checkpoint);
|
|
2066
|
+
await writeCurrentLayout(deps, earlier, targets, loaded.fileRefs);
|
|
2067
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "completed", layout: earlier.kind });
|
|
2068
|
+
return { checkpoint: targets, documentCount: deps.manager.countDocuments() };
|
|
2069
|
+
} catch (error) {
|
|
2070
|
+
const failure = error instanceof Error ? error : new Error(String(error));
|
|
2071
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "failed", layout: earlier.kind, error: failure });
|
|
2072
|
+
throw failure;
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
// src/persistence/durability/checkpoint-recovery.ts
|
|
2077
|
+
async function earlierLayoutOf(deps, decoded) {
|
|
2078
|
+
if (decoded?.kind === "earlier") return { kind: "manifest", manifest: decodeEarlierManifest(decoded.payload) };
|
|
2079
|
+
if (decoded !== null) return null;
|
|
2080
|
+
const bundle = await deps.store.read(snapshotBundleKey(deps.indexName));
|
|
2081
|
+
return bundle === null ? null : { kind: "bundle", bytes: bundle };
|
|
2082
|
+
}
|
|
2083
|
+
async function recoverCheckpoint(deps) {
|
|
2084
|
+
const decoded = await readSegmentManifest(deps.store, deps.indexName);
|
|
2085
|
+
const earlier = await earlierLayoutOf(deps, decoded);
|
|
2086
|
+
if (earlier !== null) {
|
|
2087
|
+
const upgraded = await upgradeEarlierLayout(deps, earlier);
|
|
2088
|
+
return { checkpoint: upgraded.checkpoint, upgradedDocumentCount: upgraded.documentCount };
|
|
2089
|
+
}
|
|
2090
|
+
if (decoded === null || decoded.kind !== "current") {
|
|
2091
|
+
if (!deps.keepsUnlistedFiles) await reclaimUnlistedKeys(deps.store, deps.indexName, /* @__PURE__ */ new Set());
|
|
2092
|
+
return { checkpoint: await deps.replay([]), upgradedDocumentCount: null };
|
|
2093
|
+
}
|
|
2094
|
+
await deps.store.remove(snapshotBundleKey(deps.indexName));
|
|
2095
|
+
if (!deps.keepsUnlistedFiles) {
|
|
2096
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, manifestReferencedKeys(decoded.manifest));
|
|
2097
|
+
}
|
|
2098
|
+
const checkpoint = await loadSegmentedCheckpoint(deps.store, deps.indexName, decoded.manifest, {
|
|
2099
|
+
manager: deps.manager,
|
|
2100
|
+
vectorIndexes: deps.vectorIndexes
|
|
2101
|
+
});
|
|
2102
|
+
return { checkpoint: await deps.replay(checkpoint), upgradedDocumentCount: null };
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
// src/persistence/durability/recover-index.ts
|
|
2106
|
+
async function writeCountedMetadata(deps, indexName, documentCount) {
|
|
2107
|
+
await deps.queueMetadataWrite(indexName, async () => {
|
|
2108
|
+
const metadata = deps.hooks.buildMetadata(indexName, documentCount);
|
|
2109
|
+
if (metadata === void 0) return;
|
|
2110
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
2111
|
+
await deps.directory.atomicWrite(`${indexName}/meta`, bytes);
|
|
2112
|
+
}).catch(() => void 0);
|
|
2113
|
+
}
|
|
2114
|
+
async function recoverPersistedIndex(deps, indexName, metadataOnly) {
|
|
2115
|
+
const { directory, hooks } = deps;
|
|
2116
|
+
const store = directoryCheckpointStore(directory);
|
|
2117
|
+
const metadata = await loadMetadata(directory, indexName);
|
|
2118
|
+
if (metadata === null) {
|
|
2119
|
+
return;
|
|
2120
|
+
}
|
|
2121
|
+
let derivedCount;
|
|
2122
|
+
if (metadataOnly && metadata.documentCount === void 0) {
|
|
2123
|
+
try {
|
|
2124
|
+
derivedCount = await countCheckpointDocuments(store, indexName);
|
|
2125
|
+
metadata.documentCount = derivedCount;
|
|
2126
|
+
} catch {
|
|
2127
|
+
derivedCount = void 0;
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
await hooks.createIndexFromMetadata(metadata, !metadataOnly);
|
|
2131
|
+
if (metadataOnly) {
|
|
2132
|
+
if (derivedCount !== void 0) await writeCountedMetadata(deps, indexName, derivedCount);
|
|
2133
|
+
return;
|
|
2134
|
+
}
|
|
2135
|
+
const manager = hooks.getManager(indexName);
|
|
2136
|
+
const current = hooks.buildMetadata(indexName);
|
|
2137
|
+
if (manager === void 0 || current === void 0) {
|
|
2138
|
+
return;
|
|
2139
|
+
}
|
|
2140
|
+
const replayDeps = {
|
|
2141
|
+
manager,
|
|
2142
|
+
vectorFieldPaths: hooks.getVectorFieldPaths(indexName),
|
|
2143
|
+
vectorIndexes: hooks.getVectorIndexes(indexName)
|
|
2144
|
+
};
|
|
2145
|
+
const replay = async (checkpoint) => {
|
|
2146
|
+
const replayed = [];
|
|
2147
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
2148
|
+
const fromSeqNo = snapshotCheckpointFor(checkpoint, partitionId);
|
|
2149
|
+
const { highestSeqNo, highestPrimaryTerm } = await replayWal(
|
|
2150
|
+
directory,
|
|
2151
|
+
indexName,
|
|
2152
|
+
partitionId,
|
|
2153
|
+
fromSeqNo,
|
|
2154
|
+
replayDeps
|
|
2155
|
+
);
|
|
2156
|
+
const primaryTerm = Math.max(
|
|
2157
|
+
checkpointPrimaryTerm(checkpoint, partitionId),
|
|
2158
|
+
highestPrimaryTerm,
|
|
2159
|
+
SINGLE_NODE_PRIMARY_TERM
|
|
2160
|
+
);
|
|
2161
|
+
replayed.push({ partitionId, lastSeqNo: highestSeqNo, primaryTerm });
|
|
2162
|
+
}
|
|
2163
|
+
return replayed;
|
|
2164
|
+
};
|
|
2165
|
+
const recovered = await recoverCheckpoint({
|
|
2166
|
+
store,
|
|
2167
|
+
indexName,
|
|
2168
|
+
metadata: current,
|
|
2169
|
+
manager,
|
|
2170
|
+
vectorIndexes: replayDeps.vectorIndexes,
|
|
2171
|
+
storedTermsAreCurrent: metadata.analysisRevision === manager.language.revision,
|
|
2172
|
+
compactionThreshold: deps.compactionThreshold,
|
|
2173
|
+
keepsUnlistedFiles: deps.holdsCheckpointFiles(indexName),
|
|
2174
|
+
replay,
|
|
2175
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade
|
|
2176
|
+
});
|
|
2177
|
+
for (const { partitionId, lastSeqNo } of recovered.checkpoint) {
|
|
2178
|
+
deps.registerPartition(indexName, partitionId, lastSeqNo);
|
|
2179
|
+
}
|
|
2180
|
+
if (recovered.upgradedDocumentCount !== null) {
|
|
2181
|
+
await writeCountedMetadata(deps, indexName, recovered.upgradedDocumentCount);
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
// src/persistence/durability/group-commit.ts
|
|
2186
|
+
function createGroupCommitCoordinator(sync) {
|
|
2187
|
+
let inFlight = null;
|
|
2188
|
+
let next = null;
|
|
2189
|
+
function makeBatch() {
|
|
2190
|
+
let resolve = () => void 0;
|
|
2191
|
+
let reject = () => void 0;
|
|
2192
|
+
const promise = new Promise((res, rej) => {
|
|
2193
|
+
resolve = res;
|
|
2194
|
+
reject = rej;
|
|
2195
|
+
});
|
|
2196
|
+
return { promise, resolve, reject, waiters: 0 };
|
|
2197
|
+
}
|
|
2198
|
+
function drain() {
|
|
2199
|
+
if (inFlight !== null || next === null) {
|
|
2200
|
+
return;
|
|
2201
|
+
}
|
|
2202
|
+
const batch = next;
|
|
2203
|
+
next = null;
|
|
2204
|
+
inFlight = sync().then(() => {
|
|
2205
|
+
batch.resolve();
|
|
2206
|
+
}).catch((err) => {
|
|
2207
|
+
batch.reject(err);
|
|
2208
|
+
}).finally(() => {
|
|
2209
|
+
inFlight = null;
|
|
2210
|
+
drain();
|
|
2211
|
+
});
|
|
2212
|
+
}
|
|
2213
|
+
return {
|
|
2214
|
+
commit() {
|
|
2215
|
+
if (next === null) {
|
|
2216
|
+
next = makeBatch();
|
|
2217
|
+
}
|
|
2218
|
+
next.waiters += 1;
|
|
2219
|
+
const promise = next.promise;
|
|
2220
|
+
drain();
|
|
2221
|
+
return promise;
|
|
2222
|
+
},
|
|
2223
|
+
get pendingCount() {
|
|
2224
|
+
return next === null ? 0 : next.waiters;
|
|
2225
|
+
}
|
|
2226
|
+
};
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
// src/persistence/durability/marker-writer.ts
|
|
2230
|
+
function markerKey(indexName, partitionId) {
|
|
2231
|
+
return `${indexName}/wal/${partitionId}/commit`;
|
|
2232
|
+
}
|
|
2233
|
+
async function createMarkerWriter(directory, indexName, partitionId) {
|
|
2234
|
+
const key = markerKey(indexName, partitionId);
|
|
2235
|
+
const handle = await directory.markerHandle(key);
|
|
2236
|
+
const existing = await handle.read();
|
|
2237
|
+
const current = readCommitMarker(existing);
|
|
2238
|
+
const createdFresh = existing.length === 0;
|
|
2239
|
+
let lastSlot = current?.slotIndex ?? null;
|
|
2240
|
+
let writeSeq = current?.state.writeSeq ?? 0;
|
|
2241
|
+
return {
|
|
2242
|
+
created: createdFresh,
|
|
2243
|
+
existingHighestDurableSeqNo: current?.state.highestDurableSeqNo ?? 0,
|
|
2244
|
+
async commit(state) {
|
|
2245
|
+
const slotIndex = nextMarkerSlot(lastSlot);
|
|
2246
|
+
writeSeq += 1;
|
|
2247
|
+
const slot = encodeMarkerSlot({ ...state, writeSeq });
|
|
2248
|
+
await handle.writeSlot(markerSlotOffset(slotIndex), slot);
|
|
2249
|
+
lastSlot = slotIndex;
|
|
2250
|
+
},
|
|
2251
|
+
async close() {
|
|
2252
|
+
await handle.close();
|
|
2253
|
+
}
|
|
2254
|
+
};
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
// src/persistence/durability/wal-writer.ts
|
|
2258
|
+
var SEGMENT_TAIL_PATTERN = /^\d{16}$/;
|
|
2259
|
+
function segmentKey(indexName, partitionId, startSeqNo) {
|
|
2260
|
+
const padded = startSeqNo.toString().padStart(16, "0");
|
|
2261
|
+
return `${indexName}/wal/${partitionId}/${padded}`;
|
|
2262
|
+
}
|
|
2263
|
+
function parseSegmentStartSeqNo2(key) {
|
|
2264
|
+
const tail = key.slice(key.lastIndexOf("/") + 1);
|
|
2265
|
+
if (!SEGMENT_TAIL_PATTERN.test(tail)) {
|
|
2266
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Malformed WAL segment key: "${key}"`, { key });
|
|
2267
|
+
}
|
|
2268
|
+
const value = Number.parseInt(tail, 10);
|
|
2269
|
+
if (!Number.isSafeInteger(value)) {
|
|
2270
|
+
throw new NarsilError(
|
|
2271
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
2272
|
+
`WAL segment start sequence number is out of range: "${key}"`,
|
|
2273
|
+
{
|
|
2274
|
+
key
|
|
2275
|
+
}
|
|
2276
|
+
);
|
|
2277
|
+
}
|
|
2278
|
+
return value;
|
|
2279
|
+
}
|
|
2280
|
+
function createWalWriter(directory, config) {
|
|
2281
|
+
const segmentMaxBytes = config.segmentMaxBytes ?? DEFAULT_SEGMENT_MAX_BYTES;
|
|
2282
|
+
const commitKey = `${config.indexName}/wal/${config.partitionId}/commit`;
|
|
2283
|
+
let handle = null;
|
|
2284
|
+
let activeKey = null;
|
|
2285
|
+
let activeStartSeqNo = 0;
|
|
2286
|
+
let activeBytes = 0;
|
|
2287
|
+
let highestAppendedSeqNo = 0;
|
|
2288
|
+
let highestDurableSeqNo = 0;
|
|
2289
|
+
let markerWriter = null;
|
|
2290
|
+
let durabilityLock = Promise.resolve();
|
|
2291
|
+
const coordinator = createGroupCommitCoordinator(() => withDurabilityLock(flushActiveSegment));
|
|
2292
|
+
function withDurabilityLock(fn) {
|
|
2293
|
+
const result = durabilityLock.then(fn);
|
|
2294
|
+
durabilityLock = result.then(
|
|
2295
|
+
() => void 0,
|
|
2296
|
+
() => void 0
|
|
2297
|
+
);
|
|
2298
|
+
return result;
|
|
2299
|
+
}
|
|
2300
|
+
async function getMarkerWriter() {
|
|
2301
|
+
if (markerWriter === null) {
|
|
2302
|
+
const writer = await createMarkerWriter(directory, config.indexName, config.partitionId);
|
|
2303
|
+
markerWriter = writer;
|
|
2304
|
+
if (writer.existingHighestDurableSeqNo > highestDurableSeqNo) {
|
|
2305
|
+
highestDurableSeqNo = writer.existingHighestDurableSeqNo;
|
|
2306
|
+
}
|
|
2307
|
+
if (writer.created) {
|
|
2308
|
+
await directory.syncDirectoryOf(commitKey);
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
return markerWriter;
|
|
2312
|
+
}
|
|
2313
|
+
async function flushActiveSegment() {
|
|
2314
|
+
if (handle === null) {
|
|
2315
|
+
return;
|
|
2316
|
+
}
|
|
2317
|
+
const writer = await getMarkerWriter();
|
|
2318
|
+
const durableByteLength = activeBytes;
|
|
2319
|
+
const activeSegmentSeqNo = activeStartSeqNo;
|
|
2320
|
+
const durableSeqNo = Math.max(highestAppendedSeqNo, highestDurableSeqNo);
|
|
2321
|
+
await handle.sync();
|
|
2322
|
+
await writer.commit({ activeSegmentSeqNo, durableByteLength, highestDurableSeqNo: durableSeqNo });
|
|
2323
|
+
if (durableSeqNo > highestDurableSeqNo) {
|
|
2324
|
+
highestDurableSeqNo = durableSeqNo;
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
async function openSegmentByKey(key) {
|
|
2328
|
+
const nextHandle = await directory.appendHandle(key);
|
|
2329
|
+
const existingSize = await nextHandle.size();
|
|
2330
|
+
let createdNew = false;
|
|
2331
|
+
if (existingSize === 0) {
|
|
2332
|
+
await nextHandle.append(writeSegmentHeader());
|
|
2333
|
+
activeBytes = SEGMENT_HEADER_SIZE;
|
|
2334
|
+
createdNew = true;
|
|
2335
|
+
} else {
|
|
2336
|
+
activeBytes = existingSize;
|
|
2337
|
+
}
|
|
2338
|
+
handle = nextHandle;
|
|
2339
|
+
activeKey = key;
|
|
2340
|
+
activeStartSeqNo = parseSegmentStartSeqNo2(key);
|
|
2341
|
+
if (createdNew) {
|
|
2342
|
+
await directory.syncDirectoryOf(key);
|
|
2343
|
+
}
|
|
2344
|
+
return createdNew;
|
|
2345
|
+
}
|
|
2346
|
+
async function findActiveSegmentKey() {
|
|
2347
|
+
const prefix = `${config.indexName}/wal/${config.partitionId}/`;
|
|
2348
|
+
const keys = (await directory.list(prefix)).filter((k) => SEGMENT_TAIL_PATTERN.test(k.slice(prefix.length))).sort(compareCodePoints);
|
|
2349
|
+
return keys.length > 0 ? keys[keys.length - 1] : null;
|
|
2350
|
+
}
|
|
2351
|
+
async function ensureSegment(seqNo) {
|
|
2352
|
+
if (handle !== null) {
|
|
2353
|
+
return;
|
|
2354
|
+
}
|
|
2355
|
+
const existing = await findActiveSegmentKey();
|
|
2356
|
+
const createdNew = await openSegmentByKey(existing ?? segmentKey(config.indexName, config.partitionId, seqNo));
|
|
2357
|
+
if (createdNew) {
|
|
2358
|
+
await withDurabilityLock(flushActiveSegment);
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
async function rollToNewSegment(startSeqNo) {
|
|
2362
|
+
await withDurabilityLock(async () => {
|
|
2363
|
+
if (handle !== null) {
|
|
2364
|
+
await flushActiveSegment();
|
|
2365
|
+
await handle.close();
|
|
2366
|
+
handle = null;
|
|
2367
|
+
}
|
|
2368
|
+
await openSegmentByKey(segmentKey(config.indexName, config.partitionId, startSeqNo));
|
|
2369
|
+
await flushActiveSegment();
|
|
2370
|
+
});
|
|
2371
|
+
}
|
|
2372
|
+
async function maybeRoll(nextSeqNo, appendedBytes) {
|
|
2373
|
+
if (handle === null || activeBytes <= SEGMENT_HEADER_SIZE) {
|
|
2374
|
+
return;
|
|
2375
|
+
}
|
|
2376
|
+
if (activeBytes >= segmentMaxBytes || activeBytes + appendedBytes > segmentMaxBytes) {
|
|
2377
|
+
await rollToNewSegment(nextSeqNo);
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
function joinFrames(frames) {
|
|
2381
|
+
if (frames.length === 1) {
|
|
2382
|
+
return frames[0];
|
|
2383
|
+
}
|
|
2384
|
+
let totalBytes = 0;
|
|
2385
|
+
for (const frame of frames) {
|
|
2386
|
+
totalBytes += frame.length;
|
|
2387
|
+
}
|
|
2388
|
+
const joined = new Uint8Array(totalBytes);
|
|
2389
|
+
let offset = 0;
|
|
2390
|
+
for (const frame of frames) {
|
|
2391
|
+
joined.set(frame, offset);
|
|
2392
|
+
offset += frame.length;
|
|
2393
|
+
}
|
|
2394
|
+
return joined;
|
|
2395
|
+
}
|
|
2396
|
+
function framesThatFit(frames, start) {
|
|
2397
|
+
const room = segmentMaxBytes - activeBytes;
|
|
2398
|
+
let end = start + 1;
|
|
2399
|
+
let bytes = frames[start].length;
|
|
2400
|
+
while (end < frames.length && bytes + frames[end].length <= room) {
|
|
2401
|
+
bytes += frames[end].length;
|
|
2402
|
+
end += 1;
|
|
2403
|
+
}
|
|
2404
|
+
return end;
|
|
2405
|
+
}
|
|
2406
|
+
async function appendGroup(frames, entries) {
|
|
2407
|
+
const activeHandle = handle;
|
|
2408
|
+
if (activeHandle === null) {
|
|
2409
|
+
throw new NarsilError(
|
|
2410
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2411
|
+
`WAL segment for "${config.indexName}" partition ${config.partitionId} is not open`,
|
|
2412
|
+
{ indexName: config.indexName, partitionId: config.partitionId }
|
|
2413
|
+
);
|
|
2414
|
+
}
|
|
2415
|
+
const joined = joinFrames([...frames]);
|
|
2416
|
+
if (activeBytes + joined.length > MAX_FILE_BYTES) {
|
|
2417
|
+
throw new NarsilError(
|
|
2418
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2419
|
+
`A log record of ${joined.length} bytes would carry WAL segment "${activeKey}" past the file size limit of ${MAX_FILE_BYTES} bytes`,
|
|
2420
|
+
{ indexName: config.indexName, partitionId: config.partitionId, bytes: joined.length }
|
|
2421
|
+
);
|
|
2422
|
+
}
|
|
2423
|
+
await activeHandle.append(joined);
|
|
2424
|
+
activeBytes += joined.length;
|
|
2425
|
+
const lastSeqNo = entries[entries.length - 1].seqNo;
|
|
2426
|
+
if (lastSeqNo > highestAppendedSeqNo) {
|
|
2427
|
+
highestAppendedSeqNo = lastSeqNo;
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
async function appendFrames(entries) {
|
|
2431
|
+
if (entries.length === 0) {
|
|
2432
|
+
return;
|
|
2433
|
+
}
|
|
2434
|
+
const frames = entries.map(frameRecord);
|
|
2435
|
+
await ensureSegment(entries[0].seqNo);
|
|
2436
|
+
let start = 0;
|
|
2437
|
+
while (start < frames.length) {
|
|
2438
|
+
await maybeRoll(entries[start].seqNo, frames[start].length);
|
|
2439
|
+
const end = framesThatFit(frames, start);
|
|
2440
|
+
await appendGroup(frames.slice(start, end), entries.slice(start, end));
|
|
2441
|
+
start = end;
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
return {
|
|
2445
|
+
async appendAll(entries) {
|
|
2446
|
+
await appendFrames(entries);
|
|
2447
|
+
},
|
|
2448
|
+
async appendDurable(entry) {
|
|
2449
|
+
await appendFrames([entry]);
|
|
2450
|
+
await coordinator.commit();
|
|
2451
|
+
},
|
|
2452
|
+
async commit() {
|
|
2453
|
+
await coordinator.commit();
|
|
2454
|
+
},
|
|
2455
|
+
rollToNewSegment,
|
|
2456
|
+
async close() {
|
|
2457
|
+
await withDurabilityLock(async () => {
|
|
2458
|
+
if (handle !== null) {
|
|
2459
|
+
await flushActiveSegment();
|
|
2460
|
+
await handle.close();
|
|
2461
|
+
handle = null;
|
|
2462
|
+
}
|
|
2463
|
+
if (markerWriter !== null) {
|
|
2464
|
+
await markerWriter.close();
|
|
2465
|
+
markerWriter = null;
|
|
2466
|
+
}
|
|
2467
|
+
activeKey = null;
|
|
2468
|
+
});
|
|
2469
|
+
},
|
|
2470
|
+
get activeSegmentKey() {
|
|
2471
|
+
return activeKey;
|
|
2472
|
+
}
|
|
2473
|
+
};
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
// src/persistence/durability/manager.ts
|
|
2477
|
+
function createDurabilityManager(config, hooks, directoryOverride) {
|
|
2478
|
+
const directory = createDurableDirectory(config.directory ?? "");
|
|
2479
|
+
const canOffloadCheckpoint = directoryOverride === void 0;
|
|
2480
|
+
const segmentMaxBytes = config.segmentMaxBytes ?? DEFAULT_SEGMENT_MAX_BYTES;
|
|
2481
|
+
const checkpointIntervalMs = config.checkpointIntervalMs ?? DEFAULT_CHECKPOINT_INTERVAL_MS;
|
|
2482
|
+
const checkpointMutationThreshold = config.checkpointMutationThreshold ?? DEFAULT_CHECKPOINT_MUTATION_THRESHOLD;
|
|
2483
|
+
const compactionThreshold = config.compactionThreshold ?? DEFAULT_COMPACTION_THRESHOLD;
|
|
2484
|
+
const mode = config.mode ?? "sync";
|
|
2485
|
+
const flushIntervalMs = config.flushIntervalMs ?? DEFAULT_ASYNC_FLUSH_INTERVAL_MS;
|
|
2486
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
2487
|
+
const metadataWrites = /* @__PURE__ */ new Map();
|
|
2488
|
+
let checkpointTimer = null;
|
|
2489
|
+
let asyncFlushTimer = null;
|
|
2490
|
+
let shuttingDown = false;
|
|
2491
|
+
let fatalError = null;
|
|
2492
|
+
const heldCheckpointFiles = /* @__PURE__ */ new Map();
|
|
2493
|
+
const directoryWatch = createDirectoryWatch(directory, () => shuttingDown || fatalError !== null, markFatal);
|
|
2494
|
+
function stopTimers() {
|
|
2495
|
+
directoryWatch.stop();
|
|
2496
|
+
if (checkpointTimer !== null) clearInterval(checkpointTimer);
|
|
2497
|
+
if (asyncFlushTimer !== null) clearInterval(asyncFlushTimer);
|
|
2498
|
+
checkpointTimer = null;
|
|
2499
|
+
asyncFlushTimer = null;
|
|
2500
|
+
}
|
|
2501
|
+
function markFatal(error) {
|
|
2502
|
+
if (fatalError !== null) return;
|
|
2503
|
+
fatalError = error;
|
|
2504
|
+
stopTimers();
|
|
2505
|
+
hooks.onFatalError(error);
|
|
2506
|
+
}
|
|
2507
|
+
function toError2(err) {
|
|
2508
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
2509
|
+
}
|
|
2510
|
+
async function closeWriterReportingFailure(closePromise) {
|
|
2511
|
+
try {
|
|
2512
|
+
await closePromise;
|
|
2513
|
+
} catch (err) {
|
|
2514
|
+
markFatal(toError2(err));
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2517
|
+
function getOrCreateIndexState(indexName) {
|
|
2518
|
+
let state = indexes.get(indexName);
|
|
2519
|
+
if (state === void 0) {
|
|
2520
|
+
state = {
|
|
2521
|
+
partitions: /* @__PURE__ */ new Map(),
|
|
2522
|
+
mutationsSinceCheckpoint: 0,
|
|
2523
|
+
documentBytesSinceCheckpoint: 0,
|
|
2524
|
+
checkpointInFlight: null,
|
|
2525
|
+
unloading: false,
|
|
2526
|
+
stalledWrites: []
|
|
2527
|
+
};
|
|
2528
|
+
indexes.set(indexName, state);
|
|
2529
|
+
}
|
|
2530
|
+
return state;
|
|
2531
|
+
}
|
|
2532
|
+
function getOrCreatePartition(indexName, partitionId, startSeqNo) {
|
|
2533
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
2534
|
+
let partition = indexState.partitions.get(partitionId);
|
|
2535
|
+
if (partition === void 0) {
|
|
2536
|
+
partition = {
|
|
2537
|
+
walWriter: createWalWriter(directory, { indexName, partitionId, segmentMaxBytes }),
|
|
2538
|
+
seqOwner: createSeqOwner(startSeqNo),
|
|
2539
|
+
appendChain: Promise.resolve(),
|
|
2540
|
+
appliedSeqNo: startSeqNo,
|
|
2541
|
+
failed: null
|
|
2542
|
+
};
|
|
2543
|
+
indexState.partitions.set(partitionId, partition);
|
|
2544
|
+
}
|
|
2545
|
+
return partition;
|
|
2546
|
+
}
|
|
2547
|
+
function queueMetadataWrite(indexName, write) {
|
|
2548
|
+
const previous = metadataWrites.get(indexName) ?? Promise.resolve();
|
|
2549
|
+
const queued = previous.then(write);
|
|
2550
|
+
metadataWrites.set(
|
|
2551
|
+
indexName,
|
|
2552
|
+
queued.catch(() => void 0)
|
|
2553
|
+
);
|
|
2554
|
+
return queued;
|
|
2555
|
+
}
|
|
2556
|
+
function startCheckpointTimer() {
|
|
2557
|
+
if (checkpointTimer !== null || shuttingDown || fatalError !== null || checkpointIntervalMs <= 0) {
|
|
2558
|
+
return;
|
|
2559
|
+
}
|
|
2560
|
+
checkpointTimer = unrefInterval(() => void runScheduledCheckpoints(), checkpointIntervalMs);
|
|
2561
|
+
}
|
|
2562
|
+
function startAsyncFlushTimer() {
|
|
2563
|
+
if (mode !== "async" || asyncFlushTimer !== null || shuttingDown || fatalError !== null || flushIntervalMs <= 0) {
|
|
2564
|
+
return;
|
|
2565
|
+
}
|
|
2566
|
+
asyncFlushTimer = unrefInterval(() => void flushAllPartitions(), flushIntervalMs);
|
|
2567
|
+
}
|
|
2568
|
+
async function flushAllPartitions() {
|
|
2569
|
+
if (fatalError !== null) {
|
|
2570
|
+
return;
|
|
2571
|
+
}
|
|
2572
|
+
for (const indexState of indexes.values()) {
|
|
2573
|
+
for (const partition of indexState.partitions.values()) {
|
|
2574
|
+
if (partition.failed !== null || fatalError !== null) {
|
|
2575
|
+
continue;
|
|
2576
|
+
}
|
|
2577
|
+
try {
|
|
2578
|
+
await partition.walWriter.commit();
|
|
2579
|
+
} catch (err) {
|
|
2580
|
+
partition.failed = toError2(err);
|
|
2581
|
+
markFatal(partition.failed);
|
|
2582
|
+
}
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
}
|
|
2586
|
+
async function runScheduledCheckpoints() {
|
|
2587
|
+
if (fatalError !== null) {
|
|
2588
|
+
return;
|
|
2589
|
+
}
|
|
2590
|
+
for (const indexName of [...indexes.keys()]) {
|
|
2591
|
+
try {
|
|
2592
|
+
await checkpointIndex(indexName);
|
|
2593
|
+
} catch (err) {
|
|
2594
|
+
markFatal(toError2(err));
|
|
2595
|
+
}
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
function buildMutationEntry(record, seqNo) {
|
|
2599
|
+
return buildEntry({
|
|
2600
|
+
seqNo,
|
|
2601
|
+
primaryTerm: SINGLE_NODE_PRIMARY_TERM,
|
|
2602
|
+
operation: record.operation,
|
|
2603
|
+
partitionId: record.partitionId,
|
|
2604
|
+
indexName: record.indexName,
|
|
2605
|
+
documentId: record.documentId,
|
|
2606
|
+
document: record.document
|
|
2607
|
+
});
|
|
2608
|
+
}
|
|
2609
|
+
async function checkpointIndex(indexName, fromMemory = false) {
|
|
2610
|
+
const indexState = indexes.get(indexName);
|
|
2611
|
+
if (indexState === void 0 || indexState.unloading) {
|
|
2612
|
+
return;
|
|
2613
|
+
}
|
|
2614
|
+
if (indexState.checkpointInFlight !== null) {
|
|
2615
|
+
await indexState.checkpointInFlight;
|
|
2616
|
+
if (!fromMemory) {
|
|
2617
|
+
return;
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
const run = performCheckpoint(indexName, indexState, fromMemory).finally(() => {
|
|
2621
|
+
indexState.checkpointInFlight = null;
|
|
2622
|
+
releaseStalledWrites(indexState);
|
|
2623
|
+
});
|
|
2624
|
+
indexState.checkpointInFlight = run;
|
|
2625
|
+
return run;
|
|
2626
|
+
}
|
|
2627
|
+
async function performCheckpoint(indexName, indexState, fromMemory = false) {
|
|
2628
|
+
await directoryWatch.verify();
|
|
2629
|
+
if (fatalError !== null) throw fatalError;
|
|
2630
|
+
await runDurableCheckpoint({
|
|
2631
|
+
directory,
|
|
2632
|
+
hooks,
|
|
2633
|
+
indexName,
|
|
2634
|
+
indexState,
|
|
2635
|
+
compactionThreshold,
|
|
2636
|
+
canOffload: canOffloadCheckpoint,
|
|
2637
|
+
fromMemory,
|
|
2638
|
+
mutationThreshold: checkpointMutationThreshold,
|
|
2639
|
+
mayContinue: () => !indexState.unloading && !shuttingDown && fatalError === null,
|
|
2640
|
+
holdsCheckpointFiles: () => heldCheckpointFiles.has(indexName),
|
|
2641
|
+
queueMetadataWrite,
|
|
2642
|
+
markFatal
|
|
2643
|
+
});
|
|
2644
|
+
}
|
|
2645
|
+
const partitionBatchDeps = {
|
|
2646
|
+
syncEachBatch: mode === "sync",
|
|
2647
|
+
fatalError: () => fatalError,
|
|
2648
|
+
markFatal,
|
|
2649
|
+
buildEntry: buildMutationEntry
|
|
2650
|
+
};
|
|
2651
|
+
async function recordMutations(records) {
|
|
2652
|
+
if (fatalError !== null) {
|
|
2653
|
+
const error = fatalError;
|
|
2654
|
+
return records.map(() => ({ ok: false, error }));
|
|
2655
|
+
}
|
|
2656
|
+
const outcomes = await recordBatchesByPartition(
|
|
2657
|
+
records,
|
|
2658
|
+
(record) => getOrCreatePartition(record.indexName, record.partitionId, 0),
|
|
2659
|
+
partitionBatchDeps
|
|
2660
|
+
);
|
|
2661
|
+
const recordedByIndex = /* @__PURE__ */ new Map();
|
|
2662
|
+
for (let i = 0; i < records.length; i++) {
|
|
2663
|
+
if (!outcomes[i].ok) continue;
|
|
2664
|
+
const recorded = recordedByIndex.get(records[i].indexName) ?? { records: 0, documentBytes: 0 };
|
|
2665
|
+
recorded.records += 1;
|
|
2666
|
+
recorded.documentBytes += records[i].document?.byteLength ?? 0;
|
|
2667
|
+
recordedByIndex.set(records[i].indexName, recorded);
|
|
2668
|
+
}
|
|
2669
|
+
for (const [indexName, recorded] of recordedByIndex) {
|
|
2670
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
2671
|
+
indexState.mutationsSinceCheckpoint += recorded.records;
|
|
2672
|
+
indexState.documentBytesSinceCheckpoint += recorded.documentBytes;
|
|
2673
|
+
if (checkpointIsDue(indexState, checkpointMutationThreshold)) {
|
|
2674
|
+
void checkpointIndex(indexName).catch((err) => {
|
|
2675
|
+
markFatal(toError2(err));
|
|
2676
|
+
});
|
|
2677
|
+
}
|
|
2678
|
+
await stallWhileCheckpointsFallBehind(indexState);
|
|
2679
|
+
}
|
|
2680
|
+
if (recordedByIndex.size === 0) return outcomes;
|
|
2681
|
+
if (mode === "sync") await directoryWatch.verify();
|
|
2682
|
+
const lost = mode === "sync" ? fatalError : null;
|
|
2683
|
+
if (lost !== null) return outcomes.map((outcome) => outcome.ok ? { ok: false, error: lost } : outcome);
|
|
2684
|
+
await directoryWatch.verifyOnce();
|
|
2685
|
+
directoryWatch.start();
|
|
2686
|
+
startCheckpointTimer();
|
|
2687
|
+
startAsyncFlushTimer();
|
|
2688
|
+
return outcomes;
|
|
2689
|
+
}
|
|
2690
|
+
function recoverIndex(indexName, metadataOnly = false) {
|
|
2691
|
+
return recoverPersistedIndex(
|
|
2692
|
+
{
|
|
2693
|
+
directory,
|
|
2694
|
+
hooks,
|
|
2695
|
+
compactionThreshold,
|
|
2696
|
+
registerPartition: (name, partitionId, startSeqNo) => {
|
|
2697
|
+
getOrCreatePartition(name, partitionId, startSeqNo);
|
|
2698
|
+
},
|
|
2699
|
+
holdsCheckpointFiles: (name) => heldCheckpointFiles.has(name),
|
|
2700
|
+
queueMetadataWrite
|
|
2701
|
+
},
|
|
2702
|
+
indexName,
|
|
2703
|
+
metadataOnly
|
|
2704
|
+
);
|
|
2705
|
+
}
|
|
2706
|
+
return {
|
|
2707
|
+
isActive() {
|
|
2708
|
+
return true;
|
|
2709
|
+
},
|
|
2710
|
+
async recover(metadataOnly = false) {
|
|
2711
|
+
await directoryWatch.claim();
|
|
2712
|
+
await removeAbandonedRestores(directory);
|
|
2713
|
+
const names = await listPersistedIndexes(directory);
|
|
2714
|
+
for (const indexName of names) {
|
|
2715
|
+
await recoverIndex(indexName, metadataOnly);
|
|
2716
|
+
}
|
|
2717
|
+
await directoryWatch.verify();
|
|
2718
|
+
directoryWatch.start();
|
|
2719
|
+
startCheckpointTimer();
|
|
2720
|
+
},
|
|
2721
|
+
recoverIndex(indexName) {
|
|
2722
|
+
return recoverIndex(indexName);
|
|
2723
|
+
},
|
|
2724
|
+
highestPersistedSeqNo(indexName, partitionId) {
|
|
2725
|
+
return indexes.get(indexName)?.partitions.get(partitionId)?.appliedSeqNo ?? 0;
|
|
2726
|
+
},
|
|
2727
|
+
async recordMutation(record) {
|
|
2728
|
+
const [outcome] = await recordMutations([record]);
|
|
2729
|
+
if (!outcome.ok) {
|
|
2730
|
+
throw outcome.error;
|
|
2731
|
+
}
|
|
2732
|
+
return outcome.seqNo;
|
|
2733
|
+
},
|
|
2734
|
+
recordMutations,
|
|
2735
|
+
persistMetadata(indexName) {
|
|
2736
|
+
return queueMetadataWrite(indexName, async () => {
|
|
2737
|
+
const metadata = hooks.buildMetadata(indexName);
|
|
2738
|
+
if (metadata === void 0) {
|
|
2739
|
+
return;
|
|
2740
|
+
}
|
|
2741
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
2742
|
+
await directory.atomicWrite(`${indexName}/meta`, bytes);
|
|
2743
|
+
getOrCreateIndexState(indexName);
|
|
2744
|
+
});
|
|
2745
|
+
},
|
|
2746
|
+
checkpoint(indexName) {
|
|
2747
|
+
return checkpointIndex(indexName);
|
|
2748
|
+
},
|
|
2749
|
+
checkpointFromMemory(indexName) {
|
|
2750
|
+
return checkpointIndex(indexName, true);
|
|
2751
|
+
},
|
|
2752
|
+
async checkpointAll() {
|
|
2753
|
+
for (const indexName of [...indexes.keys()]) {
|
|
2754
|
+
await checkpointIndex(indexName);
|
|
2755
|
+
}
|
|
2756
|
+
},
|
|
2757
|
+
async removeIndex(indexName) {
|
|
2758
|
+
const indexState = indexes.get(indexName);
|
|
2759
|
+
if (indexState !== void 0) {
|
|
2760
|
+
await drainIndexStateForUnload(indexState, metadataWrites.get(indexName), closeWriterReportingFailure);
|
|
2761
|
+
indexes.delete(indexName);
|
|
2762
|
+
metadataWrites.delete(indexName);
|
|
2763
|
+
}
|
|
2764
|
+
for (const vectorIndex of hooks.getVectorIndexes(indexName).values()) await vectorIndex.releaseVectorFiles();
|
|
2765
|
+
for (const key of await directory.list(`${indexName}/`)) {
|
|
2766
|
+
await directory.remove(key);
|
|
2767
|
+
}
|
|
2768
|
+
},
|
|
2769
|
+
async unloadIndex(indexName) {
|
|
2770
|
+
const indexState = indexes.get(indexName);
|
|
2771
|
+
if (indexState === void 0) {
|
|
2772
|
+
return;
|
|
2773
|
+
}
|
|
2774
|
+
await drainIndexStateForUnload(indexState, metadataWrites.get(indexName), (close) => close);
|
|
2775
|
+
indexes.delete(indexName);
|
|
2776
|
+
metadataWrites.delete(indexName);
|
|
2777
|
+
},
|
|
2778
|
+
...createArchiveCheckpointAccess({
|
|
2779
|
+
directory,
|
|
2780
|
+
held: heldCheckpointFiles,
|
|
2781
|
+
checkpoint: (name) => checkpointIndex(name),
|
|
2782
|
+
registerPartition: (name, partitionId, startSeqNo) => getOrCreatePartition(name, partitionId, startSeqNo)
|
|
2783
|
+
}),
|
|
2784
|
+
async shutdown() {
|
|
2785
|
+
shuttingDown = true;
|
|
2786
|
+
stopTimers();
|
|
2787
|
+
for (const indexState of indexes.values()) {
|
|
2788
|
+
if (indexState.checkpointInFlight !== null) {
|
|
2789
|
+
await indexState.checkpointInFlight.catch(() => void 0);
|
|
2790
|
+
}
|
|
2791
|
+
for (const partition of indexState.partitions.values()) {
|
|
2792
|
+
await closeWriterReportingFailure(partition.walWriter.close());
|
|
2793
|
+
}
|
|
2794
|
+
}
|
|
2795
|
+
indexes.clear();
|
|
2796
|
+
terminateCheckpointWorker();
|
|
2797
|
+
await directoryWatch.release();
|
|
2798
|
+
}
|
|
2799
|
+
};
|
|
2800
|
+
}
|
|
2801
|
+
|
|
2802
|
+
// src/persistence/durability/snapshot-only-checkpoint.ts
|
|
2803
|
+
function capture(input, prior) {
|
|
2804
|
+
const listedIds = new Set((prior?.partitions ?? []).map((partition) => partition.partitionId));
|
|
2805
|
+
const partitionCountChanged = prior !== null && listedIds.size !== input.manager.partitionCount;
|
|
2806
|
+
const targets = [];
|
|
2807
|
+
const contents = [];
|
|
2808
|
+
for (let partitionId = 0; partitionId < input.manager.partitionCount; partitionId += 1) {
|
|
2809
|
+
const lastSeqNo = input.appliedSeqNoByPartition.get(partitionId) ?? 0;
|
|
2810
|
+
const unchanged = !input.rewritesEveryPartition && prior !== null && !partitionCountChanged && listedIds.has(partitionId) && checkpointLastSeqNo(prior.checkpoint, partitionId) === lastSeqNo;
|
|
2811
|
+
if (unchanged) continue;
|
|
2812
|
+
targets.push({ partitionId, lastSeqNo, primaryTerm: SINGLE_NODE_PRIMARY_TERM });
|
|
2813
|
+
contents.push(partitionContentsOf(input.manager, partitionId));
|
|
2814
|
+
}
|
|
2815
|
+
const plans = /* @__PURE__ */ new Map();
|
|
2816
|
+
for (const [fieldPath, vectorIndex] of input.vectorIndexes) {
|
|
2817
|
+
const listed = prior?.vectors.find((ref) => ref.fieldPath === fieldPath);
|
|
2818
|
+
plans.set(fieldPath, vectorIndex.planCheckpoint(listed === void 0 ? null : listed.files.map((file) => file.key)));
|
|
2819
|
+
}
|
|
2820
|
+
return { targets, plans, documentCount: input.manager.countDocuments(), contents };
|
|
2821
|
+
}
|
|
2822
|
+
async function writeCaptured(input, store, prior, captured) {
|
|
2823
|
+
const partitionCount = input.manager.partitionCount;
|
|
2824
|
+
const priorWithinCount = prior === null ? null : {
|
|
2825
|
+
checkpoint: prior.checkpoint.filter((entry) => entry.partitionId < partitionCount),
|
|
2826
|
+
partitions: prior.partitions.filter((partition) => partition.partitionId < partitionCount)
|
|
2827
|
+
};
|
|
2828
|
+
const written = await writeCheckpointSegments({
|
|
2829
|
+
store,
|
|
2830
|
+
metadata: input.metadata,
|
|
2831
|
+
targets: captured.targets,
|
|
2832
|
+
compactionThreshold: DEFAULT_COMPACTION_THRESHOLD,
|
|
2833
|
+
prior: priorWithinCount,
|
|
2834
|
+
wholePartitions: captured.contents
|
|
2835
|
+
});
|
|
2836
|
+
const vectors = await writeLiveVectors({
|
|
2837
|
+
store,
|
|
2838
|
+
indexName: input.indexName,
|
|
2839
|
+
plans: captured.plans,
|
|
2840
|
+
priorVectors: prior?.vectors ?? []
|
|
2841
|
+
});
|
|
2842
|
+
const committed = await commitCheckpointManifest(store, input.metadata, written, vectors.refs);
|
|
2843
|
+
recordWrittenVectors(input.vectorIndexes, vectors.written);
|
|
2844
|
+
await removeCheckpointGarbage(
|
|
2845
|
+
store,
|
|
2846
|
+
await garbageThatNoFieldStillReads(store, input.vectorIndexes, committed.garbage)
|
|
2847
|
+
);
|
|
2848
|
+
}
|
|
2849
|
+
async function writeSnapshotOnlyCheckpoint(input) {
|
|
2850
|
+
const store = adapterCheckpointStore(input.adapter);
|
|
2851
|
+
for (const vectorIndex of input.vectorIndexes.values()) await vectorIndex.completeGraph();
|
|
2852
|
+
const prior = await readCurrentManifest(store, input.indexName);
|
|
2853
|
+
const captured = capture(input, prior);
|
|
2854
|
+
await writeCaptured(input, store, prior, captured);
|
|
2855
|
+
return captured.documentCount;
|
|
2856
|
+
}
|
|
2857
|
+
|
|
2858
|
+
// src/persistence/durability/snapshot-only.ts
|
|
2859
|
+
function metadataKey(indexName) {
|
|
2860
|
+
return `${indexName}/meta`;
|
|
2861
|
+
}
|
|
2862
|
+
function createSnapshotOnlyManager(adapter, config, hooks, publisher) {
|
|
2863
|
+
const checkpointIntervalMs = config.checkpointIntervalMs ?? DEFAULT_CHECKPOINT_INTERVAL_MS;
|
|
2864
|
+
const checkpointMutationThreshold = config.checkpointMutationThreshold ?? DEFAULT_CHECKPOINT_MUTATION_THRESHOLD;
|
|
2865
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
2866
|
+
const metadataWrites = /* @__PURE__ */ new Map();
|
|
2867
|
+
let checkpointTimer = null;
|
|
2868
|
+
let shuttingDown = false;
|
|
2869
|
+
function getOrCreateIndexState(indexName) {
|
|
2870
|
+
let state = indexes.get(indexName);
|
|
2871
|
+
if (state === void 0) {
|
|
2872
|
+
state = {
|
|
2873
|
+
mutationsSinceCheckpoint: 0,
|
|
2874
|
+
checkpointInFlight: null,
|
|
2875
|
+
applyChain: Promise.resolve(),
|
|
2876
|
+
unloading: false,
|
|
2877
|
+
appliedSeqNoByPartition: /* @__PURE__ */ new Map()
|
|
2878
|
+
};
|
|
2879
|
+
indexes.set(indexName, state);
|
|
2880
|
+
}
|
|
2881
|
+
return state;
|
|
2882
|
+
}
|
|
2883
|
+
function startCheckpointTimer() {
|
|
2884
|
+
if (checkpointTimer !== null || shuttingDown || checkpointIntervalMs <= 0) {
|
|
2885
|
+
return;
|
|
2886
|
+
}
|
|
2887
|
+
checkpointTimer = setInterval(() => {
|
|
2888
|
+
void runScheduledCheckpoints();
|
|
2889
|
+
}, checkpointIntervalMs);
|
|
2890
|
+
if (typeof checkpointTimer.unref === "function") {
|
|
2891
|
+
checkpointTimer.unref();
|
|
2892
|
+
}
|
|
2893
|
+
}
|
|
2894
|
+
async function runScheduledCheckpoints() {
|
|
2895
|
+
for (const indexName of [...indexes.keys()]) {
|
|
2896
|
+
try {
|
|
2897
|
+
await checkpointIndex(indexName);
|
|
2898
|
+
} catch (err) {
|
|
2899
|
+
hooks.onFatalError(err instanceof Error ? err : new Error(String(err)));
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2902
|
+
}
|
|
2903
|
+
async function performCheckpoint(indexName, indexState, fromMemory) {
|
|
2904
|
+
const manager = hooks.getManager(indexName);
|
|
2905
|
+
const metadata = hooks.buildMetadata(indexName);
|
|
2906
|
+
if (manager === void 0 || metadata === void 0) {
|
|
2907
|
+
return;
|
|
2908
|
+
}
|
|
2909
|
+
const documentCount = await writeSnapshotOnlyCheckpoint({
|
|
2910
|
+
adapter,
|
|
2911
|
+
indexName,
|
|
2912
|
+
metadata,
|
|
2913
|
+
manager,
|
|
2914
|
+
vectorIndexes: hooks.getVectorIndexes(indexName),
|
|
2915
|
+
appliedSeqNoByPartition: indexState.appliedSeqNoByPartition,
|
|
2916
|
+
rewritesEveryPartition: fromMemory
|
|
2917
|
+
});
|
|
2918
|
+
await queueMetadataWrite(indexName, async () => {
|
|
2919
|
+
const checkpointMetadata = hooks.buildMetadata(indexName, documentCount);
|
|
2920
|
+
if (checkpointMetadata === void 0) return;
|
|
2921
|
+
const metadataBytes = await writeMetadataEnvelope(checkpointMetadata, { checksum: true });
|
|
2922
|
+
await adapter.save(metadataKey(indexName), metadataBytes);
|
|
2923
|
+
hooks.recordCheckpoint?.(indexName, documentCount, manager.partitionCount);
|
|
2924
|
+
});
|
|
2925
|
+
indexState.mutationsSinceCheckpoint = 0;
|
|
2926
|
+
if (publisher !== void 0) {
|
|
2927
|
+
const partitionIds = [];
|
|
2928
|
+
for (let i = 0; i < manager.partitionCount; i += 1) {
|
|
2929
|
+
partitionIds.push(i);
|
|
2930
|
+
}
|
|
2931
|
+
await publisher.publishPartitions(indexName, partitionIds);
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
async function checkpointIndex(indexName, fromMemory = false) {
|
|
2935
|
+
const indexState = indexes.get(indexName);
|
|
2936
|
+
if (indexState === void 0 || indexState.unloading) {
|
|
2937
|
+
return;
|
|
2938
|
+
}
|
|
2939
|
+
if (indexState.checkpointInFlight !== null) {
|
|
2940
|
+
await indexState.checkpointInFlight;
|
|
2941
|
+
if (!fromMemory) {
|
|
2942
|
+
return;
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
const run = indexState.applyChain.then(() => performCheckpoint(indexName, indexState, fromMemory)).finally(() => {
|
|
2946
|
+
indexState.checkpointInFlight = null;
|
|
2947
|
+
});
|
|
2948
|
+
indexState.checkpointInFlight = run;
|
|
2949
|
+
return run;
|
|
2950
|
+
}
|
|
2951
|
+
function queueMetadataWrite(indexName, write) {
|
|
2952
|
+
const previous = metadataWrites.get(indexName) ?? Promise.resolve();
|
|
2953
|
+
const queued = previous.then(write);
|
|
2954
|
+
metadataWrites.set(
|
|
2955
|
+
indexName,
|
|
2956
|
+
queued.catch(() => void 0)
|
|
2957
|
+
);
|
|
2958
|
+
return queued;
|
|
2959
|
+
}
|
|
2960
|
+
async function recoverIndex(indexName, metadataOnly = false) {
|
|
2961
|
+
const metaBytes = await adapter.load(metadataKey(indexName));
|
|
2962
|
+
if (metaBytes === null) {
|
|
2963
|
+
return;
|
|
2964
|
+
}
|
|
2965
|
+
const { metadata } = await readMetadataEnvelope(metaBytes);
|
|
2966
|
+
let derivedCount;
|
|
2967
|
+
if (metadataOnly && metadata.documentCount === void 0) {
|
|
2968
|
+
try {
|
|
2969
|
+
derivedCount = await countStoredBundleDocuments(indexName);
|
|
2970
|
+
metadata.documentCount = derivedCount;
|
|
2971
|
+
} catch {
|
|
2972
|
+
derivedCount = void 0;
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
await hooks.createIndexFromMetadata(metadata, !metadataOnly);
|
|
2976
|
+
if (metadataOnly) {
|
|
2977
|
+
if (derivedCount !== void 0) {
|
|
2978
|
+
await queueMetadataWrite(indexName, async () => {
|
|
2979
|
+
const upgraded = hooks.buildMetadata(indexName, derivedCount);
|
|
2980
|
+
if (upgraded === void 0) {
|
|
2981
|
+
return;
|
|
2982
|
+
}
|
|
2983
|
+
const bytes = await writeMetadataEnvelope(upgraded, { checksum: true });
|
|
2984
|
+
await adapter.save(metadataKey(indexName), bytes);
|
|
2985
|
+
}).catch(() => void 0);
|
|
2986
|
+
}
|
|
2987
|
+
return;
|
|
2988
|
+
}
|
|
2989
|
+
const manager = hooks.getManager(indexName);
|
|
2990
|
+
const current = hooks.buildMetadata(indexName);
|
|
2991
|
+
if (manager === void 0 || current === void 0) {
|
|
2992
|
+
return;
|
|
2993
|
+
}
|
|
2994
|
+
const recovered = await recoverCheckpoint({
|
|
2995
|
+
store: adapterCheckpointStore(adapter),
|
|
2996
|
+
indexName,
|
|
2997
|
+
metadata: current,
|
|
2998
|
+
manager,
|
|
2999
|
+
vectorIndexes: hooks.getVectorIndexes(indexName),
|
|
3000
|
+
storedTermsAreCurrent: metadata.analysisRevision === manager.language.revision,
|
|
3001
|
+
compactionThreshold: DEFAULT_COMPACTION_THRESHOLD,
|
|
3002
|
+
keepsUnlistedFiles: false,
|
|
3003
|
+
replay: async (checkpoint) => checkpoint,
|
|
3004
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade
|
|
3005
|
+
});
|
|
3006
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
3007
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
3008
|
+
const persistedSeqNo = snapshotCheckpointFor(recovered.checkpoint, partitionId);
|
|
3009
|
+
if (persistedSeqNo > 0) {
|
|
3010
|
+
indexState.appliedSeqNoByPartition.set(partitionId, persistedSeqNo);
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
if (recovered.upgradedDocumentCount !== null) {
|
|
3014
|
+
const upgradedCount = recovered.upgradedDocumentCount;
|
|
3015
|
+
await queueMetadataWrite(indexName, async () => {
|
|
3016
|
+
const upgraded = hooks.buildMetadata(indexName, upgradedCount);
|
|
3017
|
+
if (upgraded === void 0) return;
|
|
3018
|
+
await adapter.save(metadataKey(indexName), await writeMetadataEnvelope(upgraded, { checksum: true }));
|
|
3019
|
+
}).catch(() => void 0);
|
|
3020
|
+
}
|
|
3021
|
+
}
|
|
3022
|
+
function countStoredBundleDocuments(indexName) {
|
|
3023
|
+
return countCheckpointDocuments(adapterCheckpointStore(adapter), indexName);
|
|
3024
|
+
}
|
|
3025
|
+
async function recordMutation(record) {
|
|
3026
|
+
const indexState = getOrCreateIndexState(record.indexName);
|
|
3027
|
+
const buffered = indexState.applyChain.then(async () => {
|
|
3028
|
+
await record.apply();
|
|
3029
|
+
const appliedSeqNo2 = (indexState.appliedSeqNoByPartition.get(record.partitionId) ?? 0) + 1;
|
|
3030
|
+
indexState.appliedSeqNoByPartition.set(record.partitionId, appliedSeqNo2);
|
|
3031
|
+
return appliedSeqNo2;
|
|
3032
|
+
});
|
|
3033
|
+
indexState.applyChain = buffered.then(
|
|
3034
|
+
() => void 0,
|
|
3035
|
+
() => void 0
|
|
3036
|
+
);
|
|
3037
|
+
const appliedSeqNo = await buffered;
|
|
3038
|
+
indexState.mutationsSinceCheckpoint += 1;
|
|
3039
|
+
startCheckpointTimer();
|
|
3040
|
+
if (indexState.mutationsSinceCheckpoint >= checkpointMutationThreshold) {
|
|
3041
|
+
void checkpointIndex(record.indexName).catch((err) => {
|
|
3042
|
+
hooks.onFatalError(err instanceof Error ? err : new Error(String(err)));
|
|
3043
|
+
});
|
|
3044
|
+
}
|
|
3045
|
+
return appliedSeqNo;
|
|
3046
|
+
}
|
|
3047
|
+
return {
|
|
3048
|
+
isActive() {
|
|
3049
|
+
return true;
|
|
3050
|
+
},
|
|
3051
|
+
async recover(metadataOnly = false) {
|
|
3052
|
+
const keys = await adapter.list("");
|
|
3053
|
+
for (const key of keys) {
|
|
3054
|
+
if (key.endsWith("/meta")) {
|
|
3055
|
+
await recoverIndex(key.slice(0, -"/meta".length), metadataOnly);
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
startCheckpointTimer();
|
|
3059
|
+
},
|
|
3060
|
+
recoverIndex(indexName) {
|
|
3061
|
+
return recoverIndex(indexName);
|
|
3062
|
+
},
|
|
3063
|
+
highestPersistedSeqNo(indexName, partitionId) {
|
|
3064
|
+
return indexes.get(indexName)?.appliedSeqNoByPartition.get(partitionId) ?? 0;
|
|
3065
|
+
},
|
|
3066
|
+
recordMutation,
|
|
3067
|
+
async recordMutations(records) {
|
|
3068
|
+
const outcomes = [];
|
|
3069
|
+
for (const record of records) {
|
|
3070
|
+
try {
|
|
3071
|
+
outcomes.push({ ok: true, seqNo: await recordMutation(record) });
|
|
3072
|
+
} catch (error) {
|
|
3073
|
+
outcomes.push({ ok: false, error });
|
|
3074
|
+
}
|
|
3075
|
+
}
|
|
3076
|
+
return outcomes;
|
|
3077
|
+
},
|
|
3078
|
+
persistMetadata(indexName) {
|
|
3079
|
+
return queueMetadataWrite(indexName, async () => {
|
|
3080
|
+
const metadata = hooks.buildMetadata(indexName);
|
|
3081
|
+
if (metadata === void 0) {
|
|
3082
|
+
return;
|
|
3083
|
+
}
|
|
3084
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
3085
|
+
await adapter.save(metadataKey(indexName), bytes);
|
|
3086
|
+
getOrCreateIndexState(indexName);
|
|
3087
|
+
});
|
|
3088
|
+
},
|
|
3089
|
+
checkpoint(indexName) {
|
|
3090
|
+
return checkpointIndex(indexName);
|
|
3091
|
+
},
|
|
3092
|
+
checkpointFromMemory(indexName) {
|
|
3093
|
+
return checkpointIndex(indexName, true);
|
|
3094
|
+
},
|
|
3095
|
+
async checkpointAll() {
|
|
3096
|
+
for (const indexName of [...indexes.keys()]) {
|
|
3097
|
+
await checkpointIndex(indexName);
|
|
3098
|
+
}
|
|
3099
|
+
},
|
|
3100
|
+
async removeIndex(indexName) {
|
|
3101
|
+
const indexState = indexes.get(indexName);
|
|
3102
|
+
if (indexState !== void 0) {
|
|
3103
|
+
indexState.unloading = true;
|
|
3104
|
+
try {
|
|
3105
|
+
await indexState.applyChain;
|
|
3106
|
+
if (indexState.checkpointInFlight !== null) await indexState.checkpointInFlight;
|
|
3107
|
+
await metadataWrites.get(indexName);
|
|
3108
|
+
} catch (error) {
|
|
3109
|
+
indexState.unloading = false;
|
|
3110
|
+
throw error;
|
|
3111
|
+
}
|
|
3112
|
+
}
|
|
3113
|
+
indexes.delete(indexName);
|
|
3114
|
+
metadataWrites.delete(indexName);
|
|
3115
|
+
for (const key of await adapter.list(`${indexName}/`)) {
|
|
3116
|
+
await adapter.delete(key);
|
|
3117
|
+
}
|
|
3118
|
+
},
|
|
3119
|
+
async unloadIndex(indexName) {
|
|
3120
|
+
const indexState = indexes.get(indexName);
|
|
3121
|
+
if (indexState !== void 0) {
|
|
3122
|
+
indexState.unloading = true;
|
|
3123
|
+
try {
|
|
3124
|
+
await indexState.applyChain;
|
|
3125
|
+
if (indexState.checkpointInFlight !== null) await indexState.checkpointInFlight;
|
|
3126
|
+
await metadataWrites.get(indexName);
|
|
3127
|
+
} catch (error) {
|
|
3128
|
+
indexState.unloading = false;
|
|
3129
|
+
throw error;
|
|
3130
|
+
}
|
|
3131
|
+
}
|
|
3132
|
+
indexes.delete(indexName);
|
|
3133
|
+
metadataWrites.delete(indexName);
|
|
3134
|
+
},
|
|
3135
|
+
async reloadIndex(indexName) {
|
|
3136
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
3137
|
+
const reload = indexState.applyChain.then(() => recoverIndex(indexName));
|
|
3138
|
+
indexState.applyChain = reload.catch(() => void 0);
|
|
3139
|
+
await reload;
|
|
3140
|
+
},
|
|
3141
|
+
async shutdown() {
|
|
3142
|
+
shuttingDown = true;
|
|
3143
|
+
if (checkpointTimer !== null) {
|
|
3144
|
+
clearInterval(checkpointTimer);
|
|
3145
|
+
checkpointTimer = null;
|
|
3146
|
+
}
|
|
3147
|
+
for (const indexState of indexes.values()) {
|
|
3148
|
+
if (indexState.checkpointInFlight !== null) {
|
|
3149
|
+
await indexState.checkpointInFlight.catch(() => void 0);
|
|
3150
|
+
}
|
|
3151
|
+
}
|
|
3152
|
+
indexes.clear();
|
|
3153
|
+
}
|
|
3154
|
+
};
|
|
3155
|
+
}
|
|
3156
|
+
|
|
3157
|
+
// src/engine/durability-integration.ts
|
|
3158
|
+
function buildMetadata(indexName, hooks, documentCount) {
|
|
3159
|
+
const config = hooks.getIndexConfig(indexName);
|
|
3160
|
+
if (config === void 0) {
|
|
3161
|
+
return void 0;
|
|
3162
|
+
}
|
|
3163
|
+
return metadataFromConfig(indexName, config, hooks.getManager(indexName)?.partitionCount, documentCount);
|
|
3164
|
+
}
|
|
3165
|
+
function createDurabilityIntegration(tier, hooks) {
|
|
3166
|
+
const managerHooks = {
|
|
3167
|
+
getManager: hooks.getManager,
|
|
3168
|
+
getVectorFieldPaths: hooks.getVectorFieldPaths,
|
|
3169
|
+
getVectorIndexes: hooks.getVectorIndexes,
|
|
3170
|
+
buildMetadata: (indexName, documentCount) => buildMetadata(indexName, hooks, documentCount),
|
|
3171
|
+
createIndexFromMetadata: hooks.createIndexFromMetadata,
|
|
3172
|
+
recordCheckpoint: hooks.recordCheckpoint,
|
|
3173
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade,
|
|
3174
|
+
onFatalError: hooks.onFatalError
|
|
3175
|
+
};
|
|
3176
|
+
const manager = tier.kind === "wal" ? createDurabilityManager(tier.config, managerHooks) : createSnapshotOnlyManager(tier.adapter, tier.config, managerHooks, hooks.checkpointPublisher);
|
|
3177
|
+
function partitionFor(indexName, docId) {
|
|
3178
|
+
const partitionManager = hooks.getManager(indexName);
|
|
3179
|
+
const partitionCount = partitionManager?.partitionCount ?? 1;
|
|
3180
|
+
return fnv1a(docId) % Math.max(1, partitionCount);
|
|
3181
|
+
}
|
|
3182
|
+
async function recordMutation(indexName, docId, operation, document, apply) {
|
|
3183
|
+
const partitionId = partitionFor(indexName, docId);
|
|
3184
|
+
const seqNo = await manager.recordMutation({
|
|
3185
|
+
indexName,
|
|
3186
|
+
partitionId,
|
|
3187
|
+
operation,
|
|
3188
|
+
documentId: docId,
|
|
3189
|
+
document,
|
|
3190
|
+
apply
|
|
3191
|
+
});
|
|
3192
|
+
return { indexName, partitionId, seqNo };
|
|
3193
|
+
}
|
|
3194
|
+
return {
|
|
3195
|
+
manager,
|
|
3196
|
+
recordInsertOrUpdate(indexName, docId, document, apply) {
|
|
3197
|
+
return recordMutation(indexName, docId, "INDEX", encodeDocument(document), apply);
|
|
3198
|
+
},
|
|
3199
|
+
async recordMutationBatch(indexName, mutations) {
|
|
3200
|
+
const records = [];
|
|
3201
|
+
const outcomes = new Array(mutations.length);
|
|
3202
|
+
const recordPositions = [];
|
|
3203
|
+
for (let i = 0; i < mutations.length; i++) {
|
|
3204
|
+
const mutation = mutations[i];
|
|
3205
|
+
try {
|
|
3206
|
+
records.push({
|
|
3207
|
+
indexName,
|
|
3208
|
+
partitionId: partitionFor(indexName, mutation.docId),
|
|
3209
|
+
operation: mutation.document === null ? "DELETE" : "INDEX",
|
|
3210
|
+
documentId: mutation.docId,
|
|
3211
|
+
document: mutation.document === null ? null : encodeDocument(mutation.document),
|
|
3212
|
+
apply: mutation.apply
|
|
3213
|
+
});
|
|
3214
|
+
recordPositions.push(i);
|
|
3215
|
+
} catch (error) {
|
|
3216
|
+
outcomes[i] = { ok: false, error };
|
|
3217
|
+
}
|
|
3218
|
+
}
|
|
3219
|
+
const recorded = await manager.recordMutations(records);
|
|
3220
|
+
for (let i = 0; i < recorded.length; i++) {
|
|
3221
|
+
const outcome = recorded[i];
|
|
3222
|
+
outcomes[recordPositions[i]] = outcome.ok ? { ok: true, write: { indexName, partitionId: records[i].partitionId, seqNo: outcome.seqNo } } : outcome;
|
|
3223
|
+
}
|
|
3224
|
+
return outcomes;
|
|
3225
|
+
},
|
|
3226
|
+
recordRemove(indexName, docId, apply) {
|
|
3227
|
+
return recordMutation(indexName, docId, "DELETE", null, apply);
|
|
3228
|
+
}
|
|
3229
|
+
};
|
|
3230
|
+
}
|
|
3231
|
+
|
|
3232
|
+
// src/engine/durability-wiring.ts
|
|
3233
|
+
function createDurabilityFromTier(tier, wiring) {
|
|
3234
|
+
return createDurabilityIntegration(tier, {
|
|
3235
|
+
checkpointPublisher: { publishPartitions: wiring.publishCheckpointedPartitions },
|
|
3236
|
+
getManager: (indexName) => wiring.indexRegistry.has(indexName) ? wiring.getManager(indexName) : void 0,
|
|
3237
|
+
getVectorFieldPaths: (indexName) => wiring.indexRegistry.get(indexName)?.vectorFieldPaths ?? /* @__PURE__ */ new Set(),
|
|
3238
|
+
getVectorIndexes: (indexName) => (wiring.indexRegistry.has(indexName) ? wiring.getManager(indexName)?.getVectorIndexes() : void 0) ?? /* @__PURE__ */ new Map(),
|
|
3239
|
+
getIndexConfig: (indexName) => {
|
|
3240
|
+
const entry = wiring.indexRegistry.get(indexName);
|
|
3241
|
+
return entry === void 0 ? void 0 : metadataConfigOf(entry);
|
|
3242
|
+
},
|
|
3243
|
+
createIndexFromMetadata: wiring.createIndexFromMetadata,
|
|
3244
|
+
recordCheckpoint: wiring.recordCheckpoint,
|
|
3245
|
+
onLayoutUpgrade: wiring.emitLayoutUpgrade,
|
|
3246
|
+
onFatalError: wiring.emitFatalError
|
|
3247
|
+
});
|
|
3248
|
+
}
|
|
3249
|
+
|
|
3250
|
+
export { createDurabilityFromTier };
|