This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathpool.py
More file actions
966 lines (798 loc) · 33.3 KB
/
pool.py
File metadata and controls
966 lines (798 loc) · 33.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
# Copyright 2016 Google LLC All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Pools managing shared Session objects."""
__CROSS_SYNC_OUTPUT__ = "google.cloud.spanner_v1.pool"
import asyncio
import datetime
import time
from warnings import warn
from google.cloud.aio._cross_sync import CrossSync
from google.cloud.exceptions import NotFound
from google.cloud.spanner_v1._async.session import Session
from google.cloud.spanner_v1._helpers import (
_metadata_with_leader_aware_routing,
_metadata_with_prefix,
)
from google.cloud.spanner_v1._opentelemetry_tracing import (
add_span_event,
get_current_span,
trace_call,
)
from google.cloud.spanner_v1.metrics.metrics_capture import MetricsCapture
from google.cloud.spanner_v1.types.spanner import BatchCreateSessionsRequest
from google.cloud.spanner_v1.types.spanner import Session as SessionProto
def _NOW():
return datetime.datetime.now(datetime.timezone.utc)
@CrossSync.convert_class
class SessionCheckout(object):
"""Context manager: hold session checked out from a pool.
Deprecated. Sessions should be checked out indirectly using context
managers or :meth:`~google.cloud.spanner_v1.database.Database.run_in_transaction`,
rather than checked out directly from the pool.
:type pool: concrete subclass of
:class:`~google.cloud.spanner_v1.pool.AbstractSessionPool`
:param pool: Pool from which to check out a session.
:param kwargs: extra keyword arguments to be passed to :meth:`pool.get`.
"""
_session = None
def __init__(self, pool, **kwargs):
self._pool = pool
self._kwargs = kwargs
self._timeout = kwargs.get("timeout")
@CrossSync.convert(sync_name="__enter__")
async def __aenter__(self):
self._session = await self._pool.get(**self._kwargs)
return self._session
@CrossSync.convert(sync_name="__exit__")
async def __aexit__(self, exc_type, exc_value, traceback):
await self._pool.put(self._session)
@CrossSync.convert_class(
docstring_format_vars={
"experimental_api": (
"\n\n .. warning::\n The Spanner AsyncIO API is experimental and may be subject to breaking changes.\n",
"",
)
}
)
class AbstractSessionPool(object):
"""{experimental_api}Specifies required API for concrete session pool implementations.
:type labels: dict (str -> str) or None
:param labels: (Optional) user-assigned labels for sessions created
by the pool.
:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
"""
_database = None
def __init__(self, labels=None, database_role=None):
if labels is None:
labels = {}
self._labels = labels
self._database_role = database_role
@property
def _resource_info(self):
"""Resource information for metrics labels."""
if self._database is None:
return None
return {
"project": self._database._instance._client.project,
"instance": self._database._instance.instance_id,
"database": self._database.database_id,
}
@property
def labels(self):
"""User-assigned labels for sessions created by the pool.
:rtype: dict (str -> str)
:returns: labels assigned by the user
"""
return self._labels
@property
def database_role(self):
"""User-assigned database_role for sessions created by the pool.
:rtype: str
:returns: database_role assigned by the user
"""
return self._database_role
def bind(self, database):
"""Associate the pool with a database.
:type database: :class:`~google.cloud.spanner_v1.database.Database`
:param database: database used by the pool to create sessions
when needed.
Concrete implementations of this method may pre-fill the pool
using the database.
:raises NotImplementedError: abstract method
"""
raise NotImplementedError()
def get(self):
"""Check a session out from the pool.
Concrete implementations of this method are allowed to raise an
error to signal that the pool is exhausted, or to block until a
session is available.
:raises NotImplementedError: abstract method
"""
raise NotImplementedError()
@CrossSync.convert
async def put(self, session):
"""Return a session to the pool.
:type session: :class:`~google.cloud.spanner_v1.session.Session`
:param session: the session being returned.
Concrete implementations of this method are allowed to raise an
error to signal that the pool is full, or to block until it is
not full.
:raises NotImplementedError: abstract method
"""
raise NotImplementedError()
def clear(self):
"""Delete all sessions in the pool.
Concrete implementations of this method are allowed to raise an
error to signal that the pool is full, or to block until it is
not full.
:raises NotImplementedError: abstract method
"""
raise NotImplementedError()
def _new_session(self):
"""Helper for concrete methods creating session instances.
:rtype: :class:`~google.cloud.spanner_v1.session.Session`
:returns: new session instance.
"""
role = self.database_role or self._database.database_role
return Session(database=self._database, labels=self.labels, database_role=role)
def session(self, **kwargs):
"""Check out a session from the pool.
Deprecated. Sessions should be checked out indirectly using context
managers or :meth:`~google.cloud.spanner_v1.database.Database.run_in_transaction`,
rather than checked out directly from the pool.
:param kwargs: (optional) keyword arguments, passed through to
the returned checkout.
:rtype: :class:`~google.cloud.spanner_v1.session.SessionCheckout`
:returns: a checkout instance, to be used as a context manager for
accessing the session and returning it to the pool.
"""
import warnings
warnings.warn(
"Sessions should be checked out indirectly using context "
"managers or Database.run_in_transaction, rather than "
"checked out directly from the pool.",
DeprecationWarning,
stacklevel=2,
)
return SessionCheckout(self, **kwargs)
@CrossSync.convert_class(
docstring_format_vars={
"experimental_api": (
"\n\n .. warning::\n The Spanner AsyncIO API is experimental and may be subject to breaking changes.\n",
"",
)
}
)
class FixedSizePool(AbstractSessionPool):
"""{experimental_api}Concrete session pool implementation:
- Pre-allocates / creates a fixed number of sessions.
- "Pings" existing sessions via :meth:`session.exists` before returning
sessions that have not been used for more than 55 minutes and replaces
expired sessions.
- Blocks, with a timeout, when :meth:`get` is called on an empty pool.
Raises after timing out.
- Raises when :meth:`put` is called on a full pool. That error is
never expected in normal practice, as users should be calling
:meth:`get` followed by :meth:`put` whenever in need of a session.
:type size: int
:param size: fixed pool size
:type default_timeout: int
:param default_timeout: default timeout, in seconds, to wait for
a returned session.
:type labels: dict (str -> str) or None
:param labels: (Optional) user-assigned labels for sessions created
by the pool.
:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
"""
DEFAULT_SIZE = 10
DEFAULT_TIMEOUT = 10
DEFAULT_MAX_AGE_MINUTES = 55
def __init__(
self,
size=DEFAULT_SIZE,
default_timeout=DEFAULT_TIMEOUT,
labels=None,
database_role=None,
max_age_minutes=DEFAULT_MAX_AGE_MINUTES,
):
super(FixedSizePool, self).__init__(labels=labels, database_role=database_role)
self.size = size
self.default_timeout = default_timeout
self._sessions = CrossSync.LifoQueue(size)
self._max_age = datetime.timedelta(minutes=max_age_minutes)
self._lock = CrossSync.Lock()
@CrossSync.convert
async def bind(self, database):
"""Associate the pool with a database.
:type database: :class:`~google.cloud.spanner_v1.database.Database`
:param database: database used by the pool to used to create sessions
when needed.
"""
self._database = database
self._database_role = self._database_role or self._database.database_role
await self._fill_pool()
@CrossSync.convert
async def _fill_pool(self):
"""Fills the pool with sessions.
.. note::
This method is not thread-safe. It should only be called from
within a thread-safe context.
"""
database = self._database
requested_session_count = self.size - self._sessions.qsize()
span = get_current_span()
span_event_attributes = {"kind": type(self).__name__}
if requested_session_count <= 0:
add_span_event(
span,
f"Invalid session pool size({requested_session_count}) <= 0",
span_event_attributes,
)
return
api = database.spanner_api
metadata = _metadata_with_prefix(database.name)
if database._route_to_leader_enabled:
metadata.append(_metadata_with_leader_aware_routing(True))
self._database_role = self._database_role or self._database.database_role
if requested_session_count > 0:
add_span_event(
span,
f"Requesting {requested_session_count} sessions",
span_event_attributes,
)
if self._sessions.full():
add_span_event(span, "Session pool is already full", span_event_attributes)
return
request = BatchCreateSessionsRequest(
database=database.name,
session_count=requested_session_count,
session_template=SessionProto(creator_role=self.database_role),
)
observability_options = getattr(self._database, "observability_options", None)
with trace_call(
"CloudSpanner.FixedPool.BatchCreateSessions",
observability_options=observability_options,
metadata=metadata,
) as span, MetricsCapture(self._resource_info):
returned_session_count = 0
while not self._sessions.full():
request.session_count = requested_session_count - self._sessions.qsize()
add_span_event(
span,
f"Creating {request.session_count} sessions",
span_event_attributes,
)
call_metadata, error_augmenter = database.with_error_augmentation(
database._next_nth_request,
1,
metadata,
span,
)
with error_augmenter:
resp = await api.batch_create_sessions(
request=request,
metadata=call_metadata,
)
add_span_event(
span,
"Created sessions",
dict(count=len(resp.session)),
)
for session_pb in resp.session:
session = self._new_session()
session._session_id = session_pb.name.split("/")[-1]
await self.put(session)
returned_session_count += 1
add_span_event(
span,
f"Requested for {requested_session_count} sessions, returned {returned_session_count}",
span_event_attributes,
)
@CrossSync.convert
async def ping(self):
"""Check all sessions in the pool.
Delete those which are defunct.
"""
current_span = get_current_span()
async with self._lock:
# Replaced with a list to iterate over sessions since we'll be
# putting them back in the pool.
sessions_to_ping = []
while not self._sessions.empty():
sessions_to_ping.append(await CrossSync.queue_get(self._sessions))
for session in sessions_to_ping:
if (_NOW() - session.last_use_time) > self._max_age:
try:
await session.ping()
except NotFound:
session = self._new_session()
await session.create()
except Exception as e:
warn(f"Failed to ping session {session.session_id}: {e}")
await CrossSync.queue_put(self._sessions, session)
add_span_event(
current_span,
"Pinged sessions",
{"count": len(sessions_to_ping)},
)
@CrossSync.convert
async def get(self, timeout=None):
"""Check a session out from the pool.
:type timeout: int
:param timeout: seconds to block waiting for an available session
:rtype: :class:`~google.cloud.spanner_v1.session.Session`
:returns: an existing session from the pool, or a newly-created
session.
:raises: :exc:`CrossSync.QueueEmpty` if the queue is empty.
"""
if timeout is None:
timeout = self.default_timeout
start_time = time.time()
current_span = get_current_span()
span_event_attributes = {"kind": type(self).__name__}
add_span_event(current_span, "Acquiring session", span_event_attributes)
session = None
try:
add_span_event(
current_span,
"Waiting for a session to become available",
span_event_attributes,
)
session = await CrossSync.queue_get(
self._sessions, block=True, timeout=timeout
)
age = _NOW() - session.last_use_time
if age >= self._max_age and not await session.exists():
if not await session.exists():
add_span_event(
current_span,
"Session is not valid, recreating it",
span_event_attributes,
)
session = self._new_session()
await session.create()
# Replacing with the updated session.id.
span_event_attributes["session.id"] = session._session_id
span_event_attributes["session.id"] = session._session_id
span_event_attributes["time.elapsed"] = time.time() - start_time
add_span_event(current_span, "Acquired session", span_event_attributes)
except CrossSync.QueueEmpty as e:
add_span_event(
current_span, "No sessions available in the pool", span_event_attributes
)
raise e
return session
@CrossSync.convert
async def put(self, session):
"""Return a session to the pool.
Never blocks: if the pool is full, raises.
:type session: :class:`~google.cloud.spanner_v1.session.Session`
:param session: the session being returned.
:raises: :exc:`queue.Full` if the queue is full.
"""
await CrossSync.queue_put(self._sessions, session, block=False)
@CrossSync.convert
async def clear(self):
"""Delete all sessions in the pool."""
while True:
try:
session = await CrossSync.queue_get(self._sessions, block=False)
except CrossSync.QueueEmpty:
break
else:
await session.delete()
@CrossSync.convert_class(
docstring_format_vars={
"experimental_api": (
"\n\n .. warning::\n The Spanner AsyncIO API is experimental and may be subject to breaking changes.\n",
"",
)
}
)
class BurstyPool(AbstractSessionPool):
"""{experimental_api}Concrete session pool implementation:
- "Pings" existing sessions via :meth:`session.exists` before returning
them.
- Creates a new session, rather than blocking, when :meth:`get` is called
on an empty pool.
- Discards the returned session, rather than blocking, when :meth:`put`
is called on a full pool.
:type target_size: int
:param target_size: max pool size
:type labels: dict (str -> str) or None
:param labels: (Optional) user-assigned labels for sessions created
by the pool.
:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
"""
def __init__(self, target_size=10, labels=None, database_role=None):
super(BurstyPool, self).__init__(labels=labels, database_role=database_role)
self.target_size = target_size
self._database = None
self._sessions = CrossSync.LifoQueue(target_size)
@CrossSync.convert
async def bind(self, database):
"""Associate the pool with a database.
:type database: :class:`~google.cloud.spanner_v1.database.Database`
:param database: database used by the pool to create sessions
when needed.
"""
self._database = database
self._database_role = self._database_role or self._database.database_role
@CrossSync.convert
async def get(self):
"""Check a session out from the pool.
:rtype: :class:`~google.cloud.spanner_v1.session.Session`
:returns: an existing session from the pool, or a newly-created
session.
"""
current_span = get_current_span()
span_event_attributes = {"kind": type(self).__name__}
add_span_event(current_span, "Acquiring session", span_event_attributes)
try:
add_span_event(
current_span,
"Waiting for a session to become available",
span_event_attributes,
)
session = await CrossSync.queue_get(self._sessions, block=False)
except (CrossSync.QueueEmpty, asyncio.QueueEmpty):
add_span_event(
current_span,
"No sessions available in pool. Creating session",
span_event_attributes,
)
session = self._new_session()
await session.create()
else:
if not await session.exists():
add_span_event(
current_span,
"Session is not valid, recreating it",
span_event_attributes,
)
session = self._new_session()
await session.create()
return session
@CrossSync.convert
async def put(self, session):
"""Return a session to the pool.
Never blocks: if the pool is full, the returned session is
discarded.
:type session: :class:`~google.cloud.spanner_v1.session.Session`
:param session: the session being returned.
"""
try:
await CrossSync.queue_put(self._sessions, session, block=False)
except CrossSync.QueueFull:
try:
# Sessions from pools are never multiplexed, so we can always delete them
await session.delete()
except NotFound:
pass
@CrossSync.convert
async def clear(self):
"""Delete all sessions in the pool."""
while True:
try:
session = await CrossSync.queue_get(self._sessions, block=False)
except CrossSync.QueueEmpty:
break
else:
await session.delete()
@CrossSync.convert_class(
docstring_format_vars={
"experimental_api": (
"\n\n .. warning::\n The Spanner AsyncIO API is experimental and may be subject to breaking changes.\n",
"",
)
}
)
class PingingPool(FixedSizePool):
"""{experimental_api}Concrete session pool implementation:
- Pre-allocates / creates a fixed number of sessions.
- Sessions are used in "round-robin" order (LRU first).
- "Pings" existing sessions in the background after a specified interval
via an API call (``session.ping()``).
- Blocks, with a timeout, when :meth:`get` is called on an empty pool.
Raises after timing out.
- Raises when :meth:`put` is called on a full pool. That error is
never expected in normal practice, as users should be calling
:meth:`get` followed by :meth:`put` whenever in need of a session.
The application is responsible for calling :meth:`ping` at appropriate
times, e.g. from a background thread.
:type size: int
:param size: fixed pool size
:type default_timeout: int
:param default_timeout: default timeout, in seconds, to wait for
a returned session.
:type ping_interval: int
:param ping_interval: interval at which to ping sessions.
:type labels: dict (str -> str) or None
:param labels: (Optional) user-assigned labels for sessions created
by the pool.
:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
"""
def __init__(
self,
size=10,
default_timeout=10,
ping_interval=3000,
labels=None,
database_role=None,
):
super(PingingPool, self).__init__(
size=size,
default_timeout=default_timeout,
labels=labels,
database_role=database_role,
max_age_minutes=ping_interval // 60,
)
self._delta = datetime.timedelta(seconds=ping_interval)
self._sessions = CrossSync.PriorityQueue(size)
self._lock = CrossSync.Lock()
@CrossSync.convert
async def bind(self, database):
"""Associate the pool with a database.
:type database: :class:`~google.cloud.spanner_v1.database.Database`
:param database: database used by the pool to create sessions
when needed.
"""
self._database = database
api = database.spanner_api
metadata = _metadata_with_prefix(database.name)
if database._route_to_leader_enabled:
metadata.append(_metadata_with_leader_aware_routing(True))
self._database_role = self._database_role or self._database.database_role
request = BatchCreateSessionsRequest(
database=database.name,
session_count=self.size,
session_template=SessionProto(creator_role=self.database_role),
)
span_event_attributes = {"kind": type(self).__name__}
current_span = get_current_span()
requested_session_count = request.session_count
if requested_session_count <= 0:
add_span_event(
current_span,
f"Invalid session pool size({requested_session_count}) <= 0",
span_event_attributes,
)
return
add_span_event(
current_span,
f"Requesting {requested_session_count} sessions",
span_event_attributes,
)
observability_options = getattr(self._database, "observability_options", None)
with trace_call(
"CloudSpanner.PingingPool.BatchCreateSessions",
observability_options=observability_options,
metadata=metadata,
) as span, MetricsCapture(self._resource_info):
returned_session_count = 0
while returned_session_count < self.size:
call_metadata, error_augmenter = database.with_error_augmentation(
database._next_nth_request,
1,
metadata,
span,
)
with error_augmenter:
resp = await api.batch_create_sessions(
request=request,
metadata=call_metadata,
)
add_span_event(
span,
f"Created {len(resp.session)} sessions",
)
for session_pb in resp.session:
session = self._new_session()
returned_session_count += 1
session._session_id = session_pb.name.split("/")[-1]
await self.put(session)
add_span_event(
span,
f"Requested for {requested_session_count} sessions, returned {returned_session_count}",
span_event_attributes,
)
@CrossSync.convert
async def get(self, timeout=None):
"""Check a session out from the pool.
:type timeout: int
:param timeout: seconds to block waiting for an available session
:rtype: :class:`~google.cloud.spanner_v1.session.Session`
:returns: an existing session from the pool, or a newly-created
session.
:raises: :exc:`queue.Empty` if the queue is empty.
"""
if timeout is None:
timeout = self.default_timeout
start_time = time.time()
span_event_attributes = {"kind": type(self).__name__}
current_span = get_current_span()
add_span_event(
current_span,
"Waiting for a session to become available",
span_event_attributes,
)
ping_after = None
session = None
try:
ping_after, session = await CrossSync.queue_get(
self._sessions, block=True, timeout=timeout
)
except CrossSync.QueueEmpty as e:
add_span_event(
current_span,
"No sessions available in the pool within the specified timeout",
span_event_attributes,
)
# Re-raising CrossSync.QueueEmpty is correct as it's the expected interface
raise e
if _NOW() > ping_after:
# Using session.exists() guarantees the returned session exists.
# session.ping() uses a cached result in the backend which could
# result in a recently deleted session being returned.
if not await session.exists():
session = self._new_session()
await session.create()
span_event_attributes.update(
{
"time.elapsed": time.time() - start_time,
"session.id": session._session_id,
"kind": "pinging_pool",
}
)
add_span_event(current_span, "Acquired session", span_event_attributes)
return session
@CrossSync.convert
async def put(self, session):
"""Return a session to the pool.
Never blocks: if the pool is full, raises.
:type session: :class:`~google.cloud.spanner_v1.session.Session`
:param session: the session being returned.
:raises: :exc:`queue.Full` if the queue is full.
"""
try:
await CrossSync.queue_put(
self._sessions, (_NOW() + self._delta, session), block=False
)
except CrossSync.QueueFull:
# PingingPool.put doesn't catch queue.Full in sync version either,
# but it's better to be safe or follow sync version exactly.
# Sync version doesn't have try/except queue.Full in PingingPool.put.
raise CrossSync.QueueFull()
@CrossSync.convert
async def clear(self):
"""Delete all sessions in the pool."""
while True:
try:
_, session = await CrossSync.queue_get(self._sessions, block=False)
except CrossSync.QueueEmpty:
break
else:
await session.delete()
@CrossSync.convert
async def ping(self):
"""Refresh maybe-expired sessions in the pool.
This method is designed to be called from a background thread,
or during the "idle" phase of an event loop.
"""
while True:
try:
ping_after, session = await CrossSync.queue_get(
self._sessions, block=False
)
except CrossSync.QueueEmpty: # all sessions in use
break
if ping_after > _NOW(): # oldest session is fresh
# Re-add to queue with existing expiration
await CrossSync.queue_put(self._sessions, (ping_after, session))
break
try:
await session.ping()
except NotFound:
session = self._new_session()
await session.create()
# Re-add to queue with new expiration
await self.put(session)
@CrossSync.convert_class(
docstring_format_vars={
"experimental_api": (
"\n\n .. warning::\n The Spanner AsyncIO API is experimental and may be subject to breaking changes.\n",
"",
)
}
)
class TransactionPingingPool(PingingPool):
"""{experimental_api}Concrete session pool implementation:
Deprecated: TransactionPingingPool no longer begins a transaction for each of its sessions at startup.
Hence the TransactionPingingPool is same as :class:`PingingPool` and maybe removed in the future.
In addition to the features of :class:`PingingPool`, this class
creates and begins a transaction for each of its sessions at startup.
When a session is returned to the pool, if its transaction has been
committed or rolled back, the pool creates a new transaction for the
session and pushes the transaction onto a separate queue of "transactions
to begin." The application is responsible for flushing this queue
as appropriate via the pool's :meth:`begin_pending_transactions` method.
:type size: int
:param size: fixed pool size
:type default_timeout: int
:param default_timeout: default timeout, in seconds, to wait for
a returned session.
:type ping_interval: int
:param ping_interval: interval at which to ping sessions.
:type labels: dict (str -> str) or None
:param labels: (Optional) user-assigned labels for sessions created
by the pool.
:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
"""
def __init__(
self,
size=10,
default_timeout=10,
ping_interval=3000,
labels=None,
database_role=None,
):
"""This throws a deprecation warning on initialization."""
warn(
f"{self.__class__.__name__} is deprecated.",
DeprecationWarning,
stacklevel=2,
)
super(TransactionPingingPool, self).__init__(
size=size,
default_timeout=default_timeout,
ping_interval=ping_interval,
labels=labels,
database_role=database_role,
)
self._pending_sessions = CrossSync.LifoQueue(size)
# self.begin_pending_transactions() # This is now async, so cannot be called here.
@CrossSync.convert
async def bind(self, database):
"""Associate the pool with a database.
:type database: :class:`~google.cloud.spanner_v1.database.Database`
:param database: database used by the pool to create sessions
when needed.
"""
await super(TransactionPingingPool, self).bind(database)
self._database_role = self._database_role or self._database.database_role
# await self.begin_pending_transactions() # This is now async, so cannot be called here.
@CrossSync.convert
async def put(self, session):
"""Return a session to the pool.
Never blocks: if the pool is full, raises.
:type session: :class:`~google.cloud.spanner_v1.session.Session`
:param session: the session being returned.
:raises: :exc:`queue.Full` if the queue is full.
"""
if session.transaction() is None:
session.transaction()
await CrossSync.queue_put(self._pending_sessions, session)
else:
await super(TransactionPingingPool, self).put(session)
@CrossSync.convert
async def begin_pending_transactions(self):
"""Begin all transactions for sessions added to the pool."""
while not self._pending_sessions.empty():
session = await CrossSync.queue_get(self._pending_sessions)
await super(TransactionPingingPool, self).put(session)