-
Notifications
You must be signed in to change notification settings - Fork 227
feat(controlplane): implement dual-bucket write for execution configs #2717
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
55d70f6
feat(controlplane): implement dual-bucket write for execution configs
pepol f5f85ba
chore: address review comments
pepol 52186fb
chore: fix tests
pepol ccf6d88
chore: implement transactional dual-write
pepol 2a302aa
chore: fix tests
pepol 41f6ab6
chore: surface rollback errors
pepol 4aef608
Merge branch 'main' into peter/eng-8555-implement-failover-cdn
pepol 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,69 @@ | ||
| import type { BlobObject, BlobStorage } from './index.js'; | ||
|
|
||
| /** | ||
| * A BlobStorage implementation that writes to two underlying stores (primary + secondary). | ||
| * | ||
| * - Writes and deletes go to both stores concurrently; both must succeed. | ||
| * - Reads try the primary first, falling back to the secondary on failure. | ||
| */ | ||
| export class DualBlobStorage implements BlobStorage { | ||
| constructor( | ||
| private primary: BlobStorage, | ||
| private secondary: BlobStorage, | ||
| ) {} | ||
|
|
||
| async putObject<Metadata extends Record<string, string>>(data: { | ||
| key: string; | ||
| abortSignal?: AbortSignal; | ||
| body: Buffer; | ||
| contentType: string; | ||
| metadata?: Metadata; | ||
| }): Promise<void> { | ||
| const results = await Promise.allSettled([this.primary.putObject(data), this.secondary.putObject(data)]); | ||
| const [primaryResult, secondaryResult] = results; | ||
|
|
||
| if (primaryResult.status === 'fulfilled' && secondaryResult.status === 'fulfilled') { | ||
| return; | ||
| } | ||
|
|
||
| // Roll back successful writes before throwing, independent of the caller's signal | ||
| const rollbacks: Promise<void>[] = []; | ||
| if (primaryResult.status === 'fulfilled') { | ||
| rollbacks.push(this.primary.deleteObject({ key: data.key })); | ||
| } | ||
| if (secondaryResult.status === 'fulfilled') { | ||
| rollbacks.push(this.secondary.deleteObject({ key: data.key })); | ||
| } | ||
| const rollbackResults = await Promise.allSettled(rollbacks); | ||
|
|
||
| const putErrors = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected').map((r) => r.reason); | ||
| const rollbackErrors = rollbackResults | ||
| .filter((r): r is PromiseRejectedResult => r.status === 'rejected') | ||
| .map((r) => r.reason); | ||
| throw new AggregateError([...putErrors, ...rollbackErrors], 'Failed to put object into storage'); | ||
| } | ||
|
|
||
| async getObject(data: { key: string; abortSignal?: AbortSignal }): Promise<BlobObject> { | ||
| try { | ||
| return await this.primary.getObject(data); | ||
| } catch (primaryError) { | ||
| try { | ||
| return await this.secondary.getObject(data); | ||
| } catch (secondaryError) { | ||
| throw new AggregateError( | ||
| [primaryError, secondaryError], | ||
| 'Both primary and secondary storage failed to get object', | ||
| ); | ||
| } | ||
| } | ||
| } | ||
pepol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async removeDirectory(data: { key: string; abortSignal?: AbortSignal }): Promise<number> { | ||
| const results = await Promise.all([this.primary.removeDirectory(data), this.secondary.removeDirectory(data)]); | ||
| return results[0]; | ||
| } | ||
|
|
||
| async deleteObject(data: { key: string; abortSignal?: AbortSignal }): Promise<void> { | ||
| await Promise.all([this.primary.deleteObject(data), this.secondary.deleteObject(data)]); | ||
| } | ||
| } | ||
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
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,185 @@ | ||
| import { describe, expect, test, vi } from 'vitest'; | ||
| import { DualBlobStorage } from '../src/core/blobstorage/dual.js'; | ||
| import type { BlobObject, BlobStorage } from '../src/core/blobstorage/index.js'; | ||
|
|
||
| function createMockBlobStorage(overrides?: Partial<BlobStorage>): BlobStorage { | ||
| return { | ||
| putObject: vi.fn().mockResolvedValue(undefined), | ||
| getObject: vi.fn().mockResolvedValue({ stream: new ReadableStream(), metadata: {} }), | ||
| removeDirectory: vi.fn().mockResolvedValue(5), | ||
| deleteObject: vi.fn().mockResolvedValue(undefined), | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('DualBlobStorage', () => { | ||
| describe('putObject', () => { | ||
| test('calls both primary and secondary', async () => { | ||
| const primary = createMockBlobStorage(); | ||
| const secondary = createMockBlobStorage(); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| const data = { key: 'test-key', body: Buffer.from('data'), contentType: 'text/plain' }; | ||
| await dual.putObject(data); | ||
|
|
||
| expect(primary.putObject).toHaveBeenCalledWith(data); | ||
| expect(secondary.putObject).toHaveBeenCalledWith(data); | ||
| }); | ||
|
|
||
| test('rejects when primary fails and rolls back secondary', async () => { | ||
| const primaryError = new Error('primary write failed'); | ||
| const primary = createMockBlobStorage({ | ||
| putObject: vi.fn().mockRejectedValue(primaryError), | ||
| }); | ||
| const secondary = createMockBlobStorage(); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| await expect( | ||
| dual.putObject({ key: 'k', body: Buffer.from('d'), contentType: 'text/plain' }), | ||
| ).rejects.toMatchObject({ | ||
| message: 'Failed to put object into storage', | ||
| errors: [primaryError], | ||
| }); | ||
|
|
||
| expect(primary.deleteObject).not.toHaveBeenCalled(); | ||
| expect(secondary.deleteObject).toHaveBeenCalledWith({ key: 'k' }); | ||
| }); | ||
|
|
||
| test('rejects when secondary fails and rolls back primary', async () => { | ||
| const secondaryError = new Error('secondary write failed'); | ||
| const primary = createMockBlobStorage(); | ||
| const secondary = createMockBlobStorage({ | ||
| putObject: vi.fn().mockRejectedValue(secondaryError), | ||
| }); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| await expect( | ||
| dual.putObject({ key: 'k', body: Buffer.from('d'), contentType: 'text/plain' }), | ||
| ).rejects.toMatchObject({ | ||
| message: 'Failed to put object into storage', | ||
| errors: [secondaryError], | ||
| }); | ||
|
|
||
| expect(primary.deleteObject).toHaveBeenCalledWith({ key: 'k' }); | ||
| expect(secondary.deleteObject).not.toHaveBeenCalled(); | ||
| }); | ||
| test('includes rollback errors in aggregate when rollback also fails', async () => { | ||
| const secondaryError = new Error('secondary write failed'); | ||
| const rollbackError = new Error('primary rollback failed'); | ||
| const primary = createMockBlobStorage({ | ||
| deleteObject: vi.fn().mockRejectedValue(rollbackError), | ||
| }); | ||
| const secondary = createMockBlobStorage({ | ||
| putObject: vi.fn().mockRejectedValue(secondaryError), | ||
| }); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| await expect( | ||
| dual.putObject({ key: 'k', body: Buffer.from('d'), contentType: 'text/plain' }), | ||
| ).rejects.toMatchObject({ | ||
| message: 'Failed to put object into storage', | ||
| errors: [secondaryError, rollbackError], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getObject', () => { | ||
| test('returns primary result when primary succeeds', async () => { | ||
| const primaryResult: BlobObject = { stream: new ReadableStream(), metadata: { source: 'primary' } }; | ||
| const primary = createMockBlobStorage({ | ||
| getObject: vi.fn().mockResolvedValue(primaryResult), | ||
| }); | ||
| const secondary = createMockBlobStorage({ | ||
| getObject: vi.fn().mockRejectedValue(new Error('secondary read failed')), | ||
| }); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| const result = await dual.getObject({ key: 'k' }); | ||
|
|
||
| expect(result).toBe(primaryResult); | ||
| }); | ||
|
|
||
| test('falls back to secondary when primary fails', async () => { | ||
| const secondaryResult: BlobObject = { stream: new ReadableStream(), metadata: { source: 'secondary' } }; | ||
| const primary = createMockBlobStorage({ | ||
| getObject: vi.fn().mockRejectedValue(new Error('primary read failed')), | ||
| }); | ||
| const secondary = createMockBlobStorage({ | ||
| getObject: vi.fn().mockResolvedValue(secondaryResult), | ||
| }); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| const result = await dual.getObject({ key: 'k' }); | ||
|
|
||
| expect(result).toBe(secondaryResult); | ||
| }); | ||
|
|
||
| test('throws aggregate error with both underlying errors when both fail', async () => { | ||
| const primaryError = new Error('primary read failed'); | ||
| const secondaryError = new Error('secondary read failed'); | ||
| const primary = createMockBlobStorage({ | ||
| getObject: vi.fn().mockRejectedValue(primaryError), | ||
| }); | ||
| const secondary = createMockBlobStorage({ | ||
| getObject: vi.fn().mockRejectedValue(secondaryError), | ||
| }); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| await expect(dual.getObject({ key: 'k' })).rejects.toMatchObject({ | ||
| message: 'Both primary and secondary storage failed to get object', | ||
| errors: [primaryError, secondaryError], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('deleteObject', () => { | ||
| test('calls both primary and secondary', async () => { | ||
| const primary = createMockBlobStorage(); | ||
| const secondary = createMockBlobStorage(); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| await dual.deleteObject({ key: 'k' }); | ||
|
|
||
| expect(primary.deleteObject).toHaveBeenCalledWith({ key: 'k' }); | ||
| expect(secondary.deleteObject).toHaveBeenCalledWith({ key: 'k' }); | ||
| }); | ||
|
|
||
| test('rejects when one fails', async () => { | ||
| const primary = createMockBlobStorage({ | ||
| deleteObject: vi.fn().mockRejectedValue(new Error('delete failed')), | ||
| }); | ||
| const secondary = createMockBlobStorage(); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| await expect(dual.deleteObject({ key: 'k' })).rejects.toThrow('delete failed'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('removeDirectory', () => { | ||
| test('returns primary count when both succeed', async () => { | ||
| const primary = createMockBlobStorage({ | ||
| removeDirectory: vi.fn().mockResolvedValue(10), | ||
| }); | ||
| const secondary = createMockBlobStorage({ | ||
| removeDirectory: vi.fn().mockResolvedValue(10), | ||
| }); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| const count = await dual.removeDirectory({ key: 'dir/' }); | ||
|
|
||
| expect(count).toBe(10); | ||
| expect(primary.removeDirectory).toHaveBeenCalledWith({ key: 'dir/' }); | ||
| expect(secondary.removeDirectory).toHaveBeenCalledWith({ key: 'dir/' }); | ||
| }); | ||
|
|
||
| test('rejects when one fails', async () => { | ||
| const primary = createMockBlobStorage(); | ||
| const secondary = createMockBlobStorage({ | ||
| removeDirectory: vi.fn().mockRejectedValue(new Error('remove failed')), | ||
| }); | ||
| const dual = new DualBlobStorage(primary, secondary); | ||
|
|
||
| await expect(dual.removeDirectory({ key: 'dir/' })).rejects.toThrow('remove failed'); | ||
| }); | ||
| }); | ||
| }); |
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.