GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — feat(dbapi): use inline begin to eliminate BeginTransaction RPC by waiho-gumloop · Pull Request #1502 · googleapis/python-spanner
This repository was archived by the owner on Jun 8, 2026. It is now read-only.
Conversation
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Summary of ChangesHello @waiho-gumloop, 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 optimizes transaction handling in the Spanner DBAPI by leveraging the existing inline begin mechanism. It removes an unnecessary explicit Highlights
Changelog
Activity
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
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This pull request introduces a valuable optimization by removing the explicit BeginTransaction RPC call in transaction_checkout. Instead, it leverages the existing inline begin functionality, where the transaction is initiated with the first SQL execution. This change effectively reduces latency by saving one network round-trip per transaction. The modification is well-supported by a new unit test ensuring begin() is not called, and the detailed description provides a thorough safety analysis, confirming the change is sound.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
f176484 to
880081e
Compare
…ests Add test_dbapi_inline_begin.py with 7 mockserver tests that verify: - Read-write DBAPI transactions send no BeginTransactionRequest - First ExecuteSqlRequest uses TransactionSelector(begin=...) - Read + write + commit request sequence is correct - DML-only transactions use inline begin - Read-only transactions still use explicit BeginTransaction - Transaction retry after abort works with inline begin Update existing mockserver tests that expected BeginTransactionRequest for read-write DBAPI transactions: - test_tags.py: Remove BeginTransactionRequest from expected sequences for all read-write tag tests, adjust tag index offsets - test_dbapi_isolation_level.py: Verify isolation level on the inline begin field of ExecuteSqlRequest instead of BeginTransactionRequest Made-with: Cursor
a98de4c to
48d4df8
Compare
Made-with: Cursor
- Consolidate 3 redundant single-read tests into one comprehensive test that verifies: no BeginTransactionRequest, inline begin on first ExecuteSqlRequest, correct request sequence, and correct query results - Rename test_second_statement_uses_transaction_id to test_read_then_write_full_lifecycle with additional assertions: CommitRequest.transaction_id matches the transaction ID from inline begin - Strengthen test_rollback to verify RollbackRequest is sent with a non-empty transaction_id (was only checking no BeginTransactionRequest) - Add CommitRequest assertions to abort retry test: both the aborted and successful commits carry valid transaction IDs - Assert cursor.fetchall() return values in read tests to verify inline begin doesn't corrupt result set metadata - Add RollbackRequest import Made-with: Cursor
|
📢 Migration Notice: 📢 This library is moving to the google-cloud-python monorepo soon. We kept this PR open due to recent activity. We would like to finalize this PR so it can be merged if it is critical. If we don't hear from the PR author, we will close this PR in the next few days. The PR can then be re-opened in the monorepo once the migration is complete and work can continue there. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Do not merge yet: This needs a bit of work, as it does not handle the situation where the first statement in a transaction fails, and thereby also fails to actually start a transaction and return a transaction ID. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Add retries if the first statement in a read/write transaction fails, as the statement then does not return a transaction ID. In order to ensure that we get a transaction ID, we first execute an explicit BeginTransaction RPC and then retry the original statement. We return the response of the retry to the application, regardless whether the retry fails or succeeds. The reason that we do a retry with a BeginTransaction AND include the first statement, is to guarantee transaction consistency. If we were to leave the first statement out of the transaction, then it will not be guaranteed that the error condition that cause the failure in the first place is actually still true when the transaction commits. This would break the transaction guarantees. Example (pseudo-code): ```sql -- The following statement fails with ALREADY_EXISTS insert into some_table (id, value) values (1, 'One'); -- Execute an explicit BeginTransaction RPC. begin; -- Retry the initial statement. This ensures that -- whatever the response is, this response will be -- valid for the entire transaction. insert into some_table (id, value) values (1, 'One'); -- This is guaranteed to return a row. select * from some_table where id=1; -- ... execute the rest of the transaction ... commit; ``` If we had not included the initial insert statement in the retried transaction, then there is no guarantee that the select statement would actually return any rows, as other transactions could in theory have deleted it in the meantime.
423d205 to
ead03f3
Compare
ead03f3 to
d65c25c
Compare
|
A couple of additional notes on this implementation: First Statement FailsThe implementation now also handles the case where the first statement in a read/write transaction fails. From the commit message: Add retries if the first statement in a read/write transaction fails, as the The reason that we do a retry with a BeginTransaction AND include the Example (pseudo-code): -- The following statement fails with ALREADY_EXISTS insert into some_table (id, value) values (1, 'One'); -- Execute an explicit BeginTransaction RPC. begin; -- Retry the initial statement. This ensures that -- whatever the response is, this response will be -- valid for the entire transaction. insert into some_table (id, value) values (1, 'One'); -- This is guaranteed to return a row. select * from some_table where id=1; -- ... execute the rest of the transaction ... commit; If we had not included the initial insert statement in the retried transaction, Performance GainThe initial description mentions a performance gain of '~16ms measured on the emulator'. The actual gain on real Spanner is likely to be somewhere between 3ms to 5ms, assuming that the applications runs in the same region as the Spanner instance it is talking to. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| @pytest.mark.skip( | ||
| reason="Concurrent statement execution at transaction start is not deterministic. " | ||
| "Will be fixed in a separate change." |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Since this is a breaking change until followup fix is merged. Can't we merge both the changes together?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what you mean. This test here is unrelated to this change (this change is only for dbapi, and this test is for the standard client library), and is currently flaky on the main branch as well. This is just a way to stop the builds from failing at random until this has actually been fixed.
The change that is needed to actually fix this, is indeed in the same part of the code, but it will probably be a relatively big change. I'm reluctant to do that in the same pull request, as it will make this change unreasonably large.
See #1510 for a recent failure of this test case in a simple auto-generated dependency update PR (https://github.com/googleapis/python-spanner/actions/runs/22779248797/job/66991165231?pr=1510)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, you can ignore that comment for now. I later noticed it is unrelated. LGTM!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| @pytest.mark.skip( | ||
| reason="Concurrent statement execution at transaction start is not deterministic. " | ||
| "Will be fixed in a separate change." |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what you mean. This test here is unrelated to this change (this change is only for dbapi, and this test is for the standard client library), and is currently flaky on the main branch as well. This is just a way to stop the builds from failing at random until this has actually been fixed.
The change that is needed to actually fix this, is indeed in the same part of the code, but it will probably be a relatively big change. I'm reluctant to do that in the same pull request, as it will make this change unreasonably large.
See #1510 for a recent failure of this test case in a simple auto-generated dependency update PR (https://github.com/googleapis/python-spanner/actions/runs/22779248797/job/66991165231?pr=1510)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ) | ||
| self.assertEqual(actual_id_suffixes, expected_id_suffixes) | ||
|
|
||
| @pytest.mark.skip( |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Just to be sure: This test here is also unrelated to this change, but is currently flaky, which is why I'm skipping it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
DBAPI: transaction_checkout() uses explicit BeginTransaction RPC instead of inline begin
There was an error while loading. Please reload this page.