Repository navigation
feat(cache): convert pinned closures, stale-chunk orphans, narinfo CDC update to Ent - #1209
Merged
Merged
Conversation
This was referenced May 21, 2026
Owner
Author
This was referenced May 21, 2026
kalbasit
force-pushed
the
feat-cache-pinned-orphan-narinfocdc-ent
branch
from
May 22, 2026 21:29
6c36682 to
359ac06
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
Restrict the orphan-check query inside cleanupStaleLockChunks to the chunk IDs we already know about from staleLockChunks, instead of pulling every orphan in the database and filtering them in memory. The post-filter `if _, ok := staleByID[oc.ID]; !ok` is no longer needed since the query already returns only candidates. Addresses gemini-code-assist comment on PR #1209 (PRRT_kwDONV6tZs6D3VF-). Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit
force-pushed
the
gs-comments-1191-1207
branch
from
May 22, 2026 21:41
8252bff to
24f0de5
Compare
kalbasit
force-pushed
the
feat-cache-pinned-orphan-narinfocdc-ent
branch
from
May 22, 2026 21:41
359ac06 to
9a4822a
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
Restrict the orphan-check query inside cleanupStaleLockChunks to the chunk IDs we already know about from staleLockChunks, instead of pulling every orphan in the database and filtering them in memory. The post-filter `if _, ok := staleByID[oc.ID]; !ok` is no longer needed since the query already returns only candidates. Addresses gemini-code-assist comment on PR #1209 (PRRT_kwDONV6tZs6D3VF-). Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit
force-pushed
the
gs-comments-1191-1207
branch
from
May 22, 2026 21:54
24f0de5 to
c34e715
Compare
kalbasit
force-pushed
the
feat-cache-pinned-orphan-narinfocdc-ent
branch
from
May 22, 2026 21:54
9a4822a to
2c4cc5e
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
Restrict the orphan-check query inside cleanupStaleLockChunks to the chunk IDs we already know about from staleLockChunks, instead of pulling every orphan in the database and filtering them in memory. The post-filter `if _, ok := staleByID[oc.ID]; !ok` is no longer needed since the query already returns only candidates. Addresses gemini-code-assist comment on PR #1209 (PRRT_kwDONV6tZs6D3VF-). Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit
force-pushed
the
gs-comments-1191-1207
branch
from
May 22, 2026 23:02
c34e715 to
6dbd2cb
Compare
kalbasit
force-pushed
the
feat-cache-pinned-orphan-narinfocdc-ent
branch
from
May 22, 2026 23:02
2c4cc5e to
77dfc49
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
Restrict the orphan-check query inside cleanupStaleLockChunks to the chunk IDs we already know about from staleLockChunks, instead of pulling every orphan in the database and filtering them in memory. The post-filter `if _, ok := staleByID[oc.ID]; !ok` is no longer needed since the query already returns only candidates. Addresses gemini-code-assist comment on PR #1209 (PRRT_kwDONV6tZs6D3VF-). Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit
force-pushed
the
gs-comments-1191-1207
branch
from
May 22, 2026 23:15
6dbd2cb to
ff12dbd
Compare
kalbasit
force-pushed
the
feat-cache-pinned-orphan-narinfocdc-ent
branch
from
May 22, 2026 23:15
77dfc49 to
798092d
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
Restrict the orphan-check query inside cleanupStaleLockChunks to the chunk IDs we already know about from staleLockChunks, instead of pulling every orphan in the database and filtering them in memory. The post-filter `if _, ok := staleByID[oc.ID]; !ok` is no longer needed since the query already returns only candidates. Addresses gemini-code-assist comment on PR #1209 (PRRT_kwDONV6tZs6D3VF-). Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit
force-pushed
the
gs-comments-1191-1207
branch
from
May 22, 2026 23:28
ff12dbd to
9d7d600
Compare
kalbasit
force-pushed
the
feat-cache-pinned-orphan-narinfocdc-ent
branch
from
May 22, 2026 23:28
798092d to
46a0899
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
Restrict the orphan-check query inside cleanupStaleLockChunks to the chunk IDs we already know about from staleLockChunks, instead of pulling every orphan in the database and filtering them in memory. The post-filter `if _, ok := staleByID[oc.ID]; !ok` is no longer needed since the query already returns only candidates. Addresses gemini-code-assist comment on PR #1209 (PRRT_kwDONV6tZs6D3VF-). Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit
force-pushed
the
gs-comments-1191-1207
branch
from
May 22, 2026 23:54
9d7d600 to
32ad08d
Compare
kalbasit
force-pushed
the
feat-cache-pinned-orphan-narinfocdc-ent
branch
from
May 22, 2026 23:54
46a0899 to
c666a31
Compare
kalbasit
added a commit
that referenced
this pull request
May 22, 2026
Restrict the orphan-check query inside cleanupStaleLockChunks to the chunk IDs we already know about from staleLockChunks, instead of pulling every orphan in the database and filtering them in memory. The post-filter `if _, ok := staleByID[oc.ID]; !ok` is no longer needed since the query already returns only candidates. Addresses gemini-code-assist comment on PR #1209 (PRRT_kwDONV6tZs6D3VF-). Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit
force-pushed
the
gs-comments-1191-1207
branch
from
May 23, 2026 00:07
32ad08d to
b33e76c
Compare
kalbasit
force-pushed
the
feat-cache-pinned-orphan-narinfocdc-ent
branch
3 times, most recently
from
May 23, 2026 00:07
c666a31 to
e5ed9c3
Compare
kalbasit
added a commit
that referenced
this pull request
May 23, 2026
Restrict the orphan-check query inside cleanupStaleLockChunks to the chunk IDs we already know about from staleLockChunks, instead of pulling every orphan in the database and filtering them in memory. The post-filter `if _, ok := staleByID[oc.ID]; !ok` is no longer needed since the query already returns only candidates. Addresses gemini-code-assist comment on PR #1209 (PRRT_kwDONV6tZs6D3VF-). Co-Authored-By: Claude Opus 4.7 <[email protected]>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/cache/cache.go (1)
7318-7319: ⚡ Quick winKeep
ListPinnedClosureson a cache-level type.This exported method now leaks
*ent.PinnedClosureto callers, which turns an internal Ent migration into a public API change and couples the cache layer to ORM-generated types. Mapping the rows back to the previous cache/database shape keeps the migration internal.♻️ Suggested shape-preserving change
-func (c *Cache) ListPinnedClosures(ctx context.Context) ([]*ent.PinnedClosure, error) { - closures, err := c.dbClient.Ent().PinnedClosure.Query().All(ctx) +func (c *Cache) ListPinnedClosures(ctx context.Context) ([]database.PinnedClosure, error) { + rows, err := c.dbClient.Ent().PinnedClosure.Query().All(ctx) if err != nil { return nil, fmt.Errorf("failed to list pinned closures: %w", err) } + closures := make([]database.PinnedClosure, len(rows)) + for i, row := range rows { + closures[i] = database.PinnedClosure{Hash: row.Hash} + } + return closures, nil }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/cache/cache.go` around lines 7318 - 7319, The exported method ListPinnedClosures on type Cache currently returns []*ent.PinnedClosure and thus leaks the ORM type; change its signature to return the cache-level shape (e.g., []PinnedClosure or the existing internal struct used elsewhere) and keep the ent types internal. In the implementation of ListPinnedClosures (where it calls c.dbClient.Ent().PinnedClosure.Query().All(ctx)), iterate the returned []*ent.PinnedClosure and map each row into the cache/local PinnedClosure struct (copy fields, convert ids/timestamps as needed), then return the slice of those cache structs and any error; keep all ent.* types confined to this function so callers never see *ent.PinnedClosure.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@pkg/cache/cache.go`:
- Around line 7318-7319: The exported method ListPinnedClosures on type Cache
currently returns []*ent.PinnedClosure and thus leaks the ORM type; change its
signature to return the cache-level shape (e.g., []PinnedClosure or the existing
internal struct used elsewhere) and keep the ent types internal. In the
implementation of ListPinnedClosures (where it calls
c.dbClient.Ent().PinnedClosure.Query().All(ctx)), iterate the returned
[]*ent.PinnedClosure and map each row into the cache/local PinnedClosure struct
(copy fields, convert ids/timestamps as needed), then return the slice of those
cache structs and any error; keep all ent.* types confined to this function so
callers never see *ent.PinnedClosure.
…C update to Ent
§11.5 first batch. Covers the standalone Querier sites in
pkg/cache that don't sit inside the LRU eviction transaction —
the pinned-closure CRUD, the stale-CDC-chunking orphan
cleanup, and the post-CDC-migration narinfo URL/compression
update. The bigger LRU + orphaned-nar_files + orphaned-chunks
block (calculateCleanupSize / deleteLRURecordsFromDB inside
runLRU) stays on the legacy Querier in this commit because
the legacy `GetLeastUsedNarInfos` is a correlated-subquery
cumulative-size SQL that doesn't translate cleanly to Ent's
fluent API. That conversion lands in a follow-up commit on
top of this one.
Converted:
- Pinned closures (PinClosure, UnpinClosure,
ListPinnedClosures, IsNarInfoPinned, GetPinnedClosureHashes
list path):
* `PinClosure`: legacy CreatePinnedClosure +
IsDuplicateKeyError tolerance → `Create().
OnConflictColumns(Hash).Ignore().Exec(ctx)`. SQL-level
idempotency, no Go-level error tolerance (which would
leave the transaction in an aborted state on Postgres).
* `UnpinClosure`: legacy DeletePinnedClosure →
`Delete().Where(HashEQ).Exec(ctx)`.
* `ListPinnedClosures` + `GetPinnedClosureHashes`:
`Query().All(ctx)`. Return type goes from
`[]database.PinnedClosure` to `[]*ent.PinnedClosure`.
The Ent struct exposes the same `.Hash` field used by
both callers.
* `IsNarInfoPinned`: legacy GetPinnedClosure +
ErrNotFound branch → `Query().Where(HashEQ).Exist(ctx)`.
Cleaner: no row materialisation, no error-shape juggling.
- Post-CDC narinfo URL/compression update
(`*Cache.swapToCDCNormalized`, line ~7124): legacy
UpdateNarInfoCompressionFileSizeHashAndURL → Ent's
`Update().Where(URL(old)).SetCompression(...).SetURL(...)
.ClearFileSize().ClearFileHash().SetUpdatedAt(time.Now())
.Save(ctx)`. Matches the legacy "set both to NULL when CDC
normalises to compression=none" behaviour via Ent's Clear
setters.
- Stale CDC chunking-lock cleanup
(`*Cache.cleanupStaleLockChunks`, line ~2300):
* `GetOrphanedChunks` → `Chunk.Query().Where(Not(HasNarFileLinks()))`.
Ent's edge-existence negation maps directly to the legacy
LEFT JOIN nar_file_chunks WHERE chunk_id IS NULL.
* `DeleteChunkByID` → `Chunk.Delete().Where(IDEQ(oc.ID))`.
* `staleByID` map keyed by `int` (Ent PK type) instead of
`int64`; the previous int64-keyed map existed only to
bridge the legacy Querier surface.
`go test -race ./pkg/cache/...` passes against SQLite +
Postgres + MySQL + Redis. Lint clean.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1209 +/- ##
======================================
Coverage ? 5.68%
======================================
Files ? 6
Lines ? 387
Branches ? 0
======================================
Hits ? 22
Misses ? 361
Partials ? 4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.5 first batch. Covers the standalone Querier sites in
pkg/cache that don't sit inside the LRU eviction transaction —
the pinned-closure CRUD, the stale-CDC-chunking orphan
cleanup, and the post-CDC-migration narinfo URL/compression
update. The bigger LRU + orphaned-nar_files + orphaned-chunks
block (calculateCleanupSize / deleteLRURecordsFromDB inside
runLRU) stays on the legacy Querier in this commit because
the legacy
GetLeastUsedNarInfosis a correlated-subquerycumulative-size SQL that doesn't translate cleanly to Ent's
fluent API. That conversion lands in a follow-up commit on
top of this one.
Converted:
Pinned closures (PinClosure, UnpinClosure,
ListPinnedClosures, IsNarInfoPinned, GetPinnedClosureHashes
list path):
PinClosure: legacy CreatePinnedClosure +IsDuplicateKeyError tolerance →
Create(). OnConflictColumns(Hash).Ignore().Exec(ctx). SQL-levelidempotency, no Go-level error tolerance (which would
leave the transaction in an aborted state on Postgres).
UnpinClosure: legacy DeletePinnedClosure →Delete().Where(HashEQ).Exec(ctx).ListPinnedClosures+GetPinnedClosureHashes:Query().All(ctx). Return type goes from[]database.PinnedClosureto[]*ent.PinnedClosure.The Ent struct exposes the same
.Hashfield used byboth callers.
IsNarInfoPinned: legacy GetPinnedClosure +ErrNotFound branch →
Query().Where(HashEQ).Exist(ctx).Cleaner: no row materialisation, no error-shape juggling.
Post-CDC narinfo URL/compression update
(
*Cache.swapToCDCNormalized, line ~7124): legacyUpdateNarInfoCompressionFileSizeHashAndURL → Ent's
Update().Where(URL(old)).SetCompression(...).SetURL(...) .ClearFileSize().ClearFileHash().SetUpdatedAt(time.Now()) .Save(ctx). Matches the legacy "set both to NULL when CDCnormalises to compression=none" behaviour via Ent's Clear
setters.
Stale CDC chunking-lock cleanup
(
*Cache.cleanupStaleLockChunks, line ~2300):GetOrphanedChunks→Chunk.Query().Where(Not(HasNarFileLinks())).Ent's edge-existence negation maps directly to the legacy
LEFT JOIN nar_file_chunks WHERE chunk_id IS NULL.
DeleteChunkByID→Chunk.Delete().Where(IDEQ(oc.ID)).staleByIDmap keyed byint(Ent PK type) instead ofint64; the previous int64-keyed map existed only tobridge the legacy Querier surface.
go test -race ./pkg/cache/...passes against SQLite +Postgres + MySQL + Redis. Lint clean.