Repository navigation
Conversation
Directed reads route single-use read-only queries to specific replica types or regions via DirectedReadOptions. Add a standalone spanner_directed_read sample demonstrating client and per-query options.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new module directed_read.py and corresponding tests to demonstrate executing snapshot SQL reads in Google Cloud Spanner using client-level and request-level DirectedReadOptions. The feedback suggests improving the usability of the script by allowing the project_id parameter and CLI argument to default to None instead of a hardcoded fallback string, enabling the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).
| def directed_read(project_id: str, instance_id: str, database_id: str) -> None: | ||
| """Executes a snapshot SQL read using client and per-request DirectedReadOptions. | ||
|
|
||
| Args: | ||
| project_id: The Google Cloud project ID. | ||
| instance_id: The Spanner instance ID. | ||
| database_id: The Spanner database ID containing Albums. | ||
| """ |
There was a problem hiding this comment.
Allow project_id to be optional (str | None) so that the Spanner client can automatically infer the project ID from Application Default Credentials (ADC) when it is not explicitly provided.
def directed_read(
project_id: str | None, instance_id: str, database_id: str
) -> None:
"""Executes a snapshot SQL read using client and per-request DirectedReadOptions.
Args:
project_id: The Google Cloud project ID. If None, it is inferred from ADC.
instance_id: The Spanner instance ID.
database_id: The Spanner database ID containing Albums.
"""There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| parser.add_argument( | ||
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | ||
| ) |
There was a problem hiding this comment.
Change the default value of --project-id to os.environ.get("GOOGLE_CLOUD_PROJECT") (which defaults to None if not set) instead of a hardcoded fallback string like "example-project-id". This allows the Spanner client to fall back to Application Default Credentials (ADC) to infer the project ID when neither the command-line argument nor the environment variable is provided.
| parser.add_argument( | |
| "--project-id", | |
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | |
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | |
| ) | |
| parser.add_argument( | |
| "--project-id", | |
| default=os.environ.get("GOOGLE_CLOUD_PROJECT"), | |
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | |
| ) |
References
- When using argparse for a command-line script, the --project_id argument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
Description
Adds
spanner_directed_readconfiguringDirectedReadOptionsonspanner.Clientand overriding replica selection onsnapshot.execute_sql().Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions