feat(google-cloud-spanner): Implement "Inline Begin Transaction"#54
Merged
Conversation
This reverts commit 83832a9.
Contributor
Author
dazuma
suggested changes
Aug 28, 2023
dazuma
reviewed
Aug 29, 2023
dazuma
reviewed
Aug 29, 2023
dazuma
approved these changes
Aug 29, 2023
dazuma
left a comment
Contributor
There was a problem hiding this comment.
I think the logic looks good now. I have a few more recommendations on internal documentation, just to help with maintainability of the concurrency and synchronization.
NivedhaSenthil
approved these changes
Sep 4, 2023
This was referenced Sep 14, 2023
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


Implements "Inline Begin Transaction" optimisation for Ruby Spanner.
Overview
Cloud Spanner allows users to query the data in several ways, including transactions. A transaction allows users to execute a series of statements as a single atomic unit, and allows them to rollback the changes in case of errors.
Most Cloud Spanner
*RequestAPIs include aTransactionSelectorfield for queries. TheTransactionSelectorfield can be used within queries to specify an existing transaction ID or start a new transaction.Currently, we make an explicit
BeginTransactionrpc to create a transaction in the backend, and get the transaction ID as the response. We pass the ID in theTransactionSelectorfield in the subsequent operations within the transaction.This process has the following drawbacks:
Startup overhead - The client sets up the environment for transactions at the start of the client instantiation. This incurs an overhead in the instantiation time.
Prepared transactions - As we cannot predict in advance how many transactions the user will need, we must prepare several transactions during client instantiation. This is done by maintaining two separate session pools in a specific ratio, one for read-only transactions and the other for read-write transactions. As the user accesses this pool of sessions, the client must rebalance the ratio of the two stacks.
Transaction latency - The client makes an additional RPC to create a transaction. When the session pool runs out of available transactions, the client has to create more transactions. Both these factors increase the latency.
To address these drawbacks, we can defer the creation of transactions until the user executes the first query within the transaction. This is known as inlining the “begin transaction” call.
Implementation details
Below are the the high-level list of changes done throughout the codebase:
Poolclass:write_ratiofield and the corresponding code that maintains transactions through write_ratio.sessions_availableandsessions_in_useto simplify the maintenance of session pool.all_sessions,session_stackandtransaction_stack.Transactionclass:existing_transaction?,safe_begin_transaction,safe_executeetc).Clientclass:.transactionmethod to handle inlining of transaction creation.check_and_propagate_err()) to reduce code complexity issues identified by Rubocop.BatchUpdateResultsclass:tx.batch_update()API's response.Projectclass:write_ratiofield as deprecated during client initialisation.Resultsclass:transactionto facilitate extraction of a transaction from the results metadata.Sessionclass:Serviceclass:Tests
Unit tests
write_ratio&transaction_stackare removed, and some are modified to fit the new mechanism of the session pool.Acceptance tets