Skip to content

fix(cache): serve whole-file NAR with its true compression under CDC - #1346

Merged
kalbasit merged 3 commits into
mainfrom
user/wnasreddine/fix-cdc-incomplete-nar-serve
Jun 7, 2026
Merged

kalbasit merged 3 commits into
mainfrom
user/wnasreddine/fix-cdc-incomplete-nar-serve

Conversation

@kalbasit

@kalbasit kalbasit commented Jun 7, 2026

Copy link
Copy Markdown
Owner

Summary

getNarFromStore streams the raw bytes of the whole file it opened (e.g. xz), but then overwrote narURL.Compression with the compression of the record returned by getNarFileFromDB, which is CDC-first and returns the coexisting chunked compression=none row during the lazy-chunking transition window. The response then advertised Compression: none while the body was raw xz, with Content-Length set to the xz size (< NarSize), so nix failed with:

  • error: NAR for '…' fetched from … is incomplete
  • error: input compression not recognized

This deterministically broke the enable-CDC-on-an-existing-whole-file-cache upgrade path. It is the NAR-serve counterpart to the narinfo-side normalization already done in maybeCDCNormalizeNarInfoURL (#1332).

Fix

Query the nar_file row for the representation actually being served (hash + served compression + query) instead of the CDC-first lookup, and stop overwriting narURL.Compression. The served compression now matches the streamed bytes; the last_accessed_at touch targets the served row.

Adds a pkg/cache regression test (TestCDCWholeFileServeReportsServedCompression) and the OpenSpec change fix-cdc-incomplete-nar-serve (adds a cdc-chunking requirement: GetNar must advertise the compression of the bytes it serves).

Test plan

  • New regression test fails before the fix (returns none, short length) and passes after
  • task fmt, task lint, full task test green
  • Caught + then validated end-to-end by the new CDC lifecycle e2e driver (top of stack)

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working go Pull requests that update go code labels Jun 7, 2026
@coderabbitai

coderabbitai Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 14b74472-636f-45b4-b953-de48c25639f8

📥 Commits

Reviewing files that changed from the base of the PR and between 03c55ce and 7c713f8.

📒 Files selected for processing (8)
  • openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/.openspec.yaml
  • openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/design.md
  • openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/proposal.md
  • openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/specs/cdc-chunking/spec.md
  • openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/tasks.md
  • openspec/specs/cdc-chunking/spec.md
  • pkg/cache/cache.go
  • pkg/cache/cdc_whole_file_serve_internal_test.go
✅ Files skipped from review due to trivial changes (3)
  • openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/specs/cdc-chunking/spec.md
  • openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/.openspec.yaml
  • pkg/cache/cache.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • openspec/specs/cdc-chunking/spec.md

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Fixed compression-label mismatches when serving archives during CDC/lazy-chunking transitions so responses report the actual encoding and served byte count.
  • Documentation

    • Added design, proposal, spec, and task docs clarifying compression-reporting requirements and upgrade behavior for CDC + chunking.
  • Tests

    • Added a regression test that validates correct compression reporting and complete serving during chunking coexistence.

Walkthrough

Fixes a bug where getNarFromStore could relabel a served whole-file NAR’s compression from a CDC-first DB row during lazy-chunking. The change queries the nar_file matching the served representation, stops mutating narURL.Compression from mismatched rows, updates LRU touch behavior, and adds a regression test for xz-vs-none coexistence.

Changes

CDC Whole-File NAR Compression Correctness

Layer / File(s) Summary
Specification and Requirements
openspec/specs/cdc-chunking/spec.md, openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/proposal.md, openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/specs/cdc-chunking/spec.md, openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/design.md, openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/tasks.md, openspec/changes/archive/2026-06-06-fix-cdc-incomplete-nar-serve/.openspec.yaml
Spec and archived docs require GetNar to advertise the compression of the actual bytes it streams and describe the CDC-first relabeling bug and test plan.
getNarFromStore Compression Fix
pkg/cache/cache.go
Query nar_file by exact served representation (hash + narURL.Compression + query) instead of using a CDC-first fallback; do not overwrite narURL.Compression from mismatched DB rows; apply LRU touch to the served representation row.
CDC Lazy-Chunking Regression Test
pkg/cache/cdc_whole_file_serve_internal_test.go
Adds an xz compression helper and TestCDCWholeFileServeReportsServedCompression that reproduces xz whole-file + chunked-none coexistence, serves the xz row, asserts returned compression is xz, validates streamed byte counts, and verifies decompressed size.

Sequence Diagram

sequenceDiagram
  participant Client
  participant GetNar
  participant WholeFileStore
  participant CDC_Database
  Client->>GetNar: Request NAR (narURL)
  GetNar->>WholeFileStore: Open bytes for requested representation (compression=xz/none)
  GetNar->>CDC_Database: (may) query nar_file rows
  Note over GetNar: BEFORE: used CDC-first row compression to relabel response\nAFTER: use compression of opened bytes
  GetNar->>Client: Respond with compression matching opened bytes + streamed bytes
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • kalbasit/ncps#1206: Related changes to pkg/cache cache.go nar_file lookup and touch logic affecting compression selection during CDC/lazy-chunking.
  • kalbasit/ncps#1255: Adjusts CDC lazy-chunking behavior to guard GetNar routing and compression normalization for chunked vs whole-file selection.
  • kalbasit/ncps#1332: Addresses url=none/xz compression desynchronization with complementary fixes and migrations.

Suggested labels

merge-stack

Poem

🐰
I zipped a file in midnight's hush,
CDC rows made labels blush.
We ask the bytes what name they wear,
Return true tags with tender care.
Now streams and headers hop in sync.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main fix: serving whole-file NARs with their true compression under CDC, which directly addresses the core issue documented in the changeset.
Description check ✅ Passed The description clearly explains the problem, fix, and validation approach, directly relating to the changes in the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

@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 fixes a bug where serving a pre-existing whole-file xz NAR during the lazy-chunking transition window mislabels the response compression as 'none' due to a CDC-first database lookup. The fix ensures that the whole-file serve path in getNarFromStore queries the database for the exact representation being served and removes the incorrect compression overwrite. A comprehensive regression test has been added to verify this behavior. The review feedback suggests a minor optimization in the new test file to handle compressed xz bytes as []byte instead of string to avoid unnecessary allocations and conversions.

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.

kalbasit and others added 3 commits June 7, 2026 00:05
getNarFromStore streams the raw bytes of the whole file it opened (e.g.
xz), but then overwrote narURL.Compression with the compression of the
record returned by getNarFileFromDB, which is CDC-first and returns the
coexisting chunked compression=none row during the lazy-chunking
transition window. The response then advertised Compression:none while
the body was raw xz, with Content-Length set to the xz size (< NarSize),
so nix failed with 'NAR is incomplete' / 'input compression not
recognized'. This deterministically broke the enable-CDC-on-an-existing-
whole-file-cache upgrade path.

Query the nar_file row for the representation actually being served
(hash + served compression + query) instead of the CDC-first lookup, and
stop overwriting narURL.Compression. The served compression now matches
the streamed bytes; the last_accessed_at touch targets the served row.

This is the NAR-serve counterpart to the narinfo-side normalization in
maybeCDCNormalizeNarInfoURL (#1332). Caught by the new CDC lifecycle e2e
(dev-scripts/test-cdc-lifecycle-e2e.py). Includes a pkg/cache regression
test and the OpenSpec change artifacts.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…nar-serve

Add the 'GetNar MUST advertise the compression of the bytes it serves'
requirement to openspec/specs/cdc-chunking and archive the completed
change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Address PR #1346 review (gemini-code-assist): handle the compressed xz
bytes as []byte rather than string and read them via bytes.NewReader,
following Go conventions for binary data. Drops the now-unused strings
import.

Threads: PRRT_kwDONV6tZs6HofRB, PRRT_kwDONV6tZs6HofRC

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-cdc-incomplete-nar-serve branch from 03c55ce to 7c713f8 Compare June 7, 2026 14:32
@kalbasit
kalbasit enabled auto-merge (squash) June 7, 2026 14:33
@kalbasit
kalbasit merged commit 4c0f813 into main Jun 7, 2026
44 of 57 checks passed
@kalbasit
kalbasit deleted the user/wnasreddine/fix-cdc-incomplete-nar-serve branch June 7, 2026 14:39
kalbasit added a commit that referenced this pull request Jun 7, 2026
…on (#1348)

## Summary

Adds end-to-end test infrastructure for the full CDC lifecycle
(successor to `test-migration-e2e.py`):

- **Local driver** `dev-scripts/test-cdc-lifecycle-e2e.py` + `task
test:cdc-lifecycle` (auto-wrapper brings up the fixed-port `nix run
.#deps` stack). Drives non-CDC -> CDC (eager+lazy) -> drain -> non-CDC
by restarting ncps, asserting DB + serving invariants per phase plus
cross-cutting checks (upload presence; fsck no-data-loss).
- **k8s-tests permutation** `ha-s3-postgres-cdc-lifecycle`
(`nix/k8s-tests`) gated on a `cdc-lifecycle` marker, with a
`_test_cdc_lifecycle` phase script (ConfigMap CDC flip + rollout
restart; in-pod `migrate-chunks-to-nar`).

This driver **caught the CDC serving bug** fixed below it in the stack
(#1346); after that fix the full lifecycle passes end-to-end. Depends on
the image fix (#stacked) for in-cluster pod start.

## Test plan

- [x] Local lifecycle e2e PASSES end-to-end (Phases 0-4 +
cross-cutting), deterministic
- [x] `task fmt`/`lint`/`test` green; k8s permutation
generates/lists/installs
- [ ] Full in-cluster k8s lifecycle run (now unblocked by the image fix)
— in progress
- [ ] OpenSpec change `cdc-lifecycle-e2e-tests` archived before merge
(6.2/6.3 + in-cluster run pending)

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
kalbasit added a commit that referenced this pull request Jun 8, 2026
## Summary

The `[Unreleased]` section was populated in #1309 but skewed toward the
recent CDC-drain, in-flight-staging, and Ent-migration work, omitting
several user-facing features and fixes that also landed since v0.9.4.
This backfills the missing notable changes, each cross-referenced by PR.

**Added**
- `ncps fsck` command + Helm fsck CronJob (#975, #996, #998, #999,
#1000, #1004, #1006, #1176)
- Closure pinning HTTP endpoints `POST`/`DELETE /pin/{hash}.narinfo`,
`GET /pins` (#1108)
- pprof server via `--pprof-addr` (#1063)
- CDC lazy chunking as a feature + `--cache-cdc-lazy-cleanup-schedule` +
recovery cron (#1082, #1083, #1089, #1096)
- Helm: auto `GOMEMLIMIT` (#1061), configurable temp volume /
`volumeClaimTemplate` (#1074, #1077), SQLite `Recreate` strategy (#1012)
- Startup storage reporting + deprecated-narinfo warning (#959, #961)
- `docker-dev` image (#1097)

**Fixed**
- `store/tmp` no longer wiped on startup (#1313)
- `nix-hash` NAR verification (#1101)
- Prometheus counters primed at startup (#1337, #1343)
- 404 on un-reassemblable chunked NARs (#1319), purged narinfo no longer
500 (#1318), fail-fast tmp validation (#1086)
- Further CDC/cache reliability fixes (#1317, #1346, #1341, #1342,
#1331, #1347)

**Changed**
- CDC config now persisted in the DB config table (#971)

Dependency bumps, CI/test-only changes, and the internal Ent-conversion
PRs are intentionally excluded; the existing Ent-migration entry already
covers that work.

## Test plan

- [x] `nix fmt` reports no changes (docs-only edit)
- [x] CHANGELOG section structure intact
(Added/Fixed/Changed/Removed/Migration/CI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code merge-stack size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant