-
Notifications
You must be signed in to change notification settings - Fork 1.8k
perf(spanner): multiple micro-optimizations for Spanner #18376
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
Draft
olavloite
wants to merge
1
commit into
main
Choose a base branch
from
spanner-python-optimizations-prototype
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -123,8 +123,8 @@ async def _get_multiplexed_session(self) -> Session: | |||||||||||
| """Returns a multiplexed session from the database session manager. | ||||||||||||
|
|
||||||||||||
| If the multiplexed session is not defined, creates a new multiplexed | ||||||||||||
| session and starts a maintenance thread to periodically delete and | ||||||||||||
| recreate it so that it remains valid. Otherwise, simply returns the | ||||||||||||
| session and starts a maintenance thread to periodically rotate | ||||||||||||
| it so that it remains valid. Otherwise, simply returns the | ||||||||||||
| current multiplexed session. | ||||||||||||
|
|
||||||||||||
| :rtype: :class:`~google.cloud.spanner_v1.session.Session` | ||||||||||||
|
|
@@ -167,8 +167,8 @@ def _build_maintenance_thread( | |||||||||||
| self, session: Optional[Session] = None | ||||||||||||
| ) -> CrossSync.Task: | ||||||||||||
| """Builds and returns a multiplexed session maintenance thread for | ||||||||||||
| the database session manager. This thread will periodically delete | ||||||||||||
| and recreate the multiplexed session to ensure that it is always valid. | ||||||||||||
| the database session manager. This thread will periodically rotate | ||||||||||||
| the multiplexed session to ensure that it is always valid. | ||||||||||||
|
|
||||||||||||
| :type session: :class:`~google.cloud.spanner_v1.session.Session` | ||||||||||||
| :param session: (Optional) The multiplexed session to maintain. | ||||||||||||
|
|
@@ -209,25 +209,18 @@ async def _rotate_multiplexed_session(self) -> bool: | |||||||||||
| return False | ||||||||||||
|
|
||||||||||||
| async with self._multiplexed_session_lock: | ||||||||||||
| old_session = self._multiplexed_session | ||||||||||||
| self._multiplexed_session = new_session | ||||||||||||
|
|
||||||||||||
| if old_session is not None: | ||||||||||||
| try: | ||||||||||||
| await CrossSync.run_if_async(old_session.delete) | ||||||||||||
| except Exception: | ||||||||||||
| pass | ||||||||||||
|
|
||||||||||||
| return True | ||||||||||||
|
|
||||||||||||
| @staticmethod | ||||||||||||
| @CrossSync.convert | ||||||||||||
| async def _maintain_multiplexed_session(session_manager_ref) -> None: | ||||||||||||
| """Maintains the multiplexed session for the database session manager. | ||||||||||||
|
|
||||||||||||
| This method will delete and recreate the referenced database session manager's | ||||||||||||
| This method will periodically rotate the referenced database session manager's | ||||||||||||
| multiplexed session to ensure that it is always valid. The method will run until | ||||||||||||
| the database session manager is deleted or the multiplexed session is deleted. | ||||||||||||
| the database session manager is garbage collected or the session manager is closed. | ||||||||||||
|
|
||||||||||||
| :type session_manager_ref: :class:`_weakref.ReferenceType` | ||||||||||||
| :param session_manager_ref: A weak reference to the database session manager.""" | ||||||||||||
|
|
@@ -292,7 +285,4 @@ async def close(self) -> None: | |||||||||||
| pass | ||||||||||||
| else: | ||||||||||||
| self._multiplexed_session_thread.join() | ||||||||||||
| if self._multiplexed_session is not None: | ||||||||||||
| session_to_delete = self._multiplexed_session | ||||||||||||
| self._multiplexed_session = None | ||||||||||||
| await session_to_delete.delete() | ||||||||||||
| self._multiplexed_session = None | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failing to delete the multiplexed session when closing the session manager leaks the session resource on the Spanner backend. We should restore the deletion of
Suggested change
|
||||||||||||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Removing the deletion of the old multiplexed session during rotation (
old_session.delete()) introduces a resource leak on the Spanner backend. Although multiplexed sessions are designed to be shared, they are still active session resources on the server. Failing to delete them when they are rotated out means they will remain active until they idle-expire (which can take up to an hour), potentially leading to exceeding the session limit on the database. We should restore the deletion of the old session.