Repository navigation
fix(sdk): make consumption-paced decrypt prefetch the default (DSPX-4654) - #1091
Closed
dmihalcik-virtru wants to merge 1 commit into
Closed
dmihalcik-virtru wants to merge 1 commit into
dmihalcik-virtru wants to merge 1 commit into
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 6 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…4654) Bounded segment scheduling was opt-in, selected only when a caller set segmentBatchSize or maxConcurrentSegmentBatches. Everyone else got updateChunkQueue, which fired 500-segment batches three at a time without waiting for the consumer, so a decrypt read and decrypted the whole payload as fast as it could regardless of how fast the output was drained. Decrypting a 64 GiB TDF with the CLI peaked at ~7 GB RSS writing to /dev/null, and grew without bound when the consumer was slower than reads. Route every decrypt through createBoundedSegmentScheduler and derive its parameters from a byte budget (DEFAULT_PREFETCH_BYTE_BUDGET, 128 MiB) rather than a segment count, so peak memory stays flat as the segment size grows: 1 MiB -> 42 x 3 = 126 MiB 16 MiB -> 2 x 3 = 96 MiB 2 MiB -> 21 x 3 = 126 MiB 64 MiB -> 1 x 2 = 128 MiB Concurrency yields before batch size, since overlapping reads are a better use of a tight budget. MAX_SEGMENT_BATCH_SIZE caps the batch at the legacy 500 so a tiny-segment file is not one absurd ranged read. A segment larger than the budget gets a window of one and knowingly blows it; there is no smaller unit. Explicit segmentBatchSize / maxConcurrentSegmentBatches still win, and an unset one keeps its derived value rather than being rescaled from the other. Deletes updateChunkQueue and the LEGACY_* / DEFAULT_BOUND_* constants outright rather than leaving a second path to rot. Behaviour change: the legacy path did `.catch(() => undefined)`, leaving the consumer awaiting a mailbox that never settled. Batch failures now surface through onError and reject the affected chunks -- a clean failure instead of a hang. This does not bound decrypt memory on its own; readStream still materializes one Chunk per segment up front. Signed-off-by: Dave Mihalcik <dmihalcik@virtru.com>
dmihalcik-virtru
force-pushed
the
DSPX-4654-paced-prefetch-on-main
branch
from
October 15, 2026 20:01
471349b to
57281d2
Compare
|
dmihalcik-virtru
marked this pull request as ready for review
October 15, 2026 20:27
dmihalcik-virtru
added a commit
that referenced
this pull request
Oct 16, 2026
…SPX-4654) Prototype alternative to #1091 for comparison. Same defaults and knobs (derivePrefetchWindow, 128 MiB budget, explicit segmentBatchSize / maxConcurrentSegmentBatches win), but the prefetch window is enforced by a ReadableStream's high-water mark instead of a hand-rolled scheduler. prefetchSegmentBatches() returns a ReadableStream whose pull() starts a batch's fetch-and-decrypt and enqueues the pending promise without awaiting it, so the stream keeps pulling until CountQueuingStrategy's high-water mark (maxConcurrentSegmentBatches - 1; the reader holds the batch it is draining) is reached. The output stream's pull() drains one batch a segment at a time. This deletes the Mailbox type, createBoundedSegmentScheduler and its state types, updateChunkQueue, rejectChunks, and the consumed-mailbox swapping: errors propagate through the stream, the queue holds order, and a drained batch is garbage as soon as the reader moves on. Cancelling the output stream now cancels the batch stream, so no further ranged reads are issued. Behaviour change: a segment that fails its integrity check fails its whole batch, so earlier segments in that batch are no longer released to the reader. Signed-off-by: Dave Mihalcik <dmihalcik@virtru.com>
Member
Author
|
Closed in favor of #1092 |
dmihalcik-virtru
added a commit
that referenced
this pull request
Oct 16, 2026
…SPX-4654) Prototype alternative to #1091 for comparison. Same defaults and knobs (derivePrefetchWindow, 128 MiB budget, explicit segmentBatchSize / maxConcurrentSegmentBatches win), but the prefetch window is enforced by a ReadableStream's high-water mark instead of a hand-rolled scheduler. prefetchSegmentBatches() returns a ReadableStream whose pull() starts a batch's fetch-and-decrypt and enqueues the pending promise without awaiting it, so the stream keeps pulling until CountQueuingStrategy's high-water mark (maxConcurrentSegmentBatches - 1; the reader holds the batch it is draining) is reached. The output stream's pull() drains one batch a segment at a time. This deletes the Mailbox type, createBoundedSegmentScheduler and its state types, updateChunkQueue, rejectChunks, and the consumed-mailbox swapping: errors propagate through the stream, the queue holds order, and a drained batch is garbage as soon as the reader moves on. Cancelling the output stream now cancels the batch stream, so no further ranged reads are issued. Behaviour change: a segment that fails its integrity check fails its whole batch, so earlier segments in that batch are no longer released to the reader. Signed-off-by: Dave Mihalcik <dmihalcik@virtru.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Jira: https://virtru.atlassian.net/browse/DSPX-4654 (epic DSPX-4502)
Related PRs: cross-reference table of all related PRs and their tickets
Supersedes #1024. This is the same change, rebased directly onto
mainwithout the stack beneath it (#1063 → #1018 → #1019 → #1020 → #1021 → #1022 → #1023). That stack wasn't needed: the bounded scheduler, its option normalisation andencryptedSegmentSizeDefaultare already onmain. The only thing #1024 took from the stack was a location: it putderivePrefetchWindowinscale-limits.ts, which #1020 creates. Here the helper lives in its ownlib/tdf3/src/utils/prefetch-window.ts, so the stack'sscale-limits.tsis untouched and #1020–#1023 can still rebase on top without conflicts.What
Bounded segment scheduling was opt-in, used only when a caller set
segmentBatchSizeormaxConcurrentSegmentBatches. Everyone else gotupdateChunkQueue, which sent 500-segment batches three at a time without waiting for the consumer.Every decrypt now goes through
createBoundedSegmentScheduler. Its window comes from a 128 MiB byte budget instead of a segment count, so peak memory stays flat as the segment size grows:segmentBatchSize/maxConcurrentSegmentBatchesstill take precedence.updateChunkQueueand theLEGACY_*/DEFAULT_BOUND_*constants are deleted.onErrorrejects every chunk the reader hasn't consumed yet..catch, so the rejected chunks the reader never reaches don't raise unhandled rejections. Before, one failed decrypt could crash a Node process. Awaiting a chunk still throws.encryptedSegmentSizeDefaultthat is present but not a positive integer is now rejected withInvalidFileErrorbefore the KAS rewrap. Missing or0still falls back to 1 MiB.Risk: this changes default decrypt memory and throughput for every caller, on a crypto path. Callers who relied on the old aggressive prefetch can raise
segmentBatchSize/maxConcurrentSegmentBatches.Measurements
These were taken before the review fixes, which only touch error paths.
Node CLI built with this lib plus #1089 (DSPX-5160, positional file reads), decrypting real 64 GiB TDFs (2 MiB segments, 32768 segments) against a live KAS on darwin-arm64. One run per cell.
main(unbounded)/dev/nullmain(unbounded)openssl dgst/dev/null(go / java TDF)openssl dgst(go / java)The FIFO runs take ~66 s because
opensslis the slow consumer. With this PR, memory stays flat while waiting on it.Tests
tsc, ESLint and Prettier are clean.cd lib && npm testpassed in full: web-test-runner 253 passed, karma (Chrome Headless) 586 executed, 6 skipped. I haven't re-run the browser suites since.tests/mocha/unit/prefetch-window.spec.ts(derivePrefetchWindow), plusgetBoundedSegmentSchedulerOptionsand failing-batch cases intdf.spec.ts.npm run lintis clean, and prettier passes on all changed files.Not verified
readStreamstill creates oneChunkper segment up front, so this bounds in-flight payload bytes, not all decrypt bookkeeping.