sliftutils 1.7.123 → 1.7.124
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -40,6 +40,10 @@ const ROLLOVER_BYTES = 5 * 1024 * 1024;
|
|
|
40
40
|
const MEMORY_WATCHDOG_INTERVAL_MS = 60 * 1000;
|
|
41
41
|
const STALE_DELETE_MS = 24 * 60 * 60 * 1000;
|
|
42
42
|
const MAX_INDEX_RELOAD_ATTEMPTS = 3;
|
|
43
|
+
// A bulk file under this is "loose" - still worth rolling up into a bigger one. Half the target file size, because that is exactly where combining stops paying: merging two files that are each over half the target just splits back into two files again, so the file count (and with it the per-file key list every read holds in memory) does not drop. Under half, any two inputs fit in one output, so phase 2 always makes the count go down.
|
|
44
|
+
//
|
|
45
|
+
// NOT the target size itself. runPlannedMerge cuts a chunk BEFORE adding the key that would exceed the target, so every file it writes is under TARGET_FILE_BYTES - testing against the target would classify the entire collection as loose and re-merge all of it, forever.
|
|
46
|
+
const LOOSE_BULK_MAX_BYTES = TARGET_FILE_BYTES / 2;
|
|
43
47
|
const KEY_GROUP_BYTES = 800 * 1024 * 1024;
|
|
44
48
|
const DUP_THRESHOLD = 0.4;
|
|
45
49
|
// Whole-tier dedup short-circuit (start of phase 3): when the combined tier is over this size AND the overall key duplication fraction is over this threshold, fold every combined file in one merge instead of the per-key-group walk (which spaces merges 5 min apart — 16 h for 200 groups).
|
|
@@ -363,16 +367,13 @@ export class BulkDatabaseBase<T extends { key: string }> {
|
|
|
363
367
|
return this.streamBytesOnDisk > bulkDatabase2Timing.streamFoldTriggerBytes;
|
|
364
368
|
}
|
|
365
369
|
|
|
366
|
-
// Stream files nobody can still be appending to,
|
|
367
|
-
// retirable - a fold may delete these the moment it has consumed them, instead of waiting out streamSealAgeMs. Includes our own sealed files and merge-carry output.
|
|
368
|
-
// ownerGone - nobody is left to fold these (a closed tab's leftovers, or a legacy name with no owner stamp), and until one is folded every reader has to decode all of it just to build the index. Worth folding at any size. Our own files are deliberately NOT in here: every merge pass seals ours, so counting them would fold on every tick; the streamFileMaxBytes rollover and the streamFoldTriggerBytes gate cover ours instead.
|
|
370
|
+
// Stream files nobody can still be appending to: our own sealed files, merge-carry output, and files whose owner is gone (a closed tab's leftovers, or a legacy name with no owner stamp). A fold may delete these the moment it has consumed them, instead of waiting out streamSealAgeMs.
|
|
369
371
|
//
|
|
370
372
|
// Foreign owners are probed over the sync channel. In Node there is no channel, so we cannot know - every foreign owner is assumed alive and the streamSealAgeMs rule stands.
|
|
371
373
|
//
|
|
372
374
|
// assumeSealed is for planning: a merge pass broadcasts a seal before it starts, so by the time it merges our current file IS final. The planner passes isSyncSupported() to predict that; anything deciding a real deletion passes false and goes by streamFileName as it actually stands.
|
|
373
|
-
private async findAbandonedStreams(streamFiles: StreamFileInfo[], assumeSealed: boolean): Promise<
|
|
375
|
+
private async findAbandonedStreams(streamFiles: StreamFileInfo[], assumeSealed: boolean): Promise<Set<string>> {
|
|
374
376
|
const retirable = new Set<string>();
|
|
375
|
-
const ownerGone = new Set<string>();
|
|
376
377
|
const hasForeignOwner = streamFiles.some(f => f.ownerId && f.ownerId !== writerId && f.ownerId !== MERGE_OUTPUT_OWNER);
|
|
377
378
|
const live = hasForeignOwner && await queryLiveWriters(this.name, bulkDatabase2Timing.liveWriterProbeMs) || undefined;
|
|
378
379
|
for (const f of streamFiles) {
|
|
@@ -382,12 +383,9 @@ export class BulkDatabaseBase<T extends { key: string }> {
|
|
|
382
383
|
if (assumeSealed || f.fileName !== this.streamFileName) retirable.add(f.fileName);
|
|
383
384
|
continue;
|
|
384
385
|
}
|
|
385
|
-
if (!f.ownerId || live && !live.has(f.ownerId))
|
|
386
|
-
retirable.add(f.fileName);
|
|
387
|
-
ownerGone.add(f.fileName);
|
|
388
|
-
}
|
|
386
|
+
if (!f.ownerId || live && !live.has(f.ownerId)) retirable.add(f.fileName);
|
|
389
387
|
}
|
|
390
|
-
return
|
|
388
|
+
return retirable;
|
|
391
389
|
}
|
|
392
390
|
|
|
393
391
|
private async automaticCompactionAllowed(): Promise<boolean> {
|
|
@@ -858,7 +856,7 @@ export class BulkDatabaseBase<T extends { key: string }> {
|
|
|
858
856
|
const streamReader = ordered.length ? streamReaderFromEntries(ordered, 0).reader : undefined;
|
|
859
857
|
|
|
860
858
|
// An abandoned stream that yielded no entries (zero bytes, or nothing but torn bytes) holds no data and has no writer left. Retire it here: the merge below never lists it as a used source, so the normal retirement path would skip it and it would re-trigger the abandoned-stream fold on every pass. replacedBy is empty because nothing supersedes it - the marker hides it from reads immediately and processMarkers deletes it once aged.
|
|
861
|
-
const
|
|
859
|
+
const retirableStreams = await this.findAbandonedStreams(streamFiles, false);
|
|
862
860
|
const contributingStreams = new Set(streamData.entries.map(e => e.fileName));
|
|
863
861
|
const emptyAbandoned = streamFiles.filter(f => retirableStreams.has(f.fileName) && !contributingStreams.has(f.fileName)).map(f => f.fileName);
|
|
864
862
|
if (emptyAbandoned.length) await writeDeleteMarker(storage, { deleteFiles: emptyAbandoned, replacedBy: [] });
|
|
@@ -966,7 +964,7 @@ export class BulkDatabaseBase<T extends { key: string }> {
|
|
|
966
964
|
return true;
|
|
967
965
|
}
|
|
968
966
|
|
|
969
|
-
// Splits the bulk tier
|
|
967
|
+
// Splits the bulk tier into files still worth rolling up ("loose", under LOOSE_BULK_MAX_BYTES - a single stream fold, or the tail end of an earlier merge) and files that are done growing ("combined", which only phase 3 touches again).
|
|
970
968
|
//
|
|
971
969
|
// A file whose size won't read is reported as combined: phase 2 can't consume one (its reader won't load, so the merge won't retire it), and calling it loose would re-trigger phase 2 on every pass until handleUnreadableFile finally deletes it.
|
|
972
970
|
private async splitBulkTier(bulkFiles: BulkFileInfo[]): Promise<{ loose: BulkFileInfo[]; looseBytes: number; combined: BulkFileInfo[]; sizes: Map<string, number> }> {
|
|
@@ -978,7 +976,7 @@ export class BulkDatabaseBase<T extends { key: string }> {
|
|
|
978
976
|
for (let i = 0; i < bulkFiles.length; i++) {
|
|
979
977
|
const bytes = logicalSizes[i];
|
|
980
978
|
sizes.set(bulkFiles[i].fileName, bytes ?? 0);
|
|
981
|
-
if (bytes === undefined || bytes >=
|
|
979
|
+
if (bytes === undefined || bytes >= LOOSE_BULK_MAX_BYTES) {
|
|
982
980
|
combined.push(bulkFiles[i]);
|
|
983
981
|
continue;
|
|
984
982
|
}
|
|
@@ -1064,28 +1062,26 @@ export class BulkDatabaseBase<T extends { key: string }> {
|
|
|
1064
1062
|
});
|
|
1065
1063
|
|
|
1066
1064
|
// A pass seals before it merges, so by then our own current file is final too - predict that rather than reporting it as still-open.
|
|
1067
|
-
const
|
|
1065
|
+
const retirable = await this.findAbandonedStreams(streamFiles, isSyncSupported());
|
|
1068
1066
|
// Only fold what we can also retire. Folding a stream a live foreign owner may still append to would copy it into bulk without removing it, so the bytes would stay in memory and just get re-folded next pass; that owner rolls its own file over at streamFileMaxBytes instead.
|
|
1069
1067
|
// Aged past streamSealAgeMs counts as retirable too (canDeleteStream's own first rule): no writer appends past the seal age, and this is the only thing that frees the tier in Node, where liveness cannot be probed at all.
|
|
1070
1068
|
const foldable = streamFiles.filter(f => f.ownerId !== MERGE_OUTPUT_OWNER
|
|
1071
1069
|
&& (retirable.has(f.fileName) || time - f.timestamp >= bulkDatabase2Timing.streamSealAgeMs));
|
|
1072
1070
|
// Merge-carry files hold nothing but tombstones and are never a REASON to fold - folding one alone would just rewrite it into another carry file, forever. They ride along whenever something else folds, which collapses however many have piled up into one.
|
|
1073
1071
|
const carry = streamFiles.filter(f => f.ownerId === MERGE_OUTPUT_OWNER);
|
|
1074
|
-
//
|
|
1075
|
-
const abandoned = foldable.filter(f => ownerGone.has(f.fileName) && streamSizes.get(f.fileName));
|
|
1072
|
+
// Size is the only reason to fold. An abandoned stream is NOT one on its own: it is already retirable, so it is counted here and gets swept up the moment the tier is worth folding, and until then it is bounded by this very threshold. Triggering on one would fold on essentially every pass - a browser leaves a file behind whose writer never answers the liveness probe on every reload - which mints a small bulk file each time and pushes the fragmentation into phase 2.
|
|
1076
1073
|
const foldTriggers = [
|
|
1077
1074
|
makeTrigger({ name: "foldableBytes", value: streamBytes(foldable), threshold: bulkDatabase2Timing.streamFoldTriggerBytes, unit: "bytes" }),
|
|
1078
|
-
makeTrigger({ name: "abandonedFiles", value: abandoned.length, threshold: 1, unit: "count" }),
|
|
1079
1075
|
];
|
|
1080
1076
|
steps.push({
|
|
1081
|
-
phase: 1, kind: "streamFold", requires: "
|
|
1077
|
+
phase: 1, kind: "streamFold", requires: "all", triggers: foldTriggers,
|
|
1082
1078
|
// Skipped when the hard limit already folds everything this would have.
|
|
1083
|
-
ready: !hardLimit.met && foldTriggers.
|
|
1079
|
+
ready: !hardLimit.met && foldTriggers.every(t => t.met),
|
|
1084
1080
|
bulkFiles: [], streamFiles: [...foldable, ...carry], bytes: streamBytes([...foldable, ...carry]),
|
|
1085
1081
|
});
|
|
1086
1082
|
|
|
1087
1083
|
// ── Phase 2: loose bulk -> combined bulk ─────────────────────────────────────────────────────
|
|
1088
|
-
// Phase 1 emits one small bulk file per fold. Each is cheap to read (index only) but holds its whole key list in memory and joins into every read, so they have to be rolled up. Merging just the loose ones also dedupes them for free - a rewrite-heavy workload collapses a gigabyte of near-identical folds into almost nothing - and it always terminates
|
|
1084
|
+
// Phase 1 emits one small bulk file per fold. Each is cheap to read (index only) but holds its whole key list in memory and joins into every read, so they have to be rolled up. Merging just the loose ones also dedupes them for free - a rewrite-heavy workload collapses a gigabyte of near-identical folds into almost nothing - and it always terminates: a chunk is only cut once the next key would take it past TARGET_FILE_BYTES, so every output but the last is over half the target and lands in the combined tier, leaving at most one loose file behind.
|
|
1089
1085
|
const { loose, looseBytes, combined, sizes } = await this.splitBulkTier(bulkFiles);
|
|
1090
1086
|
const looseTriggers = [
|
|
1091
1087
|
makeTrigger({ name: "looseBytes", value: looseBytes, threshold: bulkDatabase2Timing.looseBulkTriggerBytes, unit: "bytes" }),
|