Repository navigation
fix(cache): retry transaction on unique-constraint violations - #1213
Conversation
c83722c to
a519808
Compare
e3a1d74 to
5c887f0
Compare
a519808 to
34fff3c
Compare
5c887f0 to
abf51c9
Compare
34fff3c to
a57a7ac
Compare
abf51c9 to
31b9a99
Compare
a57a7ac to
f154c25
Compare
31b9a99 to
e817247
Compare
f154c25 to
d0c63d9
Compare
e817247 to
dc990c0
Compare
d0c63d9 to
332ecf3
Compare
dc990c0 to
404a624
Compare
332ecf3 to
75ea137
Compare
404a624 to
033cc1c
Compare
033cc1c to
db25ae9
Compare
db25ae9 to
58957ef
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1213 +/- ##
=====================================
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:
|
58957ef to
009d2ca
Compare
withEntTransactionRetry now retries when the database reports a duplicate-key error (PostgreSQL 23505, MySQL 1062, SQLite CONSTRAINT) in addition to the existing deadlock/busy classes. The narinfo upsert in storeNarInfoInDatabase follows a SELECT then INSERT/UPDATE pattern; under PostgreSQL READ COMMITTED two concurrent transactions can both miss the row, both attempt INSERT, and the loser surfaces 23505 to the caller. On retry the SELECT sees the row and takes the UPDATE branch. Fixes the flaky TestCacheBackends/PostgreSQL#01/GetNarInfoMultipleConcurrentPutsDuringMigration under -race. Co-Authored-By: Claude Opus 4.7 <[email protected]>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/cache/cache.go (1)
5495-5515: ⚡ Quick winConsider adding a comment explaining when duplicate-key errors are retryable.
The logic change looks correct for the SELECT-then-INSERT upsert race described in the PR (two transactions both miss the row, both attempt INSERT, loser gets 23505/1062/CONSTRAINT). Re-running the transaction closure causes the SELECT to find the existing row and take the UPDATE path instead.
However, unlike deadlock/busy errors (which are clearly transient), duplicate-key errors can also indicate application bugs. A brief inline comment explaining that this retry is specifically for concurrent upsert races would help future maintainers understand when this behavior is expected vs. when a duplicate-key might indicate a real issue.
// Retry on deadlock/busy AND duplicate-key — concurrent // transactions doing a "select-then-insert" can both see the // row as missing, both attempt INSERT, and the loser gets a - // 23505/1062. Re-running the closure picks the existing row - // up via the SELECT and takes the UPDATE branch instead. + // 23505/1062/CONSTRAINT. Re-running the closure picks the + // existing row up via the SELECT and takes the UPDATE branch + // instead. This pattern is safe for idempotent upsert-style + // transactions; genuine duplicate-key bugs from application + // logic will surface as non-transient errors after retries. retryable := database.IsDeadlockError(err) || database.IsDuplicateKeyError(err)The bounded retry count (5 attempts) and exponential backoff already prevent infinite loops, and the log message clearly indicates what's being retried. The change aligns with the PR objective of fixing the flaky PostgreSQL migration test.
🤖 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 5495 - 5515, Add a short inline comment above the retryable determination (the lines using database.IsDeadlockError(err) || database.IsDuplicateKeyError(err)) clarifying that duplicate-key errors are only being retried to handle concurrent "SELECT-then-INSERT" upsert races (where both txns miss the row, both INSERT, the loser gets 23505/1062 and a retry will let the SELECT find the row and take the UPDATE branch), and note that non-transient duplicate-key violations (application bugs) are not the target and that retries are bounded by maxAttempts and exponential backoff (see variables attempt, maxAttempts, delay and the retry log via zerolog.Ctx(ctx)).
🤖 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 5495-5515: Add a short inline comment above the retryable
determination (the lines using database.IsDeadlockError(err) ||
database.IsDuplicateKeyError(err)) clarifying that duplicate-key errors are only
being retried to handle concurrent "SELECT-then-INSERT" upsert races (where both
txns miss the row, both INSERT, the loser gets 23505/1062 and a retry will let
the SELECT find the row and take the UPDATE branch), and note that non-transient
duplicate-key violations (application bugs) are not the target and that retries
are bounded by maxAttempts and exponential backoff (see variables attempt,
maxAttempts, delay and the retry log via zerolog.Ctx(ctx)).
009d2ca to
e85b5cc
Compare
withEntTransactionRetry now retries when the database reports a
duplicate-key error (PostgreSQL 23505, MySQL 1062, SQLite CONSTRAINT)
in addition to the existing deadlock/busy classes. The narinfo upsert
in storeNarInfoInDatabase follows a SELECT then INSERT/UPDATE pattern;
under PostgreSQL READ COMMITTED two concurrent transactions can both
miss the row, both attempt INSERT, and the loser surfaces 23505 to the
caller. On retry the SELECT sees the row and takes the UPDATE branch.
Fixes the flaky
TestCacheBackends/PostgreSQL#01/GetNarInfoMultipleConcurrentPutsDuringMigration
under -race.
Co-Authored-By: Claude Opus 4.7 [email protected]