8000 feat: Add support for multiplexed sessions by rahul2393 · Pull Request #1332 · googleapis/python-spanner · GitHub
[go: up one dir, main page]

Skip to content

feat: Add support for multiplexed sessions #1332

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions google/cloud/spanner_dbapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from google.cloud.spanner_dbapi.transaction_helper import TransactionRetryHelper
from google.cloud.spanner_dbapi.cursor import Cursor
from google.cloud.spanner_v1 import RequestOptions, TransactionOptions
from google.cloud.spanner_v1.session_options import TransactionType
from google.cloud.spanner_v1.snapshot import Snapshot

from google.cloud.spanner_dbapi.exceptions import (
Expand Down Expand Up @@ -354,8 +355,14 @@ def _session_checkout(self):
"""
if self.database is None:
raise ValueError("Database needs to be passed for this operation")

if not self._session:
self._session = self.database._pool.get()
transaction_type = (
TransactionType.READ_ONLY
if self.read_only
else TransactionType.READ_WRITE
)
self._session = self.database._session_manager.get_session(transaction_type)

return self._session

Expand All @@ -368,7 +375,7 @@ def _release_session(self):
return
if self.database is None:
raise ValueError("Database needs to be passed for this operation")
self.database._pool.put(self._session)
self.database._session_manager.put_session(self._session)
self._session = None

def transaction_checkout(self):
Expand Down Expand Up @@ -430,7 +437,7 @@ def close(self):
self._transaction.rollback()

if self._own_pool and self.database:
self.database._pool.clear()
self.database._session_manager._pool.clear()

self.is_closed = True

Expand Down Expand Up @@ -623,7 +630,6 @@ def partition_query(
self._partitioned_query_validation(partitioned_query, statement)

batch_snapshot = self._database.batch_snapshot()
partition_ids = []
partitions = list(
batch_snapshot.generate_query_batches(
partitioned_query,
Expand All @@ -634,6 +640,8 @@ def partition_query(
)

batch_transaction_id = batch_snapshot.get_batch_transaction_id()

partition_ids = []
for partition in partitions:
partition_ids.append(
partition_helper.encode_to_string(batch_transaction_id, partition)
Expand Down
16 changes: 16 additions & 0 deletions google/cloud/spanner_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
except ImportError: # pragma: NO COVER
HAS_GOOGLE_CLOUD_MONITORING_INSTALLED = False

from google.cloud.spanner_v1.session_options import SessionOptions

_CLIENT_INFO = client_info.ClientInfo(client_library_version=__version__)
EMULATOR_ENV_VAR = "SPANNER_EMULATOR_HOST"
Expand Down Expand Up @@ -171,6 +172,9 @@ class Client(ClientWithProject):
or :class:`dict`
:param default_transaction_options: (Optional) Default options to use for all transactions.

:type session_options: :class:`~google.cloud.spanner_v1.SessionOptions`
:param session_options: (Optional) Options for client sessions.

:raises: :class:`ValueError <exceptions.ValueError>` if both ``read_only``
and ``admin`` are :data:`True`
"""
Expand All @@ -193,6 +197,7 @@ def __init__(
directed_read_options=None,
observability_options=None,
default_transaction_options: Optional[DefaultTransactionOptions] = None,
session_options=None,
):
self._emulator_host = _get_spanner_emulator_host()

Expand Down Expand Up @@ -262,6 +267,8 @@ def __init__(
)
self._default_transaction_options = default_transaction_options

self._session_options = session_options or SessionOptions()

@property
def credentials(self):
"""Getter for client's credentials.
Expand Down Expand Up @@ -525,3 +532,12 @@ def default_transaction_options(
)

self._default_transaction_options = default_transaction_options

@property
def session_options(self):
"""Returns the session options for the client.

:rtype: :class:`~google.cloud.spanner_v1.SessionOptions`
:returns: The session options for the client.
"""
return self._session_options
Loading
Loading
0