@delali/narsil 0.2.4-next.59 → 0.3.1-next.101
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/README.md +9 -8
- package/dist/adapters/filesystem.d.ts +1 -1
- package/dist/adapters/filesystem.mjs +1 -1
- package/dist/adapters/indexeddb.d.ts +1 -1
- package/dist/adapters/indexeddb.mjs +1 -1
- package/dist/adapters/memory.d.ts +1 -1
- package/dist/{adapters-79OIa4xm.d.ts → adapters-BQFQ4PRz.d.ts} +27 -1
- package/dist/browser/chunk-BDOSMEEP.mjs +493 -0
- package/dist/browser/chunk-EJDUFWSO.mjs +602 -0
- package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
- package/dist/browser/chunk-JOVZ6HMT.mjs +4358 -0
- package/dist/browser/chunk-JPTQZWJQ.mjs +10 -0
- package/dist/browser/chunk-KPJMDPHG.mjs +24558 -0
- package/dist/browser/chunk-PGIM3HIM.mjs +2743 -0
- package/dist/browser/chunk-RHG4O77A.mjs +206 -0
- package/dist/browser/chunk-RIJSLY2I.mjs +360 -0
- package/dist/browser/durability-wiring-SRUYT3VV.mjs +3875 -0
- package/dist/browser/module-CBB2ETYP.mjs +3111 -0
- package/dist/browser/release-76MXRPGV.mjs +33 -0
- package/dist/browser/replace-RURXTGVG.mjs +57 -0
- package/dist/browser/restore-queue-EEZQO6RF.mjs +35 -0
- package/dist/browser/runtime-7ZAHCHWR.mjs +557 -0
- package/dist/browser/snapshot-OAAA5UJ7.mjs +1210 -0
- package/dist/browser/write-7D6VIK6O.mjs +208 -0
- package/dist/{capabilities-DSsOGz6F.d.ts → capabilities-DiGlB6Sr.d.ts} +68 -7
- package/dist/chunk-2RIRGOUL.mjs +113 -0
- package/dist/chunk-37CD3OKS.mjs +87 -0
- package/dist/chunk-44W3CBNG.mjs +7 -0
- package/dist/chunk-46D6JQIG.mjs +426 -0
- package/dist/{chunk-VMRR3DQU.mjs → chunk-5JNA5SKS.mjs} +546 -35
- package/dist/chunk-747OHOUA.mjs +11 -0
- package/dist/chunk-75XIEF6H.mjs +126 -0
- package/dist/chunk-7X5JST5L.mjs +114 -0
- package/dist/chunk-BOFBLJFZ.mjs +209 -0
- package/dist/{chunk-USKFU7J5.mjs → chunk-BWJVNZPR.mjs} +8 -1
- package/dist/chunk-C7SLA6CM.mjs +19 -0
- package/dist/{chunk-POWOTUKJ.mjs → chunk-CQUMNIIG.mjs} +3 -6
- package/dist/chunk-CVXLLEQ3.mjs +1011 -0
- package/dist/chunk-DJHHF2L5.mjs +17 -0
- package/dist/chunk-E74YLAIG.mjs +24 -0
- package/dist/{chunk-UXNWQZST.mjs → chunk-EMRMGYRE.mjs} +236 -18
- package/dist/chunk-EWZLIYHV.mjs +57 -0
- package/dist/chunk-GBEEG5AN.mjs +35 -0
- package/dist/chunk-GES7U2PM.mjs +68 -0
- package/dist/chunk-HJSN7UYG.mjs +10 -0
- package/dist/chunk-HLPRHOSO.mjs +17 -0
- package/dist/chunk-HMFNFVP7.mjs +14 -0
- package/dist/chunk-J5QP52HA.mjs +1751 -0
- package/dist/{chunk-YNVIUGF5.mjs → chunk-JZVARCRZ.mjs} +3 -28
- package/dist/chunk-KOKM4PQN.mjs +52 -0
- package/dist/{chunk-B2TFROFX.mjs → chunk-KWTW6VTG.mjs} +465 -250
- package/dist/chunk-KZ3NX2P7.mjs +21 -0
- package/dist/chunk-L5ORED7V.mjs +25 -0
- package/dist/{chunk-THYRHHO2.mjs → chunk-LGAMKHA2.mjs} +2009 -2351
- package/dist/chunk-LKOB5COA.mjs +117 -0
- package/dist/chunk-LPQOEU5M.mjs +96 -0
- package/dist/{chunk-3LHGNRYE.mjs → chunk-LXWDQLTW.mjs} +7 -2
- package/dist/{chunk-L6TNPLAR.mjs → chunk-OEYI3KRB.mjs} +270 -108
- package/dist/chunk-OM362BRO.mjs +948 -0
- package/dist/chunk-OZDFA4WC.mjs +67 -0
- package/dist/chunk-P36MUFEJ.mjs +10 -0
- package/dist/chunk-PC2G76VW.mjs +2540 -0
- package/dist/{chunk-GCM3JFVA.mjs → chunk-PNEYIHA4.mjs} +11 -2
- package/dist/chunk-PNFKUCG3.mjs +1130 -0
- package/dist/chunk-Q6XWVUZX.mjs +44 -0
- package/dist/chunk-R27EMH5M.mjs +617 -0
- package/dist/{chunk-YMZCMH5M.mjs → chunk-R6A773WK.mjs} +19 -24
- package/dist/chunk-RF7PLG43.mjs +106 -0
- package/dist/chunk-RFBLQLLZ.mjs +16 -0
- package/dist/{chunk-Y3L5MDRV.mjs → chunk-RFVUDM5E.mjs} +17 -2
- package/dist/chunk-S7FL6UAH.mjs +521 -0
- package/dist/chunk-S7GNDRPS.mjs +651 -0
- package/dist/chunk-SP3NM3ZG.mjs +3537 -0
- package/dist/chunk-U476IXKQ.mjs +10 -0
- package/dist/chunk-UA3AUGNV.mjs +462 -0
- package/dist/chunk-UTG5YNXV.mjs +507 -0
- package/dist/chunk-VI4R6W25.mjs +8484 -0
- package/dist/chunk-WQBCBKY3.mjs +8 -0
- package/dist/{chunk-24RXHMEL.mjs → chunk-WVCOCKBU.mjs} +3794 -7570
- package/dist/chunk-X5CV2E2B.mjs +1 -0
- package/dist/chunk-XADZPPZI.mjs +614 -0
- package/dist/chunk-YEEQHT4V.mjs +212 -0
- package/dist/chunk-YTJCGKM4.mjs +56 -0
- package/dist/chunk-YYQIODMF.mjs +275 -0
- package/dist/chunk-ZI34TVOA.mjs +144 -0
- package/dist/{chunk-OOSIDHA3.mjs → chunk-ZKWYWW4Y.mjs} +1 -1
- package/dist/chunk-ZM7KU4YW.mjs +35 -0
- package/dist/{client-BQK6Tf6A.d.ts → client-BOStAkc5.d.ts} +160 -37
- package/dist/client.d.ts +11 -10
- package/dist/client.mjs +209 -45
- package/dist/{config-DmXULPa-.d.ts → config-Cjvzbs_j.d.ts} +51 -3
- package/dist/config-DffQlmRq.d.ts +421 -0
- package/dist/direct-executor-ZD64PPEZ.mjs +24 -0
- package/dist/distribution/coordinator/etcd.d.ts +79 -18
- package/dist/distribution/coordinator/etcd.mjs +553 -109
- package/dist/distribution/coordinator/in-memory.d.ts +4 -3
- package/dist/distribution/coordinator/in-memory.mjs +22 -3
- package/dist/distribution/transport/grpc.d.ts +2 -2
- package/dist/distribution/transport/grpc.mjs +79 -14
- package/dist/distribution/transport/in-memory.d.ts +2 -2
- package/dist/distribution/transport/in-memory.mjs +12 -7
- package/dist/distribution/transport/tcp.d.ts +2 -2
- package/dist/distribution/transport/tcp.mjs +50 -28
- package/dist/distribution.d.ts +144 -23
- package/dist/distribution.mjs +14479 -8173
- package/dist/durability-wiring-FEUBR5BT.mjs +3330 -0
- package/dist/embeddings/openai.d.ts +2 -2
- package/dist/embeddings/openai.mjs +22 -133
- package/dist/{engine-CIOyFozK.d.ts → engine-rC0vjjY-.d.ts} +165 -141
- package/dist/{memory-yFaVn2xZ.d.ts → events-DqOIg5xa.d.ts} +645 -136
- package/dist/{types-DAK7mnUZ.d.ts → index-Bkdt8nDw.d.ts} +182 -45
- package/dist/index.browser.mjs +4 -32180
- package/dist/index.d.ts +91 -20
- package/dist/index.mjs +33 -16
- package/dist/invalidation/broadcast-channel.d.ts +1 -1
- package/dist/invalidation/filesystem.d.ts +1 -1
- package/dist/invalidation/filesystem.mjs +2 -1
- package/dist/invalidation/noop.d.ts +1 -1
- package/dist/module-3S65MVCM.mjs +2151 -0
- package/dist/partial-words-DTIHO8G4.d.ts +19 -0
- package/dist/partial-words.d.ts +22 -0
- package/dist/partial-words.mjs +17 -0
- package/dist/pattern-Cledccbl.d.ts +18 -0
- package/dist/pattern.d.ts +22 -0
- package/dist/pattern.mjs +12 -0
- package/dist/persistence/durability/checkpoint-worker.d.ts +119 -72
- package/dist/persistence/durability/checkpoint-worker.mjs +51 -18
- package/dist/react.d.ts +279 -183
- package/dist/react.mjs +201 -48
- package/dist/release-BH3DKW3T.mjs +33 -0
- package/dist/replace-CEO5KJJ3.mjs +57 -0
- package/dist/rerank-K7C4WZFG.mjs +60 -0
- package/dist/rerankers/http.d.ts +50 -0
- package/dist/rerankers/http.mjs +104 -0
- package/dist/restore-queue-KQHA7ZV2.mjs +35 -0
- package/dist/runtime-6Y4ZYXOG.mjs +177 -0
- package/dist/{schema-d5Afu03A.d.ts → schema-4W_7tk1f.d.ts} +81 -11
- package/dist/server.d.ts +13 -12
- package/dist/server.mjs +611 -80
- package/dist/snapshot-QGZPODM5.mjs +799 -0
- package/dist/synonyms-CpOAHPa-.d.ts +52 -0
- package/dist/synonyms.d.ts +22 -0
- package/dist/synonyms.mjs +12 -0
- package/dist/{tasks-CH2Dod8w.d.ts → tasks-CwPTz-tP.d.ts} +10 -3
- package/dist/thread-N3IUGUS3.mjs +697 -0
- package/dist/{types-BQDlIvfS.d.ts → types-2qoitTXg.d.ts} +1 -1
- package/dist/{types-D5OwhtsQ.d.ts → types-Cz5okLml.d.ts} +2 -2
- package/dist/vector/search-worker.d.ts +46 -4
- package/dist/vector/search-worker.mjs +21 -11
- package/dist/workers/entry.mjs +9 -6
- package/dist/write-U5RI7MAX.mjs +241 -0
- package/package.json +55 -9
- package/dist/chunk-3L4U6EJQ.mjs +0 -49
- package/dist/chunk-IFBKLBGB.mjs +0 -7
- package/dist/chunk-ORBLD3EL.mjs +0 -6
- package/dist/chunk-U7KYW4BA.mjs +0 -135
- package/dist/chunk-ZJ4CSAB2.mjs +0 -13
- package/dist/chunk-ZNZBXMP5.mjs +0 -2117
- package/dist/chunk-ZZT3EAUC.mjs +0 -11168
- package/dist/config-BhldZgaK.d.ts +0 -194
- package/dist/direct-executor-ALAVE7FC.mjs +0 -12
- package/dist/serialization/crc32-worker.d.ts +0 -20
- package/dist/serialization/crc32-worker.mjs +0 -62
- package/dist/simd-DW5AtJ6B.d.ts +0 -41
- package/dist/thread-DYHEQRVY.mjs +0 -402
|
@@ -0,0 +1,3875 @@
|
|
|
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, adapterCheckpointStore, SINGLE_NODE_PRIMARY_TERM, readMetadataEnvelope, writeMetadataEnvelope, metadataFromConfig, DIRECTORY_LOCK_ATTEMPTS, createSeqOwner, readSegmentManifest, snapshotBundleKey, reclaimUnlistedKeys, manifestReferencedKeys, loadSegmentedCheckpoint, checkpointLastSeqNo, manifestFileRefs, DIRECTORY_LOCK_SETTLE_MS, readSegmentFile, isRecord, invalidManifest, deserializePayloadV2, unpackEnvelopeBytes, checkpointPrimaryTerm, frameRecord, isId, isUint32, isSequenceNumber, writeCheckpointSegments, partitionContentsOf, writeLiveVectors, commitCheckpointManifest, removeCheckpointGarbage, readWalSegments, durableRecordMissing, MAX_SEGMENTS_PER_PARTITION, growPartitionsTo, writeSegmentHeader, VECTOR_FILE_ENVELOPE_VERSION, SEGMENT_HEADER_SIZE, documentsOfPayloadV2, readCommitMarker, nextMarkerSlot, encodeMarkerSlot, markerSlotOffset, DIRECTORY_IDENTITY_CHECK_INTERVAL_MS, metadataConfigOf, buildEntry, readCurrentManifest, WHOLE_PARTITION_LARGEST_DOCUMENT_COUNT, WHOLE_PARTITION_MIN_CHANGED_SHARE, captureWholePartition, CHECKPOINT_TIMEOUT_RECOVERY_BACKOFF_MS, CHECKPOINT_WORKER_TIMEOUT_MS, CHECKPOINT_WORKER_IDLE_MS, LOG_RECORDS_PER_CHECKPOINT } from './chunk-PGIM3HIM.mjs';
|
|
3
|
+
import { applyDeleteEntry, applyIndexEntry } from './chunk-EJDUFWSO.mjs';
|
|
4
|
+
import { encodeDocument, fnv1a, requireValidFieldTypes, openChecksummedEnvelope, HEADER_SIZE, buildSegmentPayload, resolvePartitionInsertOptions, freezeSegmentShared, createSharedFrozenSegment } from './chunk-KPJMDPHG.mjs';
|
|
5
|
+
import { decodeVectorIndexParts, VECTOR_FILE_MAX_VECTORS, deadBytesFor, countDead, decodeVectorFilePayload, decodeVectorGraphPayload, graphPartsOfEarlierPayload } from './chunk-RIJSLY2I.mjs';
|
|
6
|
+
import { compareCodePoints, MIN_M, MAX_FILE_BYTES, detectRuntime, resolveWorkerEntry } from './chunk-BDOSMEEP.mjs';
|
|
7
|
+
import './chunk-JPTQZWJQ.mjs';
|
|
8
|
+
import { NarsilError, ErrorCodes, MAX_INDEX_NAME_LENGTH, INDEX_NAME_PATTERN } from './chunk-RHG4O77A.mjs';
|
|
9
|
+
import { decode } from '@msgpack/msgpack';
|
|
10
|
+
|
|
11
|
+
// src/persistence/durability/node-fs.browser.ts
|
|
12
|
+
function filesystemUnavailable() {
|
|
13
|
+
throw new NarsilError(
|
|
14
|
+
ErrorCodes.CONFIG_INVALID,
|
|
15
|
+
"Filesystem durability requires a Node.js runtime; use the memory or IndexedDB persistence adapters in the browser"
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
async function getFs() {
|
|
19
|
+
return filesystemUnavailable();
|
|
20
|
+
}
|
|
21
|
+
async function getPath() {
|
|
22
|
+
return filesystemUnavailable();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/persistence/durability/lock-holder.browser.ts
|
|
26
|
+
async function holderIsLive(_holder, _self, _lockPath) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
function holdDirectoryOpen(_directory) {
|
|
30
|
+
return () => void 0;
|
|
31
|
+
}
|
|
32
|
+
async function removeStaleLock(_lockPath, _asidePath, _isStale) {
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/persistence/durability/directory-lock.ts
|
|
36
|
+
var LOCK_FILE_NAME = ".narsil.lock";
|
|
37
|
+
var HARD_LINK_UNSUPPORTED_CODES = /* @__PURE__ */ new Set(["EPERM", "ENOTSUP", "EOPNOTSUPP", "ENOSYS"]);
|
|
38
|
+
var lockedDirectories = /* @__PURE__ */ new Set();
|
|
39
|
+
function errnoOf(err) {
|
|
40
|
+
return err?.code;
|
|
41
|
+
}
|
|
42
|
+
function integerOrNull(text) {
|
|
43
|
+
const value = Number.parseInt(text ?? "", 10);
|
|
44
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
45
|
+
}
|
|
46
|
+
function parseHolder(text) {
|
|
47
|
+
const [pidText, threadText, startText, nonceText] = text.split("\n");
|
|
48
|
+
const pid = integerOrNull(pidText);
|
|
49
|
+
if (pid === null || pid <= 0) return null;
|
|
50
|
+
const nonce = nonceText?.trim() ?? "";
|
|
51
|
+
return {
|
|
52
|
+
pid,
|
|
53
|
+
threadId: integerOrNull(threadText),
|
|
54
|
+
processStartMs: integerOrNull(startText),
|
|
55
|
+
nonce: nonce.length > 0 ? nonce : null
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function formatHolder(holder) {
|
|
59
|
+
return `${holder.pid}
|
|
60
|
+
${holder.threadId ?? ""}
|
|
61
|
+
${holder.processStartMs ?? ""}
|
|
62
|
+
${holder.nonce ?? ""}
|
|
63
|
+
`;
|
|
64
|
+
}
|
|
65
|
+
async function readHolder(fs, lockPath) {
|
|
66
|
+
try {
|
|
67
|
+
return parseHolder(await fs.readFile(lockPath, "utf8")) ?? "unreadable";
|
|
68
|
+
} catch (err) {
|
|
69
|
+
if (errnoOf(err) === "ENOENT") return "absent";
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function settledHolder(fs, lockPath) {
|
|
74
|
+
const first = await readHolder(fs, lockPath);
|
|
75
|
+
if (first !== "unreadable") return first;
|
|
76
|
+
await new Promise((resolve) => setTimeout(resolve, DIRECTORY_LOCK_SETTLE_MS));
|
|
77
|
+
return readHolder(fs, lockPath);
|
|
78
|
+
}
|
|
79
|
+
function heldElsewhere(root, holder, lockPath) {
|
|
80
|
+
return new NarsilError(
|
|
81
|
+
ErrorCodes.CONFIG_INVALID,
|
|
82
|
+
`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}"`,
|
|
83
|
+
{ directory: root }
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
function holderLabel(holder, self) {
|
|
87
|
+
if (holder.pid !== self.pid) return `process ${holder.pid}`;
|
|
88
|
+
return holder.threadId === self.threadId ? "another copy of Narsil in this thread" : "another thread of this process";
|
|
89
|
+
}
|
|
90
|
+
async function createLockFileExclusively(fs, lockPath, content) {
|
|
91
|
+
try {
|
|
92
|
+
await fs.writeFile(lockPath, content, { flag: "wx" });
|
|
93
|
+
return true;
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (errnoOf(err) === "EEXIST") return false;
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async function createLockFile(fs, lockPath, draftPath, content) {
|
|
100
|
+
await fs.writeFile(draftPath, content, { flag: "w" });
|
|
101
|
+
try {
|
|
102
|
+
await fs.link(draftPath, lockPath);
|
|
103
|
+
return true;
|
|
104
|
+
} catch (err) {
|
|
105
|
+
const code = errnoOf(err);
|
|
106
|
+
if (code === "EEXIST") return false;
|
|
107
|
+
if (code !== void 0 && HARD_LINK_UNSUPPORTED_CODES.has(code)) {
|
|
108
|
+
return createLockFileExclusively(fs, lockPath, content);
|
|
109
|
+
}
|
|
110
|
+
throw err;
|
|
111
|
+
} finally {
|
|
112
|
+
await fs.unlink(draftPath).catch(() => void 0);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function acquire(fs, root, lockPath, self) {
|
|
116
|
+
const draftPath = `${lockPath}.${self.pid}.${self.threadId ?? 0}`;
|
|
117
|
+
const content = formatHolder(self);
|
|
118
|
+
for (let attempt = 0; attempt < DIRECTORY_LOCK_ATTEMPTS; attempt++) {
|
|
119
|
+
if (await createLockFile(fs, lockPath, draftPath, content)) return;
|
|
120
|
+
const holder = await settledHolder(fs, lockPath);
|
|
121
|
+
if (typeof holder === "object" && await holderIsLive()) {
|
|
122
|
+
throw heldElsewhere(root, holderLabel(holder, self), lockPath);
|
|
123
|
+
}
|
|
124
|
+
await removeStaleLock();
|
|
125
|
+
}
|
|
126
|
+
throw heldElsewhere(root, "another process", lockPath);
|
|
127
|
+
}
|
|
128
|
+
async function describeSelf() {
|
|
129
|
+
return {
|
|
130
|
+
pid: process.pid,
|
|
131
|
+
threadId: await nodeThreadId(),
|
|
132
|
+
processStartMs: Math.round(Date.now() - process.uptime() * 1e3),
|
|
133
|
+
nonce: globalThis.crypto.randomUUID()
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
async function lockDirectory(root) {
|
|
137
|
+
const fs = await getFs();
|
|
138
|
+
const pathMod = await getPath();
|
|
139
|
+
const resolvedRoot = pathMod.resolve(root);
|
|
140
|
+
const lockPath = pathMod.join(resolvedRoot, LOCK_FILE_NAME);
|
|
141
|
+
const self = await describeSelf();
|
|
142
|
+
async function holds() {
|
|
143
|
+
const holder = await readHolder(fs, lockPath);
|
|
144
|
+
return typeof holder === "object" && holder.nonce === self.nonce;
|
|
145
|
+
}
|
|
146
|
+
async function removeOwnLockFile() {
|
|
147
|
+
if (await holds().catch(() => false)) await fs.unlink(lockPath).catch(() => void 0);
|
|
148
|
+
}
|
|
149
|
+
let directoryKey = null;
|
|
150
|
+
let closeDirectory = () => void 0;
|
|
151
|
+
try {
|
|
152
|
+
await fs.mkdir(resolvedRoot, { recursive: true });
|
|
153
|
+
const stat = await fs.stat(resolvedRoot, { bigint: true });
|
|
154
|
+
const key = `${stat.dev}:${stat.ino}`;
|
|
155
|
+
if (lockedDirectories.has(key)) throw heldElsewhere(root, "another engine in this process", lockPath);
|
|
156
|
+
lockedDirectories.add(key);
|
|
157
|
+
directoryKey = key;
|
|
158
|
+
await acquire(fs, root, lockPath, self);
|
|
159
|
+
closeDirectory = holdDirectoryOpen(resolvedRoot);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
if (directoryKey !== null) lockedDirectories.delete(directoryKey);
|
|
162
|
+
await removeOwnLockFile();
|
|
163
|
+
if (err instanceof NarsilError) throw err;
|
|
164
|
+
throw new NarsilError(
|
|
165
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
166
|
+
`The engine cannot lock the durability directory "${root}"`,
|
|
167
|
+
{ directory: root, cause: err instanceof Error ? err.message : String(err) }
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const heldKey = directoryKey;
|
|
171
|
+
let released = false;
|
|
172
|
+
return {
|
|
173
|
+
holds,
|
|
174
|
+
async release() {
|
|
175
|
+
if (released) return;
|
|
176
|
+
released = true;
|
|
177
|
+
lockedDirectories.delete(heldKey);
|
|
178
|
+
await removeOwnLockFile();
|
|
179
|
+
closeDirectory();
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/persistence/durability/durable-filesystem.ts
|
|
185
|
+
function validateRelativeKey(key) {
|
|
186
|
+
if (key.length === 0) {
|
|
187
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: empty");
|
|
188
|
+
}
|
|
189
|
+
if (key.includes("\0")) {
|
|
190
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: null byte detected", { key });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function wrapFsyncError(err, key) {
|
|
194
|
+
throw new NarsilError(
|
|
195
|
+
ErrorCodes.PERSISTENCE_FSYNC_FAILED,
|
|
196
|
+
`fsync failed for "${key}"; the write is not acknowledged and the node must recover from the durable log`,
|
|
197
|
+
{ key, cause: err instanceof Error ? err.message : String(err) }
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
async function openReadWriteCreating(filePath, fs) {
|
|
201
|
+
try {
|
|
202
|
+
return await fs.open(filePath, "r+");
|
|
203
|
+
} catch (err) {
|
|
204
|
+
if (err.code !== "ENOENT") {
|
|
205
|
+
throw err;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
return await fs.open(filePath, "wx+");
|
|
210
|
+
} catch (err) {
|
|
211
|
+
if (err.code !== "EEXIST") {
|
|
212
|
+
throw err;
|
|
213
|
+
}
|
|
214
|
+
return fs.open(filePath, "r+");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async function writeFully(handle, buffer, key) {
|
|
218
|
+
let offset = 0;
|
|
219
|
+
while (offset < buffer.length) {
|
|
220
|
+
const { bytesWritten } = await handle.write(buffer, offset, buffer.length - offset);
|
|
221
|
+
if (bytesWritten <= 0) {
|
|
222
|
+
throw new NarsilError(
|
|
223
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
224
|
+
`Write to "${key}" made no progress; the file would be truncated and the write is not acknowledged`,
|
|
225
|
+
{ key, written: offset, total: buffer.length }
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
offset += bytesWritten;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async function fsyncDirectory(dirPath, fs) {
|
|
232
|
+
let handle = null;
|
|
233
|
+
try {
|
|
234
|
+
handle = await fs.open(dirPath, "r");
|
|
235
|
+
await handle.sync();
|
|
236
|
+
} catch (err) {
|
|
237
|
+
const code = err.code;
|
|
238
|
+
if (code === "EISDIR" || code === "EPERM" || code === "EINVAL" || code === "ENOTSUP") {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
wrapFsyncError(err, dirPath);
|
|
242
|
+
} finally {
|
|
243
|
+
if (handle !== null) {
|
|
244
|
+
await handle.close().catch(() => void 0);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function createDurableDirectory(root) {
|
|
249
|
+
if (root.trim().length === 0) {
|
|
250
|
+
throw new NarsilError(ErrorCodes.CONFIG_INVALID, "Durable directory requires a non-empty root path");
|
|
251
|
+
}
|
|
252
|
+
async function resolve(key) {
|
|
253
|
+
validateRelativeKey(key);
|
|
254
|
+
const pathMod = await getPath();
|
|
255
|
+
const resolvedBase = pathMod.resolve(root);
|
|
256
|
+
const resolvedPath = pathMod.resolve(root, key);
|
|
257
|
+
if (resolvedPath !== resolvedBase && !resolvedPath.startsWith(resolvedBase + pathMod.sep)) {
|
|
258
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "Invalid key: path traversal detected", { key });
|
|
259
|
+
}
|
|
260
|
+
return { path: resolvedPath, resolvedBase, pathMod };
|
|
261
|
+
}
|
|
262
|
+
async function ensureDir(filePath, pathMod, fs) {
|
|
263
|
+
await fs.mkdir(pathMod.dirname(filePath), { recursive: true });
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
get root() {
|
|
267
|
+
return root;
|
|
268
|
+
},
|
|
269
|
+
lock() {
|
|
270
|
+
return lockDirectory(root);
|
|
271
|
+
},
|
|
272
|
+
async identity() {
|
|
273
|
+
const fs = await getFs();
|
|
274
|
+
const pathMod = await getPath();
|
|
275
|
+
try {
|
|
276
|
+
const stat = await fs.stat(pathMod.resolve(root), { bigint: true });
|
|
277
|
+
return `${stat.dev}:${stat.ino}`;
|
|
278
|
+
} catch (err) {
|
|
279
|
+
if (err.code === "ENOENT") return null;
|
|
280
|
+
throw err;
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
async appendHandle(key) {
|
|
284
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
285
|
+
const fs = await getFs();
|
|
286
|
+
await ensureDir(filePath, pathMod, fs);
|
|
287
|
+
const handle = await fs.open(filePath, "a");
|
|
288
|
+
return {
|
|
289
|
+
async append(bytes) {
|
|
290
|
+
await writeFully(handle, bytes, key);
|
|
291
|
+
},
|
|
292
|
+
async sync() {
|
|
293
|
+
try {
|
|
294
|
+
await handle.sync();
|
|
295
|
+
} catch (err) {
|
|
296
|
+
wrapFsyncError(err, key);
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
async truncate(byteLength) {
|
|
300
|
+
await handle.truncate(byteLength);
|
|
301
|
+
try {
|
|
302
|
+
await handle.sync();
|
|
303
|
+
} catch (err) {
|
|
304
|
+
wrapFsyncError(err, key);
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
async size() {
|
|
308
|
+
const stat = await handle.stat();
|
|
309
|
+
return stat.size;
|
|
310
|
+
},
|
|
311
|
+
async close() {
|
|
312
|
+
await handle.close();
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
},
|
|
316
|
+
async markerHandle(key) {
|
|
317
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
318
|
+
const fs = await getFs();
|
|
319
|
+
await ensureDir(filePath, pathMod, fs);
|
|
320
|
+
const handle = await openReadWriteCreating(filePath, fs);
|
|
321
|
+
return {
|
|
322
|
+
async read() {
|
|
323
|
+
const stat = await handle.stat();
|
|
324
|
+
if (stat.size === 0) {
|
|
325
|
+
return new Uint8Array(0);
|
|
326
|
+
}
|
|
327
|
+
const buffer = Buffer.alloc(stat.size);
|
|
328
|
+
await handle.read(buffer, 0, stat.size, 0);
|
|
329
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
330
|
+
},
|
|
331
|
+
async writeSlot(offset, slot) {
|
|
332
|
+
await handle.write(slot, 0, slot.length, offset);
|
|
333
|
+
try {
|
|
334
|
+
await handle.sync();
|
|
335
|
+
} catch (err) {
|
|
336
|
+
wrapFsyncError(err, key);
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
async close() {
|
|
340
|
+
await handle.close();
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
},
|
|
344
|
+
async syncDirectoryOf(key) {
|
|
345
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
346
|
+
const fs = await getFs();
|
|
347
|
+
await fsyncDirectory(pathMod.dirname(filePath), fs);
|
|
348
|
+
},
|
|
349
|
+
async atomicWrite(key, data) {
|
|
350
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
351
|
+
const fs = await getFs();
|
|
352
|
+
await ensureDir(filePath, pathMod, fs);
|
|
353
|
+
const dir = pathMod.dirname(filePath);
|
|
354
|
+
const tempName = `${pathMod.basename(filePath)}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
355
|
+
const tempPath = pathMod.join(dir, tempName);
|
|
356
|
+
const buffers = Array.isArray(data) ? data : [data];
|
|
357
|
+
let handle = null;
|
|
358
|
+
try {
|
|
359
|
+
handle = await fs.open(tempPath, "w");
|
|
360
|
+
for (const buffer of buffers) {
|
|
361
|
+
await writeFully(handle, buffer, tempPath);
|
|
362
|
+
}
|
|
363
|
+
try {
|
|
364
|
+
await handle.sync();
|
|
365
|
+
} catch (err) {
|
|
366
|
+
wrapFsyncError(err, tempPath);
|
|
367
|
+
}
|
|
368
|
+
await handle.close();
|
|
369
|
+
handle = null;
|
|
370
|
+
await fs.rename(tempPath, filePath);
|
|
371
|
+
await fsyncDirectory(dir, fs);
|
|
372
|
+
} catch (err) {
|
|
373
|
+
if (handle !== null) {
|
|
374
|
+
await handle.close().catch(() => void 0);
|
|
375
|
+
}
|
|
376
|
+
await fs.unlink(tempPath).catch(() => void 0);
|
|
377
|
+
throw err;
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
async read(key) {
|
|
381
|
+
const { path: filePath } = await resolve(key);
|
|
382
|
+
const fs = await getFs();
|
|
383
|
+
try {
|
|
384
|
+
const data = await fs.readFile(filePath);
|
|
385
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
386
|
+
} catch (err) {
|
|
387
|
+
if (err.code === "ENOENT") {
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Failed to read "${key}"`, {
|
|
391
|
+
key,
|
|
392
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
async readRange(key, offset, length) {
|
|
397
|
+
const { path: filePath } = await resolve(key);
|
|
398
|
+
const fs = await getFs();
|
|
399
|
+
let handle = null;
|
|
400
|
+
try {
|
|
401
|
+
handle = await fs.open(filePath, "r");
|
|
402
|
+
const bytes = new Uint8Array(length);
|
|
403
|
+
let filled = 0;
|
|
404
|
+
while (filled < length) {
|
|
405
|
+
const { bytesRead } = await handle.read(bytes, filled, length - filled, offset + filled);
|
|
406
|
+
if (bytesRead === 0) break;
|
|
407
|
+
filled += bytesRead;
|
|
408
|
+
}
|
|
409
|
+
if (filled < length) {
|
|
410
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `"${key}" ends before the bytes a read asked for`, {
|
|
411
|
+
key,
|
|
412
|
+
offset,
|
|
413
|
+
length
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
return bytes;
|
|
417
|
+
} finally {
|
|
418
|
+
await handle?.close();
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
async byteLength(key) {
|
|
422
|
+
const { path: filePath } = await resolve(key);
|
|
423
|
+
const fs = await getFs();
|
|
424
|
+
try {
|
|
425
|
+
return (await fs.stat(filePath)).size;
|
|
426
|
+
} catch (err) {
|
|
427
|
+
if (err.code === "ENOENT") {
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Failed to read the length of "${key}"`, {
|
|
431
|
+
key,
|
|
432
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
async remove(key) {
|
|
437
|
+
const { path: filePath, pathMod } = await resolve(key);
|
|
438
|
+
const fs = await getFs();
|
|
439
|
+
try {
|
|
440
|
+
await fs.unlink(filePath);
|
|
441
|
+
} catch (err) {
|
|
442
|
+
if (err.code === "ENOENT") {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_DELETE_FAILED, `Failed to delete "${key}"`, {
|
|
446
|
+
key,
|
|
447
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
await fsyncDirectory(pathMod.dirname(filePath), fs);
|
|
451
|
+
},
|
|
452
|
+
async move(from, to) {
|
|
453
|
+
const source = await resolve(from);
|
|
454
|
+
const target = await resolve(to);
|
|
455
|
+
const fs = await getFs();
|
|
456
|
+
await ensureDir(target.path, target.pathMod, fs);
|
|
457
|
+
await fs.rename(source.path, target.path);
|
|
458
|
+
await fsyncDirectory(target.pathMod.dirname(target.path), fs);
|
|
459
|
+
await fsyncDirectory(source.pathMod.dirname(source.path), fs);
|
|
460
|
+
},
|
|
461
|
+
async list(prefix) {
|
|
462
|
+
const fs = await getFs();
|
|
463
|
+
const pathMod = await getPath();
|
|
464
|
+
const resolvedBase = pathMod.resolve(root);
|
|
465
|
+
const all = await listRecursive(resolvedBase, resolvedBase, pathMod, fs);
|
|
466
|
+
return all.filter((entry) => entry.startsWith(prefix)).sort(compareCodePoints);
|
|
467
|
+
},
|
|
468
|
+
async pathOf(key) {
|
|
469
|
+
const { path: filePath } = await resolve(key);
|
|
470
|
+
return filePath;
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
async function listRecursive(dir, base, pathMod, fs) {
|
|
475
|
+
const results = [];
|
|
476
|
+
let entries;
|
|
477
|
+
try {
|
|
478
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
479
|
+
} catch (err) {
|
|
480
|
+
if (err.code === "ENOENT") {
|
|
481
|
+
return results;
|
|
482
|
+
}
|
|
483
|
+
throw err;
|
|
484
|
+
}
|
|
485
|
+
for (const entry of entries) {
|
|
486
|
+
const full = pathMod.join(dir, entry.name);
|
|
487
|
+
if (entry.isDirectory()) {
|
|
488
|
+
results.push(...await listRecursive(full, base, pathMod, fs));
|
|
489
|
+
} else {
|
|
490
|
+
results.push(pathMod.relative(base, full).split(pathMod.sep).join("/"));
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return results;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// src/persistence/durability/sha256.ts
|
|
497
|
+
var ROUND_CONSTANTS = new Uint32Array([
|
|
498
|
+
1116352408,
|
|
499
|
+
1899447441,
|
|
500
|
+
3049323471,
|
|
501
|
+
3921009573,
|
|
502
|
+
961987163,
|
|
503
|
+
1508970993,
|
|
504
|
+
2453635748,
|
|
505
|
+
2870763221,
|
|
506
|
+
3624381080,
|
|
507
|
+
310598401,
|
|
508
|
+
607225278,
|
|
509
|
+
1426881987,
|
|
510
|
+
1925078388,
|
|
511
|
+
2162078206,
|
|
512
|
+
2614888103,
|
|
513
|
+
3248222580,
|
|
514
|
+
3835390401,
|
|
515
|
+
4022224774,
|
|
516
|
+
264347078,
|
|
517
|
+
604807628,
|
|
518
|
+
770255983,
|
|
519
|
+
1249150122,
|
|
520
|
+
1555081692,
|
|
521
|
+
1996064986,
|
|
522
|
+
2554220882,
|
|
523
|
+
2821834349,
|
|
524
|
+
2952996808,
|
|
525
|
+
3210313671,
|
|
526
|
+
3336571891,
|
|
527
|
+
3584528711,
|
|
528
|
+
113926993,
|
|
529
|
+
338241895,
|
|
530
|
+
666307205,
|
|
531
|
+
773529912,
|
|
532
|
+
1294757372,
|
|
533
|
+
1396182291,
|
|
534
|
+
1695183700,
|
|
535
|
+
1986661051,
|
|
536
|
+
2177026350,
|
|
537
|
+
2456956037,
|
|
538
|
+
2730485921,
|
|
539
|
+
2820302411,
|
|
540
|
+
3259730800,
|
|
541
|
+
3345764771,
|
|
542
|
+
3516065817,
|
|
543
|
+
3600352804,
|
|
544
|
+
4094571909,
|
|
545
|
+
275423344,
|
|
546
|
+
430227734,
|
|
547
|
+
506948616,
|
|
548
|
+
659060556,
|
|
549
|
+
883997877,
|
|
550
|
+
958139571,
|
|
551
|
+
1322822218,
|
|
552
|
+
1537002063,
|
|
553
|
+
1747873779,
|
|
554
|
+
1955562222,
|
|
555
|
+
2024104815,
|
|
556
|
+
2227730452,
|
|
557
|
+
2361852424,
|
|
558
|
+
2428436474,
|
|
559
|
+
2756734187,
|
|
560
|
+
3204031479,
|
|
561
|
+
3329325298
|
|
562
|
+
]);
|
|
563
|
+
var INITIAL_STATE = [1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225];
|
|
564
|
+
var BLOCK_BYTES = 64;
|
|
565
|
+
var LENGTH_BYTES = 8;
|
|
566
|
+
function rotateRight(value, bits) {
|
|
567
|
+
return value >>> bits | value << 32 - bits;
|
|
568
|
+
}
|
|
569
|
+
function padded(bytes) {
|
|
570
|
+
const total = Math.ceil((bytes.byteLength + 1 + LENGTH_BYTES) / BLOCK_BYTES) * BLOCK_BYTES;
|
|
571
|
+
const message = new Uint8Array(total);
|
|
572
|
+
message.set(bytes);
|
|
573
|
+
message[bytes.byteLength] = 128;
|
|
574
|
+
const view = new DataView(message.buffer);
|
|
575
|
+
const bitLength = bytes.byteLength * 8;
|
|
576
|
+
view.setUint32(total - LENGTH_BYTES, Math.floor(bitLength / 4294967296), false);
|
|
577
|
+
view.setUint32(total - 4, bitLength >>> 0, false);
|
|
578
|
+
return view;
|
|
579
|
+
}
|
|
580
|
+
function sha256(bytes) {
|
|
581
|
+
const view = padded(bytes);
|
|
582
|
+
const state = INITIAL_STATE.slice();
|
|
583
|
+
const words = new Uint32Array(64);
|
|
584
|
+
for (let block = 0; block < view.byteLength; block += BLOCK_BYTES) {
|
|
585
|
+
for (let i = 0; i < 16; i++) words[i] = view.getUint32(block + i * 4, false);
|
|
586
|
+
for (let i = 16; i < 64; i++) {
|
|
587
|
+
const w15 = words[i - 15];
|
|
588
|
+
const w2 = words[i - 2];
|
|
589
|
+
const s0 = rotateRight(w15, 7) ^ rotateRight(w15, 18) ^ w15 >>> 3;
|
|
590
|
+
const s1 = rotateRight(w2, 17) ^ rotateRight(w2, 19) ^ w2 >>> 10;
|
|
591
|
+
words[i] = words[i - 16] + s0 + words[i - 7] + s1 >>> 0;
|
|
592
|
+
}
|
|
593
|
+
let [a, b, c, d, e, f, g, h] = state;
|
|
594
|
+
for (let i = 0; i < 64; i++) {
|
|
595
|
+
const s1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
|
|
596
|
+
const choice = e & f ^ ~e & g;
|
|
597
|
+
const t1 = h + s1 + choice + ROUND_CONSTANTS[i] + words[i] >>> 0;
|
|
598
|
+
const s0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
|
|
599
|
+
const majority = a & b ^ a & c ^ b & c;
|
|
600
|
+
const t2 = s0 + majority >>> 0;
|
|
601
|
+
h = g;
|
|
602
|
+
g = f;
|
|
603
|
+
f = e;
|
|
604
|
+
e = d + t1 >>> 0;
|
|
605
|
+
d = c;
|
|
606
|
+
c = b;
|
|
607
|
+
b = a;
|
|
608
|
+
a = t1 + t2 >>> 0;
|
|
609
|
+
}
|
|
610
|
+
state[0] = state[0] + a >>> 0;
|
|
611
|
+
state[1] = state[1] + b >>> 0;
|
|
612
|
+
state[2] = state[2] + c >>> 0;
|
|
613
|
+
state[3] = state[3] + d >>> 0;
|
|
614
|
+
state[4] = state[4] + e >>> 0;
|
|
615
|
+
state[5] = state[5] + f >>> 0;
|
|
616
|
+
state[6] = state[6] + g >>> 0;
|
|
617
|
+
state[7] = state[7] + h >>> 0;
|
|
618
|
+
}
|
|
619
|
+
const digest = new Uint8Array(32);
|
|
620
|
+
const out = new DataView(digest.buffer);
|
|
621
|
+
for (let i = 0; i < 8; i++) out.setUint32(i * 4, state[i], false);
|
|
622
|
+
return digest;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// src/persistence/durability/restore-journal.ts
|
|
626
|
+
var RESTORE_JOURNAL_PREFIX = ".swap-";
|
|
627
|
+
var RESTORE_STAGING_PREFIX = ".restore-";
|
|
628
|
+
var RETIRED_INDEX_PREFIX = ".retired-";
|
|
629
|
+
var LOCK_FILE_KEY = ".narsil.lock";
|
|
630
|
+
var STAGING_PREFIX_PATTERN = /^\.[A-Za-z0-9][A-Za-z0-9._-]*\/$/;
|
|
631
|
+
function digestOf(indexName) {
|
|
632
|
+
let hex = "";
|
|
633
|
+
for (const byte of sha256(new TextEncoder().encode(indexName))) hex += byte.toString(16).padStart(2, "0");
|
|
634
|
+
return hex;
|
|
635
|
+
}
|
|
636
|
+
function journalKey(indexName) {
|
|
637
|
+
return `${RESTORE_JOURNAL_PREFIX}${digestOf(indexName)}`;
|
|
638
|
+
}
|
|
639
|
+
function randomSuffix() {
|
|
640
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
641
|
+
}
|
|
642
|
+
function newStagingPrefix() {
|
|
643
|
+
return `${RESTORE_STAGING_PREFIX}${randomSuffix()}/`;
|
|
644
|
+
}
|
|
645
|
+
function newRetiredPrefix() {
|
|
646
|
+
return `${RETIRED_INDEX_PREFIX}${randomSuffix()}/`;
|
|
647
|
+
}
|
|
648
|
+
function encodeJournal(indexName, stagingPrefix) {
|
|
649
|
+
return new TextEncoder().encode(`${indexName}
|
|
650
|
+
${stagingPrefix}`);
|
|
651
|
+
}
|
|
652
|
+
function isStagingPrefix(prefix) {
|
|
653
|
+
return STAGING_PREFIX_PATTERN.test(prefix) && !prefix.includes("..");
|
|
654
|
+
}
|
|
655
|
+
function corruptJournal(key, reason) {
|
|
656
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The restore journal "${key}" ${reason}`, { key });
|
|
657
|
+
}
|
|
658
|
+
function readJournal(key, bytes) {
|
|
659
|
+
const text = new TextDecoder().decode(bytes);
|
|
660
|
+
const lineEnd = text.indexOf("\n");
|
|
661
|
+
const indexName = lineEnd === -1 ? text : text.slice(0, lineEnd);
|
|
662
|
+
if (key.slice(RESTORE_JOURNAL_PREFIX.length) !== digestOf(indexName)) return { kind: "leftover" };
|
|
663
|
+
if (indexName.length > MAX_INDEX_NAME_LENGTH || !INDEX_NAME_PATTERN.test(indexName) || indexName.includes("..")) {
|
|
664
|
+
corruptJournal(key, "names an index that no index name can match");
|
|
665
|
+
}
|
|
666
|
+
const stagingPrefix = lineEnd === -1 ? "" : text.slice(lineEnd + 1);
|
|
667
|
+
if (!isStagingPrefix(stagingPrefix) || stagingPrefix === `${LOCK_FILE_KEY}/`) {
|
|
668
|
+
corruptJournal(key, "names no staging prefix that a restore writes");
|
|
669
|
+
}
|
|
670
|
+
return { kind: "journal", indexName, stagingPrefix };
|
|
671
|
+
}
|
|
672
|
+
async function listedKeys(storage, indexName, stagingPrefix) {
|
|
673
|
+
const manifestBytes = await storage.read(`${stagingPrefix}manifest`) ?? await storage.read(manifestKey(indexName));
|
|
674
|
+
if (manifestBytes === null) {
|
|
675
|
+
throw new NarsilError(
|
|
676
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
677
|
+
`The restore of index "${indexName}" lost its manifest, both from staging and from the index`,
|
|
678
|
+
{ indexName, stagingPrefix }
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
const decoded = await decodeSegmentManifest(manifestBytes, indexName);
|
|
682
|
+
if (decoded.kind !== "current") {
|
|
683
|
+
throw new NarsilError(
|
|
684
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
685
|
+
`The restore of index "${indexName}" staged a manifest in the layout of an earlier release`,
|
|
686
|
+
{ indexName }
|
|
687
|
+
);
|
|
688
|
+
}
|
|
689
|
+
const root = `${indexName}/`;
|
|
690
|
+
const keys = /* @__PURE__ */ new Set(["meta", "manifest"]);
|
|
691
|
+
for (const ref of manifestFileRefs(decoded.manifest)) keys.add(ref.key.slice(root.length));
|
|
692
|
+
return keys;
|
|
693
|
+
}
|
|
694
|
+
function withoutTrailingSlash(prefix) {
|
|
695
|
+
return prefix.slice(0, -1);
|
|
696
|
+
}
|
|
697
|
+
async function finishJournal(storage, indexName, stagingPrefix) {
|
|
698
|
+
const root = `${indexName}/`;
|
|
699
|
+
const listed = await listedKeys(storage, indexName, stagingPrefix);
|
|
700
|
+
const staged = await storage.list(stagingPrefix);
|
|
701
|
+
let retiredPrefix = null;
|
|
702
|
+
if (storage.moveTree !== void 0 && staged.length > 0) {
|
|
703
|
+
retiredPrefix = newRetiredPrefix();
|
|
704
|
+
await storage.moveTree(indexName, withoutTrailingSlash(retiredPrefix));
|
|
705
|
+
await storage.moveTree(withoutTrailingSlash(stagingPrefix), indexName);
|
|
706
|
+
} else {
|
|
707
|
+
const stagedKeys = new Set(staged);
|
|
708
|
+
for (const key of listed) {
|
|
709
|
+
if (stagedKeys.has(`${stagingPrefix}${key}`)) await storage.moveKey(`${stagingPrefix}${key}`, `${root}${key}`);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
for (const key of await storage.list(root)) {
|
|
713
|
+
if (!listed.has(key.slice(root.length))) await storage.remove(key);
|
|
714
|
+
}
|
|
715
|
+
await storage.remove(journalKey(indexName));
|
|
716
|
+
return retiredPrefix;
|
|
717
|
+
}
|
|
718
|
+
async function removePrefix(storage, prefix) {
|
|
719
|
+
if (storage.removeTree !== void 0) {
|
|
720
|
+
await storage.removeTree(prefix);
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
for (const key of await storage.list(prefix)) await storage.remove(key);
|
|
724
|
+
}
|
|
725
|
+
async function commitJournal(storage, indexName, stagingPrefix) {
|
|
726
|
+
await storage.writeJournal(journalKey(indexName), encodeJournal(indexName, stagingPrefix));
|
|
727
|
+
}
|
|
728
|
+
function isTopLevelJournal(key) {
|
|
729
|
+
return key.startsWith(RESTORE_JOURNAL_PREFIX) && !key.includes("/");
|
|
730
|
+
}
|
|
731
|
+
async function removeStagingPrefixes(storage) {
|
|
732
|
+
const prefixes = /* @__PURE__ */ new Set();
|
|
733
|
+
for (const key of await storage.list(".")) {
|
|
734
|
+
const slash = key.indexOf("/");
|
|
735
|
+
if (slash !== -1 && isStagingPrefix(key.slice(0, slash + 1))) prefixes.add(key.slice(0, slash + 1));
|
|
736
|
+
}
|
|
737
|
+
for (const prefix of prefixes) await removePrefix(storage, prefix);
|
|
738
|
+
}
|
|
739
|
+
async function recoverRestoreJournals(storage) {
|
|
740
|
+
for (const key of await storage.list(RESTORE_JOURNAL_PREFIX)) {
|
|
741
|
+
if (!isTopLevelJournal(key)) continue;
|
|
742
|
+
const bytes = await storage.read(key);
|
|
743
|
+
if (bytes === null) continue;
|
|
744
|
+
const reading = readJournal(key, bytes);
|
|
745
|
+
if (reading.kind === "leftover") {
|
|
746
|
+
await storage.remove(key);
|
|
747
|
+
continue;
|
|
748
|
+
}
|
|
749
|
+
await finishJournal(storage, reading.indexName, reading.stagingPrefix);
|
|
750
|
+
}
|
|
751
|
+
await removeStagingPrefixes(storage);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// src/persistence/durability/restore-directory.ts
|
|
755
|
+
function missing(error) {
|
|
756
|
+
return error.code === "ENOENT";
|
|
757
|
+
}
|
|
758
|
+
function storageFailed(code, message, key, error) {
|
|
759
|
+
return new NarsilError(code, message, { key, cause: error instanceof Error ? error.message : String(error) });
|
|
760
|
+
}
|
|
761
|
+
function directoryRestoreStorage(directory) {
|
|
762
|
+
return {
|
|
763
|
+
read: (key) => directory.read(key),
|
|
764
|
+
write: (key, chunks) => directory.atomicWrite(key, chunks),
|
|
765
|
+
writeJournal: (key, bytes) => directory.atomicWrite(key, bytes),
|
|
766
|
+
remove: (key) => directory.remove(key),
|
|
767
|
+
list: (prefix) => directory.list(prefix),
|
|
768
|
+
moveKey: (from, to) => directory.move(from, to),
|
|
769
|
+
pathOf: (key) => directory.pathOf(key),
|
|
770
|
+
async moveTree(from, to) {
|
|
771
|
+
const source = await directory.pathOf(from);
|
|
772
|
+
const target = await directory.pathOf(to);
|
|
773
|
+
const fs = await getFs();
|
|
774
|
+
try {
|
|
775
|
+
await fs.rename(source, target);
|
|
776
|
+
} catch (error) {
|
|
777
|
+
if (missing(error)) return;
|
|
778
|
+
throw storageFailed(ErrorCodes.PERSISTENCE_SAVE_FAILED, `Failed to move "${from}" to "${to}"`, from, error);
|
|
779
|
+
}
|
|
780
|
+
await directory.syncDirectoryOf(to);
|
|
781
|
+
},
|
|
782
|
+
async removeTree(prefix) {
|
|
783
|
+
const key = prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
784
|
+
const fs = await getFs();
|
|
785
|
+
try {
|
|
786
|
+
await fs.rm(await directory.pathOf(key), { recursive: true, force: true });
|
|
787
|
+
} catch (error) {
|
|
788
|
+
throw storageFailed(ErrorCodes.PERSISTENCE_DELETE_FAILED, `Failed to delete "${key}"`, key, error);
|
|
789
|
+
}
|
|
790
|
+
await directory.syncDirectoryOf(key);
|
|
791
|
+
}
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
function recoverDirectoryRestores(directory) {
|
|
795
|
+
return recoverRestoreJournals(directoryRestoreStorage(directory));
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// src/persistence/durability/restore-staging.ts
|
|
799
|
+
function toError(error) {
|
|
800
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
801
|
+
}
|
|
802
|
+
function pathUnder(path, root) {
|
|
803
|
+
if (!path.startsWith(root)) return false;
|
|
804
|
+
const next = path.charAt(root.length);
|
|
805
|
+
return next === "" || next === "/" || next === "\\";
|
|
806
|
+
}
|
|
807
|
+
function createRestoreStaging(storage, indexName, options = {}) {
|
|
808
|
+
const stagingPrefix = newStagingPrefix();
|
|
809
|
+
const indexRoot = `${indexName}/`;
|
|
810
|
+
let committed = false;
|
|
811
|
+
const staged = (key) => {
|
|
812
|
+
if (!key.startsWith(indexRoot)) {
|
|
813
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `A restore of "${indexName}" cannot stage "${key}"`, {
|
|
814
|
+
key
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
return `${stagingPrefix}${key.slice(indexRoot.length)}`;
|
|
818
|
+
};
|
|
819
|
+
const unstaged = (key) => `${indexRoot}${key.slice(stagingPrefix.length)}`;
|
|
820
|
+
const store = {
|
|
821
|
+
read: (key) => storage.read(staged(key)),
|
|
822
|
+
write: (key, chunks) => storage.write(staged(key), chunks),
|
|
823
|
+
remove: (key) => storage.remove(staged(key)),
|
|
824
|
+
list: async (prefix) => (await storage.list(staged(prefix))).map(unstaged)
|
|
825
|
+
};
|
|
826
|
+
const pathOf = storage.pathOf;
|
|
827
|
+
if (pathOf !== void 0) store.pathOf = (key) => pathOf.call(storage, staged(key));
|
|
828
|
+
let retiredPrefix = null;
|
|
829
|
+
async function finishOnStorage() {
|
|
830
|
+
retiredPrefix = await finishJournal(storage, indexName, stagingPrefix);
|
|
831
|
+
}
|
|
832
|
+
return {
|
|
833
|
+
store,
|
|
834
|
+
async relocation() {
|
|
835
|
+
if (pathOf === void 0 || (await storage.list(stagingPrefix)).length > 0) return null;
|
|
836
|
+
const from = await pathOf.call(storage, stagingPrefix.slice(0, -1));
|
|
837
|
+
const to = await pathOf.call(storage, indexName);
|
|
838
|
+
return (path) => pathUnder(path, from) ? `${to}${path.slice(from.length)}` : path;
|
|
839
|
+
},
|
|
840
|
+
async commit() {
|
|
841
|
+
try {
|
|
842
|
+
await commitJournal(storage, indexName, stagingPrefix);
|
|
843
|
+
} catch (error) {
|
|
844
|
+
try {
|
|
845
|
+
await storage.remove(journalKey(indexName));
|
|
846
|
+
} catch (removal) {
|
|
847
|
+
committed = true;
|
|
848
|
+
options.onStorageLost?.(toError(removal));
|
|
849
|
+
}
|
|
850
|
+
throw error;
|
|
851
|
+
}
|
|
852
|
+
committed = true;
|
|
853
|
+
},
|
|
854
|
+
async finish() {
|
|
855
|
+
try {
|
|
856
|
+
await (options.finish === void 0 ? finishOnStorage() : options.finish(finishOnStorage));
|
|
857
|
+
} catch (error) {
|
|
858
|
+
options.onStorageLost?.(toError(error));
|
|
859
|
+
}
|
|
860
|
+
},
|
|
861
|
+
async removeRetired() {
|
|
862
|
+
const prefix = retiredPrefix;
|
|
863
|
+
retiredPrefix = null;
|
|
864
|
+
if (prefix !== null) await removePrefix(storage, prefix);
|
|
865
|
+
},
|
|
866
|
+
async discard() {
|
|
867
|
+
if (committed) return;
|
|
868
|
+
await removePrefix(storage, stagingPrefix);
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
function adapterRestoreStorage(adapter) {
|
|
873
|
+
const store = adapterCheckpointStore(adapter);
|
|
874
|
+
return {
|
|
875
|
+
read: (key) => store.read(key),
|
|
876
|
+
write: (key, chunks) => store.write(key, chunks),
|
|
877
|
+
writeJournal: (key, bytes) => store.write(key, [bytes]),
|
|
878
|
+
remove: (key) => store.remove(key),
|
|
879
|
+
list: (prefix) => store.list(prefix),
|
|
880
|
+
async moveKey(from, to) {
|
|
881
|
+
const bytes = await store.read(from);
|
|
882
|
+
if (bytes === null) return;
|
|
883
|
+
await store.write(to, [bytes]);
|
|
884
|
+
await store.remove(from);
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// src/persistence/durability/archive-checkpoint.ts
|
|
890
|
+
function toError2(error) {
|
|
891
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
892
|
+
}
|
|
893
|
+
function stageDirectoryRestore(deps, indexName) {
|
|
894
|
+
return createRestoreStaging(directoryRestoreStorage(deps.directory), indexName, {
|
|
895
|
+
async finish(run) {
|
|
896
|
+
try {
|
|
897
|
+
await run();
|
|
898
|
+
} catch (error) {
|
|
899
|
+
deps.markFatal(toError2(error));
|
|
900
|
+
throw error;
|
|
901
|
+
}
|
|
902
|
+
},
|
|
903
|
+
onStorageLost: deps.markFatal
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
function releaser(held, indexName) {
|
|
907
|
+
let released = false;
|
|
908
|
+
return () => {
|
|
909
|
+
if (released) return;
|
|
910
|
+
released = true;
|
|
911
|
+
const count = (held.get(indexName) ?? 1) - 1;
|
|
912
|
+
if (count <= 0) held.delete(indexName);
|
|
913
|
+
else held.set(indexName, count);
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
function createArchiveCheckpointAccess(deps) {
|
|
917
|
+
const store = directoryCheckpointStore(deps.directory);
|
|
918
|
+
return {
|
|
919
|
+
async openArchiveCheckpoint(indexName) {
|
|
920
|
+
deps.held.set(indexName, (deps.held.get(indexName) ?? 0) + 1);
|
|
921
|
+
const release = releaser(deps.held, indexName);
|
|
922
|
+
try {
|
|
923
|
+
await deps.checkpoint(indexName);
|
|
924
|
+
await deps.checkpoint(indexName);
|
|
925
|
+
const manifestBytes = await deps.directory.read(manifestKey(indexName));
|
|
926
|
+
if (manifestBytes === null) {
|
|
927
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `Index "${indexName}" holds no checkpoint`, {
|
|
928
|
+
indexName
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
const decoded = await decodeSegmentManifest(manifestBytes, indexName);
|
|
932
|
+
if (decoded.kind !== "current") {
|
|
933
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, `Index "${indexName}" holds an earlier layout`, {
|
|
934
|
+
indexName
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
return {
|
|
938
|
+
manifestBytes,
|
|
939
|
+
manifest: decoded.manifest,
|
|
940
|
+
readFile: (ref) => readListedFile(store, ref.key, { bytes: ref.bytes, what: "checkpoint file" }),
|
|
941
|
+
readRange: async (ref, offset, length) => {
|
|
942
|
+
const readRange = deps.directory.readRange;
|
|
943
|
+
if (readRange !== void 0) return readRange.call(deps.directory, ref.key, offset, length);
|
|
944
|
+
const whole = await readListedFile(store, ref.key, { bytes: ref.bytes, what: "checkpoint file" });
|
|
945
|
+
return whole.slice(offset, offset + length);
|
|
946
|
+
},
|
|
947
|
+
release
|
|
948
|
+
};
|
|
949
|
+
} catch (error) {
|
|
950
|
+
release();
|
|
951
|
+
throw error;
|
|
952
|
+
}
|
|
953
|
+
},
|
|
954
|
+
stageRestore(indexName) {
|
|
955
|
+
return stageDirectoryRestore(deps, indexName);
|
|
956
|
+
},
|
|
957
|
+
adoptRestoredCheckpoint(indexName, checkpoint) {
|
|
958
|
+
for (const { partitionId, lastSeqNo, primaryTerm } of checkpoint) {
|
|
959
|
+
deps.registerPartition(indexName, partitionId, lastSeqNo, primaryTerm);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
// src/persistence/durability/checkpoint.ts
|
|
966
|
+
function walPrefix(indexName, partitionId) {
|
|
967
|
+
return `${indexName}/wal/${partitionId}/`;
|
|
968
|
+
}
|
|
969
|
+
async function truncateCoveredSegments(directory, indexName, checkpoint) {
|
|
970
|
+
for (const { partitionId, lastSeqNo } of checkpoint) {
|
|
971
|
+
const prefix = walPrefix(indexName, partitionId);
|
|
972
|
+
const markerBytes = await directory.read(`${prefix}commit`);
|
|
973
|
+
const marker = markerBytes === null ? null : readCommitMarker(markerBytes);
|
|
974
|
+
if (marker === null) {
|
|
975
|
+
continue;
|
|
976
|
+
}
|
|
977
|
+
const activeSegmentSeqNo = marker.state.activeSegmentSeqNo;
|
|
978
|
+
const segments = await directory.list(prefix);
|
|
979
|
+
const startSeqNos = segments.map((key) => parseSegmentStartSeqNo(key, prefix)).filter((value) => value !== null).sort((a, b) => a - b);
|
|
980
|
+
for (let i = 0; i < startSeqNos.length; i += 1) {
|
|
981
|
+
const start = startSeqNos[i];
|
|
982
|
+
if (start >= activeSegmentSeqNo) {
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
const nextStart = startSeqNos[i + 1];
|
|
986
|
+
const segmentCoversBeyondCheckpoint = nextStart === void 0 || nextStart > lastSeqNo + 1;
|
|
987
|
+
if (segmentCoversBeyondCheckpoint) {
|
|
988
|
+
break;
|
|
989
|
+
}
|
|
990
|
+
await directory.remove(`${prefix}${formatSegmentStart(start)}`);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
async function reclaimWalBeyondCount(directory, indexName, partitionCount, partitions, onCloseError) {
|
|
995
|
+
for (const [partitionId, partition] of [...partitions]) {
|
|
996
|
+
if (partitionId < partitionCount) continue;
|
|
997
|
+
try {
|
|
998
|
+
await partition.walWriter.close();
|
|
999
|
+
} catch (err) {
|
|
1000
|
+
onCloseError(err instanceof Error ? err : new Error(String(err)));
|
|
1001
|
+
}
|
|
1002
|
+
partitions.delete(partitionId);
|
|
1003
|
+
}
|
|
1004
|
+
const walRoot = `${indexName}/wal/`;
|
|
1005
|
+
for (const key of await directory.list(walRoot)) {
|
|
1006
|
+
const partitionId = walKeyPartitionId(key, walRoot);
|
|
1007
|
+
if (partitionId === null || partitionId < partitionCount) continue;
|
|
1008
|
+
await directory.remove(key);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
function walKeyPartitionId(key, walRoot) {
|
|
1012
|
+
const tail = key.slice(walRoot.length);
|
|
1013
|
+
const separator = tail.indexOf("/");
|
|
1014
|
+
if (separator <= 0) {
|
|
1015
|
+
return null;
|
|
1016
|
+
}
|
|
1017
|
+
const segment = tail.slice(0, separator);
|
|
1018
|
+
if (!/^\d+$/.test(segment)) {
|
|
1019
|
+
return null;
|
|
1020
|
+
}
|
|
1021
|
+
const value = Number.parseInt(segment, 10);
|
|
1022
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
1023
|
+
}
|
|
1024
|
+
function parseSegmentStartSeqNo(key, prefix) {
|
|
1025
|
+
if (!key.startsWith(prefix)) {
|
|
1026
|
+
return null;
|
|
1027
|
+
}
|
|
1028
|
+
const tail = key.slice(prefix.length);
|
|
1029
|
+
if (!/^\d{16}$/.test(tail)) {
|
|
1030
|
+
return null;
|
|
1031
|
+
}
|
|
1032
|
+
const value = Number.parseInt(tail, 10);
|
|
1033
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
1034
|
+
}
|
|
1035
|
+
function formatSegmentStart(startSeqNo) {
|
|
1036
|
+
return startSeqNo.toString().padStart(16, "0");
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// src/persistence/durability/recovery.ts
|
|
1040
|
+
async function listPersistedIndexes(directory) {
|
|
1041
|
+
const metaKeys = await directory.list("");
|
|
1042
|
+
const names = [];
|
|
1043
|
+
for (const key of metaKeys) {
|
|
1044
|
+
if (key.endsWith("/meta")) {
|
|
1045
|
+
names.push(key.slice(0, -"/meta".length));
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
return names;
|
|
1049
|
+
}
|
|
1050
|
+
async function loadMetadata(directory, indexName) {
|
|
1051
|
+
const bytes = await directory.read(`${indexName}/meta`);
|
|
1052
|
+
if (bytes === null) {
|
|
1053
|
+
return null;
|
|
1054
|
+
}
|
|
1055
|
+
const { metadata } = await readMetadataEnvelope(bytes);
|
|
1056
|
+
return metadata;
|
|
1057
|
+
}
|
|
1058
|
+
async function replayWal(directory, indexName, partitionId, fromSeqNoExclusive, deps) {
|
|
1059
|
+
const read = await readWalSegments(directory, indexName, partitionId);
|
|
1060
|
+
if (read === null) {
|
|
1061
|
+
return { highestSeqNo: fromSeqNoExclusive, highestPrimaryTerm: 0 };
|
|
1062
|
+
}
|
|
1063
|
+
const { marker, segments, durableEntries, activeTail, highestReadFromWal } = read;
|
|
1064
|
+
if (Math.max(fromSeqNoExclusive, highestReadFromWal) < marker.state.highestDurableSeqNo) {
|
|
1065
|
+
throw durableRecordMissing(indexName, partitionId, highestReadFromWal, marker);
|
|
1066
|
+
}
|
|
1067
|
+
let highestSeqNo = fromSeqNoExclusive;
|
|
1068
|
+
let highestPrimaryTerm = 0;
|
|
1069
|
+
const replay = (entries) => {
|
|
1070
|
+
for (const entry of entries) {
|
|
1071
|
+
if (entry.seqNo <= highestSeqNo) {
|
|
1072
|
+
continue;
|
|
1073
|
+
}
|
|
1074
|
+
applyEntry(entry, deps);
|
|
1075
|
+
highestSeqNo = entry.seqNo;
|
|
1076
|
+
if (entry.primaryTerm > highestPrimaryTerm) {
|
|
1077
|
+
highestPrimaryTerm = entry.primaryTerm;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
replay(durableEntries);
|
|
1082
|
+
if (activeTail !== null) {
|
|
1083
|
+
replay(activeTail.entries);
|
|
1084
|
+
}
|
|
1085
|
+
await deleteOrphanSegments(directory, segments, marker.state.activeSegmentSeqNo);
|
|
1086
|
+
if (activeTail !== null && activeTail.cleanEnd < activeTail.segmentLength) {
|
|
1087
|
+
await truncateSegmentTail(directory, activeTail.key, activeTail.cleanEnd);
|
|
1088
|
+
}
|
|
1089
|
+
return { highestSeqNo, highestPrimaryTerm };
|
|
1090
|
+
}
|
|
1091
|
+
async function deleteOrphanSegments(directory, segments, activeSegmentSeqNo) {
|
|
1092
|
+
for (const { key, startSeqNo } of segments) {
|
|
1093
|
+
if (startSeqNo > activeSegmentSeqNo) {
|
|
1094
|
+
await directory.remove(key);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
async function truncateSegmentTail(directory, key, durableByteLength) {
|
|
1099
|
+
const cleanLength = Math.max(durableByteLength, SEGMENT_HEADER_SIZE);
|
|
1100
|
+
const handle = await directory.appendHandle(key);
|
|
1101
|
+
try {
|
|
1102
|
+
const currentSize = await handle.size();
|
|
1103
|
+
if (cleanLength < currentSize) {
|
|
1104
|
+
await handle.truncate(cleanLength);
|
|
1105
|
+
}
|
|
1106
|
+
} finally {
|
|
1107
|
+
await handle.close();
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
function applyEntry(entry, deps) {
|
|
1111
|
+
if (entry.operation === "DELETE") {
|
|
1112
|
+
applyDeleteEntry(entry, deps.manager, deps.vectorIndexes);
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
applyIndexEntry(entry, deps.manager, deps.vectorFieldPaths, deps.vectorIndexes);
|
|
1116
|
+
}
|
|
1117
|
+
function snapshotCheckpointFor(checkpoint, partitionId) {
|
|
1118
|
+
return checkpointLastSeqNo(checkpoint, partitionId);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
// src/persistence/durability/checkpoint-capture.ts
|
|
1122
|
+
function whileNoMutationApplies(partitions, capture2) {
|
|
1123
|
+
return new Promise((resolve, reject) => {
|
|
1124
|
+
let release = () => void 0;
|
|
1125
|
+
const released = new Promise((settle) => {
|
|
1126
|
+
release = settle;
|
|
1127
|
+
});
|
|
1128
|
+
function captureNow() {
|
|
1129
|
+
try {
|
|
1130
|
+
resolve(capture2());
|
|
1131
|
+
} catch (error) {
|
|
1132
|
+
reject(error);
|
|
1133
|
+
} finally {
|
|
1134
|
+
release();
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
let waiting = partitions.length;
|
|
1138
|
+
if (waiting === 0) {
|
|
1139
|
+
captureNow();
|
|
1140
|
+
return;
|
|
1141
|
+
}
|
|
1142
|
+
for (const partition of partitions) {
|
|
1143
|
+
partition.appendChain = partition.appendChain.then(() => {
|
|
1144
|
+
waiting -= 1;
|
|
1145
|
+
if (waiting === 0) captureNow();
|
|
1146
|
+
return released;
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
async function makeEveryAppliedMutationDurable(indexState, failPartition2) {
|
|
1152
|
+
for (const [partitionId, partition] of [...indexState.partitions]) {
|
|
1153
|
+
await partition.appendChain;
|
|
1154
|
+
if (partition.failed !== null) continue;
|
|
1155
|
+
try {
|
|
1156
|
+
await partition.walWriter.commit();
|
|
1157
|
+
} catch (error) {
|
|
1158
|
+
failPartition2(partitionId, partition, error);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
function aTargetFailed(indexState, targets) {
|
|
1163
|
+
return targets.some((target) => (indexState.partitions.get(target.partitionId)?.failed ?? null) !== null);
|
|
1164
|
+
}
|
|
1165
|
+
function noWholePartitions() {
|
|
1166
|
+
return { contents: [], captured: [] };
|
|
1167
|
+
}
|
|
1168
|
+
function wholePartitionsWhereMostChanged(manager, rules) {
|
|
1169
|
+
const { priorCheckpoint, priorPartitionIds, everyPartition, aWorkerCanSerialise } = rules;
|
|
1170
|
+
return (targets) => {
|
|
1171
|
+
const whole = { contents: [], captured: [] };
|
|
1172
|
+
const changed = targets.filter(
|
|
1173
|
+
(target) => everyPartition || target.lastSeqNo !== snapshotCheckpointFor(priorCheckpoint, target.partitionId)
|
|
1174
|
+
);
|
|
1175
|
+
const mostOfEachChanged = changed.every((target) => {
|
|
1176
|
+
const records = target.lastSeqNo - snapshotCheckpointFor(priorCheckpoint, target.partitionId);
|
|
1177
|
+
const documents = manager.getPartition(target.partitionId).count();
|
|
1178
|
+
return documents <= WHOLE_PARTITION_LARGEST_DOCUMENT_COUNT && records >= documents * WHOLE_PARTITION_MIN_CHANGED_SHARE;
|
|
1179
|
+
});
|
|
1180
|
+
const logMatchesMemory = targets.length === 1 && priorPartitionIds.every((partitionId) => partitionId === 0);
|
|
1181
|
+
if (!everyPartition && !(mostOfEachChanged && logMatchesMemory)) return whole;
|
|
1182
|
+
for (const { partitionId } of changed) {
|
|
1183
|
+
const captured = aWorkerCanSerialise ? captureWholePartition(manager, partitionId) : null;
|
|
1184
|
+
if (captured !== null) {
|
|
1185
|
+
whole.captured.push(captured);
|
|
1186
|
+
} else if (everyPartition || !aWorkerCanSerialise) {
|
|
1187
|
+
whole.contents.push(partitionContentsOf(manager, partitionId));
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
return whole;
|
|
1191
|
+
};
|
|
1192
|
+
}
|
|
1193
|
+
async function captureCheckpoint(indexState, manager, vectorIndexes, wholePartitionsOf = noWholePartitions, priorCheckpointThatLimitsTheRecords, listedVectorFields = []) {
|
|
1194
|
+
for (const vectorIndex of vectorIndexes.values()) await vectorIndex.completeGraph();
|
|
1195
|
+
return whileNoMutationApplies([...indexState.partitions.values()], () => {
|
|
1196
|
+
const documentCount = manager.countDocuments();
|
|
1197
|
+
const targets = [];
|
|
1198
|
+
let recordsLeftInLog = 0;
|
|
1199
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
1200
|
+
const partition = indexState.partitions.get(partitionId);
|
|
1201
|
+
if (partition !== void 0 && partition.failed !== null) continue;
|
|
1202
|
+
const appliedSeqNo = partition?.appliedSeqNo ?? 0;
|
|
1203
|
+
const lastSeqNo = priorCheckpointThatLimitsTheRecords === void 0 ? appliedSeqNo : Math.min(
|
|
1204
|
+
appliedSeqNo,
|
|
1205
|
+
snapshotCheckpointFor(priorCheckpointThatLimitsTheRecords, partitionId) + LOG_RECORDS_PER_CHECKPOINT
|
|
1206
|
+
);
|
|
1207
|
+
recordsLeftInLog += appliedSeqNo - lastSeqNo;
|
|
1208
|
+
targets.push({
|
|
1209
|
+
partitionId,
|
|
1210
|
+
lastSeqNo,
|
|
1211
|
+
primaryTerm: partition?.seqOwner.primaryTerm ?? SINGLE_NODE_PRIMARY_TERM
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
const vectorPlans = /* @__PURE__ */ new Map();
|
|
1215
|
+
for (const [fieldPath, vectorIndex] of vectorIndexes) {
|
|
1216
|
+
const listed = listedVectorFields.find((ref) => ref.fieldPath === fieldPath);
|
|
1217
|
+
const listedKeys2 = listed === void 0 ? null : listed.files.map((file) => file.key);
|
|
1218
|
+
vectorPlans.set(fieldPath, vectorIndex.planCheckpoint(listedKeys2));
|
|
1219
|
+
}
|
|
1220
|
+
const memoryMatchesTheTargets = recordsLeftInLog === 0;
|
|
1221
|
+
const wholePartitions = memoryMatchesTheTargets ? wholePartitionsOf(targets) : noWholePartitions();
|
|
1222
|
+
return { targets, documentCount, vectorPlans, wholePartitions, recordsLeftInLog };
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
async function writeCapturedVectors(store, indexName, capture2, priorManifest, rewriteUnchanged) {
|
|
1226
|
+
if (capture2.vectorPlans.size === 0) return { vectors: [], layouts: [], written: [] };
|
|
1227
|
+
const priorVectors = priorManifest?.vectors ?? [];
|
|
1228
|
+
const priorCheckpoint = priorManifest?.checkpoint ?? [];
|
|
1229
|
+
const noPartitionChanged = capture2.targets.every(
|
|
1230
|
+
(target) => target.lastSeqNo === snapshotCheckpointFor(priorCheckpoint, target.partitionId)
|
|
1231
|
+
);
|
|
1232
|
+
const everyFieldWritten = [...capture2.vectorPlans.keys()].every(
|
|
1233
|
+
(fieldPath) => priorVectors.some((ref) => ref.fieldPath === fieldPath)
|
|
1234
|
+
);
|
|
1235
|
+
const everyPlanUnchanged = [...capture2.vectorPlans.values()].every((plan) => plan.unchanged);
|
|
1236
|
+
if (!rewriteUnchanged && everyFieldWritten && noPartitionChanged && everyPlanUnchanged) {
|
|
1237
|
+
return { vectors: priorVectors, layouts: [], written: [] };
|
|
1238
|
+
}
|
|
1239
|
+
const written = await writeLiveVectors({ store, indexName, plans: capture2.vectorPlans, priorVectors });
|
|
1240
|
+
return { vectors: written.refs, layouts: written.layouts, written: written.written };
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
// src/persistence/durability/checkpoint-worker-dispatch.ts
|
|
1244
|
+
var workerUsable = true;
|
|
1245
|
+
var pooledWorker = null;
|
|
1246
|
+
var workerBusy = false;
|
|
1247
|
+
var spawnedWorkerCount = 0;
|
|
1248
|
+
var workerIdleMs = CHECKPOINT_WORKER_IDLE_MS;
|
|
1249
|
+
var idleTimer = null;
|
|
1250
|
+
async function spawnWorker() {
|
|
1251
|
+
const entryPoint = resolveWorkerEntry(
|
|
1252
|
+
import.meta.url,
|
|
1253
|
+
/\/src\/persistence\/durability\/[^/]+$/,
|
|
1254
|
+
"persistence/durability/checkpoint-worker.mjs"
|
|
1255
|
+
);
|
|
1256
|
+
if (entryPoint === null) return null;
|
|
1257
|
+
try {
|
|
1258
|
+
const worker = await spawnNodeWorker(new URL(entryPoint));
|
|
1259
|
+
if (worker === null) {
|
|
1260
|
+
return null;
|
|
1261
|
+
}
|
|
1262
|
+
if (typeof worker.unref === "function") {
|
|
1263
|
+
worker.unref();
|
|
1264
|
+
}
|
|
1265
|
+
worker.on("error", () => discardWorker(worker));
|
|
1266
|
+
worker.on("exit", () => discardWorker(worker));
|
|
1267
|
+
spawnedWorkerCount += 1;
|
|
1268
|
+
return worker;
|
|
1269
|
+
} catch {
|
|
1270
|
+
return null;
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
function discardWorker(worker) {
|
|
1274
|
+
if (pooledWorker === worker) {
|
|
1275
|
+
pooledWorker = null;
|
|
1276
|
+
}
|
|
1277
|
+
try {
|
|
1278
|
+
void worker.terminate();
|
|
1279
|
+
} catch {
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
function unrefTimer(timer) {
|
|
1283
|
+
if (typeof timer.unref === "function") {
|
|
1284
|
+
timer.unref();
|
|
1285
|
+
}
|
|
1286
|
+
return timer;
|
|
1287
|
+
}
|
|
1288
|
+
function runWorker(worker, request) {
|
|
1289
|
+
return new Promise((resolve) => {
|
|
1290
|
+
let settled = false;
|
|
1291
|
+
let timeoutId = armSilenceTimeout();
|
|
1292
|
+
function armSilenceTimeout() {
|
|
1293
|
+
return unrefTimer(setTimeout(() => settle({ written: null, timedOut: true }, true), CHECKPOINT_WORKER_TIMEOUT_MS));
|
|
1294
|
+
}
|
|
1295
|
+
const onMessage = (msg) => {
|
|
1296
|
+
const response = msg;
|
|
1297
|
+
if (response.type === "heartbeat") {
|
|
1298
|
+
clearTimeout(timeoutId);
|
|
1299
|
+
timeoutId = armSilenceTimeout();
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1302
|
+
if (response.type === "success") {
|
|
1303
|
+
settle({ written: response.segments, timedOut: false }, false);
|
|
1304
|
+
} else {
|
|
1305
|
+
settle({ written: null, timedOut: false }, true);
|
|
1306
|
+
}
|
|
1307
|
+
};
|
|
1308
|
+
const onError = () => {
|
|
1309
|
+
settle({ written: null, timedOut: false }, true);
|
|
1310
|
+
};
|
|
1311
|
+
const onExit = () => {
|
|
1312
|
+
settle({ written: null, timedOut: false }, true);
|
|
1313
|
+
};
|
|
1314
|
+
function settle(outcome, discard) {
|
|
1315
|
+
if (settled) {
|
|
1316
|
+
return;
|
|
1317
|
+
}
|
|
1318
|
+
settled = true;
|
|
1319
|
+
clearTimeout(timeoutId);
|
|
1320
|
+
worker.off("message", onMessage);
|
|
1321
|
+
worker.off("error", onError);
|
|
1322
|
+
worker.off("exit", onExit);
|
|
1323
|
+
if (discard) {
|
|
1324
|
+
discardWorker(worker);
|
|
1325
|
+
}
|
|
1326
|
+
resolve(outcome);
|
|
1327
|
+
}
|
|
1328
|
+
worker.on("message", onMessage);
|
|
1329
|
+
worker.on("error", onError);
|
|
1330
|
+
worker.on("exit", onExit);
|
|
1331
|
+
try {
|
|
1332
|
+
worker.postMessage(request);
|
|
1333
|
+
} catch {
|
|
1334
|
+
settle({ written: null, timedOut: false }, true);
|
|
1335
|
+
}
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1338
|
+
function delay(ms) {
|
|
1339
|
+
return new Promise((resolve) => {
|
|
1340
|
+
unrefTimer(setTimeout(resolve, ms));
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
function checkpointWorkerIsUsable() {
|
|
1344
|
+
const runtime = detectRuntime();
|
|
1345
|
+
return runtime.supportsWorkerThreads && runtime.supportsFileSystem && workerUsable;
|
|
1346
|
+
}
|
|
1347
|
+
async function runCheckpointOnWorker(request) {
|
|
1348
|
+
const runtime = detectRuntime();
|
|
1349
|
+
if (!runtime.supportsWorkerThreads || !runtime.supportsFileSystem || !workerUsable) {
|
|
1350
|
+
return null;
|
|
1351
|
+
}
|
|
1352
|
+
if (workerBusy) {
|
|
1353
|
+
return null;
|
|
1354
|
+
}
|
|
1355
|
+
workerBusy = true;
|
|
1356
|
+
cancelIdleRetirement();
|
|
1357
|
+
try {
|
|
1358
|
+
if (pooledWorker === null) {
|
|
1359
|
+
pooledWorker = await spawnWorker();
|
|
1360
|
+
}
|
|
1361
|
+
const worker = pooledWorker;
|
|
1362
|
+
if (worker === null) {
|
|
1363
|
+
workerUsable = false;
|
|
1364
|
+
return null;
|
|
1365
|
+
}
|
|
1366
|
+
const outcome = await runWorker(worker, request);
|
|
1367
|
+
if (outcome.written !== null) {
|
|
1368
|
+
return outcome.written;
|
|
1369
|
+
}
|
|
1370
|
+
if (outcome.timedOut) {
|
|
1371
|
+
await delay(CHECKPOINT_TIMEOUT_RECOVERY_BACKOFF_MS);
|
|
1372
|
+
}
|
|
1373
|
+
return null;
|
|
1374
|
+
} finally {
|
|
1375
|
+
workerBusy = false;
|
|
1376
|
+
retireWorkerOnceIdle();
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
function cancelIdleRetirement() {
|
|
1380
|
+
if (idleTimer !== null) {
|
|
1381
|
+
clearTimeout(idleTimer);
|
|
1382
|
+
idleTimer = null;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
function retireWorkerOnceIdle() {
|
|
1386
|
+
cancelIdleRetirement();
|
|
1387
|
+
idleTimer = unrefTimer(
|
|
1388
|
+
setTimeout(() => {
|
|
1389
|
+
idleTimer = null;
|
|
1390
|
+
if (!workerBusy) terminateCheckpointWorker();
|
|
1391
|
+
}, workerIdleMs)
|
|
1392
|
+
);
|
|
1393
|
+
}
|
|
1394
|
+
function terminateCheckpointWorker() {
|
|
1395
|
+
cancelIdleRetirement();
|
|
1396
|
+
const worker = pooledWorker;
|
|
1397
|
+
if (worker === null) {
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
pooledWorker = null;
|
|
1401
|
+
try {
|
|
1402
|
+
void worker.terminate();
|
|
1403
|
+
} catch {
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
// src/persistence/durability/checkpoint-write.ts
|
|
1408
|
+
function workerCanWriteEverySegment(input) {
|
|
1409
|
+
if (!input.canOffload || input.wholePartitions.length > 0) return false;
|
|
1410
|
+
const analysesTextInProcessOnly = input.metadata.tokenizer !== void 0 || input.metadata.stopWords !== void 0;
|
|
1411
|
+
if (!analysesTextInProcessOnly) return true;
|
|
1412
|
+
return input.targets.every(
|
|
1413
|
+
(target) => input.capturedPartitions.some((captured) => captured.partitionId === target.partitionId)
|
|
1414
|
+
);
|
|
1415
|
+
}
|
|
1416
|
+
async function writeIndexCheckpointSegments(input) {
|
|
1417
|
+
const { directory, metadata, targets, compactionThreshold, prior, wholePartitions, capturedPartitions } = input;
|
|
1418
|
+
const offloaded = workerCanWriteEverySegment(input) ? await runCheckpointOnWorker({
|
|
1419
|
+
root: directory.root,
|
|
1420
|
+
metadata,
|
|
1421
|
+
targets,
|
|
1422
|
+
compactionThreshold,
|
|
1423
|
+
prior,
|
|
1424
|
+
capturedPartitions: [...capturedPartitions]
|
|
1425
|
+
}) : null;
|
|
1426
|
+
if (offloaded !== null) {
|
|
1427
|
+
return offloaded;
|
|
1428
|
+
}
|
|
1429
|
+
return writeCheckpointSegments({
|
|
1430
|
+
store: directoryCheckpointStore(directory),
|
|
1431
|
+
directory,
|
|
1432
|
+
metadata,
|
|
1433
|
+
targets,
|
|
1434
|
+
compactionThreshold,
|
|
1435
|
+
prior,
|
|
1436
|
+
wholePartitions,
|
|
1437
|
+
capturedPartitions
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
// src/persistence/durability/segment/vector-adoption.ts
|
|
1442
|
+
function recordWrittenVectors(vectorIndexes, written) {
|
|
1443
|
+
for (const field of written) {
|
|
1444
|
+
vectorIndexes.get(field.fieldPath)?.recordCheckpoint(field.plan, field.files);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
async function adoptVectorLayouts(store, vectorIndexes, layouts) {
|
|
1448
|
+
const pathOf = store.pathOf;
|
|
1449
|
+
if (pathOf === void 0) return;
|
|
1450
|
+
for (const layout of layouts) {
|
|
1451
|
+
const vecIndex = vectorIndexes.get(layout.fieldPath);
|
|
1452
|
+
if (vecIndex === void 0 || vecIndex.storage !== "disk") continue;
|
|
1453
|
+
const path = await pathOf.call(store, layout.key);
|
|
1454
|
+
await vecIndex.adoptDiskLayout({
|
|
1455
|
+
path,
|
|
1456
|
+
docIds: layout.docIds,
|
|
1457
|
+
dead: layout.dead,
|
|
1458
|
+
vectorsOffset: layout.vectorsOffset
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
async function garbageThatNoFieldStillReads(store, vectorIndexes, keys) {
|
|
1463
|
+
const pathOf = store.pathOf;
|
|
1464
|
+
if (pathOf === void 0) return [...keys];
|
|
1465
|
+
const read = /* @__PURE__ */ new Set();
|
|
1466
|
+
for (const vectorIndex of vectorIndexes.values()) {
|
|
1467
|
+
for (const path of vectorIndex.vectorFilesInUse()) read.add(path);
|
|
1468
|
+
}
|
|
1469
|
+
if (read.size === 0) return [...keys];
|
|
1470
|
+
const unread = [];
|
|
1471
|
+
for (const key of keys) {
|
|
1472
|
+
if (!read.has(await pathOf.call(store, key))) unread.push(key);
|
|
1473
|
+
}
|
|
1474
|
+
return unread;
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
// src/persistence/durability/write-stall.ts
|
|
1478
|
+
function checkpointIsDue(indexState, mutationThreshold, stallAboveBytes = WRITES_STALL_ABOVE_UNCHECKPOINTED_BYTES) {
|
|
1479
|
+
return indexState.mutationsSinceCheckpoint >= mutationThreshold || indexState.documentBytesSinceCheckpoint >= stallAboveBytes;
|
|
1480
|
+
}
|
|
1481
|
+
async function stallWhileCheckpointsFallBehind(indexState, stallAboveBytes = WRITES_STALL_ABOVE_UNCHECKPOINTED_BYTES) {
|
|
1482
|
+
while (indexState.documentBytesSinceCheckpoint >= stallAboveBytes && indexState.checkpointInFlight !== null && !indexState.unloading) {
|
|
1483
|
+
await new Promise((resume) => {
|
|
1484
|
+
indexState.stalledWrites.push(resume);
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
function noteCheckpointCommitted(indexState, recordsBeyondTheCheckpoint) {
|
|
1489
|
+
const counted = indexState.mutationsSinceCheckpoint;
|
|
1490
|
+
const shareLeft = counted <= 0 ? 0 : Math.min(1, recordsBeyondTheCheckpoint / counted);
|
|
1491
|
+
indexState.documentBytesSinceCheckpoint = Math.round(indexState.documentBytesSinceCheckpoint * shareLeft);
|
|
1492
|
+
indexState.mutationsSinceCheckpoint = recordsBeyondTheCheckpoint;
|
|
1493
|
+
releaseStalledWrites(indexState);
|
|
1494
|
+
}
|
|
1495
|
+
function releaseStalledWrites(indexState) {
|
|
1496
|
+
const waiting = indexState.stalledWrites.splice(0);
|
|
1497
|
+
for (const resume of waiting) resume();
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
// src/persistence/durability/checkpoint-run.ts
|
|
1501
|
+
async function bothOrTheFirstFailure(first, second) {
|
|
1502
|
+
const [firstSettled, secondSettled] = await Promise.allSettled([first, second]);
|
|
1503
|
+
if (firstSettled.status === "rejected") throw firstSettled.reason;
|
|
1504
|
+
if (secondSettled.status === "rejected") throw secondSettled.reason;
|
|
1505
|
+
return [firstSettled.value, secondSettled.value];
|
|
1506
|
+
}
|
|
1507
|
+
async function runDurableCheckpoint(input) {
|
|
1508
|
+
try {
|
|
1509
|
+
let recordsLeftInLog = await writeOneBoundedCheckpoint(input);
|
|
1510
|
+
while (recordsLeftInLog > 0 && input.mayContinue()) {
|
|
1511
|
+
recordsLeftInLog = await writeOneBoundedCheckpoint(input);
|
|
1512
|
+
}
|
|
1513
|
+
} catch (err) {
|
|
1514
|
+
if (!isSystemError(err)) throw err;
|
|
1515
|
+
const error = new NarsilError(
|
|
1516
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1517
|
+
`The engine cannot write the checkpoint of index "${input.indexName}" to the durability directory "${input.directory.root}"`,
|
|
1518
|
+
{
|
|
1519
|
+
indexName: input.indexName,
|
|
1520
|
+
directory: input.directory.root,
|
|
1521
|
+
cause: err.message
|
|
1522
|
+
}
|
|
1523
|
+
);
|
|
1524
|
+
input.markFatal(error);
|
|
1525
|
+
throw error;
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
function isSystemError(err) {
|
|
1529
|
+
return err instanceof Error && !(err instanceof NarsilError) && typeof err.code === "string";
|
|
1530
|
+
}
|
|
1531
|
+
async function writeOneBoundedCheckpoint(input) {
|
|
1532
|
+
const manager = input.hooks.getManager(input.indexName);
|
|
1533
|
+
if (manager === void 0) {
|
|
1534
|
+
return 0;
|
|
1535
|
+
}
|
|
1536
|
+
const metadata = input.hooks.buildMetadata(input.indexName);
|
|
1537
|
+
if (metadata === void 0) {
|
|
1538
|
+
return 0;
|
|
1539
|
+
}
|
|
1540
|
+
const vectorIndexes = input.hooks.getVectorIndexes(input.indexName);
|
|
1541
|
+
const store = directoryCheckpointStore(input.directory);
|
|
1542
|
+
const priorManifest = await readCurrentManifest(store, input.indexName);
|
|
1543
|
+
const capture2 = await captureCheckpoint(
|
|
1544
|
+
input.indexState,
|
|
1545
|
+
manager,
|
|
1546
|
+
vectorIndexes,
|
|
1547
|
+
wholePartitionsWhereMostChanged(manager, {
|
|
1548
|
+
priorCheckpoint: priorManifest?.checkpoint ?? [],
|
|
1549
|
+
priorPartitionIds: (priorManifest?.partitions ?? []).map((partition) => partition.partitionId),
|
|
1550
|
+
everyPartition: input.fromMemory,
|
|
1551
|
+
aWorkerCanSerialise: input.canOffload && checkpointWorkerIsUsable()
|
|
1552
|
+
}),
|
|
1553
|
+
input.fromMemory ? void 0 : priorManifest?.checkpoint ?? [],
|
|
1554
|
+
priorManifest?.vectors ?? []
|
|
1555
|
+
);
|
|
1556
|
+
const { targets, documentCount } = capture2;
|
|
1557
|
+
await makeEveryAppliedMutationDurable(input.indexState, input.failPartition);
|
|
1558
|
+
if (aTargetFailed(input.indexState, targets)) return 0;
|
|
1559
|
+
const [liveVectors, segments] = await bothOrTheFirstFailure(
|
|
1560
|
+
writeCapturedVectors(store, input.indexName, capture2, priorManifest, input.fromMemory),
|
|
1561
|
+
writeIndexCheckpointSegments({
|
|
1562
|
+
directory: input.directory,
|
|
1563
|
+
metadata,
|
|
1564
|
+
targets,
|
|
1565
|
+
compactionThreshold: input.compactionThreshold,
|
|
1566
|
+
canOffload: input.canOffload,
|
|
1567
|
+
prior: priorManifest,
|
|
1568
|
+
wholePartitions: capture2.wholePartitions.contents,
|
|
1569
|
+
capturedPartitions: capture2.wholePartitions.captured
|
|
1570
|
+
})
|
|
1571
|
+
);
|
|
1572
|
+
await makeEveryAppliedMutationDurable(input.indexState, input.failPartition);
|
|
1573
|
+
if (aTargetFailed(input.indexState, targets)) return 0;
|
|
1574
|
+
const written = await commitCheckpointManifest(store, metadata, segments, liveVectors.vectors);
|
|
1575
|
+
recordWrittenVectors(vectorIndexes, liveVectors.written);
|
|
1576
|
+
await adoptVectorLayouts(store, vectorIndexes, liveVectors.layouts);
|
|
1577
|
+
if (!input.holdsCheckpointFiles()) {
|
|
1578
|
+
await removeCheckpointGarbage(store, await garbageThatNoFieldStillReads(store, vectorIndexes, written.garbage));
|
|
1579
|
+
}
|
|
1580
|
+
const checkpointDocumentCount = documentCount;
|
|
1581
|
+
await input.queueMetadataWrite(input.indexName, async () => {
|
|
1582
|
+
const checkpointMetadata = input.hooks.buildMetadata(input.indexName, checkpointDocumentCount);
|
|
1583
|
+
if (checkpointMetadata === void 0) {
|
|
1584
|
+
return;
|
|
1585
|
+
}
|
|
1586
|
+
const bytes = await writeMetadataEnvelope(checkpointMetadata, { checksum: true });
|
|
1587
|
+
await input.directory.atomicWrite(`${input.indexName}/meta`, bytes);
|
|
1588
|
+
input.hooks.recordCheckpoint?.(input.indexName, checkpointDocumentCount, manager.partitionCount);
|
|
1589
|
+
});
|
|
1590
|
+
await truncateCoveredSegments(input.directory, input.indexName, targets);
|
|
1591
|
+
await reclaimWalBeyondCount(
|
|
1592
|
+
input.directory,
|
|
1593
|
+
input.indexName,
|
|
1594
|
+
manager.partitionCount,
|
|
1595
|
+
input.indexState.partitions,
|
|
1596
|
+
input.markFatal
|
|
1597
|
+
);
|
|
1598
|
+
const beyondTheCheckpoint = recordsBeyond(input.indexState, targets);
|
|
1599
|
+
noteCheckpointCommitted(input.indexState, beyondTheCheckpoint);
|
|
1600
|
+
const anotherIsDue = input.mutationThreshold > 0 && beyondTheCheckpoint >= input.mutationThreshold;
|
|
1601
|
+
return anotherIsDue ? beyondTheCheckpoint : capture2.recordsLeftInLog;
|
|
1602
|
+
}
|
|
1603
|
+
function recordsBeyond(indexState, targets) {
|
|
1604
|
+
let records = 0;
|
|
1605
|
+
for (const [partitionId, partition] of indexState.partitions) {
|
|
1606
|
+
if (partition.failed !== null) continue;
|
|
1607
|
+
records += Math.max(0, partition.appliedSeqNo - snapshotCheckpointFor(targets, partitionId));
|
|
1608
|
+
}
|
|
1609
|
+
return records;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
// src/persistence/durability/checkpoint-turn.ts
|
|
1613
|
+
function createIndexLoads(vectorIndexesOf2) {
|
|
1614
|
+
const loads = /* @__PURE__ */ new Map();
|
|
1615
|
+
const startGraphRepairs = (indexName) => {
|
|
1616
|
+
for (const vectorIndex of vectorIndexesOf2(indexName).values()) vectorIndex.startShuffledRebuildWhenDue();
|
|
1617
|
+
};
|
|
1618
|
+
return {
|
|
1619
|
+
track(indexName, load, startsGraphRepairs = true) {
|
|
1620
|
+
const loaded = startsGraphRepairs ? load.then(() => startGraphRepairs(indexName)) : load;
|
|
1621
|
+
loads.set(
|
|
1622
|
+
indexName,
|
|
1623
|
+
loaded.catch(() => void 0)
|
|
1624
|
+
);
|
|
1625
|
+
return loaded;
|
|
1626
|
+
},
|
|
1627
|
+
settled: (indexName) => loads.get(indexName) ?? Promise.resolve(),
|
|
1628
|
+
startGraphRepairs
|
|
1629
|
+
};
|
|
1630
|
+
}
|
|
1631
|
+
async function checkpointInTurn(turns, turn) {
|
|
1632
|
+
if (turn.joinsInFlight && turns.checkpointInFlight !== null) return turns.checkpointInFlight;
|
|
1633
|
+
while (turns.checkpointInFlight !== null) await turns.checkpointInFlight.catch(() => void 0);
|
|
1634
|
+
if (!turn.mayStart()) return;
|
|
1635
|
+
const run = turn.perform().finally(() => {
|
|
1636
|
+
turns.checkpointInFlight = null;
|
|
1637
|
+
turn.afterSettling();
|
|
1638
|
+
});
|
|
1639
|
+
turns.checkpointInFlight = run;
|
|
1640
|
+
return run;
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
// src/persistence/durability/directory-watch.ts
|
|
1644
|
+
function unrefInterval(tick, intervalMs) {
|
|
1645
|
+
const timer = setInterval(tick, intervalMs);
|
|
1646
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
1647
|
+
return timer;
|
|
1648
|
+
}
|
|
1649
|
+
function createDirectoryWatch(directory, isStopped, onLost) {
|
|
1650
|
+
let expected = null;
|
|
1651
|
+
let timer = null;
|
|
1652
|
+
let lock = null;
|
|
1653
|
+
function cannotCheck(err) {
|
|
1654
|
+
return new NarsilError(
|
|
1655
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1656
|
+
`The engine cannot check the durability directory "${directory.root}", so it cannot confirm that a write is durable`,
|
|
1657
|
+
{ directory: directory.root, cause: err instanceof Error ? err.message : String(err) }
|
|
1658
|
+
);
|
|
1659
|
+
}
|
|
1660
|
+
async function currentIdentity() {
|
|
1661
|
+
try {
|
|
1662
|
+
return await directory.identity?.() ?? null;
|
|
1663
|
+
} catch (err) {
|
|
1664
|
+
return cannotCheck(err);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
async function lockTakenOver() {
|
|
1668
|
+
if (lock === null) return null;
|
|
1669
|
+
try {
|
|
1670
|
+
if (await lock.holds()) return null;
|
|
1671
|
+
} catch (err) {
|
|
1672
|
+
return cannotCheck(err);
|
|
1673
|
+
}
|
|
1674
|
+
return new NarsilError(
|
|
1675
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1676
|
+
`The lock file of the durability directory "${directory.root}" records another engine, which may be writing the same log`,
|
|
1677
|
+
{ directory: directory.root }
|
|
1678
|
+
);
|
|
1679
|
+
}
|
|
1680
|
+
async function verify() {
|
|
1681
|
+
if (directory.identity === void 0 || isStopped()) return true;
|
|
1682
|
+
const current = await currentIdentity();
|
|
1683
|
+
if (current instanceof Error) {
|
|
1684
|
+
onLost(current);
|
|
1685
|
+
return false;
|
|
1686
|
+
}
|
|
1687
|
+
if (expected === null) {
|
|
1688
|
+
expected = current;
|
|
1689
|
+
} else if (current !== expected) {
|
|
1690
|
+
onLost(
|
|
1691
|
+
new NarsilError(
|
|
1692
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1693
|
+
`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`,
|
|
1694
|
+
{ directory: directory.root }
|
|
1695
|
+
)
|
|
1696
|
+
);
|
|
1697
|
+
return false;
|
|
1698
|
+
}
|
|
1699
|
+
const takenOver = await lockTakenOver();
|
|
1700
|
+
if (takenOver === null) return true;
|
|
1701
|
+
onLost(takenOver);
|
|
1702
|
+
return false;
|
|
1703
|
+
}
|
|
1704
|
+
function stop() {
|
|
1705
|
+
if (timer !== null) {
|
|
1706
|
+
clearInterval(timer);
|
|
1707
|
+
timer = null;
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
return {
|
|
1711
|
+
async claim() {
|
|
1712
|
+
if (lock === null && directory.lock !== void 0) lock = await directory.lock();
|
|
1713
|
+
},
|
|
1714
|
+
async release() {
|
|
1715
|
+
const held = lock;
|
|
1716
|
+
lock = null;
|
|
1717
|
+
await held?.release();
|
|
1718
|
+
},
|
|
1719
|
+
verify,
|
|
1720
|
+
async verifyOnce() {
|
|
1721
|
+
if (expected === null && directory.identity !== void 0) await verify();
|
|
1722
|
+
},
|
|
1723
|
+
start() {
|
|
1724
|
+
if (directory.identity === void 0 || timer !== null || isStopped()) return;
|
|
1725
|
+
timer = unrefInterval(() => void verify(), DIRECTORY_IDENTITY_CHECK_INTERVAL_MS);
|
|
1726
|
+
},
|
|
1727
|
+
stop
|
|
1728
|
+
};
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
// src/persistence/durability/held-checkpoints.ts
|
|
1732
|
+
function failedPartitionIds(indexState) {
|
|
1733
|
+
const failed = [];
|
|
1734
|
+
for (const [partitionId, partition] of indexState.partitions) if (partition.failed !== null) failed.push(partitionId);
|
|
1735
|
+
return failed;
|
|
1736
|
+
}
|
|
1737
|
+
function createHeldCheckpoints(hooks) {
|
|
1738
|
+
const reported = /* @__PURE__ */ new WeakSet();
|
|
1739
|
+
function heldReport(indexName, indexState) {
|
|
1740
|
+
const partitionIds = failedPartitionIds(indexState);
|
|
1741
|
+
if (partitionIds.length === 0 || hooks.getVectorIndexes(indexName).size === 0) return null;
|
|
1742
|
+
return {
|
|
1743
|
+
indexName,
|
|
1744
|
+
partitionIds,
|
|
1745
|
+
recordsSinceCheckpoint: indexState.mutationsSinceCheckpoint,
|
|
1746
|
+
documentBytesSinceCheckpoint: indexState.documentBytesSinceCheckpoint
|
|
1747
|
+
};
|
|
1748
|
+
}
|
|
1749
|
+
return {
|
|
1750
|
+
refuse(indexName, indexState) {
|
|
1751
|
+
const partitionIds = indexState === void 0 ? [] : failedPartitionIds(indexState);
|
|
1752
|
+
if (partitionIds.length === 0) return;
|
|
1753
|
+
throw new NarsilError(
|
|
1754
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1755
|
+
`Index "${indexName}" cannot take a checkpoint that covers every acknowledged write, because partitions ${partitionIds.join(", ")} failed and have not recovered`,
|
|
1756
|
+
{ indexName, partitionIds }
|
|
1757
|
+
);
|
|
1758
|
+
},
|
|
1759
|
+
holds(indexName, indexState) {
|
|
1760
|
+
const held = heldReport(indexName, indexState);
|
|
1761
|
+
if (held === null) return false;
|
|
1762
|
+
const unreported = held.partitionIds.some((partitionId) => {
|
|
1763
|
+
const partition = indexState.partitions.get(partitionId);
|
|
1764
|
+
return partition !== void 0 && !reported.has(partition);
|
|
1765
|
+
});
|
|
1766
|
+
if (unreported) {
|
|
1767
|
+
for (const partitionId of held.partitionIds) {
|
|
1768
|
+
const partition = indexState.partitions.get(partitionId);
|
|
1769
|
+
if (partition !== void 0) reported.add(partition);
|
|
1770
|
+
}
|
|
1771
|
+
hooks.onCheckpointHeld?.(held);
|
|
1772
|
+
}
|
|
1773
|
+
return true;
|
|
1774
|
+
},
|
|
1775
|
+
report(indexes) {
|
|
1776
|
+
const held = [];
|
|
1777
|
+
for (const [indexName, indexState] of indexes) {
|
|
1778
|
+
const entry = heldReport(indexName, indexState);
|
|
1779
|
+
if (entry !== null) held.push(entry);
|
|
1780
|
+
}
|
|
1781
|
+
return held;
|
|
1782
|
+
}
|
|
1783
|
+
};
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
// src/persistence/durability/manager-state.ts
|
|
1787
|
+
function closePartitionWriter(partition) {
|
|
1788
|
+
return partition.failed === null ? partition.walWriter.close() : partition.walWriter.abandon();
|
|
1789
|
+
}
|
|
1790
|
+
async function drainIndexStateForUnload(indexState, pendingMetadataWrite, closeWalWriter) {
|
|
1791
|
+
indexState.unloading = true;
|
|
1792
|
+
try {
|
|
1793
|
+
if (indexState.checkpointInFlight !== null) await indexState.checkpointInFlight;
|
|
1794
|
+
await pendingMetadataWrite;
|
|
1795
|
+
for (const partition of indexState.partitions.values()) {
|
|
1796
|
+
await closeWalWriter(closePartitionWriter(partition));
|
|
1797
|
+
}
|
|
1798
|
+
} catch (error) {
|
|
1799
|
+
indexState.unloading = false;
|
|
1800
|
+
throw error;
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
// src/persistence/durability/mutation-batch.ts
|
|
1805
|
+
function toError3(err) {
|
|
1806
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
1807
|
+
}
|
|
1808
|
+
function requireAscendingSeqNo(record, seqNo, previousSeqNo) {
|
|
1809
|
+
if (seqNo > previousSeqNo) return;
|
|
1810
|
+
throw new NarsilError(
|
|
1811
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
1812
|
+
`Log record ${seqNo} for partition ${record.partitionId} of index "${record.indexName}" does not follow record ${previousSeqNo}`,
|
|
1813
|
+
{ indexName: record.indexName, partitionId: record.partitionId, seqNo, previousSeqNo }
|
|
1814
|
+
);
|
|
1815
|
+
}
|
|
1816
|
+
function failUnlessAlreadyFailed(outcomes, count, error) {
|
|
1817
|
+
for (let i = 0; i < count; i++) {
|
|
1818
|
+
const outcome = outcomes[i];
|
|
1819
|
+
if (outcome === void 0 || outcome.ok) {
|
|
1820
|
+
outcomes[i] = { ok: false, error };
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
function failPartition(partition, address, deps, err) {
|
|
1825
|
+
const error = toError3(err);
|
|
1826
|
+
if (partition.failed !== null) return partition.failed;
|
|
1827
|
+
partition.failed = error;
|
|
1828
|
+
if (!deps.failsPartitionAlone(address.indexName, address.partitionId, error)) deps.markFatal(error);
|
|
1829
|
+
return error;
|
|
1830
|
+
}
|
|
1831
|
+
async function commitEveryPartition(indexes, deps) {
|
|
1832
|
+
for (const [indexName, indexState] of indexes) {
|
|
1833
|
+
for (const [partitionId, partition] of indexState.partitions) {
|
|
1834
|
+
if (partition.failed !== null || deps.fatalError() !== null) continue;
|
|
1835
|
+
try {
|
|
1836
|
+
await partition.walWriter.commit();
|
|
1837
|
+
} catch (err) {
|
|
1838
|
+
failPartition(partition, { indexName, partitionId }, deps, err);
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
function partitionFailureFor(indexName, deps) {
|
|
1844
|
+
return (partitionId, partition, error) => failPartition(partition, { indexName, partitionId }, deps, error);
|
|
1845
|
+
}
|
|
1846
|
+
async function recordBatchesByPartition(records, partitionOf, deps) {
|
|
1847
|
+
const positionsByPartition = /* @__PURE__ */ new Map();
|
|
1848
|
+
for (let i = 0; i < records.length; i++) {
|
|
1849
|
+
const partition = partitionOf(records[i]);
|
|
1850
|
+
const positions = positionsByPartition.get(partition);
|
|
1851
|
+
if (positions === void 0) {
|
|
1852
|
+
positionsByPartition.set(partition, [i]);
|
|
1853
|
+
} else {
|
|
1854
|
+
positions.push(i);
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
const outcomes = new Array(records.length);
|
|
1858
|
+
await Promise.all(
|
|
1859
|
+
[...positionsByPartition].map(async ([partition, positions]) => {
|
|
1860
|
+
const partitionOutcomes = await recordPartitionBatch(
|
|
1861
|
+
partition,
|
|
1862
|
+
positions.map((position) => records[position]),
|
|
1863
|
+
deps
|
|
1864
|
+
);
|
|
1865
|
+
for (let i = 0; i < positions.length; i++) {
|
|
1866
|
+
outcomes[positions[i]] = partitionOutcomes[i];
|
|
1867
|
+
}
|
|
1868
|
+
})
|
|
1869
|
+
);
|
|
1870
|
+
return outcomes;
|
|
1871
|
+
}
|
|
1872
|
+
async function recordPartitionBatch(partition, records, deps) {
|
|
1873
|
+
const outcomes = new Array(records.length);
|
|
1874
|
+
let appendedCount = 0;
|
|
1875
|
+
let commitsHere = false;
|
|
1876
|
+
const appended = partition.appendChain.then(async () => {
|
|
1877
|
+
const fatal = deps.fatalError();
|
|
1878
|
+
if (fatal !== null) {
|
|
1879
|
+
throw fatal;
|
|
1880
|
+
}
|
|
1881
|
+
if (partition.failed !== null) {
|
|
1882
|
+
throw partition.failed;
|
|
1883
|
+
}
|
|
1884
|
+
const applied = [];
|
|
1885
|
+
for (let i = 0; i < records.length; i++) {
|
|
1886
|
+
try {
|
|
1887
|
+
await records[i].apply();
|
|
1888
|
+
applied.push(i);
|
|
1889
|
+
} catch (err) {
|
|
1890
|
+
outcomes[i] = { ok: false, error: err };
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
if (applied.length === 0) {
|
|
1894
|
+
return;
|
|
1895
|
+
}
|
|
1896
|
+
let lastSeqNo = partition.appliedSeqNo;
|
|
1897
|
+
const entries = [];
|
|
1898
|
+
try {
|
|
1899
|
+
for (const i of applied) {
|
|
1900
|
+
let numbered;
|
|
1901
|
+
try {
|
|
1902
|
+
numbered = deps.numberEntry(records[i]);
|
|
1903
|
+
} catch (err) {
|
|
1904
|
+
outcomes[i] = { ok: false, error: err };
|
|
1905
|
+
continue;
|
|
1906
|
+
}
|
|
1907
|
+
if (numbered === null) {
|
|
1908
|
+
lastSeqNo = partition.seqOwner.next();
|
|
1909
|
+
entries.push(deps.buildEntry(records[i], lastSeqNo));
|
|
1910
|
+
} else {
|
|
1911
|
+
requireAscendingSeqNo(records[i], numbered.seqNo, lastSeqNo);
|
|
1912
|
+
partition.seqOwner.advanceTo(numbered.seqNo, numbered.primaryTerm);
|
|
1913
|
+
lastSeqNo = numbered.seqNo;
|
|
1914
|
+
entries.push(numbered);
|
|
1915
|
+
}
|
|
1916
|
+
outcomes[i] = { ok: true, seqNo: lastSeqNo };
|
|
1917
|
+
if (!deps.defersCommit(records[i])) commitsHere = true;
|
|
1918
|
+
}
|
|
1919
|
+
if (entries.length > 0) await partition.walWriter.appendAll(entries);
|
|
1920
|
+
} catch (err) {
|
|
1921
|
+
throw failPartition(partition, records[0], deps, err);
|
|
1922
|
+
}
|
|
1923
|
+
partition.appliedSeqNo = lastSeqNo;
|
|
1924
|
+
appendedCount = entries.length;
|
|
1925
|
+
});
|
|
1926
|
+
partition.appendChain = appended.catch(() => void 0);
|
|
1927
|
+
try {
|
|
1928
|
+
await appended;
|
|
1929
|
+
if (deps.syncEachBatch && appendedCount > 0 && commitsHere) {
|
|
1930
|
+
try {
|
|
1931
|
+
await partition.walWriter.commit();
|
|
1932
|
+
} catch (err) {
|
|
1933
|
+
throw failPartition(partition, records[0], deps, err);
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
} catch (err) {
|
|
1937
|
+
failUnlessAlreadyFailed(outcomes, records.length, toError3(err));
|
|
1938
|
+
}
|
|
1939
|
+
return outcomes;
|
|
1940
|
+
}
|
|
1941
|
+
async function commitAppended(partition, address, deps) {
|
|
1942
|
+
await partition.appendChain;
|
|
1943
|
+
const failed = partition.failed ?? deps.fatalError();
|
|
1944
|
+
if (failed !== null) throw failed;
|
|
1945
|
+
try {
|
|
1946
|
+
await partition.walWriter.commit();
|
|
1947
|
+
} catch (err) {
|
|
1948
|
+
throw failPartition(partition, address, deps, err);
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
// src/persistence/durability/partition-failure.ts
|
|
1953
|
+
function createPartitionFailureRouting() {
|
|
1954
|
+
let onFailed = null;
|
|
1955
|
+
return {
|
|
1956
|
+
isolate(handler) {
|
|
1957
|
+
onFailed = handler;
|
|
1958
|
+
},
|
|
1959
|
+
failsAlone(indexName, partitionId, error) {
|
|
1960
|
+
if (onFailed === null) return false;
|
|
1961
|
+
onFailed(indexName, partitionId, error);
|
|
1962
|
+
return true;
|
|
1963
|
+
}
|
|
1964
|
+
};
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
// src/persistence/durability/group-commit.ts
|
|
1968
|
+
function createGroupCommitCoordinator(sync) {
|
|
1969
|
+
let inFlight = null;
|
|
1970
|
+
let next = null;
|
|
1971
|
+
function makeBatch() {
|
|
1972
|
+
let resolve = () => void 0;
|
|
1973
|
+
let reject = () => void 0;
|
|
1974
|
+
const promise = new Promise((res, rej) => {
|
|
1975
|
+
resolve = res;
|
|
1976
|
+
reject = rej;
|
|
1977
|
+
});
|
|
1978
|
+
return { promise, resolve, reject, waiters: 0 };
|
|
1979
|
+
}
|
|
1980
|
+
function drain() {
|
|
1981
|
+
if (inFlight !== null || next === null) {
|
|
1982
|
+
return;
|
|
1983
|
+
}
|
|
1984
|
+
const batch = next;
|
|
1985
|
+
next = null;
|
|
1986
|
+
inFlight = sync().then(() => {
|
|
1987
|
+
batch.resolve();
|
|
1988
|
+
}).catch((err) => {
|
|
1989
|
+
batch.reject(err);
|
|
1990
|
+
}).finally(() => {
|
|
1991
|
+
inFlight = null;
|
|
1992
|
+
drain();
|
|
1993
|
+
});
|
|
1994
|
+
}
|
|
1995
|
+
return {
|
|
1996
|
+
commit() {
|
|
1997
|
+
if (next === null) {
|
|
1998
|
+
next = makeBatch();
|
|
1999
|
+
}
|
|
2000
|
+
next.waiters += 1;
|
|
2001
|
+
const promise = next.promise;
|
|
2002
|
+
drain();
|
|
2003
|
+
return promise;
|
|
2004
|
+
},
|
|
2005
|
+
get pendingCount() {
|
|
2006
|
+
return next === null ? 0 : next.waiters;
|
|
2007
|
+
}
|
|
2008
|
+
};
|
|
2009
|
+
}
|
|
2010
|
+
|
|
2011
|
+
// src/persistence/durability/marker-writer.ts
|
|
2012
|
+
function markerKey(indexName, partitionId) {
|
|
2013
|
+
return `${indexName}/wal/${partitionId}/commit`;
|
|
2014
|
+
}
|
|
2015
|
+
async function createMarkerWriter(directory, indexName, partitionId) {
|
|
2016
|
+
const key = markerKey(indexName, partitionId);
|
|
2017
|
+
const handle = await directory.markerHandle(key);
|
|
2018
|
+
const existing = await handle.read();
|
|
2019
|
+
const current = readCommitMarker(existing);
|
|
2020
|
+
const createdFresh = existing.length === 0;
|
|
2021
|
+
let lastSlot = current?.slotIndex ?? null;
|
|
2022
|
+
let writeSeq = current?.state.writeSeq ?? 0;
|
|
2023
|
+
return {
|
|
2024
|
+
created: createdFresh,
|
|
2025
|
+
existingHighestDurableSeqNo: current?.state.highestDurableSeqNo ?? 0,
|
|
2026
|
+
async commit(state) {
|
|
2027
|
+
const slotIndex = nextMarkerSlot(lastSlot);
|
|
2028
|
+
writeSeq += 1;
|
|
2029
|
+
const slot = encodeMarkerSlot({ ...state, writeSeq });
|
|
2030
|
+
await handle.writeSlot(markerSlotOffset(slotIndex), slot);
|
|
2031
|
+
lastSlot = slotIndex;
|
|
2032
|
+
},
|
|
2033
|
+
async close() {
|
|
2034
|
+
await handle.close();
|
|
2035
|
+
}
|
|
2036
|
+
};
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
// src/persistence/durability/wal-writer.ts
|
|
2040
|
+
var SEGMENT_TAIL_PATTERN = /^\d{16}$/;
|
|
2041
|
+
function segmentKey(indexName, partitionId, startSeqNo) {
|
|
2042
|
+
const padded2 = startSeqNo.toString().padStart(16, "0");
|
|
2043
|
+
return `${indexName}/wal/${partitionId}/${padded2}`;
|
|
2044
|
+
}
|
|
2045
|
+
function parseSegmentStartSeqNo2(key) {
|
|
2046
|
+
const tail = key.slice(key.lastIndexOf("/") + 1);
|
|
2047
|
+
if (!SEGMENT_TAIL_PATTERN.test(tail)) {
|
|
2048
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Malformed WAL segment key: "${key}"`, { key });
|
|
2049
|
+
}
|
|
2050
|
+
const value = Number.parseInt(tail, 10);
|
|
2051
|
+
if (!Number.isSafeInteger(value)) {
|
|
2052
|
+
throw new NarsilError(
|
|
2053
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
2054
|
+
`WAL segment start sequence number is out of range: "${key}"`,
|
|
2055
|
+
{
|
|
2056
|
+
key
|
|
2057
|
+
}
|
|
2058
|
+
);
|
|
2059
|
+
}
|
|
2060
|
+
return value;
|
|
2061
|
+
}
|
|
2062
|
+
function createWalWriter(directory, config) {
|
|
2063
|
+
const segmentMaxBytes = config.segmentMaxBytes ?? DEFAULT_SEGMENT_MAX_BYTES;
|
|
2064
|
+
const commitKey = `${config.indexName}/wal/${config.partitionId}/commit`;
|
|
2065
|
+
let handle = null;
|
|
2066
|
+
let activeKey = null;
|
|
2067
|
+
let activeStartSeqNo = 0;
|
|
2068
|
+
let activeBytes = 0;
|
|
2069
|
+
let highestAppendedSeqNo = 0;
|
|
2070
|
+
let highestDurableSeqNo = 0;
|
|
2071
|
+
let markerWriter = null;
|
|
2072
|
+
let durabilityLock = Promise.resolve();
|
|
2073
|
+
const coordinator = createGroupCommitCoordinator(() => withDurabilityLock(flushActiveSegment));
|
|
2074
|
+
function withDurabilityLock(fn) {
|
|
2075
|
+
const result = durabilityLock.then(fn);
|
|
2076
|
+
durabilityLock = result.then(
|
|
2077
|
+
() => void 0,
|
|
2078
|
+
() => void 0
|
|
2079
|
+
);
|
|
2080
|
+
return result;
|
|
2081
|
+
}
|
|
2082
|
+
async function getMarkerWriter() {
|
|
2083
|
+
if (markerWriter === null) {
|
|
2084
|
+
const writer = await createMarkerWriter(directory, config.indexName, config.partitionId);
|
|
2085
|
+
markerWriter = writer;
|
|
2086
|
+
if (writer.existingHighestDurableSeqNo > highestDurableSeqNo) {
|
|
2087
|
+
highestDurableSeqNo = writer.existingHighestDurableSeqNo;
|
|
2088
|
+
}
|
|
2089
|
+
if (writer.created) {
|
|
2090
|
+
await directory.syncDirectoryOf(commitKey);
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
return markerWriter;
|
|
2094
|
+
}
|
|
2095
|
+
async function flushActiveSegment() {
|
|
2096
|
+
if (handle === null) {
|
|
2097
|
+
return;
|
|
2098
|
+
}
|
|
2099
|
+
const writer = await getMarkerWriter();
|
|
2100
|
+
const durableByteLength = activeBytes;
|
|
2101
|
+
const activeSegmentSeqNo = activeStartSeqNo;
|
|
2102
|
+
const durableSeqNo = Math.max(highestAppendedSeqNo, highestDurableSeqNo);
|
|
2103
|
+
await handle.sync();
|
|
2104
|
+
await writer.commit({ activeSegmentSeqNo, durableByteLength, highestDurableSeqNo: durableSeqNo });
|
|
2105
|
+
if (durableSeqNo > highestDurableSeqNo) {
|
|
2106
|
+
highestDurableSeqNo = durableSeqNo;
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
async function openSegmentByKey(key) {
|
|
2110
|
+
const nextHandle = await directory.appendHandle(key);
|
|
2111
|
+
const existingSize = await nextHandle.size();
|
|
2112
|
+
let createdNew = false;
|
|
2113
|
+
if (existingSize === 0) {
|
|
2114
|
+
await nextHandle.append(writeSegmentHeader());
|
|
2115
|
+
activeBytes = SEGMENT_HEADER_SIZE;
|
|
2116
|
+
createdNew = true;
|
|
2117
|
+
} else {
|
|
2118
|
+
activeBytes = existingSize;
|
|
2119
|
+
}
|
|
2120
|
+
handle = nextHandle;
|
|
2121
|
+
activeKey = key;
|
|
2122
|
+
activeStartSeqNo = parseSegmentStartSeqNo2(key);
|
|
2123
|
+
if (createdNew) {
|
|
2124
|
+
await directory.syncDirectoryOf(key);
|
|
2125
|
+
}
|
|
2126
|
+
return createdNew;
|
|
2127
|
+
}
|
|
2128
|
+
async function findActiveSegmentKey() {
|
|
2129
|
+
const prefix = `${config.indexName}/wal/${config.partitionId}/`;
|
|
2130
|
+
const keys = (await directory.list(prefix)).filter((k) => SEGMENT_TAIL_PATTERN.test(k.slice(prefix.length))).sort(compareCodePoints);
|
|
2131
|
+
return keys.length > 0 ? keys[keys.length - 1] : null;
|
|
2132
|
+
}
|
|
2133
|
+
async function ensureSegment(seqNo) {
|
|
2134
|
+
if (handle !== null) {
|
|
2135
|
+
return;
|
|
2136
|
+
}
|
|
2137
|
+
const existing = await findActiveSegmentKey();
|
|
2138
|
+
const createdNew = await openSegmentByKey(existing ?? segmentKey(config.indexName, config.partitionId, seqNo));
|
|
2139
|
+
if (createdNew) {
|
|
2140
|
+
await withDurabilityLock(flushActiveSegment);
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
async function rollToNewSegment(startSeqNo) {
|
|
2144
|
+
await withDurabilityLock(async () => {
|
|
2145
|
+
if (handle !== null) {
|
|
2146
|
+
await flushActiveSegment();
|
|
2147
|
+
await handle.close();
|
|
2148
|
+
handle = null;
|
|
2149
|
+
}
|
|
2150
|
+
await openSegmentByKey(segmentKey(config.indexName, config.partitionId, startSeqNo));
|
|
2151
|
+
await flushActiveSegment();
|
|
2152
|
+
});
|
|
2153
|
+
}
|
|
2154
|
+
async function maybeRoll(nextSeqNo, appendedBytes) {
|
|
2155
|
+
if (handle === null || activeBytes <= SEGMENT_HEADER_SIZE) {
|
|
2156
|
+
return;
|
|
2157
|
+
}
|
|
2158
|
+
if (activeBytes >= segmentMaxBytes || activeBytes + appendedBytes > segmentMaxBytes) {
|
|
2159
|
+
await rollToNewSegment(nextSeqNo);
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
function joinFrames(frames) {
|
|
2163
|
+
if (frames.length === 1) {
|
|
2164
|
+
return frames[0];
|
|
2165
|
+
}
|
|
2166
|
+
let totalBytes = 0;
|
|
2167
|
+
for (const frame of frames) {
|
|
2168
|
+
totalBytes += frame.length;
|
|
2169
|
+
}
|
|
2170
|
+
const joined = new Uint8Array(totalBytes);
|
|
2171
|
+
let offset = 0;
|
|
2172
|
+
for (const frame of frames) {
|
|
2173
|
+
joined.set(frame, offset);
|
|
2174
|
+
offset += frame.length;
|
|
2175
|
+
}
|
|
2176
|
+
return joined;
|
|
2177
|
+
}
|
|
2178
|
+
function framesThatFit(frames, start) {
|
|
2179
|
+
const room = segmentMaxBytes - activeBytes;
|
|
2180
|
+
let end = start + 1;
|
|
2181
|
+
let bytes = frames[start].length;
|
|
2182
|
+
while (end < frames.length && bytes + frames[end].length <= room) {
|
|
2183
|
+
bytes += frames[end].length;
|
|
2184
|
+
end += 1;
|
|
2185
|
+
}
|
|
2186
|
+
return end;
|
|
2187
|
+
}
|
|
2188
|
+
async function appendGroup(frames, entries) {
|
|
2189
|
+
const activeHandle = handle;
|
|
2190
|
+
if (activeHandle === null) {
|
|
2191
|
+
throw new NarsilError(
|
|
2192
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2193
|
+
`WAL segment for "${config.indexName}" partition ${config.partitionId} is not open`,
|
|
2194
|
+
{ indexName: config.indexName, partitionId: config.partitionId }
|
|
2195
|
+
);
|
|
2196
|
+
}
|
|
2197
|
+
const joined = joinFrames([...frames]);
|
|
2198
|
+
if (activeBytes + joined.length > MAX_FILE_BYTES) {
|
|
2199
|
+
throw new NarsilError(
|
|
2200
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2201
|
+
`A log record of ${joined.length} bytes would carry WAL segment "${activeKey}" past the file size limit of ${MAX_FILE_BYTES} bytes`,
|
|
2202
|
+
{ indexName: config.indexName, partitionId: config.partitionId, bytes: joined.length }
|
|
2203
|
+
);
|
|
2204
|
+
}
|
|
2205
|
+
await activeHandle.append(joined);
|
|
2206
|
+
activeBytes += joined.length;
|
|
2207
|
+
const lastSeqNo = entries[entries.length - 1].seqNo;
|
|
2208
|
+
if (lastSeqNo > highestAppendedSeqNo) {
|
|
2209
|
+
highestAppendedSeqNo = lastSeqNo;
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
async function appendFrames(entries) {
|
|
2213
|
+
if (entries.length === 0) {
|
|
2214
|
+
return;
|
|
2215
|
+
}
|
|
2216
|
+
const frames = entries.map(frameRecord);
|
|
2217
|
+
await ensureSegment(entries[0].seqNo);
|
|
2218
|
+
let start = 0;
|
|
2219
|
+
while (start < frames.length) {
|
|
2220
|
+
await maybeRoll(entries[start].seqNo, frames[start].length);
|
|
2221
|
+
const end = framesThatFit(frames, start);
|
|
2222
|
+
await appendGroup(frames.slice(start, end), entries.slice(start, end));
|
|
2223
|
+
start = end;
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
return {
|
|
2227
|
+
async appendAll(entries) {
|
|
2228
|
+
await appendFrames(entries);
|
|
2229
|
+
},
|
|
2230
|
+
async appendDurable(entry) {
|
|
2231
|
+
await appendFrames([entry]);
|
|
2232
|
+
await coordinator.commit();
|
|
2233
|
+
},
|
|
2234
|
+
async commit() {
|
|
2235
|
+
await coordinator.commit();
|
|
2236
|
+
},
|
|
2237
|
+
rollToNewSegment,
|
|
2238
|
+
async close() {
|
|
2239
|
+
await withDurabilityLock(async () => {
|
|
2240
|
+
if (handle !== null) {
|
|
2241
|
+
await flushActiveSegment();
|
|
2242
|
+
await handle.close();
|
|
2243
|
+
handle = null;
|
|
2244
|
+
}
|
|
2245
|
+
if (markerWriter !== null) {
|
|
2246
|
+
await markerWriter.close();
|
|
2247
|
+
markerWriter = null;
|
|
2248
|
+
}
|
|
2249
|
+
activeKey = null;
|
|
2250
|
+
});
|
|
2251
|
+
},
|
|
2252
|
+
async abandon() {
|
|
2253
|
+
await withDurabilityLock(async () => {
|
|
2254
|
+
const closing = [handle?.close(), markerWriter?.close()];
|
|
2255
|
+
handle = null;
|
|
2256
|
+
markerWriter = null;
|
|
2257
|
+
activeKey = null;
|
|
2258
|
+
await Promise.allSettled(closing);
|
|
2259
|
+
});
|
|
2260
|
+
},
|
|
2261
|
+
get activeSegmentKey() {
|
|
2262
|
+
return activeKey;
|
|
2263
|
+
}
|
|
2264
|
+
};
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
// src/persistence/durability/partition-registry.ts
|
|
2268
|
+
function createPartitionRegistry(directory, segmentMaxBytes) {
|
|
2269
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
2270
|
+
function indexState(indexName) {
|
|
2271
|
+
let state = indexes.get(indexName);
|
|
2272
|
+
if (state === void 0) {
|
|
2273
|
+
state = {
|
|
2274
|
+
partitions: /* @__PURE__ */ new Map(),
|
|
2275
|
+
mutationsSinceCheckpoint: 0,
|
|
2276
|
+
documentBytesSinceCheckpoint: 0,
|
|
2277
|
+
checkpointInFlight: null,
|
|
2278
|
+
unloading: false,
|
|
2279
|
+
stalledWrites: []
|
|
2280
|
+
};
|
|
2281
|
+
indexes.set(indexName, state);
|
|
2282
|
+
}
|
|
2283
|
+
return state;
|
|
2284
|
+
}
|
|
2285
|
+
function freshPartition(indexName, partitionId, position) {
|
|
2286
|
+
return {
|
|
2287
|
+
walWriter: createWalWriter(directory, { indexName, partitionId, segmentMaxBytes }),
|
|
2288
|
+
seqOwner: createSeqOwner(position.seqNo, position.primaryTerm),
|
|
2289
|
+
appendChain: Promise.resolve(),
|
|
2290
|
+
appliedSeqNo: position.seqNo,
|
|
2291
|
+
failed: null
|
|
2292
|
+
};
|
|
2293
|
+
}
|
|
2294
|
+
return {
|
|
2295
|
+
directory,
|
|
2296
|
+
indexes,
|
|
2297
|
+
indexState,
|
|
2298
|
+
freshPartition,
|
|
2299
|
+
partition(indexName, partitionId, startSeqNo, primaryTerm = SINGLE_NODE_PRIMARY_TERM) {
|
|
2300
|
+
const state = indexState(indexName);
|
|
2301
|
+
let partition = state.partitions.get(partitionId);
|
|
2302
|
+
if (partition === void 0) {
|
|
2303
|
+
partition = freshPartition(indexName, partitionId, { seqNo: startSeqNo, primaryTerm });
|
|
2304
|
+
state.partitions.set(partitionId, partition);
|
|
2305
|
+
}
|
|
2306
|
+
return partition;
|
|
2307
|
+
}
|
|
2308
|
+
};
|
|
2309
|
+
}
|
|
2310
|
+
var SNAPSHOT_BUNDLE_VERSION = 2;
|
|
2311
|
+
function invalid(message) {
|
|
2312
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Snapshot bundle ${message}`);
|
|
2313
|
+
}
|
|
2314
|
+
function vectorIndexesOf(raw) {
|
|
2315
|
+
if (raw === void 0 || raw === null) return {};
|
|
2316
|
+
if (typeof raw !== "object" || Array.isArray(raw)) invalid("vectorIndexes must be a map");
|
|
2317
|
+
const result = {};
|
|
2318
|
+
for (const [fieldPath, parts] of Object.entries(raw)) {
|
|
2319
|
+
result[fieldPath] = decodeVectorIndexParts(parts);
|
|
2320
|
+
}
|
|
2321
|
+
return result;
|
|
2322
|
+
}
|
|
2323
|
+
function checkpointOf(raw) {
|
|
2324
|
+
if (!Array.isArray(raw)) return [];
|
|
2325
|
+
const result = [];
|
|
2326
|
+
for (const entry of raw) {
|
|
2327
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
2328
|
+
const { partitionId, lastSeqNo, primaryTerm } = entry;
|
|
2329
|
+
if (typeof partitionId !== "number" || typeof lastSeqNo !== "number" || typeof primaryTerm !== "number") continue;
|
|
2330
|
+
result.push({ partitionId, lastSeqNo, primaryTerm });
|
|
2331
|
+
}
|
|
2332
|
+
return result;
|
|
2333
|
+
}
|
|
2334
|
+
async function decodeSnapshotBundle(data) {
|
|
2335
|
+
const { payloadBytes } = await unpackEnvelopeBytes(data);
|
|
2336
|
+
const raw = decode(payloadBytes);
|
|
2337
|
+
if (raw.version !== SNAPSHOT_BUNDLE_VERSION) {
|
|
2338
|
+
throw new NarsilError(
|
|
2339
|
+
ErrorCodes.ENVELOPE_VERSION_MISMATCH,
|
|
2340
|
+
`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`,
|
|
2341
|
+
{ version: raw.version }
|
|
2342
|
+
);
|
|
2343
|
+
}
|
|
2344
|
+
if (raw.schema === null || typeof raw.schema !== "object" || Array.isArray(raw.schema)) invalid("missing schema");
|
|
2345
|
+
requireValidFieldTypes(raw.schema);
|
|
2346
|
+
if (typeof raw.language !== "string") invalid("missing language");
|
|
2347
|
+
if (!Array.isArray(raw.partitions) || !raw.partitions.every((part) => part instanceof Uint8Array)) {
|
|
2348
|
+
invalid("missing partitions");
|
|
2349
|
+
}
|
|
2350
|
+
return {
|
|
2351
|
+
schema: raw.schema,
|
|
2352
|
+
language: raw.language,
|
|
2353
|
+
partitions: raw.partitions,
|
|
2354
|
+
vectorIndexes: vectorIndexesOf(raw.vectorIndexes),
|
|
2355
|
+
checkpoint: checkpointOf(raw.checkpoint)
|
|
2356
|
+
};
|
|
2357
|
+
}
|
|
2358
|
+
function countSnapshotBundleDocuments(bundle) {
|
|
2359
|
+
let total = 0;
|
|
2360
|
+
for (const partitionBytes of bundle.partitions)
|
|
2361
|
+
total += Object.keys(deserializePayloadV2(partitionBytes).documents).length;
|
|
2362
|
+
return total;
|
|
2363
|
+
}
|
|
2364
|
+
async function loadSnapshotBundle(bytes, deps) {
|
|
2365
|
+
const bundle = await decodeSnapshotBundle(bytes);
|
|
2366
|
+
while (deps.manager.partitionCount < bundle.partitions.length) deps.manager.addPartition();
|
|
2367
|
+
if (bundle.partitions.length > 0) deps.manager.trimPartitions(bundle.partitions.length);
|
|
2368
|
+
for (let i = 0; i < bundle.partitions.length; i += 1) {
|
|
2369
|
+
deps.manager.deserializePartition(i, deserializePayloadV2(bundle.partitions[i]));
|
|
2370
|
+
}
|
|
2371
|
+
for (const [fieldPath, parts] of Object.entries(bundle.vectorIndexes)) {
|
|
2372
|
+
const rebuildsGraph = parts.some((part) => part.graphs.some((graph) => graph.m < MIN_M));
|
|
2373
|
+
deps.vectorIndexes.get(fieldPath)?.deserialize(rebuildsGraph ? parts.map((part) => ({ ...part, graphs: [] })) : parts);
|
|
2374
|
+
}
|
|
2375
|
+
return bundle.checkpoint;
|
|
2376
|
+
}
|
|
2377
|
+
|
|
2378
|
+
// src/persistence/durability/earlier-layout/manifest.ts
|
|
2379
|
+
function isKey(value) {
|
|
2380
|
+
return typeof value === "string" && value.length > 0 && !value.includes("\0");
|
|
2381
|
+
}
|
|
2382
|
+
function checkpointOf2(raw) {
|
|
2383
|
+
if (!Array.isArray(raw)) return [];
|
|
2384
|
+
const result = [];
|
|
2385
|
+
for (const entry of raw) {
|
|
2386
|
+
if (!isRecord(entry) || !isUint32(entry.partitionId)) continue;
|
|
2387
|
+
if (!isSequenceNumber(entry.lastSeqNo) || !isSequenceNumber(entry.primaryTerm)) continue;
|
|
2388
|
+
result.push({ partitionId: entry.partitionId, lastSeqNo: entry.lastSeqNo, primaryTerm: entry.primaryTerm });
|
|
2389
|
+
}
|
|
2390
|
+
return result;
|
|
2391
|
+
}
|
|
2392
|
+
function segmentsOf(raw, partitionId) {
|
|
2393
|
+
if (raw === void 0) return [];
|
|
2394
|
+
if (!Array.isArray(raw) || raw.length > MAX_SEGMENTS_PER_PARTITION) {
|
|
2395
|
+
invalidManifest("has malformed segment references", { partitionId });
|
|
2396
|
+
}
|
|
2397
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2398
|
+
return raw.map((segment) => {
|
|
2399
|
+
if (!isRecord(segment) || !isId(segment.id) || !isUint32(segment.docCount) || !isUint32(segment.tombstoneCount) || !isKey(segment.key) || seen.has(segment.id)) {
|
|
2400
|
+
invalidManifest("has an invalid segment reference", { partitionId });
|
|
2401
|
+
}
|
|
2402
|
+
seen.add(segment.id);
|
|
2403
|
+
return { id: segment.id, key: segment.key, docCount: segment.docCount, tombstoneCount: segment.tombstoneCount };
|
|
2404
|
+
});
|
|
2405
|
+
}
|
|
2406
|
+
function partitionsOf(raw) {
|
|
2407
|
+
if (!Array.isArray(raw)) invalidManifest("is missing partition entries");
|
|
2408
|
+
return raw.map((entry) => {
|
|
2409
|
+
if (!isRecord(entry) || !isUint32(entry.partitionId)) invalidManifest("has a partition without a valid id");
|
|
2410
|
+
const segments = segmentsOf(entry.segments, entry.partitionId);
|
|
2411
|
+
const nextSegmentId = entry.nextSegmentId;
|
|
2412
|
+
if (!isId(nextSegmentId) || segments.some((segment) => segment.id >= nextSegmentId)) {
|
|
2413
|
+
invalidManifest("has an invalid next segment id", { partitionId: entry.partitionId });
|
|
2414
|
+
}
|
|
2415
|
+
return { partitionId: entry.partitionId, nextSegmentId, segments };
|
|
2416
|
+
});
|
|
2417
|
+
}
|
|
2418
|
+
function vectorFileOf(raw, fieldPath) {
|
|
2419
|
+
if (!isRecord(raw) || !isId(raw.id) || !isKey(raw.key)) invalidManifest("has an invalid vector file reference");
|
|
2420
|
+
const count = raw.count;
|
|
2421
|
+
if (!isUint32(count) || count > VECTOR_FILE_MAX_VECTORS) invalidManifest(`gives "${fieldPath}" an invalid count`);
|
|
2422
|
+
const dead = raw.dead ?? null;
|
|
2423
|
+
if (dead !== null && (!(dead instanceof Uint8Array) || dead.byteLength !== deadBytesFor(count))) {
|
|
2424
|
+
invalidManifest(`marks the dead vectors of "${fieldPath}" with the wrong number of bytes`);
|
|
2425
|
+
}
|
|
2426
|
+
return { id: raw.id, key: raw.key, count, dead };
|
|
2427
|
+
}
|
|
2428
|
+
function vectorsOf(raw) {
|
|
2429
|
+
if (raw === void 0 || raw === null) return [];
|
|
2430
|
+
if (!Array.isArray(raw)) invalidManifest("has malformed vector references");
|
|
2431
|
+
return raw.map((field) => {
|
|
2432
|
+
if (!isRecord(field) || typeof field.fieldPath !== "string") invalidManifest("has an invalid vector reference");
|
|
2433
|
+
const fieldPath = field.fieldPath;
|
|
2434
|
+
if (!isId(field.nextFileId) || !Array.isArray(field.files) || !isId(field.graphGeneration)) {
|
|
2435
|
+
invalidManifest(`gives "${fieldPath}" an invalid vector reference`);
|
|
2436
|
+
}
|
|
2437
|
+
const graphKey = field.graphKey ?? null;
|
|
2438
|
+
if (graphKey !== null && (!isKey(graphKey) || field.graphGeneration === 0)) {
|
|
2439
|
+
invalidManifest(`gives "${fieldPath}" an invalid graph key`);
|
|
2440
|
+
}
|
|
2441
|
+
return {
|
|
2442
|
+
fieldPath,
|
|
2443
|
+
nextFileId: field.nextFileId,
|
|
2444
|
+
files: field.files.map((file) => vectorFileOf(file, fieldPath)),
|
|
2445
|
+
graphGeneration: field.graphGeneration,
|
|
2446
|
+
graphKey
|
|
2447
|
+
};
|
|
2448
|
+
});
|
|
2449
|
+
}
|
|
2450
|
+
function decodeEarlierManifest(raw) {
|
|
2451
|
+
const schema = raw.schema;
|
|
2452
|
+
if (!isRecord(schema)) invalidManifest("is missing its schema");
|
|
2453
|
+
requireValidFieldTypes(schema);
|
|
2454
|
+
if (typeof raw.language !== "string") invalidManifest("is missing its language");
|
|
2455
|
+
return {
|
|
2456
|
+
schema,
|
|
2457
|
+
language: raw.language,
|
|
2458
|
+
checkpoint: checkpointOf2(raw.checkpoint),
|
|
2459
|
+
partitions: partitionsOf(raw.partitions),
|
|
2460
|
+
vectors: vectorsOf(raw.vectors)
|
|
2461
|
+
};
|
|
2462
|
+
}
|
|
2463
|
+
function earlierReferencedKeys(manifest) {
|
|
2464
|
+
const keys = /* @__PURE__ */ new Set();
|
|
2465
|
+
for (const partition of manifest.partitions) {
|
|
2466
|
+
for (const segment of partition.segments) keys.add(segment.key);
|
|
2467
|
+
}
|
|
2468
|
+
for (const vector of manifest.vectors) {
|
|
2469
|
+
for (const file of vector.files) keys.add(file.key);
|
|
2470
|
+
if (vector.graphKey !== null) keys.add(vector.graphKey);
|
|
2471
|
+
}
|
|
2472
|
+
return keys;
|
|
2473
|
+
}
|
|
2474
|
+
|
|
2475
|
+
// src/persistence/durability/earlier-layout/merge.ts
|
|
2476
|
+
function mergeTimeOrderedSegments(ordered, fallback) {
|
|
2477
|
+
const winner = resolveWinners(ordered);
|
|
2478
|
+
const first = ordered[0]?.partition;
|
|
2479
|
+
const merged = {
|
|
2480
|
+
indexName: first?.indexName ?? fallback.indexName,
|
|
2481
|
+
partitionId: first?.partitionId ?? fallback.partitionId,
|
|
2482
|
+
totalPartitions: first?.totalPartitions ?? fallback.totalPartitions,
|
|
2483
|
+
language: first?.language ?? fallback.language,
|
|
2484
|
+
schema: {},
|
|
2485
|
+
docCount: 0,
|
|
2486
|
+
avgDocLength: 0,
|
|
2487
|
+
documents: /* @__PURE__ */ Object.create(null),
|
|
2488
|
+
invertedIndex: /* @__PURE__ */ Object.create(null),
|
|
2489
|
+
fieldIndexes: {
|
|
2490
|
+
numeric: /* @__PURE__ */ Object.create(null),
|
|
2491
|
+
boolean: /* @__PURE__ */ Object.create(null),
|
|
2492
|
+
enum: /* @__PURE__ */ Object.create(null),
|
|
2493
|
+
geopoint: /* @__PURE__ */ Object.create(null)
|
|
2494
|
+
},
|
|
2495
|
+
statistics: {
|
|
2496
|
+
totalDocuments: 0,
|
|
2497
|
+
totalFieldLengths: /* @__PURE__ */ Object.create(null),
|
|
2498
|
+
averageFieldLengths: /* @__PURE__ */ Object.create(null),
|
|
2499
|
+
docFrequencies: /* @__PURE__ */ Object.create(null)
|
|
2500
|
+
}
|
|
2501
|
+
};
|
|
2502
|
+
for (let segIndex = 0; segIndex < ordered.length; segIndex += 1) {
|
|
2503
|
+
const part = ordered[segIndex].partition;
|
|
2504
|
+
addSegmentSchema(merged.schema, part.schema);
|
|
2505
|
+
collectDocuments(merged, part, winner, segIndex);
|
|
2506
|
+
collectInvertedIndex(merged, part, winner, segIndex);
|
|
2507
|
+
collectFieldIndexes(merged, part, winner, segIndex);
|
|
2508
|
+
collectSurfaceForms(merged, part);
|
|
2509
|
+
}
|
|
2510
|
+
recomputeStatistics(merged);
|
|
2511
|
+
validateMergedPartition(merged);
|
|
2512
|
+
return merged;
|
|
2513
|
+
}
|
|
2514
|
+
function countLiveDocuments(ordered) {
|
|
2515
|
+
return resolveWinners(ordered).size;
|
|
2516
|
+
}
|
|
2517
|
+
function resolveWinners(ordered) {
|
|
2518
|
+
const winner = /* @__PURE__ */ new Map();
|
|
2519
|
+
for (let segIndex = 0; segIndex < ordered.length; segIndex += 1) {
|
|
2520
|
+
const segment = ordered[segIndex];
|
|
2521
|
+
for (const docId of segment.tombstones) {
|
|
2522
|
+
winner.delete(docId);
|
|
2523
|
+
}
|
|
2524
|
+
for (const docId of Object.keys(segment.partition.documents)) {
|
|
2525
|
+
winner.set(docId, segIndex);
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
return winner;
|
|
2529
|
+
}
|
|
2530
|
+
function owns(winner, docId, segIndex) {
|
|
2531
|
+
return winner.get(docId) === segIndex;
|
|
2532
|
+
}
|
|
2533
|
+
function addSegmentSchema(agreed, schema) {
|
|
2534
|
+
for (const [field, type] of Object.entries(schema)) {
|
|
2535
|
+
const existing = agreed[field];
|
|
2536
|
+
if (existing !== void 0 && existing !== type) {
|
|
2537
|
+
throw new NarsilError(
|
|
2538
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
2539
|
+
`Segment schemas disagree on field "${field}": "${existing}" vs "${type}"`,
|
|
2540
|
+
{ field, existing, found: type }
|
|
2541
|
+
);
|
|
2542
|
+
}
|
|
2543
|
+
agreed[field] = type;
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
function collectDocuments(merged, part, winner, segIndex) {
|
|
2547
|
+
for (const [docId, doc] of Object.entries(part.documents)) {
|
|
2548
|
+
if (owns(winner, docId, segIndex)) {
|
|
2549
|
+
merged.documents[docId] = doc;
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
function collectInvertedIndex(merged, part, winner, segIndex) {
|
|
2554
|
+
for (const [token, list] of Object.entries(part.invertedIndex)) {
|
|
2555
|
+
let target = merged.invertedIndex[token];
|
|
2556
|
+
for (const posting of list.postings) {
|
|
2557
|
+
if (!owns(winner, posting.docId, segIndex)) continue;
|
|
2558
|
+
if (target === void 0) {
|
|
2559
|
+
target = { docFrequency: 0, postings: [] };
|
|
2560
|
+
merged.invertedIndex[token] = target;
|
|
2561
|
+
}
|
|
2562
|
+
target.postings.push(posting);
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
function collectEntries(target, source, winner, segIndex) {
|
|
2567
|
+
for (const [path, entries] of Object.entries(source)) {
|
|
2568
|
+
for (const entry of entries) {
|
|
2569
|
+
if (!owns(winner, entry.docId, segIndex)) continue;
|
|
2570
|
+
let bucket = target[path];
|
|
2571
|
+
if (bucket === void 0) {
|
|
2572
|
+
bucket = [];
|
|
2573
|
+
target[path] = bucket;
|
|
2574
|
+
}
|
|
2575
|
+
bucket.push(entry);
|
|
2576
|
+
}
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
function collectFieldIndexes(merged, part, winner, segIndex) {
|
|
2580
|
+
collectEntries(merged.fieldIndexes.numeric, part.fieldIndexes.numeric, winner, segIndex);
|
|
2581
|
+
collectEntries(merged.fieldIndexes.geopoint, part.fieldIndexes.geopoint, winner, segIndex);
|
|
2582
|
+
for (const [path, idx] of Object.entries(part.fieldIndexes.boolean)) {
|
|
2583
|
+
for (const [docIds, flag] of [
|
|
2584
|
+
[idx.trueDocs, true],
|
|
2585
|
+
[idx.falseDocs, false]
|
|
2586
|
+
]) {
|
|
2587
|
+
for (const docId of docIds) {
|
|
2588
|
+
if (!owns(winner, docId, segIndex)) continue;
|
|
2589
|
+
let target = merged.fieldIndexes.boolean[path];
|
|
2590
|
+
if (target === void 0) {
|
|
2591
|
+
target = { trueDocs: [], falseDocs: [] };
|
|
2592
|
+
merged.fieldIndexes.boolean[path] = target;
|
|
2593
|
+
}
|
|
2594
|
+
(flag ? target.trueDocs : target.falseDocs).push(docId);
|
|
2595
|
+
}
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
for (const [path, values] of Object.entries(part.fieldIndexes.enum)) {
|
|
2599
|
+
for (const [value, docIds] of Object.entries(values)) {
|
|
2600
|
+
for (const docId of docIds) {
|
|
2601
|
+
if (!owns(winner, docId, segIndex)) continue;
|
|
2602
|
+
let target = merged.fieldIndexes.enum[path];
|
|
2603
|
+
if (target === void 0) {
|
|
2604
|
+
target = /* @__PURE__ */ Object.create(null);
|
|
2605
|
+
merged.fieldIndexes.enum[path] = target;
|
|
2606
|
+
}
|
|
2607
|
+
let bucket = target[value];
|
|
2608
|
+
if (bucket === void 0) {
|
|
2609
|
+
bucket = [];
|
|
2610
|
+
target[value] = bucket;
|
|
2611
|
+
}
|
|
2612
|
+
bucket.push(docId);
|
|
2613
|
+
}
|
|
2614
|
+
}
|
|
2615
|
+
}
|
|
2616
|
+
}
|
|
2617
|
+
function collectSurfaceForms(merged, part) {
|
|
2618
|
+
if (!part.surfaceForms) return;
|
|
2619
|
+
let target = merged.surfaceForms;
|
|
2620
|
+
if (!target) {
|
|
2621
|
+
target = /* @__PURE__ */ Object.create(null);
|
|
2622
|
+
merged.surfaceForms = target;
|
|
2623
|
+
}
|
|
2624
|
+
for (const [surface, value] of Object.entries(part.surfaceForms)) {
|
|
2625
|
+
const count = typeof value === "number" ? value : value[0];
|
|
2626
|
+
const token = typeof value === "number" ? surface : value[1];
|
|
2627
|
+
const existing = target[surface];
|
|
2628
|
+
if (existing === void 0) {
|
|
2629
|
+
target[surface] = value;
|
|
2630
|
+
continue;
|
|
2631
|
+
}
|
|
2632
|
+
const existingCount = typeof existing === "number" ? existing : existing[0];
|
|
2633
|
+
const total = existingCount + count;
|
|
2634
|
+
target[surface] = token === surface ? total : [total, token];
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
function recomputeStatistics(merged) {
|
|
2638
|
+
const totalFieldLengths = /* @__PURE__ */ Object.create(null);
|
|
2639
|
+
let totalDocuments = 0;
|
|
2640
|
+
for (const doc of Object.values(merged.documents)) {
|
|
2641
|
+
totalDocuments += 1;
|
|
2642
|
+
for (const [field, length] of Object.entries(doc.fieldLengths)) {
|
|
2643
|
+
totalFieldLengths[field] = (totalFieldLengths[field] ?? 0) + length;
|
|
2644
|
+
}
|
|
2645
|
+
}
|
|
2646
|
+
const docFrequencies = /* @__PURE__ */ Object.create(null);
|
|
2647
|
+
for (const [token, list] of Object.entries(merged.invertedIndex)) {
|
|
2648
|
+
const distinct = /* @__PURE__ */ new Set();
|
|
2649
|
+
for (const posting of list.postings) distinct.add(posting.docId);
|
|
2650
|
+
list.docFrequency = distinct.size;
|
|
2651
|
+
docFrequencies[token] = distinct.size;
|
|
2652
|
+
}
|
|
2653
|
+
const averageFieldLengths = /* @__PURE__ */ Object.create(null);
|
|
2654
|
+
for (const [field, length] of Object.entries(totalFieldLengths)) {
|
|
2655
|
+
averageFieldLengths[field] = totalDocuments > 0 ? length / totalDocuments : 0;
|
|
2656
|
+
}
|
|
2657
|
+
merged.statistics.totalDocuments = totalDocuments;
|
|
2658
|
+
merged.statistics.totalFieldLengths = totalFieldLengths;
|
|
2659
|
+
merged.statistics.averageFieldLengths = averageFieldLengths;
|
|
2660
|
+
merged.statistics.docFrequencies = docFrequencies;
|
|
2661
|
+
merged.docCount = totalDocuments;
|
|
2662
|
+
merged.avgDocLength = Object.values(averageFieldLengths).reduce((sum, value) => sum + value, 0);
|
|
2663
|
+
}
|
|
2664
|
+
function validateMergedPartition(merged) {
|
|
2665
|
+
for (const [token, list] of Object.entries(merged.invertedIndex)) {
|
|
2666
|
+
for (const posting of list.postings) {
|
|
2667
|
+
if (!(posting.docId in merged.documents)) {
|
|
2668
|
+
throw new NarsilError(
|
|
2669
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
2670
|
+
`Merged segment posting references document "${posting.docId}" that is absent from the partition`,
|
|
2671
|
+
{ token, docId: posting.docId }
|
|
2672
|
+
);
|
|
2673
|
+
}
|
|
2674
|
+
}
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
function normalizeTombstones(raw) {
|
|
2678
|
+
if (raw === void 0) return [];
|
|
2679
|
+
if (!Array.isArray(raw) || !raw.every((value) => typeof value === "string")) {
|
|
2680
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, "Segment file has a malformed tombstone list");
|
|
2681
|
+
}
|
|
2682
|
+
return raw;
|
|
2683
|
+
}
|
|
2684
|
+
async function openContainer(store, key) {
|
|
2685
|
+
const bytes = await store.read(key);
|
|
2686
|
+
if (bytes === null) {
|
|
2687
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `Segment file "${key}" is missing`, { key });
|
|
2688
|
+
}
|
|
2689
|
+
const { payloadBytes } = await unpackEnvelopeBytes(bytes);
|
|
2690
|
+
const raw = decode(payloadBytes);
|
|
2691
|
+
if (!(raw.payload instanceof Uint8Array)) {
|
|
2692
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, "Segment file is missing its partition payload", { key });
|
|
2693
|
+
}
|
|
2694
|
+
return { payload: raw.payload, tombstones: normalizeTombstones(raw.tombstones) };
|
|
2695
|
+
}
|
|
2696
|
+
async function readEarlierSegmentContents(store, key) {
|
|
2697
|
+
const { payload, tombstones } = await openContainer(store, key);
|
|
2698
|
+
return { partition: deserializePayloadV2(payload), tombstones };
|
|
2699
|
+
}
|
|
2700
|
+
async function readEarlierSegmentDocuments(store, key) {
|
|
2701
|
+
const { payload, tombstones } = await openContainer(store, key);
|
|
2702
|
+
const stored = documentsOfPayloadV2(payload);
|
|
2703
|
+
const documents = [];
|
|
2704
|
+
for (const [docId, document] of Object.entries(stored.documents)) {
|
|
2705
|
+
documents.push({ docId, document: document.fields });
|
|
2706
|
+
}
|
|
2707
|
+
return { schema: stored.schema, documents, tombstones };
|
|
2708
|
+
}
|
|
2709
|
+
|
|
2710
|
+
// src/persistence/durability/checkpoint-count.ts
|
|
2711
|
+
var decoder = new TextDecoder();
|
|
2712
|
+
async function countCurrentLayout(store, manifest) {
|
|
2713
|
+
let total = 0;
|
|
2714
|
+
for (const partition of manifest.partitions) {
|
|
2715
|
+
const live = /* @__PURE__ */ new Set();
|
|
2716
|
+
for (const ref of partition.segments) {
|
|
2717
|
+
const contents = await readSegmentFile(store, ref);
|
|
2718
|
+
for (const docId of contents.tombstones) live.delete(docId);
|
|
2719
|
+
const ids = contents.arrays.idTable;
|
|
2720
|
+
for (let ordinal = 0; ordinal < contents.arrays.documentCount; ordinal++) {
|
|
2721
|
+
live.add(decoder.decode(ids.blob.subarray(ids.offsets[ordinal], ids.offsets[ordinal + 1])));
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
total += live.size;
|
|
2725
|
+
}
|
|
2726
|
+
return total;
|
|
2727
|
+
}
|
|
2728
|
+
async function countCheckpointDocuments(store, indexName) {
|
|
2729
|
+
const decoded = await readSegmentManifest(store, indexName);
|
|
2730
|
+
if (decoded?.kind === "current") return countCurrentLayout(store, decoded.manifest);
|
|
2731
|
+
if (decoded?.kind === "earlier") {
|
|
2732
|
+
let total = 0;
|
|
2733
|
+
for (const partition of decodeEarlierManifest(decoded.payload).partitions) {
|
|
2734
|
+
const ordered = [];
|
|
2735
|
+
for (const segment of partition.segments) ordered.push(await readEarlierSegmentContents(store, segment.key));
|
|
2736
|
+
total += countLiveDocuments(ordered);
|
|
2737
|
+
}
|
|
2738
|
+
return total;
|
|
2739
|
+
}
|
|
2740
|
+
const bundle = await store.read(snapshotBundleKey(indexName));
|
|
2741
|
+
return bundle === null ? 0 : countSnapshotBundleDocuments(await decodeSnapshotBundle(bundle));
|
|
2742
|
+
}
|
|
2743
|
+
function removeWhatTheSegmentReplaces(manager, segment) {
|
|
2744
|
+
manager.beginBatchRemove();
|
|
2745
|
+
try {
|
|
2746
|
+
for (const docId of segment.tombstones) {
|
|
2747
|
+
if (manager.has(docId)) manager.remove(docId);
|
|
2748
|
+
}
|
|
2749
|
+
for (const { docId } of segment.documents) {
|
|
2750
|
+
if (manager.has(docId)) manager.remove(docId);
|
|
2751
|
+
}
|
|
2752
|
+
} finally {
|
|
2753
|
+
manager.endBatchRemove();
|
|
2754
|
+
}
|
|
2755
|
+
}
|
|
2756
|
+
function attachDocuments(manager, partitionId, segment) {
|
|
2757
|
+
if (segment.documents.length === 0) return;
|
|
2758
|
+
const payload = buildSegmentPayload(
|
|
2759
|
+
segment.documents,
|
|
2760
|
+
manager.config.schema,
|
|
2761
|
+
manager.language,
|
|
2762
|
+
resolvePartitionInsertOptions(manager.config, manager.analysis),
|
|
2763
|
+
manager.config.trackPositions ?? true
|
|
2764
|
+
);
|
|
2765
|
+
const documents = segment.documents.map((entry) => entry.document);
|
|
2766
|
+
const snapshot = freezeSegmentShared(payload, documents);
|
|
2767
|
+
if (snapshot === null) {
|
|
2768
|
+
manager.mergeSegment(partitionId, payload, documents);
|
|
2769
|
+
return;
|
|
2770
|
+
}
|
|
2771
|
+
manager.attachFrozenSegment(partitionId, createSharedFrozenSegment(snapshot));
|
|
2772
|
+
}
|
|
2773
|
+
async function loadPartition(store, indexName, partition, deps) {
|
|
2774
|
+
if (deps.storedTermsAreCurrent) {
|
|
2775
|
+
const agreedSchema = {};
|
|
2776
|
+
for (const ref of partition.segments) {
|
|
2777
|
+
const segment = await readEarlierSegmentDocuments(store, ref.key);
|
|
2778
|
+
addSegmentSchema(agreedSchema, segment.schema);
|
|
2779
|
+
removeWhatTheSegmentReplaces(deps.manager, segment);
|
|
2780
|
+
attachDocuments(deps.manager, partition.partitionId, segment);
|
|
2781
|
+
}
|
|
2782
|
+
return;
|
|
2783
|
+
}
|
|
2784
|
+
const ordered = [];
|
|
2785
|
+
for (const ref of partition.segments) ordered.push(await readEarlierSegmentContents(store, ref.key));
|
|
2786
|
+
const merged = mergeTimeOrderedSegments(ordered, {
|
|
2787
|
+
indexName,
|
|
2788
|
+
partitionId: partition.partitionId,
|
|
2789
|
+
totalPartitions: deps.manager.partitionCount,
|
|
2790
|
+
language: deps.manager.language.name
|
|
2791
|
+
});
|
|
2792
|
+
deps.manager.deserializePartition(partition.partitionId, merged);
|
|
2793
|
+
}
|
|
2794
|
+
async function readGraph(store, ref) {
|
|
2795
|
+
if (ref.graphKey === null) return null;
|
|
2796
|
+
const bytes = await store.read(ref.graphKey);
|
|
2797
|
+
if (bytes === null) {
|
|
2798
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The graph file "${ref.graphKey}" is missing`, {
|
|
2799
|
+
key: ref.graphKey
|
|
2800
|
+
});
|
|
2801
|
+
}
|
|
2802
|
+
const { payload } = await openChecksummedEnvelope(bytes, {
|
|
2803
|
+
envelopeFormatVersion: VECTOR_FILE_ENVELOPE_VERSION,
|
|
2804
|
+
what: `graph file "${ref.graphKey}"`,
|
|
2805
|
+
allowsCompression: true
|
|
2806
|
+
});
|
|
2807
|
+
const graph = decodeVectorGraphPayload(decode(payload));
|
|
2808
|
+
if (graph.graphs.some((numbered) => numbered.m < MIN_M)) return null;
|
|
2809
|
+
return graphPartsOfEarlierPayload(graph);
|
|
2810
|
+
}
|
|
2811
|
+
async function loadVectorField(store, ref, vectorIndex, fileRefs) {
|
|
2812
|
+
let liveVectors = 0;
|
|
2813
|
+
for (const file of ref.files) liveVectors += file.count - countDead(file.dead, file.count);
|
|
2814
|
+
const restore = vectorIndex.restoreCheckpoint({
|
|
2815
|
+
liveVectors,
|
|
2816
|
+
holdsGraph: ref.graphKey !== null,
|
|
2817
|
+
graph: await readGraph(store, ref)
|
|
2818
|
+
});
|
|
2819
|
+
for (const file of ref.files) {
|
|
2820
|
+
const bytes = await store.read(file.key);
|
|
2821
|
+
if (bytes === null) {
|
|
2822
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The vector file "${file.key}" is missing`, {
|
|
2823
|
+
key: file.key
|
|
2824
|
+
});
|
|
2825
|
+
}
|
|
2826
|
+
const { header, payload: payloadBytes } = await openChecksummedEnvelope(bytes, {
|
|
2827
|
+
envelopeFormatVersion: VECTOR_FILE_ENVELOPE_VERSION,
|
|
2828
|
+
what: `vector file "${file.key}"`,
|
|
2829
|
+
allowsCompression: false
|
|
2830
|
+
});
|
|
2831
|
+
const payload = decodeVectorFilePayload(decode(payloadBytes));
|
|
2832
|
+
if (payload.docIds.length !== file.count) {
|
|
2833
|
+
throw new NarsilError(
|
|
2834
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
2835
|
+
`The vector file "${file.key}" holds ${payload.docIds.length} vectors where the manifest counts ${file.count}`,
|
|
2836
|
+
{ key: file.key }
|
|
2837
|
+
);
|
|
2838
|
+
}
|
|
2839
|
+
fileRefs.set(file.key, { key: file.key, bytes: bytes.byteLength, checksum: header.checksum });
|
|
2840
|
+
restore.addFile({
|
|
2841
|
+
id: file.id,
|
|
2842
|
+
key: file.key,
|
|
2843
|
+
payload,
|
|
2844
|
+
dead: file.dead,
|
|
2845
|
+
location: store.pathOf === void 0 ? null : {
|
|
2846
|
+
path: await store.pathOf(file.key),
|
|
2847
|
+
vectorsOffset: HEADER_SIZE + header.payloadLength - payload.vectors.byteLength
|
|
2848
|
+
}
|
|
2849
|
+
});
|
|
2850
|
+
}
|
|
2851
|
+
restore.finish();
|
|
2852
|
+
}
|
|
2853
|
+
async function loadEarlierLayout(store, indexName, manifest, deps) {
|
|
2854
|
+
let highestPartitionId = manifest.partitions.length - 1;
|
|
2855
|
+
for (const partition of manifest.partitions) {
|
|
2856
|
+
if (partition.partitionId > highestPartitionId) highestPartitionId = partition.partitionId;
|
|
2857
|
+
}
|
|
2858
|
+
growPartitionsTo(deps.manager, highestPartitionId);
|
|
2859
|
+
for (const partition of manifest.partitions) {
|
|
2860
|
+
if (partition.partitionId >= deps.manager.partitionCount) {
|
|
2861
|
+
throw new NarsilError(
|
|
2862
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
2863
|
+
`Segment manifest references partition ${partition.partitionId} beyond the partition count ${deps.manager.partitionCount}`,
|
|
2864
|
+
{ indexName, partitionId: partition.partitionId }
|
|
2865
|
+
);
|
|
2866
|
+
}
|
|
2867
|
+
await loadPartition(store, indexName, partition, deps);
|
|
2868
|
+
}
|
|
2869
|
+
const vectorFileRefs = /* @__PURE__ */ new Map();
|
|
2870
|
+
for (const vector of manifest.vectors) {
|
|
2871
|
+
const vectorIndex = deps.vectorIndexes.get(vector.fieldPath);
|
|
2872
|
+
if (vectorIndex === void 0) continue;
|
|
2873
|
+
await loadVectorField(store, vector, vectorIndex, vectorFileRefs);
|
|
2874
|
+
}
|
|
2875
|
+
return { checkpoint: manifest.checkpoint, vectorFileRefs };
|
|
2876
|
+
}
|
|
2877
|
+
|
|
2878
|
+
// src/persistence/durability/earlier-layout/upgrade.ts
|
|
2879
|
+
async function loadEarlier(deps, earlier) {
|
|
2880
|
+
const loadDeps = {
|
|
2881
|
+
manager: deps.manager,
|
|
2882
|
+
vectorIndexes: deps.vectorIndexes,
|
|
2883
|
+
storedTermsAreCurrent: deps.storedTermsAreCurrent
|
|
2884
|
+
};
|
|
2885
|
+
if (earlier.kind === "bundle") {
|
|
2886
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, /* @__PURE__ */ new Set());
|
|
2887
|
+
return { checkpoint: await loadSnapshotBundle(earlier.bytes, loadDeps), fileRefs: /* @__PURE__ */ new Map() };
|
|
2888
|
+
}
|
|
2889
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, earlierReferencedKeys(earlier.manifest));
|
|
2890
|
+
const loaded = await loadEarlierLayout(deps.store, deps.indexName, earlier.manifest, loadDeps);
|
|
2891
|
+
return { checkpoint: loaded.checkpoint, fileRefs: loaded.vectorFileRefs };
|
|
2892
|
+
}
|
|
2893
|
+
function priorVectorsOf(earlier, fileRefs) {
|
|
2894
|
+
if (earlier.kind === "bundle") return [];
|
|
2895
|
+
return earlier.manifest.vectors.map((field) => ({
|
|
2896
|
+
fieldPath: field.fieldPath,
|
|
2897
|
+
nextFileId: field.nextFileId,
|
|
2898
|
+
graphGeneration: field.graphGeneration,
|
|
2899
|
+
graphParts: [],
|
|
2900
|
+
files: field.files.flatMap((file) => {
|
|
2901
|
+
const ref = fileRefs.get(file.key);
|
|
2902
|
+
return ref === void 0 ? [] : [{ ...file, bytes: ref.bytes, checksum: ref.checksum }];
|
|
2903
|
+
})
|
|
2904
|
+
}));
|
|
2905
|
+
}
|
|
2906
|
+
function priorPartitionsOf(earlier, partitionCount) {
|
|
2907
|
+
if (earlier.kind === "bundle") return [];
|
|
2908
|
+
return earlier.manifest.partitions.filter((partition) => partition.partitionId < partitionCount).map((partition) => ({ partitionId: partition.partitionId, nextSegmentId: partition.nextSegmentId, segments: [] }));
|
|
2909
|
+
}
|
|
2910
|
+
async function writeCurrentLayout(deps, earlier, targets, fileRefs) {
|
|
2911
|
+
const priorVectors = priorVectorsOf(earlier, fileRefs);
|
|
2912
|
+
const plans = /* @__PURE__ */ new Map();
|
|
2913
|
+
for (const [fieldPath, vectorIndex] of deps.vectorIndexes) {
|
|
2914
|
+
await vectorIndex.completeGraph();
|
|
2915
|
+
const listed = priorVectors.find((ref) => ref.fieldPath === fieldPath);
|
|
2916
|
+
plans.set(
|
|
2917
|
+
fieldPath,
|
|
2918
|
+
vectorIndex.planCheckpoint(listed === void 0 ? null : listed.files.map((file) => file.key), true)
|
|
2919
|
+
);
|
|
2920
|
+
}
|
|
2921
|
+
const written = await writeCheckpointSegments({
|
|
2922
|
+
store: deps.store,
|
|
2923
|
+
metadata: deps.metadata,
|
|
2924
|
+
targets,
|
|
2925
|
+
compactionThreshold: deps.compactionThreshold,
|
|
2926
|
+
prior: { checkpoint: [], partitions: priorPartitionsOf(earlier, deps.manager.partitionCount) },
|
|
2927
|
+
wholePartitions: targets.map((target) => partitionContentsOf(deps.manager, target.partitionId))
|
|
2928
|
+
});
|
|
2929
|
+
const vectors = await writeLiveVectors({
|
|
2930
|
+
store: deps.store,
|
|
2931
|
+
indexName: deps.indexName,
|
|
2932
|
+
plans,
|
|
2933
|
+
priorVectors,
|
|
2934
|
+
priorFileRefs: fileRefs
|
|
2935
|
+
});
|
|
2936
|
+
const committed = await commitCheckpointManifest(deps.store, deps.metadata, written, vectors.refs);
|
|
2937
|
+
recordWrittenVectors(deps.vectorIndexes, vectors.written);
|
|
2938
|
+
await adoptVectorLayouts(deps.store, deps.vectorIndexes, vectors.layouts);
|
|
2939
|
+
await removeCheckpointGarbage(
|
|
2940
|
+
deps.store,
|
|
2941
|
+
await garbageThatNoFieldStillReads(deps.store, deps.vectorIndexes, committed.garbage)
|
|
2942
|
+
);
|
|
2943
|
+
}
|
|
2944
|
+
async function upgradeEarlierLayout(deps, earlier) {
|
|
2945
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "started", layout: earlier.kind });
|
|
2946
|
+
try {
|
|
2947
|
+
const loaded = await loadEarlier(deps, earlier);
|
|
2948
|
+
const targets = await deps.replay(loaded.checkpoint);
|
|
2949
|
+
await writeCurrentLayout(deps, earlier, targets, loaded.fileRefs);
|
|
2950
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "completed", layout: earlier.kind });
|
|
2951
|
+
return { checkpoint: targets, documentCount: deps.manager.countDocuments() };
|
|
2952
|
+
} catch (error) {
|
|
2953
|
+
const failure = error instanceof Error ? error : new Error(String(error));
|
|
2954
|
+
deps.onLayoutUpgrade?.({ indexName: deps.indexName, status: "failed", layout: earlier.kind, error: failure });
|
|
2955
|
+
throw failure;
|
|
2956
|
+
}
|
|
2957
|
+
}
|
|
2958
|
+
|
|
2959
|
+
// src/persistence/durability/checkpoint-recovery.ts
|
|
2960
|
+
async function earlierLayoutOf(deps, decoded) {
|
|
2961
|
+
if (decoded?.kind === "earlier") return { kind: "manifest", manifest: decodeEarlierManifest(decoded.payload) };
|
|
2962
|
+
if (decoded !== null) return null;
|
|
2963
|
+
const bundle = await deps.store.read(snapshotBundleKey(deps.indexName));
|
|
2964
|
+
return bundle === null ? null : { kind: "bundle", bytes: bundle };
|
|
2965
|
+
}
|
|
2966
|
+
async function recoverCheckpoint(deps) {
|
|
2967
|
+
const decoded = await readSegmentManifest(deps.store, deps.indexName);
|
|
2968
|
+
const earlier = await earlierLayoutOf(deps, decoded);
|
|
2969
|
+
if (earlier !== null) {
|
|
2970
|
+
const upgraded = await upgradeEarlierLayout(deps, earlier);
|
|
2971
|
+
return { checkpoint: upgraded.checkpoint, upgradedDocumentCount: upgraded.documentCount };
|
|
2972
|
+
}
|
|
2973
|
+
if (decoded === null || decoded.kind !== "current") {
|
|
2974
|
+
if (!deps.keepsUnlistedFiles) await reclaimUnlistedKeys(deps.store, deps.indexName, /* @__PURE__ */ new Set());
|
|
2975
|
+
return { checkpoint: await deps.replay([]), upgradedDocumentCount: null };
|
|
2976
|
+
}
|
|
2977
|
+
await deps.store.remove(snapshotBundleKey(deps.indexName));
|
|
2978
|
+
if (!deps.keepsUnlistedFiles) {
|
|
2979
|
+
await reclaimUnlistedKeys(deps.store, deps.indexName, manifestReferencedKeys(decoded.manifest));
|
|
2980
|
+
}
|
|
2981
|
+
const checkpoint = await loadSegmentedCheckpoint(deps.store, deps.indexName, decoded.manifest, {
|
|
2982
|
+
manager: deps.manager,
|
|
2983
|
+
vectorIndexes: deps.vectorIndexes
|
|
2984
|
+
});
|
|
2985
|
+
return { checkpoint: await deps.replay(checkpoint), upgradedDocumentCount: null };
|
|
2986
|
+
}
|
|
2987
|
+
|
|
2988
|
+
// src/persistence/durability/recover-index.ts
|
|
2989
|
+
async function writeCountedMetadata(deps, indexName, documentCount) {
|
|
2990
|
+
await deps.queueMetadataWrite(indexName, async () => {
|
|
2991
|
+
const metadata = deps.hooks.buildMetadata(indexName, documentCount);
|
|
2992
|
+
if (metadata === void 0) return;
|
|
2993
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
2994
|
+
await deps.directory.atomicWrite(`${indexName}/meta`, bytes);
|
|
2995
|
+
}).catch(() => void 0);
|
|
2996
|
+
}
|
|
2997
|
+
async function recoverPersistedIndex(deps, indexName, metadataOnly) {
|
|
2998
|
+
const { directory, hooks } = deps;
|
|
2999
|
+
const store = directoryCheckpointStore(directory);
|
|
3000
|
+
const metadata = await loadMetadata(directory, indexName);
|
|
3001
|
+
if (metadata === null) {
|
|
3002
|
+
return;
|
|
3003
|
+
}
|
|
3004
|
+
let derivedCount;
|
|
3005
|
+
if (metadataOnly && metadata.documentCount === void 0) {
|
|
3006
|
+
try {
|
|
3007
|
+
derivedCount = await countCheckpointDocuments(store, indexName);
|
|
3008
|
+
metadata.documentCount = derivedCount;
|
|
3009
|
+
} catch {
|
|
3010
|
+
derivedCount = void 0;
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
await hooks.createIndexFromMetadata(metadata, !metadataOnly);
|
|
3014
|
+
if (metadataOnly) {
|
|
3015
|
+
if (derivedCount !== void 0) await writeCountedMetadata(deps, indexName, derivedCount);
|
|
3016
|
+
return;
|
|
3017
|
+
}
|
|
3018
|
+
const manager = hooks.getManager(indexName);
|
|
3019
|
+
const current = hooks.buildMetadata(indexName);
|
|
3020
|
+
if (manager === void 0 || current === void 0) {
|
|
3021
|
+
return;
|
|
3022
|
+
}
|
|
3023
|
+
const replayDeps = {
|
|
3024
|
+
manager,
|
|
3025
|
+
vectorFieldPaths: hooks.getVectorFieldPaths(indexName),
|
|
3026
|
+
vectorIndexes: hooks.getVectorIndexes(indexName)
|
|
3027
|
+
};
|
|
3028
|
+
const replay = async (checkpoint) => {
|
|
3029
|
+
const replayed = [];
|
|
3030
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
3031
|
+
const fromSeqNo = snapshotCheckpointFor(checkpoint, partitionId);
|
|
3032
|
+
const { highestSeqNo, highestPrimaryTerm } = await replayWal(
|
|
3033
|
+
directory,
|
|
3034
|
+
indexName,
|
|
3035
|
+
partitionId,
|
|
3036
|
+
fromSeqNo,
|
|
3037
|
+
replayDeps
|
|
3038
|
+
);
|
|
3039
|
+
const primaryTerm = Math.max(
|
|
3040
|
+
checkpointPrimaryTerm(checkpoint, partitionId),
|
|
3041
|
+
highestPrimaryTerm,
|
|
3042
|
+
SINGLE_NODE_PRIMARY_TERM
|
|
3043
|
+
);
|
|
3044
|
+
replayed.push({ partitionId, lastSeqNo: highestSeqNo, primaryTerm });
|
|
3045
|
+
}
|
|
3046
|
+
return replayed;
|
|
3047
|
+
};
|
|
3048
|
+
const recovered = await recoverCheckpoint({
|
|
3049
|
+
store,
|
|
3050
|
+
indexName,
|
|
3051
|
+
metadata: current,
|
|
3052
|
+
manager,
|
|
3053
|
+
vectorIndexes: replayDeps.vectorIndexes,
|
|
3054
|
+
storedTermsAreCurrent: metadata.analysisRevision === manager.language.revision,
|
|
3055
|
+
compactionThreshold: deps.compactionThreshold,
|
|
3056
|
+
keepsUnlistedFiles: deps.holdsCheckpointFiles(indexName),
|
|
3057
|
+
replay,
|
|
3058
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade
|
|
3059
|
+
});
|
|
3060
|
+
for (const { partitionId, lastSeqNo, primaryTerm } of recovered.checkpoint) {
|
|
3061
|
+
deps.registerPartition(indexName, partitionId, lastSeqNo, primaryTerm);
|
|
3062
|
+
}
|
|
3063
|
+
if (recovered.upgradedDocumentCount !== null) {
|
|
3064
|
+
await writeCountedMetadata(deps, indexName, recovered.upgradedDocumentCount);
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
|
|
3068
|
+
// src/persistence/durability/manager.ts
|
|
3069
|
+
function createDurabilityManager(config, hooks, directoryOverride) {
|
|
3070
|
+
const directory = createDurableDirectory(config.directory ?? "");
|
|
3071
|
+
const canOffloadCheckpoint = directoryOverride === void 0;
|
|
3072
|
+
const segmentMaxBytes = config.segmentMaxBytes ?? DEFAULT_SEGMENT_MAX_BYTES;
|
|
3073
|
+
const checkpointIntervalMs = config.checkpointIntervalMs ?? DEFAULT_CHECKPOINT_INTERVAL_MS;
|
|
3074
|
+
const checkpointMutationThreshold = config.checkpointMutationThreshold ?? DEFAULT_CHECKPOINT_MUTATION_THRESHOLD;
|
|
3075
|
+
const compactionThreshold = config.compactionThreshold ?? DEFAULT_COMPACTION_THRESHOLD;
|
|
3076
|
+
const mode = config.mode ?? "sync";
|
|
3077
|
+
const flushIntervalMs = config.flushIntervalMs ?? DEFAULT_ASYNC_FLUSH_INTERVAL_MS;
|
|
3078
|
+
const registry = createPartitionRegistry(directory, segmentMaxBytes);
|
|
3079
|
+
const indexes = registry.indexes;
|
|
3080
|
+
const getOrCreateIndexState = registry.indexState;
|
|
3081
|
+
const getOrCreatePartition = registry.partition;
|
|
3082
|
+
let entryNumbering = null;
|
|
3083
|
+
let commitDeferral = null;
|
|
3084
|
+
const partitionFailures = createPartitionFailureRouting();
|
|
3085
|
+
const heldCheckpoints = createHeldCheckpoints(hooks);
|
|
3086
|
+
const metadataWrites = /* @__PURE__ */ new Map();
|
|
3087
|
+
let checkpointTimer = null;
|
|
3088
|
+
let asyncFlushTimer = null;
|
|
3089
|
+
let shuttingDown = false;
|
|
3090
|
+
let fatalError = null;
|
|
3091
|
+
const heldCheckpointFiles = /* @__PURE__ */ new Map();
|
|
3092
|
+
const loads = createIndexLoads(hooks.getVectorIndexes);
|
|
3093
|
+
const directoryWatch = createDirectoryWatch(directory, () => shuttingDown || fatalError !== null, markFatal);
|
|
3094
|
+
function stopTimers() {
|
|
3095
|
+
directoryWatch.stop();
|
|
3096
|
+
if (checkpointTimer !== null) clearInterval(checkpointTimer);
|
|
3097
|
+
if (asyncFlushTimer !== null) clearInterval(asyncFlushTimer);
|
|
3098
|
+
checkpointTimer = null;
|
|
3099
|
+
asyncFlushTimer = null;
|
|
3100
|
+
}
|
|
3101
|
+
function markFatal(error) {
|
|
3102
|
+
if (fatalError !== null) return;
|
|
3103
|
+
fatalError = error;
|
|
3104
|
+
stopTimers();
|
|
3105
|
+
hooks.onFatalError(error);
|
|
3106
|
+
}
|
|
3107
|
+
function toError5(err) {
|
|
3108
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
3109
|
+
}
|
|
3110
|
+
async function closeWriterReportingFailure(closePromise) {
|
|
3111
|
+
try {
|
|
3112
|
+
await closePromise;
|
|
3113
|
+
} catch (err) {
|
|
3114
|
+
markFatal(toError5(err));
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
3117
|
+
function queueMetadataWrite(indexName, write) {
|
|
3118
|
+
const previous = metadataWrites.get(indexName) ?? Promise.resolve();
|
|
3119
|
+
const queued = previous.then(write);
|
|
3120
|
+
metadataWrites.set(
|
|
3121
|
+
indexName,
|
|
3122
|
+
queued.catch(() => void 0)
|
|
3123
|
+
);
|
|
3124
|
+
return queued;
|
|
3125
|
+
}
|
|
3126
|
+
function startCheckpointTimer() {
|
|
3127
|
+
if (checkpointTimer !== null || shuttingDown || fatalError !== null || checkpointIntervalMs <= 0) {
|
|
3128
|
+
return;
|
|
3129
|
+
}
|
|
3130
|
+
checkpointTimer = unrefInterval(() => void runScheduledCheckpoints(), checkpointIntervalMs);
|
|
3131
|
+
}
|
|
3132
|
+
function startAsyncFlushTimer() {
|
|
3133
|
+
if (mode !== "async" || asyncFlushTimer !== null || shuttingDown || fatalError !== null || flushIntervalMs <= 0) {
|
|
3134
|
+
return;
|
|
3135
|
+
}
|
|
3136
|
+
asyncFlushTimer = unrefInterval(() => void flushAllPartitions(), flushIntervalMs);
|
|
3137
|
+
}
|
|
3138
|
+
function flushAllPartitions() {
|
|
3139
|
+
return commitEveryPartition(indexes, partitionBatchDeps);
|
|
3140
|
+
}
|
|
3141
|
+
async function runScheduledCheckpoints() {
|
|
3142
|
+
if (fatalError !== null) {
|
|
3143
|
+
return;
|
|
3144
|
+
}
|
|
3145
|
+
for (const indexName of [...indexes.keys()]) {
|
|
3146
|
+
try {
|
|
3147
|
+
await checkpointIndex(indexName);
|
|
3148
|
+
} catch (err) {
|
|
3149
|
+
markFatal(toError5(err));
|
|
3150
|
+
}
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
function buildMutationEntry(record, seqNo) {
|
|
3154
|
+
return buildEntry({
|
|
3155
|
+
seqNo,
|
|
3156
|
+
primaryTerm: SINGLE_NODE_PRIMARY_TERM,
|
|
3157
|
+
operation: record.operation,
|
|
3158
|
+
partitionId: record.partitionId,
|
|
3159
|
+
indexName: record.indexName,
|
|
3160
|
+
documentId: record.documentId,
|
|
3161
|
+
document: record.document
|
|
3162
|
+
});
|
|
3163
|
+
}
|
|
3164
|
+
async function checkpointIndex(indexName, fromMemory = false, afterInFlight = fromMemory) {
|
|
3165
|
+
const indexState = indexes.get(indexName);
|
|
3166
|
+
if (indexState === void 0 || indexState.unloading || heldCheckpoints.holds(indexName, indexState)) return;
|
|
3167
|
+
return checkpointInTurn(indexState, {
|
|
3168
|
+
joinsInFlight: !afterInFlight,
|
|
3169
|
+
mayStart: () => !indexState.unloading && !shuttingDown,
|
|
3170
|
+
perform: () => performCheckpoint(indexName, indexState, fromMemory),
|
|
3171
|
+
afterSettling: () => releaseStalledWrites(indexState)
|
|
3172
|
+
});
|
|
3173
|
+
}
|
|
3174
|
+
async function requestedCheckpoint(indexName, fromMemory = false) {
|
|
3175
|
+
heldCheckpoints.refuse(indexName, indexes.get(indexName));
|
|
3176
|
+
return checkpointIndex(indexName, fromMemory);
|
|
3177
|
+
}
|
|
3178
|
+
async function performCheckpoint(indexName, indexState, fromMemory = false) {
|
|
3179
|
+
await directoryWatch.verify();
|
|
3180
|
+
if (fatalError !== null) throw fatalError;
|
|
3181
|
+
await runDurableCheckpoint({
|
|
3182
|
+
directory,
|
|
3183
|
+
hooks,
|
|
3184
|
+
indexName,
|
|
3185
|
+
indexState,
|
|
3186
|
+
compactionThreshold,
|
|
3187
|
+
canOffload: canOffloadCheckpoint,
|
|
3188
|
+
fromMemory,
|
|
3189
|
+
mutationThreshold: checkpointMutationThreshold,
|
|
3190
|
+
mayContinue: () => !indexState.unloading && !shuttingDown && fatalError === null,
|
|
3191
|
+
holdsCheckpointFiles: () => heldCheckpointFiles.has(indexName),
|
|
3192
|
+
queueMetadataWrite,
|
|
3193
|
+
markFatal,
|
|
3194
|
+
failPartition: partitionFailureFor(indexName, partitionBatchDeps)
|
|
3195
|
+
});
|
|
3196
|
+
}
|
|
3197
|
+
const partitionBatchDeps = {
|
|
3198
|
+
syncEachBatch: mode === "sync",
|
|
3199
|
+
fatalError: () => fatalError,
|
|
3200
|
+
markFatal,
|
|
3201
|
+
failsPartitionAlone: partitionFailures.failsAlone,
|
|
3202
|
+
buildEntry: buildMutationEntry,
|
|
3203
|
+
numberEntry: (record) => record.replicated ?? (entryNumbering === null ? null : entryNumbering(record)),
|
|
3204
|
+
defersCommit: (record) => commitDeferral?.(record) === true
|
|
3205
|
+
};
|
|
3206
|
+
async function recordMutations(records) {
|
|
3207
|
+
if (fatalError !== null) {
|
|
3208
|
+
const error = fatalError;
|
|
3209
|
+
return records.map(() => ({ ok: false, error }));
|
|
3210
|
+
}
|
|
3211
|
+
const outcomes = await recordBatchesByPartition(
|
|
3212
|
+
records,
|
|
3213
|
+
(record) => getOrCreatePartition(record.indexName, record.partitionId, 0),
|
|
3214
|
+
partitionBatchDeps
|
|
3215
|
+
);
|
|
3216
|
+
const recordedByIndex = /* @__PURE__ */ new Map();
|
|
3217
|
+
for (let i = 0; i < records.length; i++) {
|
|
3218
|
+
if (!outcomes[i].ok) continue;
|
|
3219
|
+
const recorded = recordedByIndex.get(records[i].indexName) ?? { records: 0, documentBytes: 0 };
|
|
3220
|
+
recorded.records += 1;
|
|
3221
|
+
recorded.documentBytes += records[i].document?.byteLength ?? 0;
|
|
3222
|
+
recordedByIndex.set(records[i].indexName, recorded);
|
|
3223
|
+
}
|
|
3224
|
+
for (const [indexName, recorded] of recordedByIndex) {
|
|
3225
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
3226
|
+
indexState.mutationsSinceCheckpoint += recorded.records;
|
|
3227
|
+
indexState.documentBytesSinceCheckpoint += recorded.documentBytes;
|
|
3228
|
+
if (checkpointIsDue(indexState, checkpointMutationThreshold)) {
|
|
3229
|
+
void checkpointIndex(indexName).catch((err) => {
|
|
3230
|
+
markFatal(toError5(err));
|
|
3231
|
+
});
|
|
3232
|
+
}
|
|
3233
|
+
await stallWhileCheckpointsFallBehind(indexState);
|
|
3234
|
+
}
|
|
3235
|
+
if (recordedByIndex.size === 0) return outcomes;
|
|
3236
|
+
if (mode === "sync") await directoryWatch.verify();
|
|
3237
|
+
const lost = mode === "sync" ? fatalError : null;
|
|
3238
|
+
if (lost !== null) return outcomes.map((outcome) => outcome.ok ? { ok: false, error: lost } : outcome);
|
|
3239
|
+
await directoryWatch.verifyOnce();
|
|
3240
|
+
directoryWatch.start();
|
|
3241
|
+
startCheckpointTimer();
|
|
3242
|
+
startAsyncFlushTimer();
|
|
3243
|
+
return outcomes;
|
|
3244
|
+
}
|
|
3245
|
+
const recoveryDeps = {
|
|
3246
|
+
directory,
|
|
3247
|
+
hooks,
|
|
3248
|
+
compactionThreshold,
|
|
3249
|
+
registerPartition: (name, partitionId, startSeqNo, primaryTerm) => {
|
|
3250
|
+
getOrCreatePartition(name, partitionId, startSeqNo, primaryTerm);
|
|
3251
|
+
},
|
|
3252
|
+
holdsCheckpointFiles: (name) => heldCheckpointFiles.has(name),
|
|
3253
|
+
queueMetadataWrite
|
|
3254
|
+
};
|
|
3255
|
+
function recoverIndex(indexName, metadataOnly = false, startsGraphRepairs = true) {
|
|
3256
|
+
return loads.track(indexName, recoverPersistedIndex(recoveryDeps, indexName, metadataOnly), startsGraphRepairs);
|
|
3257
|
+
}
|
|
3258
|
+
return {
|
|
3259
|
+
isActive: () => true,
|
|
3260
|
+
async recover(metadataOnly = false) {
|
|
3261
|
+
await directoryWatch.claim();
|
|
3262
|
+
await recoverDirectoryRestores(directory);
|
|
3263
|
+
const names = await listPersistedIndexes(directory);
|
|
3264
|
+
for (const indexName of names) {
|
|
3265
|
+
await recoverIndex(indexName, metadataOnly, false);
|
|
3266
|
+
}
|
|
3267
|
+
await directoryWatch.verify();
|
|
3268
|
+
directoryWatch.start();
|
|
3269
|
+
startCheckpointTimer();
|
|
3270
|
+
for (const indexName of names) loads.startGraphRepairs(indexName);
|
|
3271
|
+
},
|
|
3272
|
+
recoverIndex(indexName) {
|
|
3273
|
+
return recoverIndex(indexName);
|
|
3274
|
+
},
|
|
3275
|
+
highestPersistedSeqNo(indexName, partitionId) {
|
|
3276
|
+
return indexes.get(indexName)?.partitions.get(partitionId)?.appliedSeqNo ?? 0;
|
|
3277
|
+
},
|
|
3278
|
+
partitionRegistry: registry,
|
|
3279
|
+
numberEntriesWith(numbering, defersCommit) {
|
|
3280
|
+
entryNumbering = numbering;
|
|
3281
|
+
commitDeferral = defersCommit ?? null;
|
|
3282
|
+
},
|
|
3283
|
+
isolatePartitionFailures: partitionFailures.isolate,
|
|
3284
|
+
heldCheckpoints: () => heldCheckpoints.report(indexes),
|
|
3285
|
+
async commitPartition(indexName, partitionId) {
|
|
3286
|
+
const partition = indexes.get(indexName)?.partitions.get(partitionId);
|
|
3287
|
+
if (mode !== "sync" || partition === void 0) return;
|
|
3288
|
+
await commitAppended(partition, { indexName, partitionId }, partitionBatchDeps);
|
|
3289
|
+
await directoryWatch.verify();
|
|
3290
|
+
if (fatalError !== null) throw fatalError;
|
|
3291
|
+
},
|
|
3292
|
+
async recordMutation(record) {
|
|
3293
|
+
const [outcome] = await recordMutations([record]);
|
|
3294
|
+
if (!outcome.ok) {
|
|
3295
|
+
throw outcome.error;
|
|
3296
|
+
}
|
|
3297
|
+
return outcome.seqNo;
|
|
3298
|
+
},
|
|
3299
|
+
recordMutations,
|
|
3300
|
+
persistMetadata(indexName) {
|
|
3301
|
+
return queueMetadataWrite(indexName, async () => {
|
|
3302
|
+
const metadata = hooks.buildMetadata(indexName);
|
|
3303
|
+
if (metadata === void 0) {
|
|
3304
|
+
return;
|
|
3305
|
+
}
|
|
3306
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
3307
|
+
await directory.atomicWrite(`${indexName}/meta`, bytes);
|
|
3308
|
+
getOrCreateIndexState(indexName);
|
|
3309
|
+
});
|
|
3310
|
+
},
|
|
3311
|
+
checkpoint(indexName) {
|
|
3312
|
+
return requestedCheckpoint(indexName);
|
|
3313
|
+
},
|
|
3314
|
+
checkpointFromMemory(indexName) {
|
|
3315
|
+
return requestedCheckpoint(indexName, true);
|
|
3316
|
+
},
|
|
3317
|
+
async checkpointAfterInFlight(indexName) {
|
|
3318
|
+
await loads.settled(indexName);
|
|
3319
|
+
await checkpointIndex(indexName, false, true).catch((err) => markFatal(toError5(err)));
|
|
3320
|
+
},
|
|
3321
|
+
async checkpointAll() {
|
|
3322
|
+
for (const indexName of [...indexes.keys()]) {
|
|
3323
|
+
await checkpointIndex(indexName);
|
|
3324
|
+
}
|
|
3325
|
+
},
|
|
3326
|
+
async removeIndex(indexName) {
|
|
3327
|
+
const indexState = indexes.get(indexName);
|
|
3328
|
+
if (indexState !== void 0) {
|
|
3329
|
+
await drainIndexStateForUnload(indexState, metadataWrites.get(indexName), closeWriterReportingFailure);
|
|
3330
|
+
indexes.delete(indexName);
|
|
3331
|
+
metadataWrites.delete(indexName);
|
|
3332
|
+
}
|
|
3333
|
+
for (const vectorIndex of hooks.getVectorIndexes(indexName).values()) await vectorIndex.releaseVectorFiles();
|
|
3334
|
+
for (const key of await directory.list(`${indexName}/`)) {
|
|
3335
|
+
await directory.remove(key);
|
|
3336
|
+
}
|
|
3337
|
+
},
|
|
3338
|
+
async unloadIndex(indexName) {
|
|
3339
|
+
const indexState = indexes.get(indexName);
|
|
3340
|
+
if (indexState === void 0) {
|
|
3341
|
+
return;
|
|
3342
|
+
}
|
|
3343
|
+
await drainIndexStateForUnload(indexState, metadataWrites.get(indexName), (close) => close);
|
|
3344
|
+
indexes.delete(indexName);
|
|
3345
|
+
metadataWrites.delete(indexName);
|
|
3346
|
+
},
|
|
3347
|
+
...createArchiveCheckpointAccess({
|
|
3348
|
+
directory,
|
|
3349
|
+
held: heldCheckpointFiles,
|
|
3350
|
+
checkpoint: (name) => requestedCheckpoint(name),
|
|
3351
|
+
registerPartition: (name, partitionId, startSeqNo, primaryTerm) => getOrCreatePartition(name, partitionId, startSeqNo, primaryTerm),
|
|
3352
|
+
markFatal
|
|
3353
|
+
}),
|
|
3354
|
+
async shutdown() {
|
|
3355
|
+
shuttingDown = true;
|
|
3356
|
+
stopTimers();
|
|
3357
|
+
for (const indexState of indexes.values()) {
|
|
3358
|
+
if (indexState.checkpointInFlight !== null) {
|
|
3359
|
+
await indexState.checkpointInFlight.catch(() => void 0);
|
|
3360
|
+
}
|
|
3361
|
+
for (const partition of indexState.partitions.values()) {
|
|
3362
|
+
await closeWriterReportingFailure(closePartitionWriter(partition));
|
|
3363
|
+
}
|
|
3364
|
+
}
|
|
3365
|
+
indexes.clear();
|
|
3366
|
+
terminateCheckpointWorker();
|
|
3367
|
+
await directoryWatch.release();
|
|
3368
|
+
}
|
|
3369
|
+
};
|
|
3370
|
+
}
|
|
3371
|
+
|
|
3372
|
+
// src/persistence/durability/snapshot-only/checkpoint.ts
|
|
3373
|
+
function capture(input, prior) {
|
|
3374
|
+
const listedIds = new Set((prior?.partitions ?? []).map((partition) => partition.partitionId));
|
|
3375
|
+
const partitionCountChanged = prior !== null && listedIds.size !== input.manager.partitionCount;
|
|
3376
|
+
const targets = [];
|
|
3377
|
+
const contents = [];
|
|
3378
|
+
for (let partitionId = 0; partitionId < input.manager.partitionCount; partitionId += 1) {
|
|
3379
|
+
const lastSeqNo = input.appliedSeqNoByPartition.get(partitionId) ?? 0;
|
|
3380
|
+
const unchanged = !input.rewritesEveryPartition && prior !== null && !partitionCountChanged && listedIds.has(partitionId) && checkpointLastSeqNo(prior.checkpoint, partitionId) === lastSeqNo;
|
|
3381
|
+
if (unchanged) continue;
|
|
3382
|
+
targets.push({ partitionId, lastSeqNo, primaryTerm: SINGLE_NODE_PRIMARY_TERM });
|
|
3383
|
+
contents.push(partitionContentsOf(input.manager, partitionId));
|
|
3384
|
+
}
|
|
3385
|
+
const plans = /* @__PURE__ */ new Map();
|
|
3386
|
+
for (const [fieldPath, vectorIndex] of input.vectorIndexes) {
|
|
3387
|
+
const listed = prior?.vectors.find((ref) => ref.fieldPath === fieldPath);
|
|
3388
|
+
plans.set(fieldPath, vectorIndex.planCheckpoint(listed === void 0 ? null : listed.files.map((file) => file.key)));
|
|
3389
|
+
}
|
|
3390
|
+
return { targets, plans, documentCount: input.manager.countDocuments(), contents };
|
|
3391
|
+
}
|
|
3392
|
+
async function writeCaptured(input, store, prior, captured) {
|
|
3393
|
+
const partitionCount = input.manager.partitionCount;
|
|
3394
|
+
const priorWithinCount = prior === null ? null : {
|
|
3395
|
+
checkpoint: prior.checkpoint.filter((entry) => entry.partitionId < partitionCount),
|
|
3396
|
+
partitions: prior.partitions.filter((partition) => partition.partitionId < partitionCount)
|
|
3397
|
+
};
|
|
3398
|
+
const written = await writeCheckpointSegments({
|
|
3399
|
+
store,
|
|
3400
|
+
metadata: input.metadata,
|
|
3401
|
+
targets: captured.targets,
|
|
3402
|
+
compactionThreshold: DEFAULT_COMPACTION_THRESHOLD,
|
|
3403
|
+
prior: priorWithinCount,
|
|
3404
|
+
wholePartitions: captured.contents
|
|
3405
|
+
});
|
|
3406
|
+
const vectors = await writeLiveVectors({
|
|
3407
|
+
store,
|
|
3408
|
+
indexName: input.indexName,
|
|
3409
|
+
plans: captured.plans,
|
|
3410
|
+
priorVectors: prior?.vectors ?? []
|
|
3411
|
+
});
|
|
3412
|
+
const committed = await commitCheckpointManifest(store, input.metadata, written, vectors.refs);
|
|
3413
|
+
recordWrittenVectors(input.vectorIndexes, vectors.written);
|
|
3414
|
+
await removeCheckpointGarbage(
|
|
3415
|
+
store,
|
|
3416
|
+
await garbageThatNoFieldStillReads(store, input.vectorIndexes, committed.garbage)
|
|
3417
|
+
);
|
|
3418
|
+
}
|
|
3419
|
+
async function writeSnapshotOnlyCheckpoint(input) {
|
|
3420
|
+
const store = adapterCheckpointStore(input.adapter);
|
|
3421
|
+
for (const vectorIndex of input.vectorIndexes.values()) await vectorIndex.completeGraph();
|
|
3422
|
+
const prior = await readCurrentManifest(store, input.indexName);
|
|
3423
|
+
const captured = capture(input, prior);
|
|
3424
|
+
await writeCaptured(input, store, prior, captured);
|
|
3425
|
+
return captured.documentCount;
|
|
3426
|
+
}
|
|
3427
|
+
|
|
3428
|
+
// src/persistence/durability/snapshot-only/restores.ts
|
|
3429
|
+
function toError4(error) {
|
|
3430
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
3431
|
+
}
|
|
3432
|
+
function createPendingRestores(onFatalError) {
|
|
3433
|
+
const pending = /* @__PURE__ */ new Map();
|
|
3434
|
+
return {
|
|
3435
|
+
finishLater(indexName, run) {
|
|
3436
|
+
let running = null;
|
|
3437
|
+
const finish = () => {
|
|
3438
|
+
running ??= run().then(
|
|
3439
|
+
() => {
|
|
3440
|
+
if (pending.get(indexName) === finish) pending.delete(indexName);
|
|
3441
|
+
},
|
|
3442
|
+
(error) => {
|
|
3443
|
+
running = null;
|
|
3444
|
+
throw error;
|
|
3445
|
+
}
|
|
3446
|
+
);
|
|
3447
|
+
return running;
|
|
3448
|
+
};
|
|
3449
|
+
pending.set(indexName, finish);
|
|
3450
|
+
void finish().catch((error) => onFatalError(toError4(error)));
|
|
3451
|
+
return Promise.resolve();
|
|
3452
|
+
},
|
|
3453
|
+
async settle(indexName) {
|
|
3454
|
+
await pending.get(indexName)?.();
|
|
3455
|
+
},
|
|
3456
|
+
forget(indexName) {
|
|
3457
|
+
pending.delete(indexName);
|
|
3458
|
+
},
|
|
3459
|
+
async settleAll() {
|
|
3460
|
+
for (const finish of [...pending.values()]) await finish().catch(() => void 0);
|
|
3461
|
+
}
|
|
3462
|
+
};
|
|
3463
|
+
}
|
|
3464
|
+
|
|
3465
|
+
// src/persistence/durability/snapshot-only/index.ts
|
|
3466
|
+
function metadataKey(indexName) {
|
|
3467
|
+
return `${indexName}/meta`;
|
|
3468
|
+
}
|
|
3469
|
+
function createSnapshotOnlyManager(adapter, config, hooks, publisher) {
|
|
3470
|
+
const checkpointIntervalMs = config.checkpointIntervalMs ?? DEFAULT_CHECKPOINT_INTERVAL_MS;
|
|
3471
|
+
const checkpointMutationThreshold = config.checkpointMutationThreshold ?? DEFAULT_CHECKPOINT_MUTATION_THRESHOLD;
|
|
3472
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
3473
|
+
const metadataWrites = /* @__PURE__ */ new Map();
|
|
3474
|
+
const loads = createIndexLoads(hooks.getVectorIndexes);
|
|
3475
|
+
const restoreStorage = adapterRestoreStorage(adapter);
|
|
3476
|
+
const restores = createPendingRestores(hooks.onFatalError);
|
|
3477
|
+
let checkpointTimer = null;
|
|
3478
|
+
let shuttingDown = false;
|
|
3479
|
+
function settleRestore(indexName) {
|
|
3480
|
+
return restores.settle(indexName);
|
|
3481
|
+
}
|
|
3482
|
+
function getOrCreateIndexState(indexName) {
|
|
3483
|
+
let state = indexes.get(indexName);
|
|
3484
|
+
if (state === void 0) {
|
|
3485
|
+
state = {
|
|
3486
|
+
mutationsSinceCheckpoint: 0,
|
|
3487
|
+
checkpointInFlight: null,
|
|
3488
|
+
applyChain: Promise.resolve(),
|
|
3489
|
+
unloading: false,
|
|
3490
|
+
appliedSeqNoByPartition: /* @__PURE__ */ new Map()
|
|
3491
|
+
};
|
|
3492
|
+
indexes.set(indexName, state);
|
|
3493
|
+
}
|
|
3494
|
+
return state;
|
|
3495
|
+
}
|
|
3496
|
+
function startCheckpointTimer() {
|
|
3497
|
+
if (checkpointTimer !== null || shuttingDown || checkpointIntervalMs <= 0) {
|
|
3498
|
+
return;
|
|
3499
|
+
}
|
|
3500
|
+
checkpointTimer = setInterval(() => {
|
|
3501
|
+
void runScheduledCheckpoints();
|
|
3502
|
+
}, checkpointIntervalMs);
|
|
3503
|
+
if (typeof checkpointTimer.unref === "function") {
|
|
3504
|
+
checkpointTimer.unref();
|
|
3505
|
+
}
|
|
3506
|
+
}
|
|
3507
|
+
async function runScheduledCheckpoints() {
|
|
3508
|
+
for (const indexName of [...indexes.keys()]) {
|
|
3509
|
+
try {
|
|
3510
|
+
await checkpointIndex(indexName);
|
|
3511
|
+
} catch (err) {
|
|
3512
|
+
hooks.onFatalError(err instanceof Error ? err : new Error(String(err)));
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
}
|
|
3516
|
+
async function performCheckpoint(indexName, indexState, fromMemory) {
|
|
3517
|
+
await settleRestore(indexName);
|
|
3518
|
+
const manager = hooks.getManager(indexName);
|
|
3519
|
+
const metadata = hooks.buildMetadata(indexName);
|
|
3520
|
+
if (manager === void 0 || metadata === void 0) {
|
|
3521
|
+
return;
|
|
3522
|
+
}
|
|
3523
|
+
const documentCount = await writeSnapshotOnlyCheckpoint({
|
|
3524
|
+
adapter,
|
|
3525
|
+
indexName,
|
|
3526
|
+
metadata,
|
|
3527
|
+
manager,
|
|
3528
|
+
vectorIndexes: hooks.getVectorIndexes(indexName),
|
|
3529
|
+
appliedSeqNoByPartition: indexState.appliedSeqNoByPartition,
|
|
3530
|
+
rewritesEveryPartition: fromMemory
|
|
3531
|
+
});
|
|
3532
|
+
await queueMetadataWrite(indexName, async () => {
|
|
3533
|
+
const checkpointMetadata = hooks.buildMetadata(indexName, documentCount);
|
|
3534
|
+
if (checkpointMetadata === void 0) return;
|
|
3535
|
+
const metadataBytes = await writeMetadataEnvelope(checkpointMetadata, { checksum: true });
|
|
3536
|
+
await adapter.save(metadataKey(indexName), metadataBytes);
|
|
3537
|
+
hooks.recordCheckpoint?.(indexName, documentCount, manager.partitionCount);
|
|
3538
|
+
});
|
|
3539
|
+
indexState.mutationsSinceCheckpoint = 0;
|
|
3540
|
+
if (publisher !== void 0) {
|
|
3541
|
+
const partitionIds = [];
|
|
3542
|
+
for (let i = 0; i < manager.partitionCount; i += 1) {
|
|
3543
|
+
partitionIds.push(i);
|
|
3544
|
+
}
|
|
3545
|
+
await publisher.publishPartitions(indexName, partitionIds);
|
|
3546
|
+
}
|
|
3547
|
+
}
|
|
3548
|
+
async function checkpointIndex(indexName, fromMemory = false, afterInFlight = fromMemory) {
|
|
3549
|
+
const indexState = indexes.get(indexName);
|
|
3550
|
+
if (indexState === void 0 || indexState.unloading) return;
|
|
3551
|
+
return checkpointInTurn(indexState, {
|
|
3552
|
+
joinsInFlight: !afterInFlight,
|
|
3553
|
+
mayStart: () => !indexState.unloading && !shuttingDown,
|
|
3554
|
+
perform: () => indexState.applyChain.then(() => performCheckpoint(indexName, indexState, fromMemory)),
|
|
3555
|
+
afterSettling: () => void 0
|
|
3556
|
+
});
|
|
3557
|
+
}
|
|
3558
|
+
function queueMetadataWrite(indexName, write) {
|
|
3559
|
+
const previous = metadataWrites.get(indexName) ?? Promise.resolve();
|
|
3560
|
+
const queued = previous.then(() => settleRestore(indexName)).then(write);
|
|
3561
|
+
metadataWrites.set(
|
|
3562
|
+
indexName,
|
|
3563
|
+
queued.catch(() => void 0)
|
|
3564
|
+
);
|
|
3565
|
+
return queued;
|
|
3566
|
+
}
|
|
3567
|
+
function recoverIndex(indexName, metadataOnly = false, startsGraphRepairs = true) {
|
|
3568
|
+
return loads.track(indexName, loadIndex(indexName, metadataOnly), startsGraphRepairs);
|
|
3569
|
+
}
|
|
3570
|
+
async function loadIndex(indexName, metadataOnly) {
|
|
3571
|
+
await settleRestore(indexName);
|
|
3572
|
+
const metaBytes = await adapter.load(metadataKey(indexName));
|
|
3573
|
+
if (metaBytes === null) {
|
|
3574
|
+
return;
|
|
3575
|
+
}
|
|
3576
|
+
const { metadata } = await readMetadataEnvelope(metaBytes);
|
|
3577
|
+
let derivedCount;
|
|
3578
|
+
if (metadataOnly && metadata.documentCount === void 0) {
|
|
3579
|
+
try {
|
|
3580
|
+
derivedCount = await countStoredBundleDocuments(indexName);
|
|
3581
|
+
metadata.documentCount = derivedCount;
|
|
3582
|
+
} catch {
|
|
3583
|
+
derivedCount = void 0;
|
|
3584
|
+
}
|
|
3585
|
+
}
|
|
3586
|
+
await hooks.createIndexFromMetadata(metadata, !metadataOnly);
|
|
3587
|
+
if (metadataOnly) {
|
|
3588
|
+
if (derivedCount !== void 0) {
|
|
3589
|
+
await queueMetadataWrite(indexName, async () => {
|
|
3590
|
+
const upgraded = hooks.buildMetadata(indexName, derivedCount);
|
|
3591
|
+
if (upgraded === void 0) {
|
|
3592
|
+
return;
|
|
3593
|
+
}
|
|
3594
|
+
const bytes = await writeMetadataEnvelope(upgraded, { checksum: true });
|
|
3595
|
+
await adapter.save(metadataKey(indexName), bytes);
|
|
3596
|
+
}).catch(() => void 0);
|
|
3597
|
+
}
|
|
3598
|
+
return;
|
|
3599
|
+
}
|
|
3600
|
+
const manager = hooks.getManager(indexName);
|
|
3601
|
+
const current = hooks.buildMetadata(indexName);
|
|
3602
|
+
if (manager === void 0 || current === void 0) {
|
|
3603
|
+
return;
|
|
3604
|
+
}
|
|
3605
|
+
const recovered = await recoverCheckpoint({
|
|
3606
|
+
store: adapterCheckpointStore(adapter),
|
|
3607
|
+
indexName,
|
|
3608
|
+
metadata: current,
|
|
3609
|
+
manager,
|
|
3610
|
+
vectorIndexes: hooks.getVectorIndexes(indexName),
|
|
3611
|
+
storedTermsAreCurrent: metadata.analysisRevision === manager.language.revision,
|
|
3612
|
+
compactionThreshold: DEFAULT_COMPACTION_THRESHOLD,
|
|
3613
|
+
keepsUnlistedFiles: false,
|
|
3614
|
+
replay: async (checkpoint) => checkpoint,
|
|
3615
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade
|
|
3616
|
+
});
|
|
3617
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
3618
|
+
for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
|
|
3619
|
+
const persistedSeqNo = snapshotCheckpointFor(recovered.checkpoint, partitionId);
|
|
3620
|
+
if (persistedSeqNo > 0) {
|
|
3621
|
+
indexState.appliedSeqNoByPartition.set(partitionId, persistedSeqNo);
|
|
3622
|
+
}
|
|
3623
|
+
}
|
|
3624
|
+
if (recovered.upgradedDocumentCount !== null) {
|
|
3625
|
+
const upgradedCount = recovered.upgradedDocumentCount;
|
|
3626
|
+
await queueMetadataWrite(indexName, async () => {
|
|
3627
|
+
const upgraded = hooks.buildMetadata(indexName, upgradedCount);
|
|
3628
|
+
if (upgraded === void 0) return;
|
|
3629
|
+
await adapter.save(metadataKey(indexName), await writeMetadataEnvelope(upgraded, { checksum: true }));
|
|
3630
|
+
}).catch(() => void 0);
|
|
3631
|
+
}
|
|
3632
|
+
}
|
|
3633
|
+
function countStoredBundleDocuments(indexName) {
|
|
3634
|
+
return countCheckpointDocuments(adapterCheckpointStore(adapter), indexName);
|
|
3635
|
+
}
|
|
3636
|
+
async function recordMutation(record) {
|
|
3637
|
+
const indexState = getOrCreateIndexState(record.indexName);
|
|
3638
|
+
const buffered = indexState.applyChain.then(async () => {
|
|
3639
|
+
await record.apply();
|
|
3640
|
+
const appliedSeqNo2 = (indexState.appliedSeqNoByPartition.get(record.partitionId) ?? 0) + 1;
|
|
3641
|
+
indexState.appliedSeqNoByPartition.set(record.partitionId, appliedSeqNo2);
|
|
3642
|
+
return appliedSeqNo2;
|
|
3643
|
+
});
|
|
3644
|
+
indexState.applyChain = buffered.then(
|
|
3645
|
+
() => void 0,
|
|
3646
|
+
() => void 0
|
|
3647
|
+
);
|
|
3648
|
+
const appliedSeqNo = await buffered;
|
|
3649
|
+
indexState.mutationsSinceCheckpoint += 1;
|
|
3650
|
+
startCheckpointTimer();
|
|
3651
|
+
if (indexState.mutationsSinceCheckpoint >= checkpointMutationThreshold) {
|
|
3652
|
+
void checkpointIndex(record.indexName).catch((err) => {
|
|
3653
|
+
hooks.onFatalError(err instanceof Error ? err : new Error(String(err)));
|
|
3654
|
+
});
|
|
3655
|
+
}
|
|
3656
|
+
return appliedSeqNo;
|
|
3657
|
+
}
|
|
3658
|
+
async function drainIndex(indexName) {
|
|
3659
|
+
const indexState = indexes.get(indexName);
|
|
3660
|
+
if (indexState !== void 0) {
|
|
3661
|
+
indexState.unloading = true;
|
|
3662
|
+
try {
|
|
3663
|
+
await indexState.applyChain;
|
|
3664
|
+
if (indexState.checkpointInFlight !== null) await indexState.checkpointInFlight;
|
|
3665
|
+
await metadataWrites.get(indexName);
|
|
3666
|
+
} catch (error) {
|
|
3667
|
+
indexState.unloading = false;
|
|
3668
|
+
throw error;
|
|
3669
|
+
}
|
|
3670
|
+
}
|
|
3671
|
+
indexes.delete(indexName);
|
|
3672
|
+
metadataWrites.delete(indexName);
|
|
3673
|
+
}
|
|
3674
|
+
return {
|
|
3675
|
+
isActive() {
|
|
3676
|
+
return true;
|
|
3677
|
+
},
|
|
3678
|
+
async recover(metadataOnly = false) {
|
|
3679
|
+
await recoverRestoreJournals(restoreStorage);
|
|
3680
|
+
const names = (await adapter.list("")).filter((key) => key.endsWith("/meta")).map((key) => key.slice(0, -"/meta".length));
|
|
3681
|
+
for (const indexName of names) await recoverIndex(indexName, metadataOnly, false);
|
|
3682
|
+
startCheckpointTimer();
|
|
3683
|
+
for (const indexName of names) loads.startGraphRepairs(indexName);
|
|
3684
|
+
},
|
|
3685
|
+
recoverIndex(indexName) {
|
|
3686
|
+
return recoverIndex(indexName);
|
|
3687
|
+
},
|
|
3688
|
+
highestPersistedSeqNo(indexName, partitionId) {
|
|
3689
|
+
return indexes.get(indexName)?.appliedSeqNoByPartition.get(partitionId) ?? 0;
|
|
3690
|
+
},
|
|
3691
|
+
recordMutation,
|
|
3692
|
+
async recordMutations(records) {
|
|
3693
|
+
const outcomes = [];
|
|
3694
|
+
for (const record of records) {
|
|
3695
|
+
try {
|
|
3696
|
+
outcomes.push({ ok: true, seqNo: await recordMutation(record) });
|
|
3697
|
+
} catch (error) {
|
|
3698
|
+
outcomes.push({ ok: false, error });
|
|
3699
|
+
}
|
|
3700
|
+
}
|
|
3701
|
+
return outcomes;
|
|
3702
|
+
},
|
|
3703
|
+
persistMetadata(indexName) {
|
|
3704
|
+
return queueMetadataWrite(indexName, async () => {
|
|
3705
|
+
const metadata = hooks.buildMetadata(indexName);
|
|
3706
|
+
if (metadata === void 0) {
|
|
3707
|
+
return;
|
|
3708
|
+
}
|
|
3709
|
+
const bytes = await writeMetadataEnvelope(metadata, { checksum: true });
|
|
3710
|
+
await adapter.save(metadataKey(indexName), bytes);
|
|
3711
|
+
getOrCreateIndexState(indexName);
|
|
3712
|
+
});
|
|
3713
|
+
},
|
|
3714
|
+
checkpoint(indexName) {
|
|
3715
|
+
return checkpointIndex(indexName);
|
|
3716
|
+
},
|
|
3717
|
+
checkpointFromMemory(indexName) {
|
|
3718
|
+
return checkpointIndex(indexName, true);
|
|
3719
|
+
},
|
|
3720
|
+
async checkpointAfterInFlight(indexName) {
|
|
3721
|
+
await loads.settled(indexName);
|
|
3722
|
+
await checkpointIndex(indexName, false, true).catch((err) => {
|
|
3723
|
+
hooks.onFatalError(err instanceof Error ? err : new Error(String(err)));
|
|
3724
|
+
});
|
|
3725
|
+
},
|
|
3726
|
+
async checkpointAll() {
|
|
3727
|
+
for (const indexName of [...indexes.keys()]) {
|
|
3728
|
+
await checkpointIndex(indexName);
|
|
3729
|
+
}
|
|
3730
|
+
},
|
|
3731
|
+
stageRestore(indexName) {
|
|
3732
|
+
return createRestoreStaging(restoreStorage, indexName, {
|
|
3733
|
+
finish: (run) => restores.finishLater(indexName, run),
|
|
3734
|
+
onStorageLost: hooks.onFatalError
|
|
3735
|
+
});
|
|
3736
|
+
},
|
|
3737
|
+
adoptRestoredCheckpoint(indexName, checkpoint) {
|
|
3738
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
3739
|
+
indexState.appliedSeqNoByPartition.clear();
|
|
3740
|
+
for (const { partitionId, lastSeqNo } of checkpoint) {
|
|
3741
|
+
if (lastSeqNo > 0) indexState.appliedSeqNoByPartition.set(partitionId, lastSeqNo);
|
|
3742
|
+
}
|
|
3743
|
+
},
|
|
3744
|
+
async removeIndex(indexName) {
|
|
3745
|
+
await settleRestore(indexName).catch(() => void 0);
|
|
3746
|
+
restores.forget(indexName);
|
|
3747
|
+
await restoreStorage.remove(journalKey(indexName));
|
|
3748
|
+
await drainIndex(indexName);
|
|
3749
|
+
for (const key of await adapter.list(`${indexName}/`)) {
|
|
3750
|
+
await adapter.delete(key);
|
|
3751
|
+
}
|
|
3752
|
+
},
|
|
3753
|
+
async unloadIndex(indexName) {
|
|
3754
|
+
await settleRestore(indexName);
|
|
3755
|
+
await drainIndex(indexName);
|
|
3756
|
+
},
|
|
3757
|
+
async reloadIndex(indexName) {
|
|
3758
|
+
const indexState = getOrCreateIndexState(indexName);
|
|
3759
|
+
const reload = indexState.applyChain.then(() => recoverIndex(indexName));
|
|
3760
|
+
indexState.applyChain = reload.catch(() => void 0);
|
|
3761
|
+
await reload;
|
|
3762
|
+
},
|
|
3763
|
+
async shutdown() {
|
|
3764
|
+
shuttingDown = true;
|
|
3765
|
+
if (checkpointTimer !== null) {
|
|
3766
|
+
clearInterval(checkpointTimer);
|
|
3767
|
+
checkpointTimer = null;
|
|
3768
|
+
}
|
|
3769
|
+
await restores.settleAll();
|
|
3770
|
+
for (const indexState of indexes.values()) {
|
|
3771
|
+
if (indexState.checkpointInFlight !== null) {
|
|
3772
|
+
await indexState.checkpointInFlight.catch(() => void 0);
|
|
3773
|
+
}
|
|
3774
|
+
}
|
|
3775
|
+
indexes.clear();
|
|
3776
|
+
}
|
|
3777
|
+
};
|
|
3778
|
+
}
|
|
3779
|
+
|
|
3780
|
+
// src/engine/durability-integration.ts
|
|
3781
|
+
function buildMetadata(indexName, hooks, documentCount) {
|
|
3782
|
+
const config = hooks.getIndexConfig(indexName);
|
|
3783
|
+
if (config === void 0) {
|
|
3784
|
+
return void 0;
|
|
3785
|
+
}
|
|
3786
|
+
return metadataFromConfig(indexName, config, hooks.getManager(indexName)?.partitionCount, documentCount);
|
|
3787
|
+
}
|
|
3788
|
+
function createDurabilityIntegration(tier, hooks) {
|
|
3789
|
+
const managerHooks = {
|
|
3790
|
+
getManager: hooks.getManager,
|
|
3791
|
+
getVectorFieldPaths: hooks.getVectorFieldPaths,
|
|
3792
|
+
getVectorIndexes: hooks.getVectorIndexes,
|
|
3793
|
+
buildMetadata: (indexName, documentCount) => buildMetadata(indexName, hooks, documentCount),
|
|
3794
|
+
createIndexFromMetadata: hooks.createIndexFromMetadata,
|
|
3795
|
+
recordCheckpoint: hooks.recordCheckpoint,
|
|
3796
|
+
onLayoutUpgrade: hooks.onLayoutUpgrade,
|
|
3797
|
+
onCheckpointHeld: hooks.onCheckpointHeld,
|
|
3798
|
+
onFatalError: hooks.onFatalError
|
|
3799
|
+
};
|
|
3800
|
+
const manager = tier.kind === "wal" ? createDurabilityManager(tier.config, managerHooks) : createSnapshotOnlyManager(tier.adapter, tier.config, managerHooks, hooks.checkpointPublisher);
|
|
3801
|
+
function partitionFor(indexName, docId) {
|
|
3802
|
+
const partitionManager = hooks.getManager(indexName);
|
|
3803
|
+
const partitionCount = partitionManager?.partitionCount ?? 1;
|
|
3804
|
+
return fnv1a(docId) % Math.max(1, partitionCount);
|
|
3805
|
+
}
|
|
3806
|
+
async function recordMutation(indexName, docId, operation, document, apply) {
|
|
3807
|
+
const partitionId = partitionFor(indexName, docId);
|
|
3808
|
+
const seqNo = await manager.recordMutation({
|
|
3809
|
+
indexName,
|
|
3810
|
+
partitionId,
|
|
3811
|
+
operation,
|
|
3812
|
+
documentId: docId,
|
|
3813
|
+
document,
|
|
3814
|
+
apply
|
|
3815
|
+
});
|
|
3816
|
+
return { indexName, partitionId, seqNo };
|
|
3817
|
+
}
|
|
3818
|
+
return {
|
|
3819
|
+
manager,
|
|
3820
|
+
recordInsertOrUpdate(indexName, docId, document, apply) {
|
|
3821
|
+
return recordMutation(indexName, docId, "INDEX", encodeDocument(document), apply);
|
|
3822
|
+
},
|
|
3823
|
+
async recordMutationBatch(indexName, mutations) {
|
|
3824
|
+
const records = [];
|
|
3825
|
+
const outcomes = new Array(mutations.length);
|
|
3826
|
+
const recordPositions = [];
|
|
3827
|
+
for (let i = 0; i < mutations.length; i++) {
|
|
3828
|
+
const mutation = mutations[i];
|
|
3829
|
+
try {
|
|
3830
|
+
records.push({
|
|
3831
|
+
indexName,
|
|
3832
|
+
partitionId: partitionFor(indexName, mutation.docId),
|
|
3833
|
+
operation: mutation.document === null ? "DELETE" : "INDEX",
|
|
3834
|
+
documentId: mutation.docId,
|
|
3835
|
+
document: mutation.document === null ? null : encodeDocument(mutation.document),
|
|
3836
|
+
apply: mutation.apply
|
|
3837
|
+
});
|
|
3838
|
+
recordPositions.push(i);
|
|
3839
|
+
} catch (error) {
|
|
3840
|
+
outcomes[i] = { ok: false, error };
|
|
3841
|
+
}
|
|
3842
|
+
}
|
|
3843
|
+
const recorded = await manager.recordMutations(records);
|
|
3844
|
+
for (let i = 0; i < recorded.length; i++) {
|
|
3845
|
+
const outcome = recorded[i];
|
|
3846
|
+
outcomes[recordPositions[i]] = outcome.ok ? { ok: true, write: { indexName, partitionId: records[i].partitionId, seqNo: outcome.seqNo } } : outcome;
|
|
3847
|
+
}
|
|
3848
|
+
return outcomes;
|
|
3849
|
+
},
|
|
3850
|
+
recordRemove(indexName, docId, apply) {
|
|
3851
|
+
return recordMutation(indexName, docId, "DELETE", null, apply);
|
|
3852
|
+
}
|
|
3853
|
+
};
|
|
3854
|
+
}
|
|
3855
|
+
|
|
3856
|
+
// src/engine/durability-wiring.ts
|
|
3857
|
+
function createDurabilityFromTier(tier, wiring) {
|
|
3858
|
+
return createDurabilityIntegration(tier, {
|
|
3859
|
+
checkpointPublisher: { publishPartitions: wiring.publishCheckpointedPartitions },
|
|
3860
|
+
getManager: (indexName) => wiring.indexRegistry.has(indexName) ? wiring.getManager(indexName) : void 0,
|
|
3861
|
+
getVectorFieldPaths: (indexName) => wiring.indexRegistry.get(indexName)?.vectorFieldPaths ?? /* @__PURE__ */ new Set(),
|
|
3862
|
+
getVectorIndexes: (indexName) => (wiring.indexRegistry.has(indexName) ? wiring.getManager(indexName)?.getVectorIndexes() : void 0) ?? /* @__PURE__ */ new Map(),
|
|
3863
|
+
getIndexConfig: (indexName) => {
|
|
3864
|
+
const entry = wiring.indexRegistry.get(indexName);
|
|
3865
|
+
return entry === void 0 ? void 0 : metadataConfigOf(entry);
|
|
3866
|
+
},
|
|
3867
|
+
createIndexFromMetadata: wiring.createIndexFromMetadata,
|
|
3868
|
+
recordCheckpoint: wiring.recordCheckpoint,
|
|
3869
|
+
onLayoutUpgrade: wiring.emitLayoutUpgrade,
|
|
3870
|
+
onCheckpointHeld: wiring.emitCheckpointHeld,
|
|
3871
|
+
onFatalError: wiring.emitFatalError
|
|
3872
|
+
});
|
|
3873
|
+
}
|
|
3874
|
+
|
|
3875
|
+
export { createDurabilityFromTier };
|