Repository navigation
feat(cache): convert non-tx NarFile reads/writes to Ent - #1204
Merged
Merged
Conversation
This was referenced May 21, 2026
Owner
Author
This was referenced May 21, 2026
kalbasit
force-pushed
the
feat-cache-narfile-non-tx-to-ent
branch
from
May 22, 2026 21:29
e14b443 to
cf006f2
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
force-pushed
the
feat-cache-migrate-narinfo-ent
branch
from
May 22, 2026 21:41
2680ac4 to
05e3683
Compare
kalbasit
force-pushed
the
feat-cache-narfile-non-tx-to-ent
branch
from
May 22, 2026 21:41
cf006f2 to
fc7e306
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
force-pushed
the
feat-cache-migrate-narinfo-ent
branch
from
May 22, 2026 21:53
05e3683 to
97bcb10
Compare
kalbasit
force-pushed
the
feat-cache-narfile-non-tx-to-ent
branch
from
May 22, 2026 21:54
fc7e306 to
ba36bb4
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
force-pushed
the
feat-cache-migrate-narinfo-ent
branch
from
May 22, 2026 23:02
97bcb10 to
d884322
Compare
kalbasit
force-pushed
the
feat-cache-narfile-non-tx-to-ent
branch
from
May 22, 2026 23:02
ba36bb4 to
5cf720f
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
§11.4 partial. The NarFile call sites in pkg/cache that sit
outside the legacy `c.withTransaction` blocks (lifecycle
queries, metric callbacks, CDC delayed-cleanup, CDC stuck-file
recovery, prefetch progress polls) all migrate cleanly to
the Ent API. Tx-bound NarFile sites in
storeNarWithCDC / findOrCreateNarFileForCDC remain on the qtx
pattern; their conversion lands in the next commit.
Converted:
- `setupMetricCallbacks` (line 706 area): three observers
rewired to Ent.
* `GetNarTotalSize` (SUM(file_size) with COALESCE-to-0) →
`tx.NarFile.Query().Aggregate(ent.Sum(FieldFileSize)).
Scan(&totals)` with a zero fallback when the table is
empty. The same `totalSize` value is reused for the
cache-utilization gauge, so it's lifted to an outer var.
* `GetNarFileCount` → `Query().Count(ctx)`.
* The narinfo count was already on Ent from §11.2; no
change.
- `ClearNarFileChunkingStarted` (deferred chunking-failure
cleanup, line ~1933) → `UpdateOneID(int(narFileID)).
ClearChunkingStartedAt().SetUpdatedAt(time.Now()).Save(ctx)`.
Matches the legacy
`SET chunking_started_at = NULL, updated_at = CURRENT_TIMESTAMP`.
- `deleteNarFromStore`'s DB step (line ~2997) →
`NarFile.Delete().Where(HashEQ, CompressionEQ, QueryEQ)
.Exec(ctx)`.
- CDC delayed-cleanup (line ~5947): the legacy
`GetOldCompressedNarFiles` did a self-join on `nar_files`
to find compressed records whose chunked counterpart
already exists. Ent's edge model doesn't represent the
self-join naturally, so the conversion is a two-step query:
1. `Select(FieldHash).Scan(&chunkedHashRows)` — gathers
the set of hashes that already have a chunked
(compression='none', total_chunks>0) version
2. `Where(TotalChunksEQ(0), CompressionNEQ('none'),
CreatedAtLT(cutoff), HashIn(chunkedHashes...))
.All(ctx)` — picks the old compressed candidates
Behaviour-equivalent; one extra round-trip in exchange for
staying inside Ent's typed API. The subsequent
`DeleteNarFileByID` → `NarFile.Delete().Where(IDEQ(...))`.
- CDC stuck-NAR recovery
`GetStuckNarFiles(cutoff_time, batch_size)` (line ~6087) →
`Query().Where(TotalChunksEQ(0), ChunkingStartedAtIsNil(),
CreatedAtLT(cutoff)).Order(Asc(ID)).Limit(batchSize)`.
Same row shape consumed downstream (Hash, Compression,
Query, FileSize, ID), no further changes needed there.
- Two `GetNarFileByID` calls in CDC prefetch (lines ~6660 and
~6746) → `NarFile.Get(ctx, int(narFileID))`. The Nullable
ChunkingStartedAt access changes from
`nr.ChunkingStartedAt.Valid` to `nr.ChunkingStartedAt ==
nil`.
Deferred to follow-up §11.4 commits:
- `storeNarWithCDC` and helpers (lines 1448, 1475, 2035,
2066, 2149, 2176, 2207, 2220, 2226) — the qtx-style CDC
pipeline that creates the nar_files row, links chunks,
updates totals, deletes partial chunk records on retry, and
marks chunking_started_at. The bulk-chunk creation and
LinkNarFileToChunks calls are the bulk of that work.
- `TouchNarFile` (lines 2894, 6424) — used by GET-time
last-access tracking.
- Orphan-cleanup paths inside the LRU eviction wrapper
(line 5688, 5709) — properly §11.5 territory.
`go test -race ./pkg/cache/...` passes against SQLite +
Postgres + MySQL + Redis. Lint clean.
kalbasit
force-pushed
the
feat-cache-narfile-non-tx-to-ent
branch
from
May 22, 2026 23:15
5cf720f to
da1e366
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
enabled auto-merge (squash)
May 22, 2026 23:16
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1204 +/- ##
=====================================
Coverage 5.68% 5.68%
=====================================
Files 6 6
Lines 387 387
=====================================
Hits 22 22
Misses 361 361
Partials 4 4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
added a commit
that referenced
this pull request
May 23, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
added a commit
that referenced
this pull request
May 23, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
added a commit
that referenced
this pull request
May 23, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
added a commit
that referenced
this pull request
May 23, 2026
PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
kalbasit
added a commit
that referenced
this pull request
May 23, 2026
fix(ncps): drop dbURL from createDatabaseQuerier error wraps PR #1192 review: 3 sec-high comments flagged that `createDatabaseQuerier`'s error wraps embed `dbURL` via `%q`. Practical database URLs include credentials, leaking them into server logs. Drop the URL from the three new wraps the §11.1 thread-through commit introduced. Resolves: - PR #1192 PRRT_kwDONV6tZs6DsTQ7 (line 990) - PR #1192 PRRT_kwDONV6tZs6DsTRK (line 998) - PR #1192 PRRT_kwDONV6tZs6DsTRQ (line 1003) fix(cache): preserve tx debug logs + use time.Timer in retry PR #1194 review surfaced two issues in withEntTransaction: 1. (PRRT_kwDONV6tZs6DsQzn) Restore "starting transaction" / "committed successfully" debug logs that the legacy executeTransaction emitted. Behind Debug level so noise stays opt-in; restores observability parity. 2. (PRRT_kwDONV6tZs6DsQzo) Replace `case <-time.After(delay):` inside the retry select with `time.NewTimer(delay)` + `t.Stop()` in the ctx-cancellation branch to avoid leaking the underlying timer when ctx wins the race. Behaviour-preserving on the happy path. fix(migrations): restore +goose NO TRANSACTION on sqlite bridge PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the `-- +goose NO TRANSACTION` directive was dropped from the SQLite ent_baseline bridge when `fix-ent-preserve-current- timestamp-default` regenerated the migration via `cmd/generate-migrations`. In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a transaction. Goose wraps migrations in a transaction by default, so without the NO TRANSACTION directive the PRAGMA silently has no effect — and the subsequent `DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps would fail in production against any database with existing FK-referenced rows. The original §8 bridge had this directive; the regeneration lost it because Atlas's GooseFormatter doesn't emit it automatically. Restored the directive plus its explanatory comment, and regenerated `migrations/sqlite/atlas.sum` so Atlas's checksum validator accepts the file. Postgres and MySQL bridges don't need an equivalent — they use real `ALTER TABLE` for column changes (not SQLite's shadow-table-recreate pattern) and don't depend on a PRAGMA to disable referential integrity mid-migration. Note: `TestSchemaEquivalence/sqlite` is currently broken at HEAD for a separate, pre-existing reason traced to the `cf28126` regen (the test fails both with and without this fix). Will be investigated as a follow-up. fix(cache): add retry-on-deadlock to storeNarInfoInDatabase PR #1203 (PRRT_kwDONV6tZs6DsScG, medium): standalone `storeNarInfoInDatabase` (Cache-free entry used by MigrateNarInfo and the `ncps migrate-narinfo` CLI) called `dbClient.WithTransaction` directly, skipping the deadlock-retry loop the `*Cache.withEntTransaction` provides. Under load — concurrent migrations of many narinfos against SQLite, or contended PostgreSQL deployments — that meant transient SQLITE_BUSY / serialization failures bubbled all the way out, where the legacy path would have retried. Fix: extract the retry policy from `*Cache.withEntTransaction` into a package-level `withEntTransactionRetry(ctx, dbClient, operation, fn)` helper. `*Cache.withEntTransaction` now forwards to it; `storeNarInfoInDatabase` uses it directly. Same retry parameters (5 attempts, 50ms initial back-off doubling per attempt, ctx-aware via time.Timer + Stop). No behaviour change for any caller already going through `*Cache.withEntTransaction`; the previously-missing retry is now present for MigrateNarInfo / CLI migrations. fix(cache): batch HashIn + null-safe SUM + unshadow ent PR #1204 surfaced three concerns in §11.4's non-tx NarFile conversion (`feat-cache-narfile-non-tx-to-ent`): 1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup replacement for `GetOldCompressedNarFiles` was a two-step query that paged ALL chunked NAR hashes into Go memory then passed them to a single `entnarfile.HashIn(...)`. On caches with many chunked NARs that risks (a) loading megabytes of hashes into memory and (b) blowing past driver parameter limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older SQLite ≤ 999). Fixed by batching HashIn in chunks of `cdcCleanupHashBatchSize` (= 500, a new package const). The batch size is well below all known limits and well above typical chunked-hash counts, so the job stays a single round-trip for any practical install. 2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM aggregator scanned into a plain `int64`. SQL `SUM` over an empty set returns NULL, which fails to scan into `int64` ("sql: Scan, storing driver.Value type <nil> into type *int64"). The metric callback would have started failing the moment any deployment booted with an empty nar_files table. Switched to `sql.NullInt64` with a Valid check; NULL is treated as zero. The `sql:"sum"` tag matches the alias Ent emits via `ent.Sum`. 3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed the `ent` package import inside the cron handler closure. No actual breakage in the current code (no `ent.X` use inside that scope) but it makes the scope brittle for future edits. Renamed to `entClient`. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean. fix(cache): batch relinkNarInfosToNarFile bulk insert PR #1205 (PRRT_kwDONV6tZs6DsRuq, medium): `relinkNarInfosToNarFileWithEntTx` passed every queried narinfo ID into a single `tx.NarInfoNarFile.CreateBulk(...)` call. Each row sends 2 parameters (narinfo_id, nar_file_id), so a closure with N narinfos sharing a NAR URL sends 2N placeholders. On older SQLite builds (limit 999) that fails for any N > 499; even on Postgres (limit 65535) very large closures could silently overflow. Fixed by chunking the loop into batches of 500 rows = 1000 placeholders per batch, well below all driver limits and well above realistic closure sizes — so the common case stays a single round-trip. batch size is a local const inside the helper rather than a package constant because the limit derives from this specific row's placeholder count (2 cols × batch size); other bulk inserts with different column counts get their own limit. `go test -race ./pkg/cache/...` passes against SQLite + Postgres + MySQL + Redis. Lint clean.
This was referenced May 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
§11.4 partial. The NarFile call sites in pkg/cache that sit
outside the legacy
c.withTransactionblocks (lifecyclequeries, metric callbacks, CDC delayed-cleanup, CDC stuck-file
recovery, prefetch progress polls) all migrate cleanly to
the Ent API. Tx-bound NarFile sites in
storeNarWithCDC / findOrCreateNarFileForCDC remain on the qtx
pattern; their conversion lands in the next commit.
Converted:
setupMetricCallbacks(line 706 area): three observersrewired to Ent.
GetNarTotalSize(SUM(file_size) with COALESCE-to-0) →tx.NarFile.Query().Aggregate(ent.Sum(FieldFileSize)). Scan(&totals)with a zero fallback when the table isempty. The same
totalSizevalue is reused for thecache-utilization gauge, so it's lifted to an outer var.
GetNarFileCount→Query().Count(ctx).change.
ClearNarFileChunkingStarted(deferred chunking-failurecleanup, line ~1933) →
UpdateOneID(int(narFileID)). ClearChunkingStartedAt().SetUpdatedAt(time.Now()).Save(ctx).Matches the legacy
SET chunking_started_at = NULL, updated_at = CURRENT_TIMESTAMP.deleteNarFromStore's DB step (line ~2997) →NarFile.Delete().Where(HashEQ, CompressionEQ, QueryEQ) .Exec(ctx).CDC delayed-cleanup (line ~5947): the legacy
GetOldCompressedNarFilesdid a self-join onnar_filesto find compressed records whose chunked counterpart
already exists. Ent's edge model doesn't represent the
self-join naturally, so the conversion is a two-step query:
Select(FieldHash).Scan(&chunkedHashRows)— gathersthe set of hashes that already have a chunked
(compression='none', total_chunks>0) version
Where(TotalChunksEQ(0), CompressionNEQ('none'), CreatedAtLT(cutoff), HashIn(chunkedHashes...)) .All(ctx)— picks the old compressed candidatesBehaviour-equivalent; one extra round-trip in exchange for
staying inside Ent's typed API. The subsequent
DeleteNarFileByID→NarFile.Delete().Where(IDEQ(...)).CDC stuck-NAR recovery
GetStuckNarFiles(cutoff_time, batch_size)(line ~6087) →Query().Where(TotalChunksEQ(0), ChunkingStartedAtIsNil(), CreatedAtLT(cutoff)).Order(Asc(ID)).Limit(batchSize).Same row shape consumed downstream (Hash, Compression,
Query, FileSize, ID), no further changes needed there.
Two
GetNarFileByIDcalls in CDC prefetch (lines ~6660 and~6746) →
NarFile.Get(ctx, int(narFileID)). The NullableChunkingStartedAt access changes from
nr.ChunkingStartedAt.Validtonr.ChunkingStartedAt == nil.Deferred to follow-up §11.4 commits:
storeNarWithCDCand helpers (lines 1448, 1475, 2035,2066, 2149, 2176, 2207, 2220, 2226) — the qtx-style CDC
pipeline that creates the nar_files row, links chunks,
updates totals, deletes partial chunk records on retry, and
marks chunking_started_at. The bulk-chunk creation and
LinkNarFileToChunks calls are the bulk of that work.
TouchNarFile(lines 2894, 6424) — used by GET-timelast-access tracking.
(line 5688, 5709) — properly §11.5 territory.
go test -race ./pkg/cache/...passes against SQLite +Postgres + MySQL + Redis. Lint clean.