-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(batch): retry R2 upload on transient failure in BatchPayloadProcessor #3331
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
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,9 @@ | ||
| --- | ||
| area: webapp | ||
| type: fix | ||
| --- | ||
|
|
||
| Fix transient R2/object store upload failures during batchTrigger() item streaming. | ||
|
|
||
| - Added p-retry (3 attempts, 500ms–2s exponential backoff) around `uploadPacketToObjectStore` in `BatchPayloadProcessor.process()` so transient network errors self-heal server-side rather than aborting the entire batch stream. | ||
| - Removed `x-should-retry: false` from the 500 response on the batch items route so the SDK's existing 5xx retry path can recover if server-side retries are exhausted. Item deduplication by index makes full-stream retries safe. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
matt-aitken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // --- Module mocks (must come before imports) --- | ||
|
|
||
| vi.mock("~/v3/objectStore.server", () => ({ | ||
| hasObjectStoreClient: vi.fn().mockReturnValue(true), | ||
| uploadPacketToObjectStore: vi.fn(), | ||
| })); | ||
|
|
||
| // Threshold of 10 bytes so any non-trivial payload triggers offloading | ||
| vi.mock("~/env.server", () => ({ | ||
| env: { | ||
| BATCH_PAYLOAD_OFFLOAD_THRESHOLD: 10, | ||
| TASK_PAYLOAD_OFFLOAD_THRESHOLD: 10, | ||
| OBJECT_STORE_DEFAULT_PROTOCOL: undefined, | ||
| }, | ||
| })); | ||
|
|
||
| // Execute the span callback synchronously without real OTel | ||
| vi.mock("~/v3/tracer.server", () => ({ | ||
| startActiveSpan: vi.fn(async (_name: string, fn: (span: any) => any) => | ||
| fn({ setAttribute: vi.fn() }) | ||
| ), | ||
| })); | ||
matt-aitken marked this conversation as resolved.
Show resolved
Hide resolved
matt-aitken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import { BatchPayloadProcessor } from "../../app/runEngine/concerns/batchPayloads.server"; | ||
| import * as objectStore from "~/v3/objectStore.server"; | ||
|
|
||
| vi.setConfig({ testTimeout: 30_000 }); | ||
|
|
||
| // Minimal AuthenticatedEnvironment shape required by BatchPayloadProcessor | ||
| const mockEnvironment = { | ||
| id: "env-test", | ||
| slug: "production", | ||
| project: { externalRef: "proj-ext-ref" }, | ||
| } as any; | ||
|
|
||
| describe("BatchPayloadProcessor", () => { | ||
| let mockUpload: ReturnType<typeof vi.mocked<typeof objectStore.uploadPacketToObjectStore>>; | ||
|
|
||
| beforeEach(() => { | ||
| mockUpload = vi.mocked(objectStore.uploadPacketToObjectStore); | ||
| mockUpload.mockReset(); | ||
| }); | ||
|
|
||
| it("offloads a large payload successfully on first attempt", async () => { | ||
| mockUpload.mockResolvedValueOnce("batch_abc/item_0/payload.json"); | ||
|
|
||
| const processor = new BatchPayloadProcessor(); | ||
| const result = await processor.process( | ||
| '{"message":"hello world"}', | ||
| "application/json", | ||
| "batch-internal-abc", | ||
| 0, | ||
| mockEnvironment | ||
| ); | ||
|
|
||
| expect(result.wasOffloaded).toBe(true); | ||
| expect(result.payloadType).toBe("application/store"); | ||
| expect(result.payload).toBe("batch_abc/item_0/payload.json"); | ||
| expect(mockUpload).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("retries on transient fetch failure and succeeds on third attempt", async () => { | ||
| mockUpload | ||
| .mockRejectedValueOnce(new Error("fetch failed")) | ||
| .mockRejectedValueOnce(new Error("fetch failed")) | ||
| .mockResolvedValueOnce("batch_abc/item_0/payload.json"); | ||
|
|
||
| const processor = new BatchPayloadProcessor(); | ||
| const result = await processor.process( | ||
| '{"message":"hello world"}', | ||
| "application/json", | ||
| "batch-internal-abc", | ||
| 0, | ||
| mockEnvironment | ||
| ); | ||
|
|
||
| expect(result.wasOffloaded).toBe(true); | ||
| expect(mockUpload).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it("throws after exhausting all retry attempts", async () => { | ||
| mockUpload.mockRejectedValue(new Error("fetch failed")); | ||
|
|
||
| const processor = new BatchPayloadProcessor(); | ||
|
|
||
| await expect( | ||
| processor.process( | ||
| '{"message":"hello world"}', | ||
| "application/json", | ||
| "batch-internal-abc", | ||
| 0, | ||
| mockEnvironment | ||
| ) | ||
| ).rejects.toThrow("Failed to upload large payload to object store: fetch failed"); | ||
|
|
||
| // 1 initial attempt + 3 retries = 4 total calls | ||
| expect(mockUpload).toHaveBeenCalledTimes(4); | ||
| }); | ||
|
|
||
| it("does not offload when there is no payload data", async () => { | ||
| const processor = new BatchPayloadProcessor(); | ||
| const result = await processor.process( | ||
| undefined, | ||
| "application/json", | ||
| "batch-internal-abc", | ||
| 0, | ||
| mockEnvironment | ||
| ); | ||
|
|
||
| expect(result.wasOffloaded).toBe(false); | ||
| expect(mockUpload).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
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.