Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(billing): prevent deadlock with timeout#4949
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -405,6 +405,16 @@ export interface RecordCumulativeUsageResult { | ||
| total: number | ||
| } | ||
| /** | ||
| * Bounds the wait for the per-event-key advisory lock (and any row/index lock | ||
| * waits inside the critical section). The Go mothership gives each UpdateCost | ||
| * POST a 5s deadline, retries 3x with backoff, then dead-letters the charge | ||
| * keyed on the same idempotency key — so a stuck lock holder must surface as | ||
| * a fast, retryable failure (SQLSTATE 55P03) within that budget rather than | ||
| * an unbounded wait that pins pooled connections. | ||
| */ | ||
| const CUMULATIVE_FLUSH_LOCK_TIMEOUT_MS = 3_000 | ||
| /** | ||
| * Record a request's CUMULATIVE cost idempotently with monotonic top-up. | ||
| * | ||
| @@ -413,6 +423,9 @@ export interface RecordCumulativeUsageResult { | ||
| * each flush. A per-key transactional advisory lock serializes concurrent | ||
| * flushes so the read-then-write — including the first insert — is race-free | ||
| * (no two flushes can both believe they are first and clobber each other). | ||
| * The billing context is resolved BEFORE the transaction and the lock wait is | ||
| * bounded by `lock_timeout`, keeping the critical section to one SELECT plus | ||
| * one INSERT/UPDATE on a single pooled connection. | ||
| * | ||
| * Because every leg flushes its cumulative and this converges to the max, | ||
| * there is no under-billing if the request recovers after a partial flush, no | ||
| @@ -424,9 +437,22 @@ export async function recordCumulativeUsage( | ||
| ): Promise<RecordCumulativeUsageResult> { | ||
| const { userId, workspaceId, source, model, cost, eventKey, metadata } = params | ||
| // Resolved before the locked transaction on purpose: resolving inside it | ||
| // ran the subscription lookups on the global pool while this tx already | ||
| // held a pooled connection plus the advisory lock, so under load N | ||
| // first-flush transactions each pinned a connection while waiting for one | ||
| // more — starving the pool and queueing every same-key flush (and the Go | ||
| // side's retries) behind the stall. | ||
| const billingContext = await resolveBillingContext(userId) | ||
icecrasher321 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return db.transaction(async (tx) => { | ||
| // Serialize all flushes for this request (lock auto-releases at tx end). | ||
| await tx.execute(sql`SELECT pg_advisory_xact_lock(hashtext(${eventKey}))`) | ||
| // Serialize all flushes for this request (lock auto-releases at tx end), | ||
| // with a bounded wait so a pathological holder fails this flush fast and | ||
| // lets the caller retry instead of hanging the connection. | ||
| await tx.execute( | ||
| sql`select set_config('lock_timeout', ${`${CUMULATIVE_FLUSH_LOCK_TIMEOUT_MS}ms`}, true)` | ||
| ) | ||
| await tx.execute(sql`select pg_advisory_xact_lock(hashtextextended(${eventKey}, 0))`) | ||
| const [existing] = await tx | ||
| .select({ id: usageLog.id, cost: usageLog.cost }) | ||
| @@ -449,12 +475,14 @@ export async function recordCumulativeUsage( | ||
| .set({ cost: newTotal.toString(), metadata: metadata ?? null }) | ||
| .where(eq(usageLog.id, existing.id)) | ||
| } else { | ||
| // First flush for this request: insert the canonical row (recordUsage | ||
| // resolves billing entity/period). Runs in the same tx + advisory lock. | ||
| // First flush for this request: insert the canonical row with the | ||
| // pre-resolved billing context. Runs in the same tx + advisory lock. | ||
| await recordUsage({ | ||
| userId, | ||
| workspaceId, | ||
| tx, | ||
| billingEntity: billingContext.billingEntity, | ||
| billingPeriod: billingContext.billingPeriod, | ||
icecrasher321 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| entries: [ | ||
| { | ||
| category: 'model', | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.