Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add retry and timeout for batch dml #1107

Merged
merged 7 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
feat(spanner): add retry, timeout for batch update
  • Loading branch information
harshachinta committed Feb 26, 2024
commit d020caf31a26a10c6d3ac41e378b872aef3c81f8
17 changes: 16 additions & 1 deletion google/cloud/spanner_v1/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,14 @@ def execute_update(

return response.stats.row_count_exact

def batch_update(self, statements, request_options=None):
def batch_update(
self,
statements,
request_options=None,
*,
retry=gapic_v1.method.DEFAULT,
timeout=gapic_v1.method.DEFAULT,
):
"""Perform a batch of DML statements via an ``ExecuteBatchDml`` request.

:type statements:
Expand All @@ -431,6 +438,12 @@ def batch_update(self, statements, request_options=None):
If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.spanner_v1.types.RequestOptions`.

:type retry: :class:`~google.api_core.retry.Retry`
:param retry: (Optional) The retry settings for this request.

:type timeout: float
:param timeout: (Optional) The timeout for this request.

:rtype:
Tuple(status, Sequence[int])
:returns:
Expand Down Expand Up @@ -486,6 +499,8 @@ def batch_update(self, statements, request_options=None):
api.execute_batch_dml,
request=request,
metadata=metadata,
retry=retry,
timeout=timeout,
)

if self._transaction_id is None:
Expand Down
25 changes: 23 additions & 2 deletions tests/unit/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,14 @@ def test_batch_update_other_error(self):
with self.assertRaises(RuntimeError):
transaction.batch_update(statements=[DML_QUERY])

def _batch_update_helper(self, error_after=None, count=0, request_options=None):
def _batch_update_helper(
self,
error_after=None,
count=0,
request_options=None,
retry=gapic_v1.method.DEFAULT,
timeout=gapic_v1.method.DEFAULT,
):
from google.rpc.status_pb2 import Status
from google.protobuf.struct_pb2 import Struct
from google.cloud.spanner_v1 import param_types
Expand Down Expand Up @@ -716,7 +723,10 @@ def _batch_update_helper(self, error_after=None, count=0, request_options=None):
request_options = RequestOptions(request_options)

status, row_counts = transaction.batch_update(
dml_statements, request_options=request_options
dml_statements,
request_options=request_options,
retry=retry,
timeout=timeout,
)

self.assertEqual(status, expected_status)
Expand Down Expand Up @@ -753,6 +763,8 @@ def _batch_update_helper(self, error_after=None, count=0, request_options=None):
("google-cloud-resource-prefix", database.name),
("x-goog-spanner-route-to-leader", "true"),
],
retry=retry,
timeout=timeout,
)

self.assertEqual(transaction._execute_sql_count, count + 1)
Expand Down Expand Up @@ -826,6 +838,15 @@ def test_batch_update_error(self):

self.assertEqual(transaction._execute_sql_count, 1)

def test_batch_update_w_timeout_param(self):
self._batch_update_helper(timeout=2.0)

def test_batch_update_w_retry_param(self):
self._batch_update_helper(retry=gapic_v1.method.DEFAULT)

def test_batch_update_w_timeout_and_retry_params(self):
self._batch_update_helper(retry=gapic_v1.method.DEFAULT, timeout=2.0)

def test_context_mgr_success(self):
import datetime
from google.cloud.spanner_v1 import CommitResponse
Expand Down