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
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
Prev Previous commit
Next Next commit
feat(spanner): update code for retry
  • Loading branch information
harshachinta committed Mar 6, 2024
commit b4a155df35c054f8301385e13b7a269ace2b1289
21 changes: 15 additions & 6 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3020,28 +3020,37 @@ def directed_read_options(
def set_custom_timeout_and_retry(instance_id, database_id):
"""Executes a snapshot read with custom timeout and retry."""
# [START spanner_set_custom_timeout_and_retry]
from google.api_core.retry import Retry
from google.api_core import retry
from google.api_core import exceptions as core_exceptions

# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

# Customize retry with an initial wait time of 5 seconds.
# Customize retry with a maximum wait time of 100 seconds.
# Customize retry with a wait time multiplier per iteration of 2.
# Customize retry with an initial wait time of 500 milliseconds.
# Customize retry with a maximum wait time of 16 seconds.
# Customize retry with a wait time multiplier per iteration of 1.5.
# Customize retry with a timeout on
# how long a certain RPC may be retried in case the server returns an error.
retry = Retry(initial=5, maximum=100, multiplier=2, timeout=60)
retry = retry.Retry(
harshachinta marked this conversation as resolved.
Show resolved Hide resolved
initial=0.5,
maximum=16,
multiplier=1.5,
timeout=60,
predicate=retry.if_exception_type(
core_exceptions.ServiceUnavailable,
harshachinta marked this conversation as resolved.
Show resolved Hide resolved
),
)

# Set a custom retry and timeout setting.
with database.snapshot() as snapshot:
results = snapshot.execute_sql(
"SELECT SingerId, AlbumId, AlbumTitle FROM Albums",
# Set custom retry setting for this request
retry=retry,
# Set custom timeout setting for this request
# Set custom timeout of 60 seconds for this request
timeout=60,
)

Expand Down