Skip to content

[DataStreamToSpanner] Improvement: Adding group by key step before Spanner write step#3249

Merged
darshan-sj merged 9 commits intoGoogleCloudPlatform:mainfrom
darshan-sj:live-gbk-1
Jan 29, 2026
Merged

[DataStreamToSpanner] Improvement: Adding group by key step before Spanner write step#3249
darshan-sj merged 9 commits intoGoogleCloudPlatform:mainfrom
darshan-sj:live-gbk-1

Conversation

@darshan-sj
Copy link
Contributor

Improvement: Adding group by key step before Spanner write step

This would serialize events happening on same key in Dataflow job and hence avoid lock contention in Spanner when there are too many events corresponding to a small set of keys.

@gemini-code-assist
Copy link

Summary of Changes

Hello @darshan-sj, 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 a crucial optimization to the DataStreamToSpanner pipeline by implementing a primary key-based grouping mechanism for change events. This enhancement aims to serialize operations on the same key, thereby significantly reducing lock contention within Spanner during high-volume write operations. The change improves the robustness and efficiency of the data replication process, particularly for scenarios with concentrated event activity on a small set of keys.

Highlights

  • New Grouping Mechanism: Introduced a new GroupChangeEventsByPrimaryKeyDoFn to group change events by their primary key, converting them into KV<Long, FailsafeElement<String, String>> pairs where the key is a hash of the table name and primary key string.
  • Pipeline Integration: The SpannerTransactionWriter now incorporates this grouping step, applying the GroupChangeEventsByPrimaryKeyDoFn followed by a Reshuffle operation before writing mutations to Spanner.
  • Enhanced Error Handling: Errors occurring during the new grouping step are now captured and merged with permanent errors from the subsequent Spanner write operations, ensuring comprehensive error reporting.
  • Input Type Update: The SpannerTransactionWriterDoFn has been updated to accept KV<Long, FailsafeElement<String, String>> as input, aligning with the output of the new grouping function.
  • New Constant for Grouped Events: A new TupleTag, SUCCESSFUL_GROUPED_EVENT_TAG, has been added to DatastreamToSpannerConstants to identify successfully grouped events within the pipeline.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

@codecov
Copy link

codecov bot commented Jan 23, 2026

Codecov Report

❌ Patch coverage is 56.36364% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 54.91%. Comparing base (63091f1) to head (826933f).
⚠️ Report is 20 commits behind head on main.

Files with missing lines Patch % Lines
...eleport/v2/templates/SpannerTransactionWriter.java 0.00% 24 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3249      +/-   ##
============================================
+ Coverage     50.93%   54.91%   +3.98%     
+ Complexity     5157     2277    -2880     
============================================
  Files           977      493     -484     
  Lines         60347    28861   -31486     
  Branches       6594     3036    -3558     
============================================
- Hits          30735    15848   -14887     
+ Misses        27457    12050   -15407     
+ Partials       2155      963    -1192     
Components Coverage Δ
spanner-templates 71.94% <56.36%> (+0.89%) ⬆️
spanner-import-export ∅ <ø> (∅)
spanner-live-forward-migration 80.04% <56.36%> (-0.08%) ⬇️
spanner-live-reverse-replication 77.58% <ø> (-0.19%) ⬇️
spanner-bulk-migration 88.08% <ø> (-0.38%) ⬇️
Files with missing lines Coverage Δ
...tes/CreateKeyValuePairsWithPrimaryKeyHashDoFn.java 100.00% <100.00%> (ø)
...ort/v2/templates/SpannerTransactionWriterDoFn.java 51.80% <100.00%> (ø)
.../v2/templates/datastream/ChangeEventConvertor.java 87.50% <ø> (+5.55%) ⬆️
...eleport/v2/templates/SpannerTransactionWriter.java 0.00% <0.00%> (ø)

... and 518 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@darshan-sj darshan-sj added the improvement Making existing code better label Jan 26, 2026
@darshan-sj darshan-sj marked this pull request as ready for review January 26, 2026 16:58
@darshan-sj darshan-sj requested a review from a team as a code owner January 26, 2026 16:58
Copy link
Contributor

@bharadwaj-aditya bharadwaj-aditya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of minor changes. otherwise LGTM

Copy link
Contributor

@bharadwaj-aditya bharadwaj-aditya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@darshan-sj darshan-sj merged commit f2c600c into GoogleCloudPlatform:main Jan 29, 2026
22 checks passed
@darshan-sj darshan-sj deleted the live-gbk-1 branch January 29, 2026 04:57
darshan-sj added a commit to darshan-sj/DataflowTemplates that referenced this pull request Jan 29, 2026
…anner write step (GoogleCloudPlatform#3249)

* Adding group by key step before Spanner write step

* Renaming class name

* Added unit tests

* Correcting UTs

* Correcting a bug

* Fixing a bug

* Correcting a bug

* Adding more Unit test cases

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

Labels

improvement Making existing code better size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants