Skip to content

Commit 0c88b16

Browse files
committed
fix:Refactoring existing retry logic for aborted transactions and clean up redundant code
1 parent c634bdb commit 0c88b16

File tree

3 files changed

+23
-49
lines changed

3 files changed

+23
-49
lines changed

google/cloud/spanner_v1/_helpers.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,22 @@ def _metadata_with_prefix(prefix, **kw):
464464
return [("google-cloud-resource-prefix", prefix)]
465465

466466

467+
def _retry_on_aborted_exception(exc, deadline, attempts, allowed_exceptions):
468+
"""
469+
Handles the retry logic for Aborted exceptions, considering the deadline.
470+
Returns True if the exception is retried, False otherwise.
471+
"""
472+
if isinstance(exc, Aborted) and deadline is not None:
473+
# The logic for handling Aborted exceptions
474+
if (
475+
allowed_exceptions is not None
476+
and allowed_exceptions.get(exc.__class__) is not None
477+
):
478+
_delay_until_retry(exc, deadline=deadline, attempts=attempts)
479+
return True
480+
return False
481+
482+
467483
def _retry(
468484
func,
469485
retry_count=5,
@@ -491,21 +507,19 @@ def _retry(
491507
The result of the function if it is successful, or raises the last exception if all retries fail.
492508
"""
493509
retries = 0
510+
attempts = 0
494511
while True:
512+
if retries > retry_count:
513+
raise Exception("Exceeded retry count.")
495514
if retries > 0 and beforeNextRetry:
496515
beforeNextRetry(retries, delay)
497516

498517
try:
518+
attempts += 1
499519
return func()
500520
except Exception as exc:
501-
if isinstance(exc, Aborted) and deadline is not None:
502-
if (
503-
allowed_exceptions is not None
504-
and allowed_exceptions.get(exc.__class__) is not None
505-
):
506-
retries += 1
507-
_delay_until_retry(exc, deadline=deadline, attempts=retries)
508-
continue
521+
if _retry_on_aborted_exception(exc, deadline, attempts, allowed_exceptions):
522+
continue
509523
if (
510524
allowed_exceptions is None or exc.__class__ in allowed_exceptions
511525
) and retries < retry_count:

google/cloud/spanner_v1/batch.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def commit(
239239
method,
240240
allowed_exceptions={
241241
InternalServerError: _check_rst_stream_error,
242-
Aborted: no_op_handler,
242+
Aborted: lambda exc: None,
243243
},
244244
deadline=deadline,
245245
)
@@ -360,16 +360,11 @@ def batch_write(
360360
request=request,
361361
metadata=metadata,
362362
)
363-
deadline = time.time() + kwargs.get(
364-
"timeout_secs", DEFAULT_RETRY_TIMEOUT_SECS
365-
)
366363
response = _retry(
367364
method,
368365
allowed_exceptions={
369366
InternalServerError: _check_rst_stream_error,
370-
Aborted: no_op_handler,
371367
},
372-
deadline=deadline,
373368
)
374369
self.committed = True
375370
return response
@@ -394,7 +389,3 @@ def _make_write_pb(table, columns, values):
394389
table=table, columns=columns, values=_make_list_value_pbs(values)
395390
)
396391

397-
398-
def no_op_handler(exc):
399-
# No-op (does nothing)
400-
pass

tests/unit/test_batch.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515

16-
import time
1716
import unittest
1817
from unittest.mock import MagicMock
1918
from tests._helpers import (
@@ -651,36 +650,6 @@ def __init__(self, database=None, name=TestBatch.SESSION_NAME):
651650
def session_id(self):
652651
return self.name
653652

654-
def run_in_transaction(self, fnc):
655-
"""
656-
Runs a function in a transaction, retrying if an exception occurs.
657-
:param fnc: The function to run in the transaction.
658-
:param max_retries: Maximum number of retry attempts.
659-
:param delay: Delay (in seconds) between retries.
660-
:return: The result of the function, or raises the exception after max retries.
661-
"""
662-
from google.api_core.exceptions import Aborted
663-
664-
attempt = 0
665-
max_retries = 3
666-
delay = 1
667-
while attempt < max_retries:
668-
try:
669-
result = fnc()
670-
return result
671-
except Aborted as exc:
672-
attempt += 1
673-
if attempt < max_retries:
674-
print(
675-
f"Attempt {attempt} failed with Aborted. Retrying in {delay} seconds..."
676-
)
677-
time.sleep(delay) # Wait before retrying
678-
else:
679-
raise exc # After max retries, raise the exception
680-
except Exception as exc:
681-
print(f"Unexpected exception occurred: {exc}")
682-
raise # Raise any other unexpected exception immediately
683-
684653

685654
class _Database(object):
686655
name = "testing"

0 commit comments

Comments
 (0)