Repository navigation
feat: implement concurrent NAR streaming - #619
Conversation
Summary of ChangesHello @kalbasit, 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 introduces a significant performance improvement by enabling concurrent streaming of NAR (Nix Archive) files. Previously, clients would experience timeouts when downloading large NARs because the entire file had to be downloaded and stored locally before streaming could begin. The changes refactor the Highlights
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 concurrent NAR streaming, a significant performance improvement for large files. The changes in coordinateDownload are well-structured, correctly using a detached context and a background goroutine to manage the distributed lock lifecycle while allowing the client request to proceed. The new test case TestNarStreaming effectively validates the streaming behavior, and existing tests are updated to accommodate the changes. I have one minor suggestion regarding a small piece of duplicated code for better maintainability, keeping in mind that duplicated code can sometimes be acceptable.
fb3bd2c to
cca276b
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces concurrent NAR streaming, a significant feature to prevent client timeouts on large file downloads. The changes are well-structured, refactoring coordinateDownload to support both streaming (for NARs) and non-streaming (for NarInfo) downloads. The use of a background goroutine to hold the distributed lock for streaming downloads until storage is complete is a clever and effective solution to prevent redundant downloads across instances. The addition of TestNarStreaming provides good coverage for the new functionality. I've found one area for improvement to enhance the robustness of lock handling.
Implement concurrent NAR streaming to clients as soon as the download starts from an upstream cache. Previously, ncps would download the entire NAR into its store before starting to stream it back to the client. For large NAR files, this often led to client timeouts. This change refactors coordinateDownload to allow an early return (after the download starts) for jobs that support streaming, like NAR downloads. It also ensures that the distributed lock is held until the asset is fully stored in the background, preventing other instances from starting a redundant download while allowing the current instance to stream the file being downloaded. Detailed changes: - Refactored coordinateDownload to accept a waitForStorage boolean. - Updated prePullNar to call coordinateDownload with waitForStorage=false. - Updated prePullNarInfo to call coordinateDownload with waitForStorage=true. - Modified distributed lock handling in coordinateDownload to release the lock in a background goroutine when waitForStorage is false. - Updated GetNar to handle the -1 size returned during streaming and correctly stream from the temporary file using io.Pipe. - Fixed a race condition in GetNar where clients could receive EOF before the NAR was fully stored in final storage by waiting for storage completion in the streaming goroutine. - Added TestNarStreaming to verify the fix and ensured it checks for asset presence after completion. - Updated TestConcurrentDownload_CancelOneClient_OthersContinue to handle -1 size during streaming.
Changes Made 1. Error Handling After Storage Wait (pkg/cache/cache.go:860-872) Added proper error checking when waiting for storage completion during streaming: - Added comment for successful storage case - Added error checking for the done case that logs warnings if the download failed - This ensures we properly handle and log any errors that occur during background storage 2. Improved Documentation (pkg/cache/cache.go:2541-2549) Expanded the comment explaining the lock release strategy to be more detailed: - Clearly explains the two different strategies based on waitForStorage - Documents why NarInfo needs immediate release (full completion required) - Documents why NAR uses background release (enables streaming while preventing duplicate downloads) 3. Better Variable Naming (pkg/cache/cache.go:2454, 2527) Renamed waitChan to completionChan in both locations: - More descriptive name that better conveys the purpose - Appears in two places within coordinateDownload function 4. Test Comment Improvement (pkg/cache/cache_test.go:1917-1918) Improved the comment explaining the fake content length: - Changed from inline comment to full line comment - Better explains that the simplified size is intentional for the streaming test 5. Test Timeout Adjustments (pkg/cache/cache_test.go:1986, 1994) Increased timeouts to be more CI-friendly: - Server start timeout: 5s → 10s - Streaming start timeout: 2s → 5s - Reduces likelihood of flaky test failures in CI environments Verification All changes have been verified: - ✅ Linter passes with 0 issues - ✅ TestNarStreaming passes - ✅ All cache package tests pass with race detector enabled The code is now ready with all the high and medium priority suggestions from the review implemented.
aadeb38 to
cb4d0d1
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces concurrent NAR streaming to improve client experience with large files. The changes are well-structured, refactoring coordinateDownload to support both blocking and non-blocking (streaming) modes via a waitForStorage flag. The distributed lock handling is cleverly modified to support this, holding the lock in the background for streaming downloads to prevent redundant work while allowing the client to receive data immediately. A race condition in GetNar is also correctly addressed by waiting for the asset to be fully stored. The addition of TestNarStreaming provides excellent test coverage for the new functionality. The implementation appears robust and correct.
|
Backport failed for Please cherry-pick the changes locally and resolve any conflicts. git fetch origin release-0.7
git worktree add -d .worktree/backport-619-to-release-0.7 origin/release-0.7
cd .worktree/backport-619-to-release-0.7
git switch --create backport-619-to-release-0.7
git cherry-pick -x c6ce7c6f69f81972ba4b1ffc4c2e7b3db716543e |
Implement concurrent NAR streaming to clients as soon as the download starts from an upstream cache. Previously, ncps would download the entire NAR into its store before starting to stream it back to the client. For large NAR files, this often led to client timeouts. This change refactors coordinateDownload to allow an early return (after the download starts) for jobs that support streaming, like NAR downloads. It also ensures that the distributed lock is held until the asset is fully stored in the background, preventing other instances from starting a redundant download while allowing the current instance to stream the file being downloaded. Detailed changes: - Refactored coordinateDownload to accept a waitForStorage boolean. - Updated prePullNar to call coordinateDownload with waitForStorage=false. - Updated prePullNarInfo to call coordinateDownload with waitForStorage=true. - Modified distributed lock handling in coordinateDownload to release the lock in a background goroutine when waitForStorage is false. - Updated GetNar to handle the -1 size returned during streaming and correctly stream from the temporary file using io.Pipe. - Fixed a race condition in GetNar where clients could receive EOF before the NAR was fully stored in final storage by waiting for storage completion in the streaming goroutine. - Added TestNarStreaming to verify the fix and ensured it checks for asset presence after completion. - Updated TestConcurrentDownload_CancelOneClient_OthersContinue to handle -1 size during streaming. fixes #618 (cherry picked from commit c6ce7c6)
Implement concurrent NAR streaming to clients as soon as the download starts from an upstream cache. Previously, ncps would download the entire NAR into its store before starting to stream it back to the client. For large NAR files, this often led to client timeouts. This change refactors coordinateDownload to allow an early return (after the download starts) for jobs that support streaming, like NAR downloads. It also ensures that the distributed lock is held until the asset is fully stored in the background, preventing other instances from starting a redundant download while allowing the current instance to stream the file being downloaded. Detailed changes: - Refactored coordinateDownload to accept a waitForStorage boolean. - Updated prePullNar to call coordinateDownload with waitForStorage=false. - Updated prePullNarInfo to call coordinateDownload with waitForStorage=true. - Modified distributed lock handling in coordinateDownload to release the lock in a background goroutine when waitForStorage is false. - Updated GetNar to handle the -1 size returned during streaming and correctly stream from the temporary file using io.Pipe. - Fixed a race condition in GetNar where clients could receive EOF before the NAR was fully stored in final storage by waiting for storage completion in the streaming goroutine. - Added TestNarStreaming to verify the fix and ensured it checks for asset presence after completion. - Updated TestConcurrentDownload_CancelOneClient_OthersContinue to handle -1 size during streaming. fixes #618 (cherry picked from commit c6ce7c6)

Implement concurrent NAR streaming to clients as soon as the download starts
from an upstream cache. Previously, ncps would download the entire NAR into its
store before starting to stream it back to the client. For large NAR files,
this often led to client timeouts.
This change refactors coordinateDownload to allow an early return (after the
download starts) for jobs that support streaming, like NAR downloads. It also
ensures that the distributed lock is held until the asset is fully stored in
the background, preventing other instances from starting a redundant download
while allowing the current instance to stream the file being downloaded.
Detailed changes:
in a background goroutine when waitForStorage is false.
stream from the temporary file using io.Pipe.
NAR was fully stored in final storage by waiting for storage completion in the
streaming goroutine.
presence after completion.
size during streaming.
fixes #618