@@ -355,13 +355,10 @@ def read(
355355 directed_read_options = directed_read_options ,
356356 )
357357
358- nth_request = getattr (database , "_next_nth_request" , 0 )
359- all_metadata = database .metadata_with_request_id (nth_request , 1 , metadata )
360-
361358 restart = functools .partial (
362359 api .streaming_read ,
363360 request = request ,
364- metadata = all_metadata ,
361+ metadata = metadata ,
365362 retry = retry ,
366363 timeout = timeout ,
367364 )
@@ -751,17 +748,24 @@ def partition_read(
751748 metadata = metadata ,
752749 ), MetricsCapture ():
753750 nth_request = getattr (database , "_next_nth_request" , 0 )
754- all_metadata = database .metadata_with_request_id (nth_request , 1 , metadata )
755- method = functools .partial (
756- api .partition_read ,
757- request = request ,
758- metadata = all_metadata ,
759- retry = retry ,
760- timeout = timeout ,
761- )
751+ counters = dict (attempt = 0 )
752+
753+ def attempt_tracking_method ():
754+ counters ["attempt" ] += 1
755+ all_metadata = database .metadata_with_request_id (
756+ nth_request , counters ["attempt" ], metadata
757+ )
758+ method = functools .partial (
759+ api .partition_read ,
760+ request = request ,
761+ metadata = all_metadata ,
762+ retry = retry ,
763+ timeout = timeout ,
764+ )
765+ return method ()
762766
763767 response = _retry (
764- method ,
768+ attempt_tracking_method ,
765769 allowed_exceptions = {InternalServerError : _check_rst_stream_error },
766770 )
767771
@@ -858,17 +862,24 @@ def partition_query(
858862 metadata = metadata ,
859863 ), MetricsCapture ():
860864 nth_request = getattr (database , "_next_nth_request" , 0 )
861- all_metadata = database .metadata_with_request_id (nth_request , 1 , metadata )
862- method = functools .partial (
863- api .partition_query ,
864- request = request ,
865- metadata = all_metadata ,
866- retry = retry ,
867- timeout = timeout ,
868- )
865+ counters = dict (attempt = 0 )
866+
867+ def attempt_tracking_method ():
868+ counters ["attempt" ] += 1
869+ all_metadata = database .metadata_with_request_id (
870+ nth_request , counters ["attempt" ], metadata
871+ )
872+ method = functools .partial (
873+ api .partition_query ,
874+ request = request ,
875+ metadata = all_metadata ,
876+ retry = retry ,
877+ timeout = timeout ,
878+ )
879+ return method ()
869880
870881 response = _retry (
871- method ,
882+ attempt_tracking_method ,
872883 allowed_exceptions = {InternalServerError : _check_rst_stream_error },
873884 )
874885
@@ -1008,16 +1019,23 @@ def begin(self):
10081019 metadata = metadata ,
10091020 ), MetricsCapture ():
10101021 nth_request = getattr (database , "_next_nth_request" , 0 )
1011- all_metadata = database .metadata_with_request_id (nth_request , 1 , metadata )
1012- method = functools .partial (
1013- api .begin_transaction ,
1014- session = self ._session .name ,
1015- options = txn_selector .begin ,
1016- metadata = all_metadata ,
1017- )
1022+ counters = dict (attempt = 0 )
1023+
1024+ def attempt_tracking_method ():
1025+ counters ["attempt" ] += 1
1026+ all_metadata = database .metadata_with_request_id (
1027+ nth_request , counters ["attempt" ], metadata
1028+ )
1029+ method = functools .partial (
1030+ api .begin_transaction ,
1031+ session = self ._session .name ,
1032+ options = txn_selector .begin ,
1033+ metadata = all_metadata ,
1034+ )
1035+ return method ()
10181036
10191037 response = _retry (
1020- method ,
1038+ attempt_tracking_method ,
10211039 allowed_exceptions = {InternalServerError : _check_rst_stream_error },
10221040 )
10231041 self ._transaction_id = response .id
0 commit comments