-
Notifications
You must be signed in to change notification settings - Fork 1
feat(topology): track per-shard row counts #3
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import type { | ||
| AsyncJob, | ||
| ReshardingState, | ||
| ShardRowCount, | ||
| SqlParam, | ||
| StorageNode, | ||
| TableMetadata, | ||
|
|
@@ -41,7 +42,7 @@ | |
| }[]; | ||
|
|
||
| if (result.length > 0 && result[0]?.value === "true") { | ||
| throw new Error( | ||
|
Check failure on line 45 in packages/big-daddy/src/engine/topology/crud.ts
|
||
| "Topology already created. Storage nodes cannot be modified after creation.", | ||
| ); | ||
| } | ||
|
|
@@ -224,4 +225,101 @@ | |
|
|
||
| return { success: true }; | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Row Count Operations | ||
| // ============================================================================ | ||
|
|
||
| /** | ||
| * Get row counts for all shards of a table | ||
| * | ||
| * @param tableName - Name of the table | ||
| * @returns Array of per-shard row counts | ||
| */ | ||
| getTableShardRowCounts(tableName: string): ShardRowCount[] { | ||
| return this.storage.sql | ||
| .exec( | ||
| `SELECT table_name, shard_id, row_count, updated_at | ||
| FROM table_shards | ||
| WHERE table_name = ? AND status = 'active' | ||
| ORDER BY shard_id`, | ||
| tableName, | ||
| ) | ||
| .toArray() as unknown as ShardRowCount[]; | ||
| } | ||
|
|
||
| /** | ||
| * Bump (increment or decrement) the row count for a specific shard | ||
| * Clamps at 0 to prevent negative counts | ||
| * | ||
| * @param tableName - Name of the table | ||
| * @param shardId - ID of the shard | ||
| * @param delta - Amount to change (positive for inserts, negative for deletes) | ||
| */ | ||
| bumpTableShardRowCount( | ||
| tableName: string, | ||
| shardId: number, | ||
| delta: number, | ||
| ): void { | ||
| const now = Date.now(); | ||
| // Use MAX(0, ...) to clamp at 0 | ||
| this.storage.sql.exec( | ||
| `UPDATE table_shards | ||
| SET row_count = MAX(0, row_count + ?), updated_at = ? | ||
| WHERE table_name = ? AND shard_id = ?`, | ||
| delta, | ||
| now, | ||
| tableName, | ||
| shardId, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Batch bump row counts for multiple shards of a table | ||
| * More efficient than calling bumpTableShardRowCount multiple times | ||
| * | ||
| * @param tableName - Name of the table | ||
| * @param deltaByShard - Map of shard_id to delta (amount to change) | ||
| */ | ||
| batchBumpTableShardRowCounts( | ||
| tableName: string, | ||
| deltaByShard: Map<number, number>, | ||
| ): void { | ||
| const now = Date.now(); | ||
| for (const [shardId, delta] of deltaByShard) { | ||
| this.storage.sql.exec( | ||
| `UPDATE table_shards | ||
| SET row_count = MAX(0, row_count + ?), updated_at = ? | ||
| WHERE table_name = ? AND shard_id = ?`, | ||
| delta, | ||
| now, | ||
| tableName, | ||
| shardId, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Set row counts for shards (used after resharding to set exact counts) | ||
| * | ||
| * @param tableName - Name of the table | ||
| * @param countsByShardId - Map of shard_id to row_count | ||
| */ | ||
| setTableShardRowCounts( | ||
| tableName: string, | ||
| countsByShardId: Map<number, number>, | ||
| ): void { | ||
| const now = Date.now(); | ||
| for (const [shardId, rowCount] of countsByShardId) { | ||
| this.storage.sql.exec( | ||
| `UPDATE table_shards | ||
| SET row_count = ?, updated_at = ? | ||
| WHERE table_name = ? AND shard_id = ?`, | ||
| rowCount, | ||
| now, | ||
| tableName, | ||
| shardId, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too sure we want to be going back and tracking this in the topology, it's going to add an extra DO operation which increases cost. The topology is also a single point of failure and increasing writes to it will lower our total possible throughput
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, I didnt realise about the new DO storage costs. Lets scrap this then