Repository navigation
fix(cache): serve a zstd request by recompressing CDC chunks - #1394
Conversation
A client whose narinfo advertises Compression:zstd requests
/nar/<hash>.nar.zst, but under steady-state CDC the NAR is stored only
as uncompressed chunks. serveNarFromStorageViaPipe returned 404 ('only
available as chunks, cannot serve as zstd'), aborting the build with
'does not exist in binary cache'. This is the inverse direction of the
narinfo<->nar_file compression desync (issue #1392) and the
counterpart to the merged uncompressed-serve fix (#1393); zstd is the
common upstream compression (cachix), so it blocks CDC-on caches.
Recompress on the fly: reassemble the uncompressed chunks via
getNarFromChunks and stream them through zstd.NewPooledWriter into an
io.Pipe (mirroring the existing none->zstd store path), serving a
zstd-labeled response. Scoped to zstd, the one streaming compressor
ncps vendors; xz (no compressor) keeps the 404/fallback and is handled
by the write-path-invariant and data-repair follow-ups.
Refs #1392
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
This change is part of the following stack: Change managed by git-spice. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughSummary by CodeRabbitRelease Notes
WalkthroughImplements on-the-fly zstd recompression for NAR serving: when a request advertises ChangesNAR Zstd Recompression via On-Demand Streaming
Sequence DiagramsequenceDiagram
participant Client
participant serveNarFromStorageViaPipe
participant serveZstdFromChunks
participant getNarFromChunks
participant ChunkStorage
participant ZstdWriter
Client->>serveNarFromStorageViaPipe: Request Compression: zstd
serveNarFromStorageViaPipe->>serveNarFromStorageViaPipe: Check if serving from chunks
serveNarFromStorageViaPipe->>serveZstdFromChunks: Route zstd request
serveZstdFromChunks->>getNarFromChunks: Read uncompressed chunk bytes
getNarFromChunks->>ChunkStorage: Fetch CDC chunks
ChunkStorage-->>getNarFromChunks: Uncompressed NAR bytes
getNarFromChunks-->>serveZstdFromChunks: Stream uncompressed data
serveZstdFromChunks->>ZstdWriter: Pipe bytes through pooled writer
ZstdWriter-->>serveZstdFromChunks: Zstd-compressed stream
serveZstdFromChunks-->>Client: Return zstd stream with size=-1
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Comment |
There was a problem hiding this comment.
Code Review
This pull request implements on-the-fly zstd recompression when serving NARs that are only available as uncompressed CDC chunks, preventing 404 errors for clients requesting zstd-compressed NARs. The feedback suggests using a defer block in the recompression goroutine to ensure that both the zstd writer and the raw reader are safely closed, preventing resource leaks in case of panics or early exits.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Move encoder/reader/pipe teardown into a single deferred block so an unexpected early exit or panic in io.Copy still returns the pooled zstd encoder, closes the chunk reader, and closes the pipe (preventing a consumer hang). PooledWriter.Close is idempotent. Addresses PR #1394 review thread PRRT_kwDONV6tZs6Ip9A-. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Good catch — fixed in the latest push. Adopted the defer-based teardown, with one improvement over the suggestion: |
The recompressed zstd does not byte-match the origin, so its FileHash/FileSize differ from the client's narinfo. Document why Nix tolerates this: the signature fingerprint excludes FileHash/FileSize/Compression/URL, narFromPath never verifies FileHash (producer-only) and uses FileSize only for progress, and integrity is the signed NarHash verified after decompression (our stream decompresses to the identical NAR). Pre-empts the recurring 'won't this break the client's recorded FileHash?' question. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…1395) ## Summary Stacked on #1394. After the serve-side fixes (#1393, #1394) ncps tolerates most narinfo↔nar_file compression drift at serve time by transcoding. The one case it still cannot serve is a narinfo advertising a compression ncps has no compressor for (`xz`) whose backing NAR is stored otherwise (none/zstd/chunks) — ~59 such pairs on the live cache. These pre-existing drifted rows cannot be healed at request time, so they need a one-shot data repair. Add `repairNarInfoCompressionDesync`, invoked from `repairFsckIssues` under `--repair`: find narinfos advertising `xz` with no matching `xz` nar_file but a backing non-xz nar_file, and rewrite each to the servable `none` form (`URL: nar/<hash>.nar`, `Compression: none`, `FileHash`/`FileSize` cleared), reusing the advertised hash so the resolve-by-hash serve path (#1393) decompresses the stored bytes. Healthy narinfos are untouched; the repair is idempotent. ## Test plan - [x] New `TestRepairNarInfoCompressionDesync`: a desynced xz-advertised/none-stored narinfo is rewritten to none; a healthy xz/xz pair is untouched; a second run is a no-op. - [x] `task fmt`/`lint` green; targeted fsck tests pass. Refs #1392 --------- Co-authored-by: Claude Opus 4.8 <[email protected]>
…1396) ## Summary Stacked on #1394 / the data-repair PR. A live cache accumulated ~5.5M orphaned chunk rows and ~8164 `nar_file_chunks` links while **zero** nar_files have `total_chunks > 0`. fsck's orphaned-chunk reclamation only deletes chunks with NO link, so chunks held by a stale link to a dechunked (`total_chunks = 0`) nar_file are never orphaned and never reclaimed; `collectNarFilesWithChunkIssues` only inspects `total_chunks > 0` rows, so the stale links escape there too. The residue grows unbounded. Add `reclaimStaleChunkLinks`, invoked from `repairFsckIssues` under `--repair` + CDC mode (which #1364's chunk-residue signal already enables post-drain): delete `nar_file_chunks` whose parent nar_file is dechunked and not actively chunking, then reclaim the chunks left with no remaining links (DB rows + chunk-store blobs). Chunks shared with a still-chunked nar_file keep a live link and are retained; chunked NARs are untouched; idempotent. ## Test plan - [x] New `TestReclaimStaleChunkLinks`: stale link + its chunk reclaimed; shared chunk and chunked NAR retained; idempotent. - [x] `task fmt`/`lint` green; targeted fsck tests pass. Refs #1392 --------- Co-authored-by: Claude Opus 4.8 <[email protected]>
Summary
Inverse direction of the merged uncompressed-serve fix (#1393) and the steady-state-CDC manifestation of the narinfo↔nar_file compression desync (#1392). A client whose narinfo advertises
Compression: zstdrequests/nar/<hash>.nar.zst, but under CDC the NAR is stored only as uncompressed chunks.serveNarFromStorageViaPipereturned404("only available as chunks, cannot serve as zstd"), aborting the build withdoes not exist in binary cache. zstd is the common upstream compression (cachix), so this blocks CDC-on caches.Recompress on the fly: reassemble the uncompressed chunks via
getNarFromChunksand stream them throughzstd.NewPooledWriterinto anio.Pipe(mirroring the existing none→zstd store path), serving azstd-labeled response. Scoped to zstd, the one streaming compressor ncps vendors;xz(no compressor) keeps the 404/fallback and is handled by the data-repair PR stacked on top.Test plan
TestServeZstdRequestFromChunks(RED→GREEN): chunk a NAR under CDC, request it aszstd, assert the served stream decompresses to the original (was 404).TestGetNar*/ CDC serve tests pass;task fmt/lint/testgreen.Refs #1392