Skip to content

Commit 041e036

Browse files
committed
fix(usage): give deferred hours their own paging budget without counting an attempt
1 parent 503fff1 commit 041e036

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

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

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ const HEAVY_RETRY_MS = 2 * 60 * 1000
147147
const MAX_HEAVY_RETRY_MS = 6 * 60 * 1000
148148

149149
/**
150-
* Backs off a heavy hour (before each retry, and when a first read finds it too heavy), so passes stop retrying it every tick and their other
151-
* steps keep running. Each blocked hour keeps its own backoff, so a heavy hour on the finalize side
152-
* and one on the backfill side never reset each other's delay. Returns the delay.
150+
* Backs off a heavy hour (before each retry, and when a first read finds it too heavy), so passes
151+
* stop retrying it every tick and their other steps keep running. Each blocked hour keeps its own
152+
* backoff, so a heavy hour on the finalize side and one on the backfill side never reset each
153+
* other's delay. Returns the delay.
153154
*/
154155
async function blockHour(hour: Date, attempts: number): Promise<number> {
155156
const retryInMs = inflightBackoffMs(attempts - 1)
@@ -175,9 +176,10 @@ async function deferPaging(hour: Date): Promise<void> {
175176

176177
/**
177178
* Aggregates and writes `hour` with the progress it completes, or leaves it: still backing off,
178-
* found too heavy to read, or out of pass budget while paging (left to the next pass). A retry of a blocked hour skips the reads known to time out and
179-
* pages through workspaces under its own budget, which grows with its attempts (the lease is
180-
* extended to cover it), and a write that completes it forgets the block in the same transaction.
179+
* found too heavy to read, or out of pass budget while paging (left to the next pass). A blocked
180+
* or deferred hour skips the reads known to time out and pages through workspaces under its own
181+
* budget (a retry's grows with its attempts; the lease is extended to cover it), and a write that
182+
* completes it forgets the block in the same transaction.
181183
*/
182184
async function computeHour(
183185
owner: string,
@@ -196,28 +198,40 @@ async function computeHour(
196198
.from(activityRollupBlocked)
197199
.where(eq(activityRollupBlocked.bucketStart, hour))
198200
if (block && block.retryAt.getTime() > Date.now()) return { state: null, runs: 0 }
199-
/** An hour a pass ran out of budget paging: paged from the start, with no backoff or attempt. */
201+
/**
202+
* An hour a pass ran out of budget paging is deferred, not a retry: it is paged from the start
203+
* under its own budget, with no attempt counted and no backoff, only a hold for that window so a
204+
* pass that dies paging it does not start it again at once. A deferred hour that runs out of its
205+
* own budget too is heavy after all, and is backed off from then on.
206+
*/
200207
const deferred = block?.attempts === 0
201-
const retry = block !== undefined && !deferred
202-
let retryBudgetMs = 0
208+
let budgetMs = 0
203209
let retryInMs: number | undefined
204-
if (retry) {
205-
/**
206-
* The retry's backoff is committed before it starts, so a retry that crashes or loses its
207-
* lease is not tried again until the backoff passes.
208-
*/
209-
const attempts = block.attempts + 1
210-
retryInMs = await blockHour(hour, attempts)
211-
retryBudgetMs = Math.min(MAX_HEAVY_RETRY_MS, HEAVY_RETRY_MS * attempts)
212-
await extendLease(owner, retryBudgetMs + LEASE_MARGIN_MS)
210+
if (block) {
211+
if (deferred) {
212+
budgetMs = HEAVY_RETRY_MS
213+
await db
214+
.update(activityRollupBlocked)
215+
.set({ retryAt: new Date(Date.now() + budgetMs + LEASE_MARGIN_MS) })
216+
.where(eq(activityRollupBlocked.bucketStart, hour))
217+
} else {
218+
/**
219+
* A retry's backoff is committed before it starts, so a retry that crashes or loses its
220+
* lease is not tried again until the backoff passes.
221+
*/
222+
const attempts = block.attempts + 1
223+
retryInMs = await blockHour(hour, attempts)
224+
budgetMs = Math.min(MAX_HEAVY_RETRY_MS, HEAVY_RETRY_MS * attempts)
225+
}
226+
await extendLease(owner, budgetMs + LEASE_MARGIN_MS)
213227
}
214228
let cells: ActivityCell[]
215229
try {
216-
cells = await aggregateHour(hour, retry ? Date.now() + retryBudgetMs : deadline, {
230+
cells = await aggregateHour(hour, block ? Date.now() + budgetMs : deadline, {
217231
byWorkspace: block !== undefined,
218232
})
219233
} catch (error) {
220-
if (error instanceof PassDeadlineError && !retry) {
234+
if (error instanceof PassDeadlineError && !block) {
221235
await deferPaging(hour)
222236
return { state: null, runs: 0 }
223237
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,9 +867,16 @@ describe('organization activity rollup', () => {
867867
try {
868868
await expect(runPass()).rejects.toThrow()
869869
expect(await blockedAttempts(hour)).toBe(0)
870+
const [held] = await connection.unsafe<{ held: boolean }[]>(
871+
`SELECT retry_at > now() + interval '1 minute' AS held FROM activity_rollup_blocked
872+
WHERE bucket_start = $1::timestamp`,
873+
[hour]
874+
)
875+
expect(held?.held).toBe(true)
870876
} finally {
871877
await ungateHour()
872878
}
879+
await connection.unsafe(`UPDATE activity_rollup_blocked SET retry_at = now()`)
873880
expect(await runPass()).toMatchObject({ finalizedHours: 1, blockedHours: 0 })
874881
await expectEquivalent()
875882
}, 300_000)

0 commit comments

Comments
 (0)