Repository navigation
perf: fix CDC eager mode performance — async chunking and batch progressive streaming [backport #1102] - #1103
Conversation
…essive streaming [backport #1102] Two separate bottlenecks caused slow NAR downloads when CDC eager mode (lazy chunking disabled) was active: **Bottleneck 1 — Server A: CDC blocked the HTTP response (Fix 1A)** In the simple download path, storeNarWithCDC was called synchronously after the download completed. The HTTP response stayed open until all CDC chunking finished, forcing the Nix client to wait for the full chunk pipeline before it could verify the NAR hash. Fix: after streamResponseToFile completes and CDC eager mode is active, spawn a background goroutine to run storeNarWithCDC asynchronously (same pattern as the existing CDC compressed path). The onNarFileReady callback closes ds.stored early — as soon as the nar_file DB record is created and before chunking starts — so the HTTP response completes at download+transfer speed. CDC chunking continues in the background. Removed the now-dead CDC eager-mode branch from storeNarFromTempFile. **Bottleneck 2 — Servers B/C: progressive chunk streaming was slow (Fixes 2A/2B)** streamProgressiveChunks polled one chunk at a time via GetChunkByNarFileIDAndIndex with a 200ms sleep between retries. For a large NAR (e.g. 370 chunks), the per-chunk polling overhead added several seconds. The prefetch buffer was also only 2 chunks. Fix 2A: added GetChunksByNarFileIDFromIndex batch query to all three database engines (SQLite, PostgreSQL, MySQL) and replaced the one-at-a- time poll loop with batch queries of up to 256 chunks per round-trip. When multiple chunks are already available (CDC goroutine has flushed a batch), they are fed into the pipeline immediately without any sleep. The 200ms sleep is only incurred when at the frontier (no new chunks yet). Re-added the abort/completion NarFile re-check inside the inner wait loop so fast-fail for aborted chunking is preserved. Fix 2B: increased prefetchBufferSize from 2 to 16, allowing more chunk I/O to overlap with data copying. (cherry picked from commit a64b63f)
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses performance bottlenecks in CDC eager mode during NAR downloads. It introduces asynchronous chunking, batch chunk queries, and an increased prefetch buffer size to improve download speeds and overall performance. These changes optimize the chunk streaming process, particularly for large NAR files, by reducing latency and improving I/O overlap. Highlights
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces significant performance improvements for CDC eager mode. The changes address two key bottlenecks:
- Asynchronous chunking for the simple download path, which unblocks the HTTP response and allows the client to proceed faster. This is achieved by moving
storeNarWithCDCto a background goroutine. - Batch fetching of chunks during progressive streaming, which drastically reduces database round-trips and polling delays. This is implemented by introducing a new
GetChunksByNarFileIDFromIndexquery.
The changes are well-implemented and align with the description. Overall, this is a great performance enhancement.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release-0.9 #1103 +/- ##
===========================================
Coverage 5.04% 5.04%
===========================================
Files 6 6
Lines 436 436
===========================================
Hits 22 22
Misses 410 410
Partials 4 4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Two separate bottlenecks caused slow NAR downloads when CDC eager mode (lazy chunking disabled) was active:
Bottleneck 1 — Server A: CDC blocked the HTTP response (Fix 1A)
In the simple download path, storeNarWithCDC was called synchronously after the download completed. The HTTP response stayed open until all CDC chunking finished, forcing the Nix client to wait for the full chunk pipeline before it could verify the NAR hash.
Fix: after streamResponseToFile completes and CDC eager mode is active, spawn a background goroutine to run storeNarWithCDC asynchronously (same pattern as the existing CDC compressed path). The onNarFileReady callback closes ds.stored early — as soon as the nar_file DB record is created and before chunking starts — so the HTTP response completes at download+transfer speed. CDC chunking continues in the background. Removed the now-dead CDC eager-mode branch from storeNarFromTempFile.
Bottleneck 2 — Servers B/C: progressive chunk streaming was slow (Fixes 2A/2B)
streamProgressiveChunks polled one chunk at a time via GetChunkByNarFileIDAndIndex with a 200ms sleep between retries. For a large NAR (e.g. 370 chunks), the per-chunk polling overhead added several seconds. The prefetch buffer was also only 2 chunks.
Fix 2A: added GetChunksByNarFileIDFromIndex batch query to all three database engines (SQLite, PostgreSQL, MySQL) and replaced the one-at-a- time poll loop with batch queries of up to 256 chunks per round-trip. When multiple chunks are already available (CDC goroutine has flushed a batch), they are fed into the pipeline immediately without any sleep. The 200ms sleep is only incurred when at the frontier (no new chunks yet). Re-added the abort/completion NarFile re-check inside the inner wait loop so fast-fail for aborted chunking is preserved.
Fix 2B: increased prefetchBufferSize from 2 to 16, allowing more chunk I/O to overlap with data copying.
(cherry picked from commit a64b63f)