fix(sessions): use read-only transactions for get/list operations#4810
Open
giulio-leone wants to merge 1 commit intogoogle:mainfrom
Open
fix(sessions): use read-only transactions for get/list operations#4810giulio-leone wants to merge 1 commit intogoogle:mainfrom
giulio-leone wants to merge 1 commit intogoogle:mainfrom
Conversation
DatabaseSessionService.get_session() and list_sessions() are pure-read operations but currently open regular read-write transactions via _rollback_on_exception_session(). On Cloud Spanner this causes RetryAborted errors when a concurrent write commits during the read, because Spanner's OCC layer sees the read-write transaction as conflicting. Add _readonly_session() context manager that: - Marks the connection as read-only (postgresql_readonly=True) which also benefits Cloud Spanner's sqlalchemy-spanner dialect - Never commits, avoiding unnecessary write-path overhead - Still rolls back on exception to release the connection cleanly Switch get_session() and list_sessions() to use _readonly_session(). Write operations (create_session, delete_session, append_event) continue to use _rollback_on_exception_session() with explicit commits. Fixes google#4771 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
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. |
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.
Summary
Fixes #4771
DatabaseSessionService.get_session()andlist_sessions()are pure-read operations but currently open regular read-write transactions via_rollback_on_exception_session(). On Cloud Spanner this causesRetryAbortederrors when a concurrent write commits during the read, because Spanner's OCC (Optimistic Concurrency Control) layer sees the read-write transaction as conflicting.Root Cause
Spanner uses OCC for read-write transactions. Even a
SELECT-only transaction opened in read-write mode will be aborted if a concurrent write touches the same data. Since reads never mutate data, they should use read-only transactions which are not subject to OCC aborts.Fix
Added a
_readonly_session()context manager that:execution_options(postgresql_readonly=True)sqlalchemy-spannerdialect to open read-only transactionsdatabase_session_factoryand connection poolSwitched
get_session()andlist_sessions()to use_readonly_session().Write operations (
create_session,delete_session,append_event) continue to use_rollback_on_exception_session()with explicit commits — unchanged.Testing
All 128 session tests pass. Full suite: 4725 passed, 0 failures, 0 regressions.