53
53
from google .cloud .spanner_v1 ._helpers import (
54
54
_metadata_with_prefix ,
55
55
_metadata_with_leader_aware_routing ,
56
+ _metadata_with_request_id ,
56
57
)
57
58
from google .cloud .spanner_v1 .batch import Batch
58
59
from google .cloud .spanner_v1 .batch import MutationGroups
@@ -151,6 +152,9 @@ class Database(object):
151
152
152
153
_spanner_api : SpannerClient = None
153
154
155
+ __transport_lock = threading .Lock ()
156
+ __transports_to_channel_id = dict ()
157
+
154
158
def __init__ (
155
159
self ,
156
160
database_id ,
@@ -188,6 +192,7 @@ def __init__(
188
192
self ._instance ._client .default_transaction_options
189
193
)
190
194
self ._proto_descriptors = proto_descriptors
195
+ self ._channel_id = 0 # It'll be created when _spanner_api is created.
191
196
192
197
if pool is None :
193
198
pool = BurstyPool (database_role = database_role )
@@ -446,8 +451,26 @@ def spanner_api(self):
446
451
client_info = client_info ,
447
452
client_options = client_options ,
448
453
)
454
+
455
+ with self .__transport_lock :
456
+ transport = self ._spanner_api ._transport
457
+ channel_id = self .__transports_to_channel_id .get (transport , None )
458
+ if channel_id is None :
459
+ channel_id = len (self .__transports_to_channel_id ) + 1
460
+ self .__transports_to_channel_id [transport ] = channel_id
461
+ self ._channel_id = channel_id
462
+
449
463
return self ._spanner_api
450
464
465
+ def metadata_with_request_id (self , nth_request , nth_attempt , prior_metadata = []):
466
+ return _metadata_with_request_id (
467
+ self ._nth_client_id ,
468
+ self ._channel_id ,
469
+ nth_request ,
470
+ nth_attempt ,
471
+ prior_metadata ,
472
+ )
473
+
451
474
def __eq__ (self , other ):
452
475
if not isinstance (other , self .__class__ ):
453
476
return NotImplemented
@@ -490,7 +513,10 @@ def create(self):
490
513
database_dialect = self ._database_dialect ,
491
514
proto_descriptors = self ._proto_descriptors ,
492
515
)
493
- future = api .create_database (request = request , metadata = metadata )
516
+ future = api .create_database (
517
+ request = request ,
518
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
519
+ )
494
520
return future
495
521
496
522
def exists (self ):
@@ -506,7 +532,12 @@ def exists(self):
506
532
metadata = _metadata_with_prefix (self .name )
507
533
508
534
try :
509
- api .get_database_ddl (database = self .name , metadata = metadata )
535
+ api .get_database_ddl (
536
+ database = self .name ,
537
+ metadata = self .metadata_with_request_id (
538
+ self ._next_nth_request , 1 , metadata
539
+ ),
540
+ )
510
541
except NotFound :
511
542
return False
512
543
return True
@@ -523,10 +554,16 @@ def reload(self):
523
554
"""
524
555
api = self ._instance ._client .database_admin_api
525
556
metadata = _metadata_with_prefix (self .name )
526
- response = api .get_database_ddl (database = self .name , metadata = metadata )
557
+ response = api .get_database_ddl (
558
+ database = self .name ,
559
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
560
+ )
527
561
self ._ddl_statements = tuple (response .statements )
528
562
self ._proto_descriptors = response .proto_descriptors
529
- response = api .get_database (name = self .name , metadata = metadata )
563
+ response = api .get_database (
564
+ name = self .name ,
565
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
566
+ )
530
567
self ._state = DatabasePB .State (response .state )
531
568
self ._create_time = response .create_time
532
569
self ._restore_info = response .restore_info
@@ -571,7 +608,10 @@ def update_ddl(self, ddl_statements, operation_id="", proto_descriptors=None):
571
608
proto_descriptors = proto_descriptors ,
572
609
)
573
610
574
- future = api .update_database_ddl (request = request , metadata = metadata )
611
+ future = api .update_database_ddl (
612
+ request = request ,
613
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
614
+ )
575
615
return future
576
616
577
617
def update (self , fields ):
@@ -609,7 +649,9 @@ def update(self, fields):
609
649
metadata = _metadata_with_prefix (self .name )
610
650
611
651
future = api .update_database (
612
- database = database_pb , update_mask = field_mask , metadata = metadata
652
+ database = database_pb ,
653
+ update_mask = field_mask ,
654
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
613
655
)
614
656
615
657
return future
@@ -622,7 +664,10 @@ def drop(self):
622
664
"""
623
665
api = self ._instance ._client .database_admin_api
624
666
metadata = _metadata_with_prefix (self .name )
625
- api .drop_database (database = self .name , metadata = metadata )
667
+ api .drop_database (
668
+ database = self .name ,
669
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
670
+ )
626
671
627
672
def execute_partitioned_dml (
628
673
self ,
@@ -711,7 +756,13 @@ def execute_pdml():
711
756
with SessionCheckout (self ._pool ) as session :
712
757
add_span_event (span , "Starting BeginTransaction" )
713
758
txn = api .begin_transaction (
714
- session = session .name , options = txn_options , metadata = metadata
759
+ session = session .name ,
760
+ options = txn_options ,
761
+ metadata = self .metadata_with_request_id (
762
+ self ._next_nth_request ,
763
+ 1 ,
764
+ metadata ,
765
+ ),
715
766
)
716
767
717
768
txn_selector = TransactionSelector (id = txn .id )
@@ -724,6 +775,7 @@ def execute_pdml():
724
775
query_options = query_options ,
725
776
request_options = request_options ,
726
777
)
778
+
727
779
method = functools .partial (
728
780
api .execute_streaming_sql ,
729
781
metadata = metadata ,
@@ -736,6 +788,7 @@ def execute_pdml():
736
788
metadata = metadata ,
737
789
transaction_selector = txn_selector ,
738
790
observability_options = self .observability_options ,
791
+ request_id_manager = self ,
739
792
)
740
793
741
794
result_set = StreamedResultSet (iterator )
@@ -745,6 +798,18 @@ def execute_pdml():
745
798
746
799
return _retry_on_aborted (execute_pdml , DEFAULT_RETRY_BACKOFF )()
747
800
801
+ @property
802
+ def _next_nth_request (self ):
803
+ if self ._instance and self ._instance ._client :
804
+ return self ._instance ._client ._next_nth_request
805
+ return 1
806
+
807
+ @property
808
+ def _nth_client_id (self ):
809
+ if self ._instance and self ._instance ._client :
810
+ return self ._instance ._client ._nth_client_id
811
+ return 0
812
+
748
813
def session (self , labels = None , database_role = None ):
749
814
"""Factory to create a session for this database.
750
815
@@ -965,7 +1030,7 @@ def restore(self, source):
965
1030
)
966
1031
future = api .restore_database (
967
1032
request = request ,
968
- metadata = metadata ,
1033
+ metadata = self . metadata_with_request_id ( self . _next_nth_request , 1 , metadata ) ,
969
1034
)
970
1035
return future
971
1036
@@ -1034,7 +1099,10 @@ def list_database_roles(self, page_size=None):
1034
1099
parent = self .name ,
1035
1100
page_size = page_size ,
1036
1101
)
1037
- return api .list_database_roles (request = request , metadata = metadata )
1102
+ return api .list_database_roles (
1103
+ request = request ,
1104
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
1105
+ )
1038
1106
1039
1107
def table (self , table_id ):
1040
1108
"""Factory to create a table object within this database.
@@ -1118,7 +1186,10 @@ def get_iam_policy(self, policy_version=None):
1118
1186
requested_policy_version = policy_version
1119
1187
),
1120
1188
)
1121
- response = api .get_iam_policy (request = request , metadata = metadata )
1189
+ response = api .get_iam_policy (
1190
+ request = request ,
1191
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
1192
+ )
1122
1193
return response
1123
1194
1124
1195
def set_iam_policy (self , policy ):
@@ -1140,7 +1211,10 @@ def set_iam_policy(self, policy):
1140
1211
resource = self .name ,
1141
1212
policy = policy ,
1142
1213
)
1143
- response = api .set_iam_policy (request = request , metadata = metadata )
1214
+ response = api .set_iam_policy (
1215
+ request = request ,
1216
+ metadata = self .metadata_with_request_id (self ._next_nth_request , 1 , metadata ),
1217
+ )
1144
1218
return response
1145
1219
1146
1220
@property
0 commit comments