-
Notifications
You must be signed in to change notification settings - Fork 436
feat(spanner): define IsolationLevel enum for Spanner transactions #15853
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: main
Are you sure you want to change the base?
feat(spanner): define IsolationLevel enum for Spanner transactions #15853
Conversation
Summary of ChangesHello @prathmeshcc, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Google Cloud Spanner C++ client library by introducing explicit support for transaction isolation levels. It defines a new Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces an IsolationLevel enum and the corresponding TransactionIsolationLevelOption to allow users to configure the isolation level for read-write transactions. The changes are well-structured and follow existing patterns in the codebase. The new functionality is well-tested with new unit and integration tests that verify the option handling and precedence. The implementation appears correct and complete. I have one minor suggestion to remove some debugging output from the unit tests.
| TEST(Transaction, IsolationLevel) { | ||
| auto opts = Transaction::ReadWriteOptions().WithIsolationLevel( | ||
| IsolationLevel::kRepeatableRead); | ||
| Transaction txn = MakeReadWriteTransaction(opts); | ||
| spanner_internal::Visit( | ||
| txn, [](spanner_internal::SessionHolder&, | ||
| StatusOr<google::spanner::v1::TransactionSelector>& s, | ||
| spanner_internal::TransactionContext const&) { | ||
| EXPECT_TRUE(s->has_begin()); | ||
| EXPECT_TRUE(s->begin().has_read_write()); | ||
| EXPECT_EQ(s->begin().isolation_level(), | ||
| google::spanner::v1::TransactionOptions::REPEATABLE_READ); | ||
|
|
||
| std::cout << "Isolation Level: " | ||
| << s->begin().isolation_level() << std::endl; | ||
| return 0; | ||
| }); | ||
| } | ||
|
|
||
| TEST(Transaction, IsolationLevelPrecedence) { | ||
| internal::OptionsSpan span(Options{}.set<TransactionIsolationLevelOption>( | ||
| IsolationLevel::kSerializable)); | ||
|
|
||
| // Case 1: Per-call overrides client default | ||
| auto opts = Transaction::ReadWriteOptions().WithIsolationLevel( | ||
| IsolationLevel::kRepeatableRead); | ||
| Transaction txn = MakeReadWriteTransaction(opts); | ||
| spanner_internal::Visit( | ||
| txn, [](spanner_internal::SessionHolder&, | ||
| StatusOr<google::spanner::v1::TransactionSelector>& s, | ||
| spanner_internal::TransactionContext const&) { | ||
| EXPECT_EQ(s->begin().isolation_level(), | ||
| google::spanner::v1::TransactionOptions::REPEATABLE_READ); | ||
| std::cout << "Isolation Level: " | ||
| << s->begin().isolation_level() << std::endl; | ||
| return 0; | ||
| }); | ||
|
|
||
| // Case 2: Fallback to client default | ||
| auto opts_default = Transaction::ReadWriteOptions(); | ||
| Transaction txn_default = MakeReadWriteTransaction(opts_default); | ||
| spanner_internal::Visit( | ||
| txn_default, [](spanner_internal::SessionHolder&, | ||
| StatusOr<google::spanner::v1::TransactionSelector>& s, | ||
| spanner_internal::TransactionContext const&) { | ||
| EXPECT_EQ(s->begin().isolation_level(), | ||
| google::spanner::v1::TransactionOptions::SERIALIZABLE); | ||
| std::cout << "Isolation Level: " | ||
| << s->begin().isolation_level() << std::endl; | ||
| return 0; | ||
| }); | ||
| } |
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.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15853 +/- ##
==========================================
- Coverage 92.95% 92.94% -0.01%
==========================================
Files 2458 2458
Lines 227589 227661 +72
==========================================
+ Hits 211553 211605 +52
- Misses 16036 16056 +20 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| opts.set_isolation_level( | ||
| static_cast<google::spanner::v1::TransactionOptions::IsolationLevel>( | ||
| isolation_level)); |
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.
We strive to not tie the C++ enum values to the protobuf enum values. See, for example, LockHint, OrderBy, ReadLockMode, ReplicaType, RequestPriority, etc. Please continue that by introducing an explicit mapping here (removing the static_cast and specific enum class values).
| for (auto const& row : rows) { | ||
| if (!row) return row.status(); | ||
| } | ||
| // std::cout << "Transaction is active." << std::endl; |
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.
Remove this.
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.
Acknowledged. Will remove this in the next push.
| // std::cout << "Transaction is active." << std::endl; | ||
| return Mutations{}; | ||
| }, | ||
| Options{}.set<TransactionIsolationLevelOption>( |
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.
Would this test behave any differently if this was ignored? That is, what are we testing?
| } | ||
| return Mutations{}; | ||
| }, | ||
| Options{}.set<TransactionIsolationLevelOption>( |
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.
Ditto.
| *selector.mutable_begin() = | ||
| MakeOpts(std::move(opts.rw_opts_), opts.isolation_level_); |
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.
[GitHub seems to have dropped a comment here. Apologies if you see this twice.]
It seems weird that the isolation level appears to only be useful in read-write mode yet it wasn't made part of message ReadWrite. Is that appearance true (and the protos should probably be fixed, in which case we would not need any special treatment here), or are we being too specific in making the isolation mode a member of ReadWriteOptions?
This PR introduces the IsolationLevel enum class in google/cloud/spanner/isolation_level.h.
This enum maps to the google.spanner.v1.TransactionOptions.IsolationLevel protobuf and defines the supported isolation levels for Cloud Spanner read-write transactions:
This addition is a prerequisite for allowing users to configure the isolation level (specifically Repeatable Read) in transaction options.