Skip to content

Commit 2791a50

Browse files
waleedlatif1claude
andcommitted
fix(usage): bound the marked-cell count's scan, release the lease when counting a failed pass
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fe10138 commit 2791a50

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎apps/sim/lib/billing/activity-rollup/pass.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,15 +666,21 @@ export async function runActivityRollupPass(
666666
}
667667

668668
/**
669-
* Counts a pass that failed after taking the lease, and logs the run of failures loudly. Matched on
670-
* the owner alone, so a pass that overran its lease is still counted; one whose lease another pass
671-
* took over was already counted by that takeover, and matches nothing.
669+
* Counts a pass that failed after taking the lease and releases the lease in the same update, so a
670+
* takeover never counts the failure again; logs the run of failures loudly. Matched on the owner
671+
* alone, so a pass that overran its lease is still counted; one whose lease another pass took over
672+
* was already counted by that takeover, and matches nothing.
672673
*/
673674
async function recordFailedPass(owner: string, error: unknown): Promise<void> {
674675
try {
675676
const [state] = await db
676677
.update(activityRollupState)
677-
.set({ failedPasses: sql`${activityRollupState.failedPasses} + 1`, updatedAt: sql`now()` })
678+
.set({
679+
failedPasses: sql`${activityRollupState.failedPasses} + 1`,
680+
leaseOwner: null,
681+
leaseExpiresAt: null,
682+
updatedAt: sql`now()`,
683+
})
678684
.where(
679685
and(
680686
eq(activityRollupState.id, WORKFLOW_ROLLUP_ID),

‎apps/sim/lib/billing/core/organization-activity-rollup-queries.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,19 @@ function storedCells(scope: ActivityScope, span: InstantRange): SQL {
151151
* The rollup cells of the scope's workspaces that have a pending dirty mark, as
152152
* `(workspace_id, h)`: known to be stale until the mark is processed (or while it backs off after
153153
* failing), so reads take exactly those workspace-hours live and every other workspace's stored
154-
* cell for the hour still serves. Read on the mark queue's key, in the read's snapshot.
154+
* cell for the hour still serves. Read on the mark queue's key, in the read's snapshot. Each row is
155+
* a distinct cell: the queue's key is the workspace-hour, and each mark joins one workspace.
155156
*/
156157
function markedCells(scope: ActivityScope, span: InstantRange): SQL {
157-
return sql`SELECT DISTINCT d.workspace_id, d.bucket_start AS h FROM ${workspace} w
158+
return sql`SELECT d.workspace_id, d.bucket_start AS h FROM ${workspace} w
158159
JOIN ${activityRollupDirty} d ON d.workspace_id = w.id
159160
AND d.bucket_start >= ${utcTimestamp(span.from)} AND d.bucket_start < ${utcTimestamp(span.to)}
160161
WHERE w.organization_id = ${scope.organizationId}
161162
${scope.workspaceId ? sql`AND w.id = ${scope.workspaceId}` : sql``}`
162163
}
163164

164165
/**
165-
* Whether the scope has more than {@link MAX_LIVE_MARKED_CELLS} marked cells in `span`, counted
166+
* Whether the scope has more than {@link MAX_LIVE_MARKED_CELLS} marked cells in `span`, reading
166167
* no further than one past the bound.
167168
*/
168169
async function exceedsLiveMarkedCells(

0 commit comments

Comments
 (0)