-
Notifications
You must be signed in to change notification settings - Fork 2.2k
kvdb/sqlbase: make NextSequence atomic, using a single SQL statement #9676
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
base: master
Are you sure you want to change the base?
Conversation
The previous implementation of NextSequence was prone to race conditions as it read the sequence, incremented it locally, and then wrote it back. This commit modifies the function to use an atomic `UPDATE ... RETURNING` SQL statement. This may have been the cause of some of the excessive conflicts we've been seeing when using postgres in the kv operating mode. This change ensures that incrementing and retrieving the next sequence number for a nested bucket is performed atomically by the database, preventing potential duplicate sequence numbers under concurrent access. The query uses `COALESCE` to handle the initial NULL case, treating it as 0 before incrementing. Error handling is improved to cover cases where the bucket ID might not be found.
Important Review skippedAuto reviews are limited to specific labels. 🏷️ Labels to auto review (1)
Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
In this commit, we optimize the Delete operation. Before we'd _always_ perform a `SELECT` first before deletion. Now we'll attempt to delete it as a non-bucket, if that works, then we can return. If that fails, then we'll do the `SELECT` to determine if the key doesn't exist, or if it's actually a bucket.
Similar to the write bucket, we can optimize for the case where the key already exists, allowing us to skip the extra `SELECT` statement.
bf10537
to
9ecb9d0
Compare
Pushed two other commits attempting to optimize the way |
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.
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (1)
kvdb/sqlbase/readwrite_bucket.go:388
- The code after the 'return nil' on line 381 is unreachable, which indicates redundant error handling logic. Consider removing or refactoring the unreachable code to improve clarity.
return fmt.Errorf("error checking if key %x exists as bucket: %w", key, err)
@ziggie1984: review reminder |
The previous implementation of NextSequence was prone to race conditions as it read the sequence, incremented it locally, and then wrote it back. This commit modifies the function to use an atomic
UPDATE ... RETURNING
SQL statement.This may have been the cause of some of the excessive conflicts we've been seeing when using postgres in the kv operating mode.
This change ensures that incrementing and retrieving the next sequence number for a nested bucket is performed atomically by the database, preventing potential duplicate sequence numbers under concurrent access. The query uses
COALESCE
to handle the initial NULL case, treating it as 0 before incrementing. Error handling is improved to cover cases where the bucket ID might not be found.