Repository navigation
fix(jobs): classify transient database failures once for background retries #8194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
waleedlatif1
merged 9 commits into
staging
from
fix/background-db-failure-classification
Sep 23, 2026
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
03ef2c9
fix(jobs): classify transient database failures once for background r…
waleedlatif1 efd0535
fix(jobs): escalate database retries without spending the breaker, co…
waleedlatif1 086f646
revert(knowledge): drop the stale-deferred Retry action and its wire …
waleedlatif1 ce14af3
fix(knowledge): fail an uploaded document whose scheduled database re…
waleedlatif1 3d6eda7
fix(knowledge): keep the deferred-retry watchdog alive through databa…
waleedlatif1 91c0228
fix(jobs): progress-aware connector database retries, stuck-sync sign…
waleedlatif1 06a6262
fix(jobs): bound the retry-history read, count member purges, match d…
waleedlatif1 ad930e8
fix(jobs): classify ENETDOWN, ENETRESET and ENETUNREACH on a database…
waleedlatif1 4f26440
test(knowledge): import the connector retry helpers statically
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { DrizzleQueryError } from 'drizzle-orm/errors' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| type BackgroundRetryDecision, | ||
| type BackgroundRetryPolicy, | ||
| backgroundRetryAttemptCeiling, | ||
| getBackgroundRetryDecision, | ||
| getDatabaseRetryAt, | ||
| } from '@/lib/core/errors/background-retry' | ||
|
|
||
| const MINUTE = 60 * 1000 | ||
| const POLICY: BackgroundRetryPolicy = { | ||
| maxAttempts: 3, | ||
| database: { maxAttempts: 6, baseDelayMs: 2 * MINUTE, maxDelayMs: 30 * MINUTE }, | ||
| } | ||
| const NOW = Date.parse('2026-01-01T00:00:00.000Z') | ||
|
|
||
| function failedQuery(code: string, message = 'private driver detail'): DrizzleQueryError { | ||
| return new DrizzleQueryError( | ||
| 'private SQL', | ||
| ['private'], | ||
| Object.assign(new Error(message), { code }) | ||
| ) | ||
| } | ||
|
|
||
| function delayOf(decision: BackgroundRetryDecision): number { | ||
| if (!decision || !('retryAt' in decision)) throw new Error('expected a scheduled retry') | ||
| return decision.retryAt.getTime() - NOW | ||
| } | ||
|
|
||
| describe('getBackgroundRetryDecision', () => { | ||
| it.each([ | ||
| ['capacity', failedQuery('57014', 'canceling statement due to statement timeout')], | ||
| ['conflict', failedQuery('40P01', 'deadlock detected')], | ||
| ['connection', failedQuery('CONNECTION_CLOSED')], | ||
| ])('waits minutes after a %s failure', (_label, error) => { | ||
| const delay = delayOf(getBackgroundRetryDecision(error, 1, POLICY, NOW)) | ||
| expect(delay).toBeGreaterThanOrEqual(2 * MINUTE * 0.8) | ||
| expect(delay).toBeLessThanOrEqual(2 * MINUTE * 1.2) | ||
| }) | ||
|
|
||
| it('doubles the delay per attempt up to the ceiling', () => { | ||
| const error = failedQuery('55P03') | ||
| const delays = [1, 2, 3, 4, 5].map((attempt) => | ||
| delayOf(getBackgroundRetryDecision(error, attempt, POLICY, NOW)) | ||
| ) | ||
| const bases = [2, 4, 8, 16, 30].map((minutes) => minutes * MINUTE) | ||
| delays.forEach((delay, index) => { | ||
| expect(delay).toBeGreaterThanOrEqual(bases[index] * 0.8) | ||
| expect(delay).toBeLessThanOrEqual(bases[index] * 1.2) | ||
| }) | ||
| const longPolicy = { ...POLICY, database: { ...POLICY.database, maxAttempts: 20 } } | ||
| expect(delayOf(getBackgroundRetryDecision(error, 12, longPolicy, NOW))).toBeLessThanOrEqual( | ||
| 30 * MINUTE * 1.2 | ||
| ) | ||
| }) | ||
|
|
||
| it('stops database retries at their own attempt ceiling', () => { | ||
| const error = failedQuery('53300') | ||
| expect(getBackgroundRetryDecision(error, 5, POLICY, NOW)).toHaveProperty('retryAt') | ||
| expect(getBackgroundRetryDecision(error, 6, POLICY, NOW)).toEqual({ skipRetrying: true }) | ||
| }) | ||
|
|
||
| it('keeps the task default for other failures until their attempt ceiling', () => { | ||
| const error = failedQuery('23505') | ||
| expect(getBackgroundRetryDecision(error, 1, POLICY, NOW)).toBeUndefined() | ||
| expect(getBackgroundRetryDecision(error, 2, POLICY, NOW)).toBeUndefined() | ||
| expect(getBackgroundRetryDecision(error, 3, POLICY, NOW)).toEqual({ skipRetrying: true }) | ||
| }) | ||
|
|
||
| it('does not stretch an explicit cancellation onto the database pacing', () => { | ||
| const cancelled = failedQuery('57014', 'canceling statement due to user request') | ||
| expect(getBackgroundRetryDecision(cancelled, 1, POLICY, NOW)).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('getDatabaseRetryAt', () => { | ||
| it('returns null for a failure that is not a transient database failure', () => { | ||
| expect(getDatabaseRetryAt(new Error('parser failed'), 1, POLICY, NOW)).toBeNull() | ||
| }) | ||
|
|
||
| it('returns null once the database attempts are spent', () => { | ||
| expect(getDatabaseRetryAt(failedQuery('40001'), 6, POLICY, NOW)).toBeNull() | ||
| }) | ||
| }) | ||
|
|
||
| describe('backgroundRetryAttemptCeiling', () => { | ||
| it('covers whichever kind of failure retries longer', () => { | ||
| expect(backgroundRetryAttemptCeiling(POLICY)).toBe(6) | ||
| expect(backgroundRetryAttemptCeiling({ ...POLICY, maxAttempts: 8 })).toBe(8) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.