Skip to content

perf: fix CDC eager mode performance — async chunking and batch progressive streaming [backport #1102] - #1103

Merged
kalbasit merged 1 commit into
release-0.9from
backport-1102
Mar 17, 2026
Merged

kalbasit merged 1 commit into
release-0.9from
backport-1102

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

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)

…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)
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Asynchronous CDC Chunking: Implemented asynchronous CDC chunking to prevent blocking the HTTP response during NAR downloads, improving download speeds.
  • Batch Chunk Queries: Introduced batch queries for retrieving chunks, reducing database round-trips and improving performance for large NARs.
  • Increased Prefetch Buffer Size: Increased the prefetch buffer size to allow more chunk I/O to overlap with data copying, further optimizing performance.
Activity
  • The pull request implements fixes for slow NAR downloads when CDC eager mode was active.
  • It addresses bottlenecks related to CDC blocking the HTTP response and slow progressive chunk streaming.
  • The changes include spawning a background goroutine for asynchronous chunking, adding batch queries for chunk retrieval, and increasing the prefetch buffer size.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant performance improvements for CDC eager mode. The changes address two key bottlenecks:

  1. Asynchronous chunking for the simple download path, which unblocks the HTTP response and allows the client to proceed faster. This is achieved by moving storeNarWithCDC to a background goroutine.
  2. Batch fetching of chunks during progressive streaming, which drastically reduces database round-trips and polling delays. This is implemented by introducing a new GetChunksByNarFileIDFromIndex query.

The changes are well-implemented and align with the description. Overall, this is a great performance enhancement.

@codecov

codecov Bot commented Mar 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.04%. Comparing base (bba3e86) to head (5e82f2c).
⚠️ Report is 1 commits behind head on release-0.9.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalbasit
kalbasit merged commit 7b1bdfa into release-0.9 Mar 17, 2026
10 checks passed
@kalbasit
kalbasit deleted the backport-1102 branch March 17, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant