Skip to content

feat(cache): convert LRU eviction + fixNarInfoFileSize to Ent - #1215

Merged
kalbasit merged 1 commit into
mainfrom
feat-cache-lru-eviction-to-ent
May 23, 2026
Merged

kalbasit merged 1 commit into
mainfrom
feat-cache-lru-eviction-to-ent

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

§11.5 last batch: deleteLRURecordsFromDB, calculateCleanupSize, and
fixNarInfoFileSize now run under withEntTransaction. The legacy
sqlc-style executeTransaction/withTransaction wrapping is dropped from
runLRU; results flow back to the caller via closed-over vars so the
store-side deletions happen outside the DB transaction the way they did
before.

Notable preserved invariant:

deleteLRURecordsFromDB now mirrors the legacy GetLeastUsedNarInfos
cumulative-bytes filter in Go. Candidates are fetched LRU-ordered
with the linked nar_file eager-loaded; we then keep the prefix whose
cumulative file_size stays within 2*cleanupSize bytes (saturating on
overflow, always including the first row so we make progress even
when a single nar_file dwarfs the budget). The actual delete loop
walks that prefix and breaks once cleanupSize bytes have been
reclaimed -- so we never over-evict the way a naive Limit(n)
replacement would when nar_file sizes vary widely.

This collapses the legacy N+1 GetNarFileByNarInfoID per-narinfo lookup
into one eager load; the orphaned-nar_file / orphaned-chunk passes
become Not(HasNarInfoNarFiles()) / Not(HasNarFileLinks()) predicates on
NarFile.Delete and Chunk.Delete.

TestCacheBackends/{SQLite,PostgreSQL,MySQL} all green under -race; the
TestRunLRU regression that prompted the original revert is covered.

Co-Authored-By: Claude Opus 4.7 [email protected]

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request go Pull requests that update go code labels May 21, 2026
This was referenced May 21, 2026
@kalbasit

kalbasit commented May 21, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 64a722f to 98d03dc Compare May 23, 2026 00:07
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from 08e7741 to 4e5a13c Compare May 23, 2026 00:35
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 98d03dc to 453905e Compare May 23, 2026 00:35
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from 4e5a13c to 0837f5b Compare May 23, 2026 03:31
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 453905e to 670f6c8 Compare May 23, 2026 03:31
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from 0837f5b to d0dcb9a Compare May 23, 2026 03:45
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 670f6c8 to 4666bf5 Compare May 23, 2026 03:45
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from d0dcb9a to 8a0ccdd Compare May 23, 2026 03:57
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 4666bf5 to 81acd51 Compare May 23, 2026 03:58
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from 8a0ccdd to bac66ef Compare May 23, 2026 04:10
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 81acd51 to 569579a Compare May 23, 2026 04:10
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from bac66ef to e1f465c Compare May 23, 2026 04:22
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 569579a to 6a3ce50 Compare May 23, 2026 04:22
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from e1f465c to 863819b Compare May 23, 2026 04:34
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 6a3ce50 to 25f0f6b Compare May 23, 2026 04:34
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from 863819b to 50639e1 Compare May 23, 2026 04:47
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from 25f0f6b to ec0d521 Compare May 23, 2026 04:47
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from 50639e1 to 18dbdaa Compare May 23, 2026 04:49
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from ec0d521 to d1dd3fc Compare May 23, 2026 04:49
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from 18dbdaa to d9d2ee9 Compare May 23, 2026 05:24
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from d1dd3fc to b57e527 Compare May 23, 2026 05:24
@kalbasit
kalbasit force-pushed the fix-ent-schema-parity-race branch from d9d2ee9 to c3ee587 Compare May 23, 2026 05:28
@kalbasit
kalbasit force-pushed the feat-cache-lru-eviction-to-ent branch from b57e527 to 30ac78c Compare May 23, 2026 05:28
§11.5 last batch: deleteLRURecordsFromDB, calculateCleanupSize, and
fixNarInfoFileSize now run under withEntTransaction. The legacy
sqlc-style executeTransaction/withTransaction wrapping is dropped from
runLRU; results flow back to the caller via closed-over vars so the
store-side deletions happen outside the DB transaction the way they did
before.

Notable preserved invariant:

  deleteLRURecordsFromDB now mirrors the legacy GetLeastUsedNarInfos
  cumulative-bytes filter in Go. Candidates are fetched LRU-ordered
  with the linked nar_file eager-loaded; we then keep the prefix whose
  cumulative file_size stays within 2*cleanupSize bytes (saturating on
  overflow, always including the first row so we make progress even
  when a single nar_file dwarfs the budget). The actual delete loop
  walks that prefix and breaks once cleanupSize bytes have been
  reclaimed -- so we never over-evict the way a naive Limit(n)
  replacement would when nar_file sizes vary widely.

This collapses the legacy N+1 GetNarFileByNarInfoID per-narinfo lookup
into one eager load; the orphaned-nar_file / orphaned-chunk passes
become Not(HasNarInfoNarFiles()) / Not(HasNarFileLinks()) predicates on
NarFile.Delete and Chunk.Delete.

TestCacheBackends/{SQLite,PostgreSQL,MySQL} all green under -race; the
TestRunLRU regression that prompted the original revert is covered.

Co-Authored-By: Claude Opus 4.7 <[email protected]>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 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.

Inline comments:
In `@pkg/cache/cache.go`:
- Around line 5737-5748: The query limits reclamation to the first maxFetchRows
rows (tx.NarInfo.Query()...Limit(maxFetchRows)), which can leave older
reclaimable rows unexamined; change the eviction routine to page through the
full LRU space using keyset pagination: loop fetching batches ordered by
entnarinfo.FieldLastAccessedAt then entnarinfo.FieldID, passing a predicate on
the next query that selects rows with (LastAccessedAt > lastSeen) OR
(LastAccessedAt == lastSeen AND ID > lastID) until no more rows, accumulating
reclaimed size and breaking when cleanupSize/other stop conditions are met;
update the code paths that use maxFetchRows, Order, Limit and the
WithNarInfoNarFiles/WithNarFile fetch to perform repeated batched queries
instead of a single Limit call.
- Around line 6080-6082: The early-return currently checks only
narInfoHashesToRemove and chunkHashesToRemove and skips calling
parallelDeleteFromStores when narURLsToRemove contains entries (so orphaned
NarFile blobs remain); modify the condition around the return to include
narURLsToRemove (i.e., only return when all three collections are empty) or
otherwise ensure parallelDeleteFromStores is invoked whenever narURLsToRemove is
non-empty so orphaned NarFile blobs (from NarFile deletions) are passed to
parallelDeleteFromStores for cleanup.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: adc4ef8e-2d24-4641-a471-fbd79de3f6d9

📥 Commits

Reviewing files that changed from the base of the PR and between 266f04a and 0bbc9dd.

📒 Files selected for processing (1)
  • pkg/cache/cache.go

Comment thread pkg/cache/cache.go Resolved
Comment thread pkg/cache/cache.go Resolved
@codecov

codecov Bot commented May 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.68%. Comparing base (266f04a) to head (0bbc9dd).

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #1215   +/-   ##
=====================================
  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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request 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