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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add sample for managed autoscaler #1111

Merged
merged 4 commits into from
Mar 7, 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
Merge branch 'main' into managed_autoscalar
  • Loading branch information
rahul2393 committed Mar 7, 2024
commit 1a67cf8d1a81faf4a3ee8b8b468c0be74d645147
48 changes: 48 additions & 0 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3017,6 +3017,51 @@ def directed_read_options(
# [END spanner_directed_read]


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 import exceptions as core_exceptions
from google.api_core import retry

# 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)

retry = retry.Retry(
# Customize retry with an initial wait time of 500 milliseconds.
initial=0.5,
# Customize retry with a maximum wait time of 16 seconds.
maximum=16,
# Customize retry with a wait time multiplier per iteration of 1.5.
multiplier=1.5,
# Customize retry with a timeout on
# how long a certain RPC may be retried in
# case the server returns an error.
timeout=60,
# Configure which errors should be retried.
predicate=retry.if_exception_type(
core_exceptions.ServiceUnavailable,
),
)

# 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 of 60 seconds for this request
timeout=60,
)

for row in results:
print("SingerId: {}, AlbumId: {}, AlbumTitle: {}".format(*row))

# [END spanner_set_custom_timeout_and_retry]


# [START spanner_create_instance_with_autoscaling_config]
def create_instance_with_autoscaling_config(instance_id):
"""Creates a Cloud Spanner instance with an autoscaling configuration."""
Expand Down Expand Up @@ -3075,6 +3120,7 @@ def create_instance_with_autoscaling_config(instance_id):

# [END spanner_create_instance_with_autoscaling_config]


if __name__ == "__main__": # noqa: C901
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
Expand Down Expand Up @@ -3351,5 +3397,7 @@ def create_instance_with_autoscaling_config(instance_id):
)
elif args.command == "directed_read_options":
directed_read_options(args.instance_id, args.database_id)
elif args.command == "set_custom_timeout_and_retry":
set_custom_timeout_and_retry(args.instance_id, args.database_id)
elif args.command == "create_instance_with_autoscaling_config":
create_instance_with_autoscaling_config(args.instance_id)
You are viewing a condensed version of this merge commit. You can view the full changes here.