Skip to content

feat(spanner): Add asymmetric autoscaling instance creation sample - #14853

Open
iennae wants to merge 3 commits into
mainfrom
feat/spanner-create-instance-asymmetric-autoscaling
Open

iennae wants to merge 3 commits into
mainfrom
feat/spanner-create-instance-asymmetric-autoscaling

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Multi-region Spanner instances on ENTERPRISE_PLUS can configure AsymmetricAutoscalingOption overrides so a high-traffic read-only replica scales independently from base autoscaling limits, which previously had only Go and Java samples.

Establishes spanner/instance_admin and adds spanner_create_instance_with_asymmetric_autoscaling.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Scaling a read-only replica region independently from base multi-region limits had Go and Java examples but lacked a Python InstanceAdminClient sample.
@iennae
iennae requested review from a team as code owners November 17, 2026 23:47
@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 17, 2026
@snippet-bot

snippet-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request adds a new Python sample for Cloud Spanner Instance Admin demonstrating managed autoscaling with asymmetric read-only replica overrides. The review feedback identifies a critical runtime error due to an invalid field total_cpu_utilization_percent in AutoscalingTargets, a potential AttributeError when accessing autoscaling_config without a defensive check, and recommends using flexible dependency ranges instead of exact version pinning in pyproject.toml.

Comment on lines +43 to +47
base_targets = spanner_admin_instance_v1.AutoscalingConfig.AutoscalingTargets(
high_priority_cpu_utilization_percent=65,
total_cpu_utilization_percent=80,
storage_utilization_percent=95,
)

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.

critical

The AutoscalingTargets message in the Cloud Spanner API does not have a total_cpu_utilization_percent field. Spanner autoscaling is based strictly on high-priority CPU utilization (high_priority_cpu_utilization_percent) and storage utilization (storage_utilization_percent). Including total_cpu_utilization_percent will cause a ValueError or TypeError at runtime when constructing the message.

    base_targets = spanner_admin_instance_v1.AutoscalingConfig.AutoscalingTargets(
        high_priority_cpu_utilization_percent=65,
        storage_utilization_percent=95,
    )

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.

Thanks for checking! AutoscalingTargets.total_cpu_utilization_percent is supported in google-cloud-spanner>=3.72.0 and is valid alongside high_priority_cpu_utilization_percent and storage_utilization_percent for asymmetric autoscaling configurations, so we are keeping this field.

Comment on lines +100 to +103
print(
"Asymmetric Overrides: "
f"{len(created.autoscaling_config.asymmetric_autoscaling_options)}"
)

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.

medium

Accessing created.autoscaling_config.asymmetric_autoscaling_options directly can raise an AttributeError if autoscaling_config is None (which can happen if the field is not returned or populated in the response). It is safer to use a defensive check or guard before accessing its attributes.

Suggested change
print(
"Asymmetric Overrides: "
f"{len(created.autoscaling_config.asymmetric_autoscaling_options)}"
)
asymmetric_options = (
created.autoscaling_config.asymmetric_autoscaling_options
if created.autoscaling_config
else []
)
print(f"Asymmetric Overrides: {len(asymmetric_options)}")

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.

Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.

Comment on lines +25 to +28
dependencies = [
"google-api-core==2.44.0",
"google-cloud-spanner==3.72.0",
]

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.

medium

Pinning dependencies to exact versions using == in pyproject.toml is generally discouraged for library/sample packages as it overly restricts dependency resolution when installed alongside other packages. It is recommended to use >= instead.

Suggested change
dependencies = [
"google-api-core==2.44.0",
"google-cloud-spanner==3.72.0",
]
dependencies = [
"google-api-core>=2.44.0",
"google-cloud-spanner>=3.72.0",
]

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.

Thanks for the suggestion! Standalone sample directories in this repository intentionally pin tested dependency versions with == for reproducible sample execution and automated dependency updates, so we are keeping the pinned versions here.

Adds a defensive check before reading asymmetric_autoscaling_options and omits project_id from stdout and stderr output.
…asymmetric_autoscaling

Problem: the sample waits on a long-running operation with a client-side
timeout. When that wait expires the client raises TimeoutError, which is
not an API call error, so the sample ended with an unhandled traceback.

Solution: catch TimeoutError and print a clear message to stderr noting
that only the wait timed out and the operation may still be running (the
timeout does not cancel it). Add a unit test covering the new handler.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants