Skip to content

Commit 321be36

Browse files
committed
use retry settings from config
1 parent 212ad88 commit 321be36

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

google/cloud/spanner_v1/database.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import warnings
2424

2525
from google.api_core.client_options import ClientOptions
26-
from google.api_core.retry import Retry, if_exception_type
26+
from google.api_core.retry import if_exception_type
2727
import google.auth.credentials
2828
from google.protobuf.struct_pb2 import Struct
2929
from google.cloud.exceptions import NotFound
@@ -468,8 +468,9 @@ def execute_pdml():
468468

469469
return result_set.stats.row_count_lower_bound
470470

471-
retry = Retry(predicate=if_exception_type(Aborted))
472-
return retry(execute_pdml)()
471+
retry_config = api._method_configs["ExecuteStreamingSql"].retry
472+
473+
return _retry_on_aborted(execute_pdml, retry_config)()
473474

474475
def session(self, labels=None):
475476
"""Factory to create a session for this database.
@@ -1029,3 +1030,19 @@ def __init__(self, source_type, backup_info):
10291030
@classmethod
10301031
def from_pb(cls, pb):
10311032
return cls(pb.source_type, pb.backup_info)
1033+
1034+
1035+
def _retry_on_aborted(func, retry_config):
1036+
"""Helper for :meth:`Database.execute_partitioned_dml`.
1037+
1038+
Wrap function in a Retry that will retry on Aborted exceptions
1039+
with the retry config specified.
1040+
1041+
:type func: callable
1042+
:param func: the function to be retried on Aborted exceptions
1043+
1044+
:type retry_config: Retry
1045+
:param retry_config: retry object with the settings to be used
1046+
"""
1047+
retry = retry_config.with_predicate(if_exception_type(Aborted))
1048+
return retry(func)

tests/unit/test_database.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ def _execute_partitioned_dml_helper(
975975
self, dml, params=None, param_types=None, query_options=None, retried=False
976976
):
977977
from google.api_core.exceptions import Aborted
978+
from google.api_core.retry import Retry
978979
from google.protobuf.struct_pb2 import Struct
979980
from google.cloud.spanner_v1.proto.result_set_pb2 import (
980981
PartialResultSet,
@@ -990,6 +991,10 @@ def _execute_partitioned_dml_helper(
990991
_merge_query_options,
991992
)
992993

994+
import collections
995+
996+
MethodConfig = collections.namedtuple("MethodConfig", ["retry"])
997+
993998
transaction_pb = TransactionPB(id=self.TRANSACTION_ID)
994999

9951000
stats_pb = ResultSetStats(row_count_lower_bound=2)
@@ -1003,6 +1008,7 @@ def _execute_partitioned_dml_helper(
10031008
pool.put(session)
10041009
database = self._make_one(self.DATABASE_ID, instance, pool=pool)
10051010
api = database._spanner_api = self._make_spanner_api()
1011+
api._method_configs = {"ExecuteStreamingSql": MethodConfig(retry=Retry())}
10061012
if retried:
10071013
retry_transaction_pb = TransactionPB(id=self.RETRY_TRANSACTION_ID)
10081014
api.begin_transaction.side_effect = [transaction_pb, retry_transaction_pb]

0 commit comments

Comments
 (0)