1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414from enum import Enum
15- from os import getenv
1615from datetime import timedelta
1716from threading import Event , Lock , Thread
1817from time import sleep , time
@@ -41,31 +40,21 @@ class DatabaseSessionsManager(object):
4140 transaction type using :meth:`get_session`, and returned to the session manager
4241 using :meth:`put_session`.
4342
44- The sessions returned by the session manager depend on the configured environment variables
45- and the provided session pool (see :class:`~google.cloud.spanner_v1.pool.AbstractSessionPool`).
43+ Multiplexed sessions are used for all transaction types.
4644
4745 :type database: :class:`~google.cloud.spanner_v1.database.Database`
4846 :param database: The database to manage sessions for.
4947
50- :type pool: :class:`~google.cloud.spanner_v1.pool.AbstractSessionPool`
51- :param pool: The pool to get non-multiplexed sessions from .
48+ :type is_experimental_host: bool
49+ :param is_experimental_host: Whether the database is using an experimental host .
5250 """
5351
54- # Environment variables for multiplexed sessions
55- _ENV_VAR_MULTIPLEXED = "GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS"
56- _ENV_VAR_MULTIPLEXED_PARTITIONED = (
57- "GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS"
58- )
59- _ENV_VAR_MULTIPLEXED_READ_WRITE = "GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW"
60-
6152 # Intervals for the maintenance thread to check and refresh the multiplexed session.
6253 _MAINTENANCE_THREAD_POLLING_INTERVAL = timedelta (minutes = 10 )
6354 _MAINTENANCE_THREAD_REFRESH_INTERVAL = timedelta (days = 7 )
6455
65- def __init__ (self , database , pool , is_experimental_host : bool = False ):
56+ def __init__ (self , database , is_experimental_host : bool = False ):
6657 self ._database = database
67- self ._pool = pool
68- self ._is_experimental_host = is_experimental_host
6958
7059 # Declare multiplexed session attributes. When a multiplexed session for the
7160 # database session manager is created, a maintenance thread is initialized to
@@ -81,17 +70,16 @@ def __init__(self, database, pool, is_experimental_host: bool = False):
8170 self ._multiplexed_session_terminate_event : Event = Event ()
8271
8372 def get_session (self , transaction_type : TransactionType ) -> Session :
84- """Returns a session for the given transaction type from the database session manager.
73+ """Returns a multiplexed session for the given transaction type.
74+
75+ :type transaction_type: :class:`TransactionType`
76+ :param transaction_type: The type of transaction (ignored, multiplexed
77+ sessions support all transaction types).
8578
8679 :rtype: :class:`~google.cloud.spanner_v1.session.Session`
87- :returns: a session for the given transaction type .
80+ :returns: a multiplexed session .
8881 """
89-
90- session = (
91- self ._get_multiplexed_session ()
92- if self ._use_multiplexed (transaction_type ) or self ._is_experimental_host
93- else self ._pool .get ()
94- )
82+ session = self ._get_multiplexed_session ()
9583
9684 add_span_event (
9785 get_current_span (),
@@ -104,21 +92,18 @@ def get_session(self, transaction_type: TransactionType) -> Session:
10492 def put_session (self , session : Session ) -> None :
10593 """Returns the session to the database session manager.
10694
95+ For multiplexed sessions, this is a no-op since they can handle
96+ multiple concurrent transactions and don't need to be returned to a pool.
97+
10798 :type session: :class:`~google.cloud.spanner_v1.session.Session`
10899 :param session: The session to return to the database session manager.
109100 """
110-
111101 add_span_event (
112102 get_current_span (),
113103 "Returning session" ,
114104 {"id" : session .session_id , "multiplexed" : session .is_multiplexed },
115105 )
116-
117- # No action is needed for multiplexed sessions: the session
118- # pool is only used for managing non-multiplexed sessions,
119- # since they can only process one transaction at a time.
120- if not session .is_multiplexed :
121- self ._pool .put (session )
106+ # Multiplexed sessions don't need to be returned to a pool
122107
123108 def _get_multiplexed_session (self ) -> Session :
124109 """Returns a multiplexed session from the database session manager.
@@ -226,53 +211,3 @@ def _maintain_multiplexed_session(session_manager_ref) -> None:
226211
227212 session_created_time = time ()
228213
229- @classmethod
230- def _use_multiplexed (cls , transaction_type : TransactionType ) -> bool :
231- """Returns whether to use multiplexed sessions for the given transaction type.
232-
233- Multiplexed sessions are enabled for read-only transactions if:
234- * _ENV_VAR_MULTIPLEXED != 'false'.
235-
236- Multiplexed sessions are enabled for partitioned transactions if:
237- * _ENV_VAR_MULTIPLEXED_PARTITIONED != 'false'.
238-
239- Multiplexed sessions are enabled for read/write transactions if:
240- * _ENV_VAR_MULTIPLEXED_READ_WRITE != 'false'.
241-
242- :type transaction_type: :class:`TransactionType`
243- :param transaction_type: the type of transaction
244-
245- :rtype: bool
246- :returns: True if multiplexed sessions should be used for the given transaction
247- type, False otherwise.
248-
249- :raises ValueError: if the transaction type is not supported.
250- """
251-
252- if transaction_type is TransactionType .READ_ONLY :
253- return cls ._getenv (cls ._ENV_VAR_MULTIPLEXED )
254-
255- elif transaction_type is TransactionType .PARTITIONED :
256- return cls ._getenv (cls ._ENV_VAR_MULTIPLEXED_PARTITIONED )
257-
258- elif transaction_type is TransactionType .READ_WRITE :
259- return cls ._getenv (cls ._ENV_VAR_MULTIPLEXED_READ_WRITE )
260-
261- raise ValueError (f"Transaction type { transaction_type } is not supported." )
262-
263- @classmethod
264- def _getenv (cls , env_var_name : str ) -> bool :
265- """Returns the value of the given environment variable as a boolean.
266-
267- True unless explicitly 'false' (case-insensitive).
268- All other values (including unset) are considered true.
269-
270- :type env_var_name: str
271- :param env_var_name: the name of the boolean environment variable
272-
273- :rtype: bool
274- :returns: True unless the environment variable is set to 'false', False otherwise.
275- """
276-
277- env_var_value = getenv (env_var_name , "true" ).lower ().strip ()
278- return env_var_value != "false"
0 commit comments