sparkforensics-mcp 0.2.0 → 0.2.3
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 +5 -4
- package/vendor-core/cli/collect-run.js +3 -2
- package/vendor-core/cli/native-zstd.js +351 -0
- package/vendor-core/detectors.js +390 -75
- package/vendor-core/docs-config.js +34 -8
- package/vendor-core/docs-content/chapters/03-memory-model.md +39 -0
- package/vendor-core/docs-content/chapters/11-cluster-config.md +40 -0
- package/vendor-core/docs-content/detection/cache.md +4 -3
- package/vendor-core/docs-content/detection/chrn.md +4 -2
- package/vendor-core/docs-content/detection/gc.md +2 -0
- package/vendor-core/docs-content/detection/host.md +2 -1
- package/vendor-core/docs-content/detection/local.md +2 -3
- package/vendor-core/docs-content/detection/mem.md +3 -3
- package/vendor-core/docs-content/detection/plan.md +3 -1
- package/vendor-core/docs-content/detection/shape.md +2 -1
- package/vendor-core/docs-content/detection/shfl.md +2 -1
- package/vendor-core/docs-content/detection/spec.md +4 -3
- package/vendor-core/docs-content/detection/spill.md +1 -1
- package/vendor-core/docs-content/detection/strag.md +2 -1
- package/vendor-core/docs-content/detection/tiny.md +2 -1
- package/vendor-core/docs-content/tuning/failures.md +1 -1
- package/vendor-core/docs-content/tuning/gc.md +11 -4
- package/vendor-core/docs-content/tuning/shuffle.md +25 -5
- package/vendor-core/docs-content/tuning/skew.md +14 -6
- package/vendor-core/docs-content/tuning/small-files.md +12 -7
- package/vendor-core/docs-content/tuning/straggler.md +34 -0
- package/vendor-core/docs-content/tuning/tiny-tasks.md +1 -1
- package/vendor-core/docs-content/tuning/utilization.md +57 -7
- package/vendor-core/docs-content/upstream.json +4 -0
- package/vendor-core/event-handlers.js +321 -69
- package/vendor-core/event-schemas.js +8 -6
- package/vendor-core/evidence-report.js +4 -2
- package/vendor-core/impact-estimator.js +150 -34
- package/vendor-core/mcp-tools.js +20 -7
- package/vendor-core/occupancy.js +70 -2
- package/vendor-core/parser-worker.js +30 -14
- package/vendor-core/plan-summary.js +4 -0
- package/vendor-core/run-comparison.js +22 -17
- package/vendor-core/shs-fetch.js +18 -7
- package/vendor-core/shs-load.js +2 -1
- package/vendor-core/stage-quantiles.js +59 -3
- package/vendor-core/string-hash.js +15 -0
- package/vendor-core/types.js +9 -1
- package/vendor-core/vendor/fzstd.js +94 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sparkforensics-mcp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"mcpName": "io.github.shuffle-works/sparkforensics-mcp",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,13 +31,14 @@
|
|
|
31
31
|
"files": ["bin/", "vendor-core/"],
|
|
32
32
|
"scripts": {
|
|
33
33
|
"prepack": "node ../../scripts/vendor-core.mjs .",
|
|
34
|
-
"test": "vitest run"
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"test:coverage": "vitest run --coverage"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
38
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
38
|
-
"zod": "^4.
|
|
39
|
+
"zod": "^4.6.5"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"vitest": "^
|
|
42
|
+
"vitest": "^5.0.1"
|
|
42
43
|
}
|
|
43
44
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readFileSync, readdirSync, statSync, openSync, readSync, closeSync } from 'node:fs';
|
|
2
2
|
import { join, basename } from 'node:path';
|
|
3
3
|
import { createState, runParse, runParseFiles, reassembleRollingEntries } from '../parser-worker.js';
|
|
4
|
+
import { nodeParseCodecs } from './native-zstd.js';
|
|
4
5
|
import { createModelCallbacks } from '../model-assembler.js';
|
|
5
6
|
import { routeMessage, } from '../ingest.js';
|
|
6
7
|
|
|
@@ -123,9 +124,9 @@ export async function collectRun(inputPath )
|
|
|
123
124
|
const files = ordered.map((name) => nodeFileFromPath(join(inputPath, name)));
|
|
124
125
|
// .catch(reject), not void: a throw past the parser's guards would otherwise leave
|
|
125
126
|
// this Promise pending forever, surfacing only as an unhandled rejection.
|
|
126
|
-
runParseFiles(files, state, { emit }).catch(reject);
|
|
127
|
+
runParseFiles(files, state, { emit, ...nodeParseCodecs }).catch(reject);
|
|
127
128
|
} else {
|
|
128
|
-
runParse(nodeFileFromPath(inputPath), state, { emit }).catch(reject);
|
|
129
|
+
runParse(nodeFileFromPath(inputPath), state, { emit, ...nodeParseCodecs }).catch(reject);
|
|
129
130
|
}
|
|
130
131
|
}, (msg) => new Error((msg ).message));
|
|
131
132
|
}
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
// Node-only zstd decoding for the CLI/MCP parse path: Node's native zlib zstd, one frame at a
|
|
2
|
+
// time, with the vendored fzstd (what the browser uses) as the fallback.
|
|
3
|
+
//
|
|
4
|
+
// Spark writes an event log as thousands of small zstd frames (one per flush: 10217 frames for
|
|
5
|
+
// the largest real log's 3.5 GB). Node's zstdDecompressSync and createZstdDecompress both stop
|
|
6
|
+
// after the first frame (the stream then fails with "Unknown frame descriptor"), so this walks
|
|
7
|
+
// frame boundaries itself and decompresses each complete frame natively: 3-5x faster than fzstd
|
|
8
|
+
// on the real logs (5.6s -> 1.3-1.8s on the largest).
|
|
9
|
+
import * as zlib from 'node:zlib';
|
|
10
|
+
import { Decompress as ZstdDecompress } from '../vendor/fzstd.js';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
// zstdDecompressSync materializes a whole frame, so a frame declaring more content than this, or
|
|
16
|
+
// still incomplete after this many compressed bytes, is streamed through fzstd instead: a log
|
|
17
|
+
// compressed as a single frame (the zstd CLI's default) must never be held whole in memory. A
|
|
18
|
+
// frame that declares no size (every frame Spark writes, and `zstd < log > log.zst`) is decoded
|
|
19
|
+
// with its output capped at this: past it, it streams too (decompressBounded).
|
|
20
|
+
const MAX_NATIVE_FRAME_BYTES = 64 * 1024 * 1024;
|
|
21
|
+
|
|
22
|
+
const ZSTD_MAGIC = 0xfd2fb528;
|
|
23
|
+
const SKIPPABLE_MAGIC = 0x184d2a50;
|
|
24
|
+
|
|
25
|
+
const INCOMPLETE = -1;
|
|
26
|
+
const NOT_NATIVE = -2;
|
|
27
|
+
|
|
28
|
+
function readUint32LE(buf , at ) {
|
|
29
|
+
return (buf[at] | (buf[at + 1] << 8) | (buf[at + 2] << 16) | (buf[at + 3] << 24)) >>> 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Where a walk of an incomplete frame stopped, relative to the frame's first byte: `next` is the
|
|
33
|
+
// next block header (or the checksum once `blocksDone`), -1 while the frame header is incomplete.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const freshWalk = () => ({ next: -1, checksum: false, blocksDone: false });
|
|
37
|
+
|
|
38
|
+
// End offset of the zstd or skippable frame starting at `off` (RFC 8878), INCOMPLETE when `buf`
|
|
39
|
+
// ends before it does, or NOT_NATIVE for a frame this path won't decode itself: oversized, or
|
|
40
|
+
// not a well-formed frame (fzstd then reports the corruption exactly as the browser would). An
|
|
41
|
+
// INCOMPLETE walk leaves its place in `walk`, so the next call on more of the frame resumes there.
|
|
42
|
+
function frameEnd(buf , off , maxFrameBytes , walk = freshWalk()) {
|
|
43
|
+
let p ;
|
|
44
|
+
let hasChecksum ;
|
|
45
|
+
if (walk.next >= 0) {
|
|
46
|
+
p = off + walk.next;
|
|
47
|
+
hasChecksum = walk.checksum;
|
|
48
|
+
} else {
|
|
49
|
+
if (off + 4 > buf.length) return INCOMPLETE;
|
|
50
|
+
const magic = readUint32LE(buf, off);
|
|
51
|
+
if ((magic & 0xfffffff0) === SKIPPABLE_MAGIC) {
|
|
52
|
+
if (off + 8 > buf.length) return INCOMPLETE;
|
|
53
|
+
const end = off + 8 + readUint32LE(buf, off + 4);
|
|
54
|
+
return end <= buf.length ? end : INCOMPLETE;
|
|
55
|
+
}
|
|
56
|
+
if (magic !== ZSTD_MAGIC) return NOT_NATIVE;
|
|
57
|
+
p = off + 4;
|
|
58
|
+
if (p >= buf.length) return INCOMPLETE;
|
|
59
|
+
const descriptor = buf[p++];
|
|
60
|
+
const contentSizeFlag = descriptor >> 6;
|
|
61
|
+
const singleSegment = (descriptor >> 5) & 1;
|
|
62
|
+
hasChecksum = ((descriptor >> 2) & 1) === 1;
|
|
63
|
+
p += (singleSegment ? 0 : 1) + [0, 1, 2, 4][descriptor & 3];
|
|
64
|
+
const contentSizeBytes = contentSizeFlag === 0 ? singleSegment : [0, 2, 4, 8][contentSizeFlag];
|
|
65
|
+
if (p + contentSizeBytes > buf.length) return INCOMPLETE;
|
|
66
|
+
const contentSize =
|
|
67
|
+
contentSizeBytes === 1 ? buf[p]
|
|
68
|
+
: contentSizeBytes === 2 ? (buf[p] | (buf[p + 1] << 8)) + 256
|
|
69
|
+
: contentSizeBytes === 4 ? readUint32LE(buf, p)
|
|
70
|
+
: contentSizeBytes === 8 ? readUint32LE(buf, p) + readUint32LE(buf, p + 4) * 2 ** 32
|
|
71
|
+
: 0;
|
|
72
|
+
if (contentSize > maxFrameBytes) return NOT_NATIVE;
|
|
73
|
+
p += contentSizeBytes;
|
|
74
|
+
walk.checksum = hasChecksum;
|
|
75
|
+
}
|
|
76
|
+
while (!walk.blocksDone) {
|
|
77
|
+
walk.next = p - off;
|
|
78
|
+
if (p + 3 > buf.length) return INCOMPLETE;
|
|
79
|
+
const header = buf[p] | (buf[p + 1] << 8) | (buf[p + 2] << 16);
|
|
80
|
+
p += 3;
|
|
81
|
+
const blockType = (header >> 1) & 3;
|
|
82
|
+
if (blockType === 3) return NOT_NATIVE; // reserved: corrupt data
|
|
83
|
+
p += blockType === 1 ? 1 : header >> 3; // an RLE block stores its one repeated byte
|
|
84
|
+
if (p > buf.length) return INCOMPLETE;
|
|
85
|
+
if (header & 1) { // last block
|
|
86
|
+
walk.blocksDone = true;
|
|
87
|
+
walk.next = p - off;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
p += hasChecksum ? 4 : 0;
|
|
91
|
+
return p <= buf.length ? p : INCOMPLETE;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const nativeZstdAvailable =
|
|
95
|
+
typeof zlib.zstdDecompressSync === 'function' && typeof zlib.createZstdDecompress === 'function';
|
|
96
|
+
|
|
97
|
+
// One step of FrameSplitter.split: a whole frame (`data` false for a skippable one), or the bytes
|
|
98
|
+
// from a frame this path won't decode natively to the end of the push, which fzstd then takes.
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
// Splits a zstd byte stream into whole frames as it arrives, for both decoders below. A frame
|
|
102
|
+
// falls back to fzstd when frameEnd says NOT_NATIVE, when it is still incomplete after
|
|
103
|
+
// `maxFrameBytes` compressed bytes, or when a final push ends inside it. A frame that arrives
|
|
104
|
+
// over many pushes is gathered into one buffer that grows by doubling, and its walk resumes where
|
|
105
|
+
// the last push left it: copying the gathered bytes into a new buffer and walking the frame from
|
|
106
|
+
// its start on every push cost about 4 GB of copying near the 64 MB limit.
|
|
107
|
+
class FrameSplitter {
|
|
108
|
+
// The incomplete frame's bytes so far, gathered[0, gatheredLength); null when none is pending.
|
|
109
|
+
gathered = null;
|
|
110
|
+
gatheredLength = 0;
|
|
111
|
+
walk = freshWalk();
|
|
112
|
+
maxFrameBytes ;
|
|
113
|
+
// The current push's bytes after the last frame split() yielded, for rest().
|
|
114
|
+
current = new Uint8Array(0);
|
|
115
|
+
restAt = 0;
|
|
116
|
+
|
|
117
|
+
constructor(maxFrameBytes ) {
|
|
118
|
+
this.maxFrameBytes = maxFrameBytes;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
gather(bytes ) {
|
|
122
|
+
const length = this.gatheredLength + bytes.length;
|
|
123
|
+
if (this.gathered === null || length > this.gathered.length) {
|
|
124
|
+
const grown = new Uint8Array(Math.max(length, (this.gathered?.length ?? 0) * 2));
|
|
125
|
+
if (this.gathered !== null) grown.set(this.gathered.subarray(0, this.gatheredLength));
|
|
126
|
+
this.gathered = grown;
|
|
127
|
+
}
|
|
128
|
+
this.gathered.set(bytes, this.gatheredLength);
|
|
129
|
+
this.gatheredLength = length;
|
|
130
|
+
return this.gathered.subarray(0, length);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Frames and fallback bytes yielded from a gathered buffer stay valid: a completed buffer is
|
|
134
|
+
// dropped, never reused.
|
|
135
|
+
*split(chunk , final ) {
|
|
136
|
+
let off = 0;
|
|
137
|
+
if (this.gathered !== null) {
|
|
138
|
+
const held = this.gather(chunk);
|
|
139
|
+
const end = frameEnd(held, 0, this.maxFrameBytes, this.walk);
|
|
140
|
+
if (end === INCOMPLETE && held.length <= this.maxFrameBytes && !final) return;
|
|
141
|
+
this.gathered = null;
|
|
142
|
+
this.gatheredLength = 0;
|
|
143
|
+
if (end === NOT_NATIVE || end === INCOMPLETE) {
|
|
144
|
+
yield { fallback: held };
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
off = chunk.length - (held.length - end); // the rest of the chunk follows that frame
|
|
148
|
+
this.current = chunk;
|
|
149
|
+
this.restAt = off;
|
|
150
|
+
yield { frame: held.subarray(0, end), data: readUint32LE(held, 0) === ZSTD_MAGIC };
|
|
151
|
+
}
|
|
152
|
+
while (off < chunk.length) {
|
|
153
|
+
this.walk = freshWalk();
|
|
154
|
+
const end = frameEnd(chunk, off, this.maxFrameBytes, this.walk);
|
|
155
|
+
if (end === NOT_NATIVE || (end === INCOMPLETE && chunk.length - off > this.maxFrameBytes)) {
|
|
156
|
+
yield { fallback: chunk.subarray(off) };
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (end === INCOMPLETE) break;
|
|
160
|
+
this.current = chunk;
|
|
161
|
+
this.restAt = end;
|
|
162
|
+
yield { frame: chunk.subarray(off, end), data: readUint32LE(chunk, off) === ZSTD_MAGIC };
|
|
163
|
+
off = end;
|
|
164
|
+
}
|
|
165
|
+
if (off < chunk.length) {
|
|
166
|
+
if (final) yield { fallback: chunk.subarray(off) };
|
|
167
|
+
else this.gather(chunk.subarray(off));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// The current push's bytes after the frame split() last yielded, for a caller that stops there
|
|
172
|
+
// and hands the rest of the stream to fzstd. The splitter is not used after this.
|
|
173
|
+
rest() {
|
|
174
|
+
return this.current.subarray(this.restAt);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// A frame's output, or null when it would pass `maxBytes`: without a declared content size
|
|
179
|
+
// nothing else bounds it, and zstdDecompressSync would build the whole thing as one buffer.
|
|
180
|
+
function decompressBounded(frame , maxBytes ) {
|
|
181
|
+
try {
|
|
182
|
+
return zlib.zstdDecompressSync(frame, { maxOutputLength: maxBytes });
|
|
183
|
+
} catch (err) {
|
|
184
|
+
if ((err ).code === 'ERR_BUFFER_TOO_LARGE') return null;
|
|
185
|
+
throw err;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Same push(chunk, final) contract as fzstd's Decompress. Once a frame falls back, fzstd takes
|
|
190
|
+
// the rest of the stream from that frame's first byte; a truncated last frame goes to fzstd too,
|
|
191
|
+
// so it fails with fzstd's own "unexpected EOF", as it always has. `maxFrameBytes` is for tests.
|
|
192
|
+
export function createNativeZstdDecoder(
|
|
193
|
+
onChunk ,
|
|
194
|
+
{ maxFrameBytes = MAX_NATIVE_FRAME_BYTES } = {},
|
|
195
|
+
) {
|
|
196
|
+
const splitter = new FrameSplitter(maxFrameBytes);
|
|
197
|
+
let fallback = null;
|
|
198
|
+
const toFallback = (bytes , final ) => {
|
|
199
|
+
fallback ??= new (ZstdDecompress )(onChunk);
|
|
200
|
+
fallback.push(bytes, final);
|
|
201
|
+
};
|
|
202
|
+
return {
|
|
203
|
+
push(chunk , final = false) {
|
|
204
|
+
if (fallback) { fallback.push(chunk, final); return; }
|
|
205
|
+
for (const step of splitter.split(chunk, final)) {
|
|
206
|
+
if (step.fallback) { toFallback(step.fallback, final); return; }
|
|
207
|
+
if (!step.data) continue;
|
|
208
|
+
const output = decompressBounded(step.frame, maxFrameBytes);
|
|
209
|
+
if (output) { onChunk(output); continue; }
|
|
210
|
+
// Past the bound: fzstd streams this frame block by block, then the rest of the stream.
|
|
211
|
+
toFallback(step.frame, false);
|
|
212
|
+
toFallback(splitter.rest(), final);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Frames at least this big (compressed) are decompressed on libuv's threadpool, up to
|
|
220
|
+
// MAX_FRAMES_IN_FLIGHT at a time, while the main thread parses the output of earlier ones.
|
|
221
|
+
// Smaller frames cost less to decompress inline than to hand off (p50 is 5.8 KB of output on the
|
|
222
|
+
// largest real log; 16-64 KB thresholds measured the same, 256 KB lost most of the gain). On that
|
|
223
|
+
// log 4 in flight (the pool's default size) parsed in 3.1s at 711 MB peak RSS, 8 in 3.2s at 886 MB
|
|
224
|
+
// and 2 in 3.5s at 607 MB. The pool's output chunk size barely mattered (64 KB to 1 MB).
|
|
225
|
+
const THREADED_MIN_FRAME_BYTES = 64 * 1024;
|
|
226
|
+
const MAX_FRAMES_IN_FLIGHT = 4;
|
|
227
|
+
const THREADED_CHUNK_BYTES = 256 * 1024;
|
|
228
|
+
// Small frames that arrive while an earlier frame is in flight wait in the queue behind it. With
|
|
229
|
+
// no cap, a first version held most of the largest real log's output (RSS 0.6 -> 4.1 GB).
|
|
230
|
+
const MAX_QUEUED_FRAMES = 64;
|
|
231
|
+
|
|
232
|
+
// One frame decompressing on the threadpool. Node's zstd stream ends after one frame, so each
|
|
233
|
+
// frame gets its own. Its output chunks are kept as they are (zstdDecompressSync would copy them
|
|
234
|
+
// into one buffer on the main thread) until deliver() hands them over, in its turn. Up to
|
|
235
|
+
// `maxBufferedBytes` of them wait; then the stream pauses until deliver() drains them, so a frame
|
|
236
|
+
// without a declared size is never held whole.
|
|
237
|
+
class OffThreadFrame {
|
|
238
|
+
chunks = [];
|
|
239
|
+
buffered = 0;
|
|
240
|
+
ended = false;
|
|
241
|
+
error = null;
|
|
242
|
+
wake = null;
|
|
243
|
+
stream ;
|
|
244
|
+
|
|
245
|
+
constructor(frame , maxBufferedBytes ) {
|
|
246
|
+
this.stream = zlib.createZstdDecompress({ chunkSize: THREADED_CHUNK_BYTES });
|
|
247
|
+
this.stream.on('data', (chunk ) => {
|
|
248
|
+
this.chunks.push(chunk);
|
|
249
|
+
this.buffered += chunk.length;
|
|
250
|
+
if (this.buffered >= maxBufferedBytes) this.stream.pause();
|
|
251
|
+
this.signal();
|
|
252
|
+
});
|
|
253
|
+
this.stream.on('end', () => { this.ended = true; this.signal(); });
|
|
254
|
+
// Kept until deliver() reaches it: rethrown there, never an unhandled error before then.
|
|
255
|
+
this.stream.on('error', (err ) => { this.error = err; this.ended = true; this.signal(); });
|
|
256
|
+
this.stream.end(frame);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
signal() {
|
|
260
|
+
const wake = this.wake;
|
|
261
|
+
this.wake = null;
|
|
262
|
+
wake?.();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async deliver(onChunk ) {
|
|
266
|
+
for (;;) {
|
|
267
|
+
const ready = this.chunks;
|
|
268
|
+
this.chunks = [];
|
|
269
|
+
this.buffered = 0;
|
|
270
|
+
for (const chunk of ready) onChunk(chunk);
|
|
271
|
+
if (this.error !== null) throw this.error;
|
|
272
|
+
if (this.ended) return;
|
|
273
|
+
const more = new Promise ((resolve) => { this.wake = resolve; });
|
|
274
|
+
this.stream.resume();
|
|
275
|
+
await more;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// createNativeZstdDecoder's contract, with large frames decompressed in parallel off the main
|
|
281
|
+
// thread: push() resolves once its complete frames are queued (or delivered, when the queue is
|
|
282
|
+
// full), and a final push resolves after every chunk has reached onChunk, in stream order. Each
|
|
283
|
+
// push must be awaited before the next. It keeps references to pushed bytes until their frames
|
|
284
|
+
// are decoded, so callers must not reuse a pushed buffer. Fallback to fzstd, for the same frames
|
|
285
|
+
// as createNativeZstdDecoder, happens only after every queued frame is delivered; a failed frame
|
|
286
|
+
// rejects the push that reaches it. `threadedMinFrameBytes` is for tests. Measured on the 14 real
|
|
287
|
+
// logs: parse 9.4s -> 8.1s; logs with few large frames (12 of 6455 on a 28 MB one) gain nothing.
|
|
288
|
+
export function createThreadedZstdDecoder(
|
|
289
|
+
onChunk ,
|
|
290
|
+
{ maxFrameBytes = MAX_NATIVE_FRAME_BYTES, threadedMinFrameBytes = THREADED_MIN_FRAME_BYTES }
|
|
291
|
+
= {},
|
|
292
|
+
) {
|
|
293
|
+
const splitter = new FrameSplitter(maxFrameBytes);
|
|
294
|
+
let fallback = null;
|
|
295
|
+
// Frames not yet delivered, oldest first: off-thread output, or a small frame still compressed,
|
|
296
|
+
// decompressed inline only when its turn comes so its output is fresh in cache for the parse.
|
|
297
|
+
|
|
298
|
+
const queued = [];
|
|
299
|
+
let threadedQueued = 0;
|
|
300
|
+
// A small frame, inline; one whose output passes maxFrameBytes streams like an off-thread one.
|
|
301
|
+
const deliverInline = async (frame ) => {
|
|
302
|
+
const output = decompressBounded(frame, maxFrameBytes);
|
|
303
|
+
if (output) onChunk(output);
|
|
304
|
+
else await new OffThreadFrame(frame, maxFrameBytes).deliver(onChunk);
|
|
305
|
+
};
|
|
306
|
+
const deliverOldest = async () => {
|
|
307
|
+
const entry = queued.shift() ;
|
|
308
|
+
if (entry.frame) { await deliverInline(entry.frame); return; }
|
|
309
|
+
threadedQueued--;
|
|
310
|
+
await entry.output .deliver(onChunk);
|
|
311
|
+
};
|
|
312
|
+
const enqueue = async (entry ) => {
|
|
313
|
+
queued.push(entry);
|
|
314
|
+
if (entry.output) threadedQueued++;
|
|
315
|
+
while (threadedQueued >= MAX_FRAMES_IN_FLIGHT || queued.length >= MAX_QUEUED_FRAMES) await deliverOldest();
|
|
316
|
+
};
|
|
317
|
+
const deliverAll = async () => {
|
|
318
|
+
while (queued.length > 0) await deliverOldest();
|
|
319
|
+
};
|
|
320
|
+
const toFallback = async (bytes , final ) => {
|
|
321
|
+
await deliverAll();
|
|
322
|
+
fallback ??= new (ZstdDecompress )(onChunk);
|
|
323
|
+
fallback.push(bytes, final);
|
|
324
|
+
};
|
|
325
|
+
return {
|
|
326
|
+
async push(chunk , final = false) {
|
|
327
|
+
if (fallback) { fallback.push(chunk, final); return; }
|
|
328
|
+
for (const step of splitter.split(chunk, final)) {
|
|
329
|
+
if (step.fallback) { await toFallback(step.fallback, final); return; }
|
|
330
|
+
if (!step.data) continue;
|
|
331
|
+
const frame = step.frame;
|
|
332
|
+
if (frame.length >= threadedMinFrameBytes) {
|
|
333
|
+
await enqueue({ output: new OffThreadFrame(frame, maxFrameBytes) });
|
|
334
|
+
} else if (queued.length === 0) {
|
|
335
|
+
await deliverInline(frame);
|
|
336
|
+
} else {
|
|
337
|
+
await enqueue({ frame });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (final) await deliverAll();
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Node's native zstd where this Node has it (22.15+/23.8+); older Nodes keep the vendored fzstd.
|
|
346
|
+
// runParse/runParseFiles (collectRun) take the threaded decoder. decodeShsArchive decodes each
|
|
347
|
+
// archive entry in one synchronous call, so the SHS archive loader takes the inline one.
|
|
348
|
+
export const nodeParseCodecs =
|
|
349
|
+
nativeZstdAvailable ? { zstdDecoder: createThreadedZstdDecoder } : {};
|
|
350
|
+
export const nodeArchiveCodecs =
|
|
351
|
+
nativeZstdAvailable ? { zstdDecoder: createNativeZstdDecoder } : {};
|