Skip to content

Add Cloud Spanner Directed Reads support to SpannerIO#39241

Open
ath-08 wants to merge 1 commit into
apache:masterfrom
ath-08:spanner-io-directed-reads
Open

Add Cloud Spanner Directed Reads support to SpannerIO#39241
ath-08 wants to merge 1 commit into
apache:masterfrom
ath-08:spanner-io-directed-reads

Conversation

@ath-08

@ath-08 ath-08 commented Jul 8, 2026

Copy link
Copy Markdown

Add Cloud Spanner Directed Reads support to SpannerIO

This PR adds support for Cloud Spanner Directed Reads, allowing Apache Beam pipeline developers to route read and Change Stream polling queries to specific replica types or geographic locations (e.g., restricting queries exclusively to READ_ONLY replicas for workload isolation).

Key Changes

  • SpannerConfig:
    • Added getDirectedReadOptions() getter, builder setter, and withDirectedReadOptions(...) overloads accepting DirectedReadOptions, ValueProvider<DirectedReadOptions>, and String.
    • Added an internal parseDirectedReadOptions(String) helper method supporting standard JSON strings (via protobuf JsonFormat).
  • SpannerAccessor:
    • Threaded spannerConfig.getDirectedReadOptions() into SpannerOptions.Builder.setDirectedReadOptions(...) during client initialization so all DatabaseClient instances and Change Stream queries (ChangeStreamDao) automatically inherit the directed read configuration without requiring per-query overrides.
  • SpannerIO:
    • Added withDirectedReadOptions(...) delegation methods to SpannerIO.Read, SpannerIO.ReadAll, and SpannerIO.ReadChangeStream.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces support for Cloud Spanner Directed Reads within the Apache Beam SpannerIO connector. By enabling users to configure replica selection, this change allows for more granular control over read traffic routing in Spanner, improving performance and latency for distributed workloads. The implementation includes robust parsing for both JSON and shorthand string configurations, alongside comprehensive unit testing to ensure proper propagation across SpannerIO read and change stream operations.

Highlights

  • Directed Read Support: Added support for Cloud Spanner DirectedReadOptions to SpannerConfig and SpannerIO, allowing users to specify replica selection for reads.
  • Flexible Configuration: Implemented multiple overloads for withDirectedReadOptions, supporting direct objects, ValueProviders, and shorthand string formats (e.g., 'location:type').
  • Automated Integration: Integrated directed read options into SpannerAccessor, ensuring DatabaseClient instances and ChangeStreamDao queries automatically inherit the configuration.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@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 support for Cloud Spanner directed read options (DirectedReadOptions) across SpannerIO's Read, ReadAll, and ReadChangeStream configurations, including support for parsing these options from both a custom string format and JSON. The review feedback highlights a critical issue where calling withDirectedReadOptions on Read, ReadAll, or ReadChangeStream can result in a NullPointerException if getSpannerConfig() returns null. To resolve this, the reviewer suggests safely initializing the SpannerConfig if it is not already present.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@ath-08 ath-08 force-pushed the spanner-io-directed-reads branch from d4baa46 to f23b092 Compare July 8, 2026 07:12
@ath-08

ath-08 commented Jul 8, 2026

Copy link
Copy Markdown
Author

Assign set of reviewers

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @chamikaramj for label java.
R: @nielm for label spanner.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

This change allows Beam pipeline developers to configure Cloud Spanner DirectedReadOptions on SpannerConfig, as well as on SpannerIO.read(), readAll(), and readChangeStream().

- Adds getDirectedReadOptions() and withDirectedReadOptions(...) overloads (accepting DirectedReadOptions, ValueProvider<DirectedReadOptions>, and String) to SpannerConfig.
- Adds an internal parseDirectedReadOptions helper supporting JSON strings via protobuf JsonFormat, matching the official Cloud Spanner Java client library behavior.
- Threads DirectedReadOptions into SpannerOptions.Builder in SpannerAccessor so that all DatabaseClient instances and ChangeStreamDao queries automatically inherit the directed read configuration.
- Adds delegation methods to SpannerIO.Read, ReadAll, and ReadChangeStream.
- Adds comprehensive unit tests in SpannerAccessorTest and SpannerIOReadChangeStreamTest.
@ath-08 ath-08 force-pushed the spanner-io-directed-reads branch from f23b092 to 9328cc2 Compare July 8, 2026 23:24
@ath-08

ath-08 commented Jul 8, 2026

Copy link
Copy Markdown
Author

Assign set of reviewers

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Reviewers are already assigned to this PR: @chamikaramj @nielm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants