Skip to content

fix(Spanner): Live: order SQL Server change events by LSN - #4654

Open
Vaibhavch9876 wants to merge 3 commits into
mainfrom
sqlserver-live-lsn-order
Open

Vaibhavch9876 wants to merge 3 commits into
mainfrom
sqlserver-live-lsn-order

Conversation

@Vaibhavch9876

Copy link
Copy Markdown
Contributor

SQL Server live migration could skip UPDATE/DELETE events and leave deleted rows and stale values in Spanner. Datastream's SQL Server change-table events don't carry each event's own commit time as source_timestamp, so ordering by timestamp first could treat a newer UPDATE/DELETE as older than the row's INSERT.

SqlServerChangeEventSequence.compareTo now compares LSNs when both events have one, and keeps timestamp-then-LSN only when an LSN is empty (backfill events).

Testing: added a unit test for an INSERT with a later timestamp but smaller LSN than its UPDATE/DELETE; in a 1K changes/s, 30-min load test the source and Spanner row counts now match exactly (405,131 = 405,131; previously 101,211 extra rows).

Fixes: b/591679245

Datastream's SQL Server change-table events do not carry their own commit
time as source_timestamp, so timestamp-first ordering could treat a newer
UPDATE/DELETE as older than the row's INSERT and skip it, leaving deleted
rows and stale values in Spanner. Compare LSNs when both events have one and
keep timestamp-then-LSN only when an LSN is empty (backfill).
@Vaibhavch9876
Vaibhavch9876 marked this pull request as ready for review November 30, 2026 12:21
@Vaibhavch9876
Vaibhavch9876 requested a review from a team as a code owner November 30, 2026 12:21
@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 addresses a critical issue in SQL Server live migrations where events were being processed out of order, leading to stale data or skipped updates in Spanner. By shifting the primary ordering mechanism from timestamps to LSNs, the system now correctly sequences change events even when Datastream timestamps are inconsistent or misleading.

Highlights

  • Ordering Logic Update: Modified SqlServerChangeEventSequence.compareTo to prioritize Log Sequence Numbers (LSN) over timestamps for SQL Server change events.
  • Reliability Improvement: Prevents data inconsistencies where newer UPDATE/DELETE events were incorrectly skipped due to unreliable Datastream commit timestamps.
  • Regression Testing: Added a new unit test case to verify that LSN ordering correctly supersedes timestamp ordering for CDC events.
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 updates the SqlServerChangeEventSequence class to prioritize LSN comparison over timestamp comparison when both events contain an LSN. This change ensures correct ordering of CDC events, preventing newer UPDATE or DELETE events from being incorrectly skipped due to Datastream's timestamp assignment behavior. A corresponding unit test, lsnWinsOverTimestampForCdcEvents, has been added to verify this logic. There are no review comments, and I have no additional feedback to provide.

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 65.22%. Comparing base (34a7ab7) to head (51eb027).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...source/sqlserver/SqlServerChangeEventSequence.java 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4654      +/-   ##
============================================
+ Coverage     59.69%   65.22%   +5.53%     
+ Complexity     8678     3969    -4709     
============================================
  Files          1122      569     -553     
  Lines         72738    34824   -37914     
  Branches       8854     4005    -4849     
============================================
- Hits          43418    22714   -20704     
+ Misses        26419    10974   -15445     
+ Partials       2901     1136    -1765     
Components Coverage Δ
spanner-templates 84.02% <80.00%> (-0.02%) ⬇️
spanner-import-export ∅ <ø> (∅)
spanner-live-forward-migration 87.61% <80.00%> (-0.04%) ⬇️
spanner-live-reverse-replication 79.91% <ø> (-0.03%) ⬇️
spanner-bulk-migration 88.27% <ø> (-0.02%) ⬇️
gcs-spanner-dv 86.53% <ø> (-0.04%) ⬇️
Files with missing lines Coverage Δ
...source/sqlserver/SqlServerChangeEventSequence.java 97.95% <80.00%> (-2.05%) ⬇️

... and 570 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.
Only let the LSN decide when both events have different LSNs; equal or empty
LSNs keep the existing timestamp-then-LSN order (fixes
SqlServerChangeEventSequenceTest.canOrderBasedOnTimestamp).
@pull-request-size pull-request-size Bot added size/M and removed size/S labels Nov 30, 2026
Backfill events carry an LSN in a different format ("0000002E:00001890:0002")
that sorts after later CDC LSNs as a string, which made CDC UPDATE/DELETE after a
backfill look older (SQL Server live ITs). Order by LSN only when both events
have a CDC (plain hex) LSN; otherwise keep timestamp-then-LSN.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant