Skip to content

fix(cache): retry transaction on unique-constraint violations - #1213

Merged
kalbasit merged 1 commit into
mainfrom
fix-cache-tx-retry-on-duplicate-key
May 23, 2026
Merged

kalbasit merged 1 commit into
mainfrom
fix-cache-tx-retry-on-duplicate-key

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

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]

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working 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-ncps-fsck-to-ent branch from c83722c to a519808 Compare May 23, 2026 00:35
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from e3a1d74 to 5c887f0 Compare May 23, 2026 00:35
@kalbasit
kalbasit force-pushed the feat-ncps-fsck-to-ent branch from a519808 to 34fff3c Compare May 23, 2026 03:31
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from 5c887f0 to abf51c9 Compare May 23, 2026 03:31
@kalbasit
kalbasit force-pushed the feat-ncps-fsck-to-ent branch from 34fff3c to a57a7ac Compare May 23, 2026 03:45
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from abf51c9 to 31b9a99 Compare May 23, 2026 03:45
@kalbasit
kalbasit force-pushed the feat-ncps-fsck-to-ent branch from a57a7ac to f154c25 Compare May 23, 2026 03:57
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from 31b9a99 to e817247 Compare May 23, 2026 03:57
@kalbasit
kalbasit force-pushed the feat-ncps-fsck-to-ent branch from f154c25 to d0c63d9 Compare May 23, 2026 04:10
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from e817247 to dc990c0 Compare May 23, 2026 04:10
@kalbasit
kalbasit force-pushed the feat-ncps-fsck-to-ent branch from d0c63d9 to 332ecf3 Compare May 23, 2026 04:22
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from dc990c0 to 404a624 Compare May 23, 2026 04:22
@kalbasit
kalbasit force-pushed the feat-ncps-fsck-to-ent branch from 332ecf3 to 75ea137 Compare May 23, 2026 04:34
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from 404a624 to 033cc1c Compare May 23, 2026 04:34
Base automatically changed from feat-ncps-fsck-to-ent to main May 23, 2026 04:47
@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels May 23, 2026
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from 033cc1c to db25ae9 Compare May 23, 2026 04:47
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels May 23, 2026
@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from db25ae9 to 58957ef Compare May 23, 2026 04:49
@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 (188b4f1) to head (e85b5cc).
⚠️ Report is 1 commits behind head on main.

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

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]>

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

🧹 Nitpick comments (1)
pkg/cache/cache.go (1)

5495-5515: ⚡ Quick win

Consider 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)).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fd132bfa-6726-412a-a6fc-f7e3a3850c88

📥 Commits

Reviewing files that changed from the base of the PR and between c5521e1 and 009d2ca.

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

@kalbasit
kalbasit force-pushed the fix-cache-tx-retry-on-duplicate-key branch from 009d2ca to e85b5cc Compare May 23, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code merge-stack size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant