Repository navigation
feat(cache): convert storeInDatabase to Ent - #1202
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
26bfbde to
bd54887
Compare
e5d9040 to
435ce97
Compare
bd54887 to
5027242
Compare
435ce97 to
e73fdb1
Compare
5027242 to
5bb2ddd
Compare
e73fdb1 to
ff60258
Compare
5bb2ddd to
636824a
Compare
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. Comment |
ff60258 to
65ea5a4
Compare
636824a to
a728305
Compare
65ea5a4 to
e4ebb15
Compare
a728305 to
36a2e1b
Compare
e4ebb15 to
71fda8d
Compare
36a2e1b to
5edd5b1
Compare
71fda8d to
32d47d0
Compare
5edd5b1 to
6a6b439
Compare
32d47d0 to
bf73661
Compare
6a6b439 to
42b3c9d
Compare
bf73661 to
44acb3d
Compare
42b3c9d to
e90b426
Compare
44acb3d to
2cfb023
Compare
e90b426 to
39f2fd6
Compare
§11.3 continuation. Replaces the legacy storeInDatabase
withTransaction closure (which composed CreateNarInfo +
AddNarInfoReferences + AddNarInfoSignatures + createOrUpdateNarFile
+ LinkNarInfoToNarFile) with an Ent-fluent pipeline.
Conditional UPSERT for narinfos
-------------------------------
The legacy SQL was `INSERT … ON CONFLICT (hash) DO UPDATE …
WHERE url IS NULL` — stub records (URL NULL) get overwritten;
records with a real URL stay untouched. Ent's UpsertOne API
doesn't expose the WHERE-on-update clause directly, so the new
upsertNarInfoFromParsed helper does a read + branch:
- row absent → Create with the supplied fields
- row present, URL nil → UpdateOneID overwriting every field
(ClearX for empty source values, to
match the legacy NULL semantics)
- row present, URL set → keep existing row
This is safe because the surrounding *ent.Tx serialises the
read and write, and the cache-level write lock on
PutNarInfo prevents two same-hash callers from racing.
Two parallel helpers — applyNarInfoCreate and
applyNarInfoUpdate — keep the field-population code DRY between
the insert and stub-update branches.
Bulk reference / signature inserts
----------------------------------
addNarInfoReferences / addNarInfoSignatures use
`tx.NarInfoReference.CreateBulk(...).OnConflictColumns(...).
Ignore().Exec(ctx)`. Ignore() compiles to
`ON CONFLICT DO UPDATE SET col = col` (a no-op update) rather
than DO NOTHING; both express the legacy
`ON CONFLICT (narinfo_id, reference) DO NOTHING` behaviour but
DO NOTHING combined with RETURNING returns no rows on Postgres,
which Ent treats as `sql.ErrNoRows` — Ignore() always returns
a row, so the surrounding tx stays valid.
Resolving the conflict at the SQL layer (rather than tolerating
duplicate-key errors in Go) is required: Postgres puts the
transaction into an aborted state after any failed statement
("current transaction is aborted, commands ignored until end
of transaction block"). The previous version's
IsDuplicateKeyError tolerance broke storeInDatabase on Postgres
the moment a re-call (e.g. shared-NAR concurrent PutNarInfo)
triggered a duplicate.
NarFile upsert
--------------
createOrUpdateNarFileEnt collapses the legacy read-then-create
into a single
`tx.NarFile.Create().OnConflictColumns(hash, compression,
query).Update(SetLastAccessedAt(now)).ID(ctx)`. Atomic by
construction, no Go-level race window. Returns int (Ent's PK
type for strong entities) instead of int64; the §11.4 CDC
conversions will retire the int64-typed callers.
NarFile<->NarInfo link
----------------------
The legacy `LinkNarInfoToNarFile` was a plain INSERT … ON
CONFLICT (narinfo_id, nar_file_id) DO NOTHING. Becomes Ent's
`tx.NarInfoNarFile.Create()...OnConflictColumns(…).Ignore().
Exec(ctx)`.
Verified
--------
`go test -race ./pkg/cache/...` passes against SQLite + Postgres
+ MySQL + Redis, including the concurrency-heavy distributed
tests (PutNarInfoConcurrentSharedNar) that exercise the
conflict-resolution paths. Lint clean.
Deferred
--------
- The parallel PutNarInfo helper at line ~4631 (still uses
the qtx pattern). It's nearly the same shape; will convert
on top of this commit reusing the new helpers.
- `createOrUpdateNarFile` (the legacy helper) is still
referenced from CDC code (§11.4 work). Left in place
alongside the new createOrUpdateNarFileEnt.
39f2fd6 to
304598b
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1202 +/- ##
=====================================
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:
|
§11.3 continuation. Replaces the legacy storeInDatabase
withTransaction closure (which composed CreateNarInfo +
AddNarInfoReferences + AddNarInfoSignatures + createOrUpdateNarFile
Conditional UPSERT for narinfos
The legacy SQL was
INSERT … ON CONFLICT (hash) DO UPDATE … WHERE url IS NULL— stub records (URL NULL) get overwritten;records with a real URL stay untouched. Ent's UpsertOne API
doesn't expose the WHERE-on-update clause directly, so the new
upsertNarInfoFromParsed helper does a read + branch:
(ClearX for empty source values, to
match the legacy NULL semantics)
This is safe because the surrounding *ent.Tx serialises the
read and write, and the cache-level write lock on
PutNarInfo prevents two same-hash callers from racing.
Two parallel helpers — applyNarInfoCreate and
applyNarInfoUpdate — keep the field-population code DRY between
the insert and stub-update branches.
Bulk reference / signature inserts
addNarInfoReferences / addNarInfoSignatures use
tx.NarInfoReference.CreateBulk(...).OnConflictColumns(...). Ignore().Exec(ctx). Ignore() compiles toON CONFLICT DO UPDATE SET col = col(a no-op update) ratherthan DO NOTHING; both express the legacy
ON CONFLICT (narinfo_id, reference) DO NOTHINGbehaviour butDO NOTHING combined with RETURNING returns no rows on Postgres,
which Ent treats as
sql.ErrNoRows— Ignore() always returnsa row, so the surrounding tx stays valid.
Resolving the conflict at the SQL layer (rather than tolerating
duplicate-key errors in Go) is required: Postgres puts the
transaction into an aborted state after any failed statement
("current transaction is aborted, commands ignored until end
of transaction block"). The previous version's
IsDuplicateKeyError tolerance broke storeInDatabase on Postgres
the moment a re-call (e.g. shared-NAR concurrent PutNarInfo)
triggered a duplicate.
NarFile upsert
createOrUpdateNarFileEnt collapses the legacy read-then-create
into a single
tx.NarFile.Create().OnConflictColumns(hash, compression, query).Update(SetLastAccessedAt(now)).ID(ctx). Atomic byconstruction, no Go-level race window. Returns int (Ent's PK
type for strong entities) instead of int64; the §11.4 CDC
conversions will retire the int64-typed callers.
NarFile<->NarInfo link
The legacy
LinkNarInfoToNarFilewas a plain INSERT … ONCONFLICT (narinfo_id, nar_file_id) DO NOTHING. Becomes Ent's
tx.NarInfoNarFile.Create()...OnConflictColumns(…).Ignore(). Exec(ctx).Verified
go test -race ./pkg/cache/...passes against SQLite + Postgrestests (PutNarInfoConcurrentSharedNar) that exercise the
conflict-resolution paths. Lint clean.
Deferred
the qtx pattern). It's nearly the same shape; will convert
on top of this commit reusing the new helpers.
createOrUpdateNarFile(the legacy helper) is stillreferenced from CDC code (§11.4 work). Left in place
alongside the new createOrUpdateNarFileEnt.