-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(webapp,run-engine): stop batchTriggerAndWait hanging when item streaming never completes #4397
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
Open
ericallam
wants to merge
7
commits into
main
Choose a base branch
from
feature/tri-8377-batchtriggerandwait-hangs-forever-when-batch-phase-2-item
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b33088a
fix(webapp,run-engine): stop batchTriggerAndWait hanging when item st…
ericallam 40dd617
fix(webapp,run-engine): scope batch stream grants to the environment …
ericallam d437524
fix(webapp): catalog the expireBatch waitpoint completion site
ericallam ffe90a6
test(run-engine): count updateMany batch writes in the routing store
ericallam befbf4f
fix(run-engine,webapp): make batch expiration resumable and schedule …
ericallam 1ce58a1
fix(webapp): keep a throwing bypass from failing the request
ericallam 2bf591f
fix(webapp): match the batch item bypass auth to the route
ericallam 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: fix | ||
| --- | ||
|
|
||
| Batch triggers no longer fail to start their runs when an environment is under heavy API load. If a batch still can't finish being created, `batchTriggerAndWait` now fails with an error instead of leaving the parent run waiting forever, and the batches page says so rather than reporting that it resumed. |
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
105 changes: 105 additions & 0 deletions
105
apps/webapp/app/runEngine/concerns/batchStreamGrants.server.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,105 @@ | ||
| import { createRedisClient, type RedisClient, type RedisWithClusterOptions } from "~/redis.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
|
|
||
| export type BatchStreamGrantsOptions = { | ||
| redis: RedisWithClusterOptions; | ||
| /** How many phase 2 requests a created batch is allowed. */ | ||
| attempts: number; | ||
| /** How long the grant survives, matching how long a batch may legitimately be sealing. */ | ||
| ttlMs: number; | ||
| }; | ||
|
|
||
| const KEY_PREFIX = "batch-stream-grant:"; | ||
|
|
||
| /** | ||
| * Admission for phase 2 of the 2-phase batch API. | ||
| * | ||
| * Phase 1 (`POST /api/v3/batches`) already passes its own batch rate limiter, which fixes | ||
| * the batch's `expectedCount` and blocks the parent run on the batch's waitpoint. Phase 2 | ||
| * (`POST /api/v3/batches/:id/items`) is the only thing that can seal that batch, so having | ||
| * the general API limiter reject it strands the batch and the parent with it. | ||
| * | ||
| * Phase 1 therefore mints a bounded grant, and phase 2 spends it to bypass the general | ||
| * limiter. Admission stays a single decision made in phase 1, but the bypass is capped at | ||
| * `attempts` requests per batch rather than being unconditional. | ||
| */ | ||
| export class BatchStreamGrants { | ||
| private readonly redis: RedisClient; | ||
|
|
||
| constructor(private readonly options: BatchStreamGrantsOptions) { | ||
| this.redis = createRedisClient("batchStreamGrants", options.redis); | ||
| this.#registerCommands(); | ||
| } | ||
|
|
||
| /** | ||
| * Grant a newly created batch its phase 2 budget. Never throws: a batch that fails to get | ||
| * a grant still works, it just falls back to the general rate limiter for streaming. | ||
| */ | ||
| async mint(environmentId: string, batchId: string): Promise<void> { | ||
| try { | ||
| await this.redis.set( | ||
| this.#key(environmentId, batchId), | ||
| this.options.attempts, | ||
| "PX", | ||
| this.options.ttlMs | ||
| ); | ||
| } catch (error) { | ||
| logger.warn("BatchStreamGrants: failed to mint grant", { | ||
| batchId, | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Consume one phase 2 request from the batch's grant. | ||
| * | ||
| * Returns false when there is no grant, when the budget is spent, or when Redis is | ||
| * unreachable, so the caller falls back to the general rate limiter rather than opening | ||
| * an unbounded bypass. | ||
| */ | ||
| async spend(environmentId: string, batchId: string): Promise<boolean> { | ||
| try { | ||
| // @ts-expect-error - Custom command defined via defineCommand | ||
| const remaining = (await this.redis.spendBatchStreamGrant( | ||
| this.#key(environmentId, batchId) | ||
| )) as number; | ||
|
|
||
| return remaining >= 0; | ||
| } catch (error) { | ||
| logger.warn("BatchStreamGrants: failed to spend grant", { | ||
| batchId, | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| async quit(): Promise<void> { | ||
| await this.redis.quit(); | ||
| } | ||
|
|
||
| /** | ||
| * Scoped to the environment as well as the batch, so a caller authenticated against a | ||
| * different environment can never spend this batch's grant even if they know its id. | ||
| */ | ||
| #key(environmentId: string, batchId: string): string { | ||
| return `${KEY_PREFIX}${environmentId}:${batchId}`; | ||
| } | ||
|
|
||
| #registerCommands(): void { | ||
| this.redis.defineCommand("spendBatchStreamGrant", { | ||
| numberOfKeys: 1, | ||
| lua: ` | ||
| local remaining = tonumber(redis.call('GET', KEYS[1])) | ||
|
|
||
| if not remaining or remaining <= 0 then | ||
| return -1 | ||
| end | ||
|
|
||
| return redis.call('DECR', KEYS[1]) | ||
| `, | ||
| }); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
apps/webapp/app/runEngine/concerns/batchStreamGrantsInstance.server.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,20 @@ | ||
| import { env } from "~/env.server"; | ||
| import { singleton } from "~/utils/singleton"; | ||
| import { BatchStreamGrants } from "./batchStreamGrants.server"; | ||
|
|
||
| export const batchStreamGrants = singleton( | ||
| "batchStreamGrants", | ||
| () => | ||
| new BatchStreamGrants({ | ||
| redis: { | ||
| port: env.RATE_LIMIT_REDIS_PORT, | ||
| host: env.RATE_LIMIT_REDIS_HOST, | ||
| username: env.RATE_LIMIT_REDIS_USERNAME, | ||
| password: env.RATE_LIMIT_REDIS_PASSWORD, | ||
| tlsDisabled: env.RATE_LIMIT_REDIS_TLS_DISABLED === "true", | ||
| clusterMode: env.RATE_LIMIT_REDIS_CLUSTER_MODE_ENABLED === "1", | ||
| }, | ||
| attempts: env.BATCH_STREAM_GRANT_ATTEMPTS, | ||
| ttlMs: env.BATCH_SEAL_TIMEOUT_MS, | ||
| }) | ||
| ); |
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
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
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.