-
Notifications
You must be signed in to change notification settings - Fork 15
refactor: refactor sharded wal to handle coordinator #1237
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
BioPhoton
merged 29 commits into
main
from
feat/utils/handle-coordinator-in-sharded-wal
Feb 25, 2026
Merged
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ffcc95d
refactor: refactor sharded wal to handle coordinator
BioPhoton cc0b481
Merge remote-tracking branch 'origin/main' into feat/utils/handle-coo…
BioPhoton aa0395f
refactor: wip
BioPhoton 000be48
Merge branch 'main' into feat/utils/handle-coordinator-in-sharded-wal
BioPhoton 234a83b
Merge remote-tracking branch 'origin/feat/utils/handle-coordinator-in…
BioPhoton eddf9cc
refactor: wip
BioPhoton f8f2847
refactor: wip
BioPhoton 14e6390
refactor: wip
BioPhoton 75ccc48
refactor: wip
BioPhoton 2d94b12
refactor: update const name
BioPhoton 6719873
refactor: fix int tests
BioPhoton 2e8910b
refactor: fix unit tests
BioPhoton d150c70
refactor: fix lint
BioPhoton b46e330
Update packages/utils/src/lib/wal.int.test.ts
BioPhoton ca1c0c8
Update packages/utils/src/lib/wal-sharded.int.test.ts
BioPhoton 0e72e8c
Update packages/utils/src/lib/wal-sharded.int.test.ts
BioPhoton d2e885d
Update packages/utils/src/lib/wal-sharded.unit.test.ts
BioPhoton 55816b9
Update packages/utils/src/lib/wal-sharded.ts
BioPhoton 287c134
Merge remote-tracking branch 'origin/main' into feat/utils/handle-coo…
BioPhoton 8e9aeeb
refactor: adjust groupID handling
BioPhoton ba94649
refactor: fix comment
BioPhoton 270d3ec
refactor: fix naming
BioPhoton ce3ea34
refactor: comment helper
BioPhoton f32147d
refactor: comment adjustments
BioPhoton 674fe70
refactor: adjust dir creation
BioPhoton 1d9aa70
Update packages/utils/src/lib/wal.unit.test.ts
BioPhoton 344180f
refactor: cleanup
BioPhoton add96f1
refactor: docs
BioPhoton 00f4516
refactor: fix unit tests
BioPhoton 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
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,258 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { SHARDED_WAL_COORDINATOR_ID_ENV_VAR } from './profiler/constants.js'; | ||
| import { ShardedWal } from './wal-sharded.js'; | ||
| import { type WalFormat, type WalRecord, stringCodec } from './wal.js'; | ||
|
|
||
| describe('ShardedWal Integration', () => { | ||
| const testDir = path.join( | ||
| process.cwd(), | ||
| 'tmp', | ||
| 'int', | ||
| 'utils', | ||
| 'wal-sharded', | ||
| ); | ||
| const makeMockFormat = <T extends WalRecord>( | ||
| overrides: Partial<WalFormat<T>>, | ||
| ): WalFormat<T> => { | ||
| const { | ||
| baseName = 'wal', | ||
| walExtension = '.log', | ||
| finalExtension = '.json', | ||
| codec = stringCodec<T>(), | ||
| finalizer = records => `${JSON.stringify(records)}\n`, | ||
| } = overrides; | ||
|
|
||
| return { | ||
| baseName, | ||
| walExtension, | ||
| finalExtension, | ||
| codec, | ||
| finalizer, | ||
| }; | ||
| }; | ||
| let shardedWal: ShardedWal; | ||
|
|
||
| beforeEach(() => { | ||
| if (fs.existsSync(testDir)) { | ||
| fs.rmSync(testDir, { recursive: true, force: true }); | ||
| } | ||
| fs.mkdirSync(testDir, { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (shardedWal) { | ||
| shardedWal.cleanupIfCoordinator(); | ||
| } | ||
| if (fs.existsSync(testDir)) { | ||
| fs.rmSync(testDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('should create and finalize shards correctly', () => { | ||
| shardedWal = new ShardedWal({ | ||
| debug: false, | ||
| dir: testDir, | ||
| format: makeMockFormat({ | ||
| baseName: 'trace', | ||
| }), | ||
| coordinatorIdEnvVar: SHARDED_WAL_COORDINATOR_ID_ENV_VAR, | ||
| groupId: 'create-finalize', | ||
| }); | ||
|
|
||
| const shard1 = shardedWal.shard(); | ||
| shard1.open(); | ||
| shard1.append('record1'); | ||
| shard1.append('record2'); | ||
| shard1.close(); | ||
|
|
||
| const shard2 = shardedWal.shard(); | ||
| shard2.open(); | ||
| shard2.append('record3'); | ||
| shard2.close(); | ||
|
|
||
| shardedWal.finalize(); | ||
|
|
||
| const finalFile = path.join( | ||
| testDir, | ||
| shardedWal.groupId, | ||
| `trace.create-finalize.json`, | ||
| ); | ||
| expect(fs.existsSync(finalFile)).toBeTrue(); | ||
|
|
||
| const content = fs.readFileSync(finalFile, 'utf8'); | ||
| const records = JSON.parse(content.trim()); | ||
| expect(records).toEqual(['record1', 'record2', 'record3']); | ||
| }); | ||
|
|
||
| it('should merge multiple shards correctly', () => { | ||
| shardedWal = new ShardedWal({ | ||
| debug: false, | ||
| dir: testDir, | ||
| format: makeMockFormat({ | ||
| baseName: 'merged', | ||
| }), | ||
| coordinatorIdEnvVar: SHARDED_WAL_COORDINATOR_ID_ENV_VAR, | ||
| groupId: 'merge-shards', | ||
| }); | ||
|
|
||
| // eslint-disable-next-line functional/no-loop-statements | ||
| for (let i = 1; i <= 5; i++) { | ||
| const shard = shardedWal.shard(); | ||
| shard.open(); | ||
| shard.append(`record-from-shard-${i}`); | ||
| shard.close(); | ||
| } | ||
|
|
||
| shardedWal.finalize(); | ||
|
|
||
| const finalFile = path.join( | ||
| testDir, | ||
| shardedWal.groupId, | ||
| `merged.merge-shards.json`, | ||
| ); | ||
| const content = fs.readFileSync(finalFile, 'utf8'); | ||
| const records = JSON.parse(content.trim()); | ||
| expect(records).toHaveLength(5); | ||
| expect(records[0]).toBe('record-from-shard-1'); | ||
| expect(records[4]).toBe('record-from-shard-5'); | ||
| }); | ||
|
|
||
| it('should expose recovery details in stats when debug is true', () => { | ||
| shardedWal = new ShardedWal({ | ||
| debug: true, | ||
| dir: testDir, | ||
| format: makeMockFormat({ | ||
| baseName: 'test', | ||
| }), | ||
| coordinatorIdEnvVar: SHARDED_WAL_COORDINATOR_ID_ENV_VAR, | ||
| groupId: 'invalid-entries', | ||
| }); | ||
|
|
||
| const shard = shardedWal.shard(); | ||
| shard.open(); | ||
| shard.append('valid1'); | ||
| shard.append('invalid'); | ||
| shard.append('valid2'); | ||
| shard.close(); | ||
|
|
||
| shardedWal.finalize(); | ||
| // When debug is true, lastRecovery should contain recovery results | ||
| expect(shardedWal.stats.lastRecovery).toHaveLength(1); | ||
| expect(shardedWal.stats.lastRecovery[0]).toMatchObject({ | ||
| file: expect.stringContaining('test.'), | ||
| result: expect.objectContaining({ | ||
| records: expect.arrayContaining(['valid1', 'invalid', 'valid2']), | ||
| errors: [], | ||
| partialTail: null, | ||
| }), | ||
| }); | ||
|
|
||
| const finalFile = path.join( | ||
| testDir, | ||
| shardedWal.groupId, | ||
| `test.invalid-entries.json`, | ||
| ); | ||
| const content = fs.readFileSync(finalFile, 'utf8'); | ||
| const records = JSON.parse(content.trim()); | ||
| expect(records).toEqual(['valid1', 'invalid', 'valid2']); | ||
| }); | ||
|
|
||
| it('should cleanup shard files after finalization', () => { | ||
| shardedWal = new ShardedWal({ | ||
| debug: false, | ||
| dir: testDir, | ||
| format: makeMockFormat({ | ||
| baseName: 'cleanup-test', | ||
| }), | ||
| coordinatorIdEnvVar: SHARDED_WAL_COORDINATOR_ID_ENV_VAR, | ||
| groupId: 'cleanup-test', | ||
| }); | ||
|
|
||
| const shard1 = shardedWal.shard(); | ||
| shard1.open(); | ||
| shard1.append('record1'); | ||
| shard1.close(); | ||
|
|
||
| const shard2 = shardedWal.shard(); | ||
| shard2.open(); | ||
| shard2.append('record2'); | ||
| shard2.close(); | ||
|
|
||
| shardedWal.finalize(); | ||
|
|
||
| const finalFile = path.join( | ||
| testDir, | ||
| shardedWal.groupId, | ||
| `cleanup-test.cleanup-test.json`, | ||
| ); | ||
| expect(fs.existsSync(finalFile)).toBeTrue(); | ||
|
|
||
| shardedWal.cleanupIfCoordinator(); | ||
|
|
||
| const groupDir = path.join(testDir, shardedWal.groupId); | ||
| const files = fs.readdirSync(groupDir); | ||
| expect(files).not.toContain(expect.stringMatching(/cleanup-test.*\.log$/)); | ||
| expect(files).toContain(`cleanup-test.cleanup-test.json`); | ||
| }); | ||
|
|
||
| it('should use custom options in finalizer', () => { | ||
| shardedWal = new ShardedWal({ | ||
| debug: false, | ||
| dir: testDir, | ||
| format: makeMockFormat({ | ||
| baseName: 'custom', | ||
| finalizer: (records, opt) => | ||
| `${JSON.stringify({ records, metadata: opt })}\n`, | ||
| }), | ||
| coordinatorIdEnvVar: SHARDED_WAL_COORDINATOR_ID_ENV_VAR, | ||
| groupId: 'custom-finalizer', | ||
| }); | ||
|
|
||
| const shard = shardedWal.shard(); | ||
| shard.open(); | ||
| shard.append('record1'); | ||
| shard.close(); | ||
|
|
||
| shardedWal.finalize({ version: '2.0', timestamp: Date.now() }); | ||
|
|
||
| const finalFile = path.join( | ||
| testDir, | ||
| shardedWal.groupId, | ||
| `custom.custom-finalizer.json`, | ||
| ); | ||
| const content = fs.readFileSync(finalFile, 'utf8'); | ||
| const result = JSON.parse(content.trim()); | ||
| expect(result.records).toEqual(['record1']); | ||
| expect(result.metadata).toEqual({ | ||
| version: '2.0', | ||
| timestamp: expect.any(Number), | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle empty shards correctly', () => { | ||
| shardedWal = new ShardedWal({ | ||
| debug: false, | ||
| dir: testDir, | ||
| format: makeMockFormat({ | ||
| baseName: 'empty', | ||
| }), | ||
| coordinatorIdEnvVar: SHARDED_WAL_COORDINATOR_ID_ENV_VAR, | ||
| groupId: 'empty-shards', | ||
| }); | ||
|
|
||
| const groupDir = path.join(testDir, shardedWal.groupId); | ||
| fs.mkdirSync(groupDir, { recursive: true }); | ||
|
|
||
| shardedWal.finalize(); | ||
|
|
||
| const finalFile = path.join( | ||
| testDir, | ||
| shardedWal.groupId, | ||
| `empty.${shardedWal.groupId}.json`, | ||
| ); | ||
| expect(fs.existsSync(finalFile)).toBeTrue(); | ||
| const content = fs.readFileSync(finalFile, 'utf8'); | ||
| expect(content.trim()).toBe('[]'); | ||
| }); | ||
| }); |
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.