@@ -147,9 +147,10 @@ const HEAVY_RETRY_MS = 2 * 60 * 1000
147147const 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 */
154155async 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 */
182184async 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 }
0 commit comments