Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from google.cloud.aio._cross_sync import CrossSync
from google.cloud.spanner_v1._async._helpers import _retry, _retry_on_aborted_exception
from google.cloud.spanner_v1._helpers import (
_check_rst_stream_error,
_is_retryable_internal_error,
_make_list_value_pb,
_make_list_value_pbs,
_make_value_pb,
Expand Down Expand Up @@ -479,7 +479,7 @@ def wrapped_method():
response = await _retry(
wrapped_method,
allowed_exceptions={
InternalServerError: _check_rst_stream_error,
InternalServerError: _is_retryable_internal_error,
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from google.cloud.spanner_v1._async.streamed import StreamedResultSet
from google.cloud.spanner_v1._helpers import (
_augment_error_with_request_id,
_check_rst_stream_error,
_is_retryable_internal_error,
_make_execute_sql_request,
_make_value_pb,
_merge_client_context,
Expand Down Expand Up @@ -66,11 +66,6 @@
TransactionSelector,
)

_STREAM_RESUMPTION_INTERNAL_ERROR_MESSAGES = (
"RST_STREAM",
"Received unexpected EOS on DATA frame from server",
)

# Maximum time that a request will wait for a concurrent request to begin the
# transaction before giving up.
_TRANSACTION_BEGIN_TIMEOUT_SECONDS = 30.0
Expand Down Expand Up @@ -205,29 +200,17 @@ async def _restart_on_unavailable(
else:
stream_finished = True

except ServiceUnavailable:
del item_buffer[:]
request.resume_token = resume_token
if transaction is not None:
transaction_selector = transaction._build_transaction_selector_pb()
request.transaction = transaction_selector
attempt += 1
iterator = None
continue

except InternalServerError as exc:
resumable_error = any(
resumable_message in exc.message
for resumable_message in _STREAM_RESUMPTION_INTERNAL_ERROR_MESSAGES
)
if not resumable_error:
except (ServiceUnavailable, InternalServerError) as exc:
if isinstance(
exc, InternalServerError
) and not _is_retryable_internal_error(exc):
raise _augment_error_with_request_id(exc, current_request_id)
del item_buffer[:]
request.resume_token = resume_token
if transaction is not None:
transaction_selector = transaction._build_transaction_selector_pb()
attempt += 1
request.transaction = transaction_selector
attempt += 1
iterator = None
continue

Expand Down Expand Up @@ -849,7 +832,7 @@ async def attempt_tracking_method():

response = await _retry(
attempt_tracking_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
)

return [partition.partition_token for partition in response.partitions]
Expand Down Expand Up @@ -932,7 +915,7 @@ async def attempt_tracking_method():

response = await _retry(
attempt_tracking_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
)

return [partition.partition_token for partition in response.partitions]
Expand Down Expand Up @@ -1020,7 +1003,7 @@ async def before_next_retry(nth_retry, delay_in_seconds):
wrapped_method,
before_next_retry=before_next_retry,
allowed_exceptions={
InternalServerError: _check_rst_stream_error,
InternalServerError: _is_retryable_internal_error,
Aborted: None,
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from google.cloud.spanner_v1._async.batch import _BatchBase
from google.cloud.spanner_v1._async.snapshot import _SnapshotBase
from google.cloud.spanner_v1._helpers import (
_check_rst_stream_error,
_is_retryable_internal_error,
_make_value_pb,
_merge_client_context,
_merge_query_options,
Expand Down Expand Up @@ -161,7 +161,7 @@ async def _execute_request(
method = functools.partial(method, request=request, span=span)
response = await _retry(
method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
)

return response
Expand Down Expand Up @@ -222,7 +222,9 @@ async def wrapped_method():

await _retry(
wrapped_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={
InternalServerError: _is_retryable_internal_error
},
)

self.rolled_back = True
Expand Down Expand Up @@ -361,7 +363,7 @@ def before_next_retry(nth_retry, delay_in_seconds):

commit_response_pb: CommitResponse = await _retry(
wrapped_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
before_next_retry=before_next_retry,
)

Expand Down
52 changes: 34 additions & 18 deletions packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import proto
from google.api_core import datetime_helpers
from google.api_core.exceptions import Aborted
from google.api_core.exceptions import Aborted, InternalServerError
from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper
from google.protobuf.message import DecodeError, Message
from google.protobuf.struct_pb2 import NULL_VALUE, ListValue, Value
Expand Down Expand Up @@ -985,8 +985,10 @@ def _retry(
func: The function to be retried.
retry_count: The maximum number of times to retry the function.
delay: The delay in seconds between retries.
allowed_exceptions: A tuple of exceptions that are allowed to occur without triggering a retry.
Passing allowed_exceptions as None will lead to retrying for all exceptions.
allowed_exceptions: A mapping from exception classes to retry on to
optional predicate callables (or None to retry all
instances). Passing allowed_exceptions as None will
lead to retrying for all exceptions.

Returns:
The result of the function if it is successful, or raises the last exception if all retries fail.
Expand All @@ -1004,29 +1006,43 @@ def _retry(
)

if is_allowed and retries < retry_count:
if (
allowed_exceptions is not None
and allowed_exceptions[exc.__class__] is not None
):
allowed_exceptions[exc.__class__](exc)
if allowed_exceptions is not None:
check_exception = allowed_exceptions[exc.__class__]
if callable(check_exception) and not check_exception(exc):
raise exc
Comment thread
olavloite marked this conversation as resolved.
Resolved
Comment thread
olavloite marked this conversation as resolved.
Resolved
time.sleep(delay)
delay = delay * 2
retries = retries + 1
else:
raise exc


def _check_rst_stream_error(exc):
resumable_error = any(
resumable_message in exc.message
for resumable_message in (
"RST_STREAM",
"Received unexpected EOS on DATA frame from server",
)
# Keep "RST_STREAM" as a substring to cover all RST_STREAM error messages
# (e.g., "stream terminated by RST_STREAM" and "Received RST_STREAM with error code ..."),
# and omit the trailing period on the auth backend error so variants with or without
# trailing punctuation match.
_RETRYABLE_INTERNAL_ERROR_MESSAGES = (
"RST_STREAM",
"Received unexpected EOS on DATA frame from server",
"HTTP/2 error code: INTERNAL_ERROR",
"Connection closed with unknown cause",
"Authentication backend internal server error. Please retry",
)


def _is_retryable_internal_error(exception: Exception) -> bool:
"""Return whether the exception is a retryable InternalServerError."""
if not isinstance(exception, InternalServerError):
return False

message = getattr(exception, "message", None)
if not isinstance(message, str):
message = str(exception)

return any(
resumable_message in message
for resumable_message in _RETRYABLE_INTERNAL_ERROR_MESSAGES
)
if not resumable_error:
raise
return True


def _metadata_with_leader_aware_routing(value, **kw):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from google.cloud._helpers import _datetime_to_pb_timestamp

from google.cloud.spanner_v1._helpers import (
_check_rst_stream_error,
_is_retryable_internal_error,
_make_list_value_pb,
_make_list_value_pbs,
_make_value_pb,
Expand Down Expand Up @@ -410,7 +410,7 @@ def wrapped_method():

response = _retry(
wrapped_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
)
self.committed = True
return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
from google.cloud.aio._cross_sync import CrossSync
from google.cloud.spanner_v1._helpers import (
_augment_error_with_request_id,
_check_rst_stream_error,
_drain_stream,
_is_retryable_internal_error,
_make_execute_sql_request,
_make_value_pb,
_merge_client_context,
Expand Down Expand Up @@ -69,10 +69,6 @@
TransactionSelector,
)

_STREAM_RESUMPTION_INTERNAL_ERROR_MESSAGES = (
"RST_STREAM",
"Received unexpected EOS on DATA frame from server",
)
_TRANSACTION_BEGIN_TIMEOUT_SECONDS = 30.0


Expand Down Expand Up @@ -181,30 +177,17 @@ def _restart_on_unavailable(
break
else:
stream_finished = True
except ServiceUnavailable:
del item_buffer[:]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we removing this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not 'removed'. It is consolidated into one except block. The ServiceUnavailable error was added to the except on line 180, as both blocks do the same thing.

request.resume_token = resume_token
if transaction is not None:
transaction_selector = transaction._build_transaction_selector_pb()
request.transaction = transaction_selector
attempt += 1
iterator = None
continue
except InternalServerError as exc:
resumable_error = any(
(
resumable_message in exc.message
for resumable_message in _STREAM_RESUMPTION_INTERNAL_ERROR_MESSAGES
)
)
if not resumable_error:
except (ServiceUnavailable, InternalServerError) as exc:
if isinstance(exc, InternalServerError) and (
not _is_retryable_internal_error(exc)
):
raise _augment_error_with_request_id(exc, current_request_id)
del item_buffer[:]
request.resume_token = resume_token
if transaction is not None:
transaction_selector = transaction._build_transaction_selector_pb()
attempt += 1
request.transaction = transaction_selector
attempt += 1
iterator = None
continue
except Exception as exc:
Expand Down Expand Up @@ -766,7 +749,7 @@ def attempt_tracking_method():

response = _retry(
attempt_tracking_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
)
return [partition.partition_token for partition in response.partitions]

Expand Down Expand Up @@ -841,7 +824,7 @@ def attempt_tracking_method():

response = _retry(
attempt_tracking_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
)
return [partition.partition_token for partition in response.partitions]

Expand Down Expand Up @@ -919,7 +902,7 @@ def before_next_retry(nth_retry, delay_in_seconds):
wrapped_method,
before_next_retry=before_next_retry,
allowed_exceptions={
InternalServerError: _check_rst_stream_error,
InternalServerError: _is_retryable_internal_error,
Aborted: None,
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from google.protobuf.struct_pb2 import Struct

from google.cloud.spanner_v1._helpers import (
_check_rst_stream_error,
_is_retryable_internal_error,
_make_value_pb,
_merge_client_context,
_merge_query_options,
Expand Down Expand Up @@ -131,7 +131,7 @@ def _execute_request(
method = functools.partial(method, request=request, span=span)
response = _retry(
method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
)
return response

Expand Down Expand Up @@ -182,7 +182,9 @@ def wrapped_method():

_retry(
wrapped_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={
InternalServerError: _is_retryable_internal_error
},
)
self.rolled_back = True

Expand Down Expand Up @@ -301,7 +303,7 @@ def before_next_retry(nth_retry, delay_in_seconds):

commit_response_pb: CommitResponse = _retry(
wrapped_method,
allowed_exceptions={InternalServerError: _check_rst_stream_error},
allowed_exceptions={InternalServerError: _is_retryable_internal_error},
before_next_retry=before_next_retry,
)
if commit_response_pb._pb.HasField("precommit_token"):
Expand Down
Loading
Loading