-
Notifications
You must be signed in to change notification settings - Fork 3.9k
fix(logs): record a run's cost before it reads finished, and hold sync execute responses until the log is final #8473
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee376e6
fix(logs): record a run's cost before it reads finished, and hold syn…
waleedlatif1 7e0af7b
fix(logs): stop the ledger-order reader when a completion throws
waleedlatif1 dca0465
fix(logs): keep a completion failure visible when the ledger-order re…
waleedlatif1 ec69d07
fix(logs): let the ledger-order reader observe every finished run
waleedlatif1 1ece8aa
fix(logs): assert ledger-before-terminal ordering deterministically a…
waleedlatif1 4a3b2c0
fix(logs): scope the ledger-lock waiter to the execution and surface …
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
185 changes: 185 additions & 0 deletions
185
apps/sim/lib/logs/execution/completion-ledger-order.integration.ts
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,185 @@ | ||
| /** | ||
| * Completion ordering against real PostgreSQL: a run's usage ledger is written before its | ||
| * log reads terminal, so a reader that sees a finished run always sees its cost. | ||
| */ | ||
| import { db } from '@sim/db' | ||
| import { | ||
| usageLog, | ||
| user, | ||
| workflow, | ||
| workflowExecutionLogs, | ||
| workflowExecutionSnapshots, | ||
| workspace, | ||
| } from '@sim/db/schema' | ||
| import { createDeferred } from '@sim/testing' | ||
| import { generateId } from '@sim/utils/id' | ||
| import { eq, sql } from 'drizzle-orm' | ||
| import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' | ||
| import { resolveBillingAttribution } from '@/lib/billing/core/billing-attribution' | ||
| import { acquireAdvisoryXactLock } from '@/lib/db/advisory-locks' | ||
| import { buildCostLedger } from '@/lib/logs/cost-ledger' | ||
| import { executionLogger } from '@/lib/logs/execution/logger' | ||
| import { calculateCostSummary } from '@/lib/logs/execution/logging-factory' | ||
| import type { WorkflowState } from '@/lib/logs/types' | ||
|
|
||
| const ids = { | ||
| owner: `ledger-order-owner-${generateId()}`, | ||
| workspace: generateId(), | ||
| workflow: generateId(), | ||
| } | ||
|
|
||
| /** The advisory lock the usage ledger write takes for its execution before inserting. */ | ||
| const USAGE_RECONCILE_LOCK = 'execution_usage_reconcile' | ||
| const EXECUTION_FEE = 0.005 | ||
|
|
||
| const workflowState: WorkflowState = { | ||
| blocks: { | ||
| start: { | ||
| id: 'start', | ||
| type: 'starter', | ||
| name: 'Start', | ||
| position: { x: 0, y: 0 }, | ||
| subBlocks: {}, | ||
| outputs: {}, | ||
| enabled: true, | ||
| }, | ||
| }, | ||
| edges: [], | ||
| loops: {}, | ||
| parallels: {}, | ||
| } | ||
|
|
||
| async function startExecution(executionId: string) { | ||
| await executionLogger.startWorkflowExecution({ | ||
| workflowId: ids.workflow, | ||
| workspaceId: ids.workspace, | ||
| executionId, | ||
| trigger: { type: 'api', source: 'api', timestamp: new Date().toISOString() }, | ||
| environment: { | ||
| variables: {}, | ||
| workflowId: ids.workflow, | ||
| executionId, | ||
| userId: ids.owner, | ||
| workspaceId: ids.workspace, | ||
| }, | ||
| workflowState, | ||
| }) | ||
| } | ||
|
|
||
| async function logRow(executionId: string) { | ||
| const [row] = await db | ||
| .select({ status: workflowExecutionLogs.status, costTotal: workflowExecutionLogs.costTotal }) | ||
| .from(workflowExecutionLogs) | ||
| .where(eq(workflowExecutionLogs.executionId, executionId)) | ||
| return row | ||
| } | ||
|
|
||
| beforeAll(async () => { | ||
| const now = new Date() | ||
| await db.insert(user).values({ | ||
| id: ids.owner, | ||
| name: 'Ledger Order', | ||
| email: `${ids.owner}@ledger-order.test`, | ||
| emailVerified: true, | ||
| createdAt: now, | ||
| updatedAt: now, | ||
| }) | ||
| await db.insert(workspace).values({ | ||
| id: ids.workspace, | ||
| name: 'Ledger Order', | ||
| ownerId: ids.owner, | ||
| billedAccountUserId: ids.owner, | ||
| }) | ||
| await db.insert(workflow).values({ | ||
| id: ids.workflow, | ||
| userId: ids.owner, | ||
| workspaceId: ids.workspace, | ||
| name: 'Ledger Order', | ||
| lastSynced: now, | ||
| createdAt: now, | ||
| updatedAt: now, | ||
| }) | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await db.delete(usageLog).where(eq(usageLog.workflowId, ids.workflow)) | ||
| await db.delete(workflowExecutionLogs).where(eq(workflowExecutionLogs.workflowId, ids.workflow)) | ||
| await db | ||
| .delete(workflowExecutionSnapshots) | ||
| .where(eq(workflowExecutionSnapshots.workflowId, ids.workflow)) | ||
| await db.delete(workspace).where(eq(workspace.id, ids.workspace)) | ||
| await db.delete(user).where(eq(user.id, ids.owner)) | ||
| }) | ||
|
|
||
| /** | ||
| * Whether a session waits on this execution's ledger lock. A bigint advisory key is stored | ||
| * split across `classid` (high 32 bits) and `objid` (low 32 bits) with `objsubid = 1`. | ||
| */ | ||
| async function isLedgerLockAwaited(executionId: string) { | ||
| const rows = await db.execute<{ waiting: boolean }>(sql` | ||
| SELECT EXISTS ( | ||
| SELECT 1 FROM pg_locks | ||
| WHERE locktype = 'advisory' AND NOT granted AND objsubid = 1 | ||
| AND ((classid::bigint << 32) | objid::bigint) = hashtextextended(${executionId}, 0) | ||
| ) AS waiting | ||
| `) | ||
| return Boolean(rows[0]?.waiting) | ||
| } | ||
|
|
||
| describe('completeWorkflowExecution', () => { | ||
| it('writes the cost ledger before the run reads finished', async () => { | ||
| const billingAttribution = await resolveBillingAttribution({ | ||
| actorUserId: ids.owner, | ||
| workspaceId: ids.workspace, | ||
| }) | ||
| const executionId = generateId() | ||
| await startExecution(executionId) | ||
|
|
||
| /** Holds the ledger write at its lock, so the log can be read while it waits there. */ | ||
| const lockHeld = createDeferred<void>() | ||
| const releaseLock = createDeferred<void>() | ||
| const holder = db.transaction(async (tx) => { | ||
| await acquireAdvisoryXactLock(tx, USAGE_RECONCILE_LOCK, executionId) | ||
| lockHeld.resolve() | ||
| await releaseLock.promise | ||
| }) | ||
| await lockHeld.promise | ||
|
|
||
| const completion = executionLogger.completeWorkflowExecution({ | ||
| executionId, | ||
| endedAt: new Date().toISOString(), | ||
| totalDurationMs: 5, | ||
| costSummary: calculateCostSummary([], { baseExecutionCharge: EXECUTION_FEE }), | ||
| finalOutput: {}, | ||
| traceSpans: [], | ||
| status: 'completed', | ||
| actorUserId: ids.owner, | ||
| billingAttribution, | ||
| }) | ||
|
|
||
| /** A completion that settles before blocking surfaces its own outcome instead of a timeout. */ | ||
| const settledWithoutBlocking = completion.then(() => { | ||
| throw new Error('Completion finished without waiting on the ledger lock') | ||
| }) | ||
|
|
||
| let statusWhileLedgerBlocked: string | undefined | ||
| try { | ||
| await Promise.race([ | ||
| vi.waitFor(async () => { | ||
| expect(await isLedgerLockAwaited(executionId)).toBe(true) | ||
| }), | ||
| settledWithoutBlocking, | ||
| ]) | ||
| statusWhileLedgerBlocked = (await logRow(executionId))?.status | ||
| } finally { | ||
| releaseLock.resolve() | ||
| await holder | ||
| } | ||
| await completion | ||
|
|
||
| expect(statusWhileLedgerBlocked).toBe('running') | ||
| expect(await logRow(executionId)).toMatchObject({ status: 'completed' }) | ||
| expect((await buildCostLedger(executionId))?.total).toBeCloseTo(EXECUTION_FEE, 8) | ||
| expect(Number((await logRow(executionId))?.costTotal)).toBeCloseTo(EXECUTION_FEE, 8) | ||
| }) | ||
| }) |
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
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.