Skip to content

VDiff: wait for workflow lock with exponential backoff#18998

Merged
mattlord merged 6 commits intomainfrom
vdiff_stream_scheduling
Jan 10, 2026
Merged

VDiff: wait for workflow lock with exponential backoff#18998
mattlord merged 6 commits intomainfrom
vdiff_stream_scheduling

Conversation

@mattlord
Copy link
Copy Markdown
Member

@mattlord mattlord commented Dec 5, 2025

Description

Workflow named locks are used to coordinate modifications to VReplication workflows (first added in: #16260).

When VDiff initializes a table diff, it performs the following steps on each target shard primary tablet:

  1. Selects a source tablet (can be more than one source if you are consolidating shards in the workflow) to use for the table diff on the shard
  2. Creates a snapshot connection on that tablet
    - Gets a READ table level lock on the table we're diffing to prevent changes
    - Initializes a consistent read view with START TRANSACTION WITH CONSISTENT SNAPSHOT
    - Gets a the current GTID snapshot/position (SELECT @@global.gtid_executed)
    - We release the table level lock
    - Now we have a consistent read for the given table at a logical position that we can mirror on the target
  3. Stops the VReplication workflow on the shard, sets the message to "for vdiff", then restarts it with a stop _pos set so that we replicate the state (GTIDs) from the source shard until we are at the same logical position that represents the same state we have in the source tablet in our consistent read view
  4. Waits up to the --filtered-replication-wait-time (default is 30s) for the workflow stream to catch up and stop
  5. Creates a consistent read view on the chosen target tablet (based on the chosen table type preferences) using the same START TRANSACTION WITH CONSISTENT SNAPSHOT command
  6. Updates the workflow to remove the stop_pos and the "for vdiff" message so that the shard's workflow stream is once again running normally
  7. Now it can run a query in each snapshot connection, which have the same logical view of the world, in order to diff the results between the source and target

During this initialization phase, the table differ takes a lock on the workflow to prevent concurrent updates by other VDiffs and other VReplication commands. It holds this lock throughout these initialization steps as it is manipulating the workflow.

When a VDiff starts — whether from create, resume, or automatic resume on error (which happens every 30 seconds) — every shard tries to start running and each primary tablet in the given shard tries to take the workflow lock in order to perform the initialization steps. There is a timeout of 45 seconds by default (--lock-timeout) when attempting to acquire this lock. When you have hundreds of shards and the replication catch up step (3 and 4 above) takes some seconds... you can easily hit the lock wait timeout on a high number of shards. This then causes the vdiff to be retried 30 seconds later (as we treat the lock error as any other error that is retryable), on all shards. So once again, you have hundreds of shards trying to initialize at the same time and competing for the lock. This can lead to much longer than necessary VDiff execution times in large busy keyspaces as we slowly start the VDiff on each of the shards. This was exacerbated by the fact that we were timing out the table diff query at the --vreplication-copy-phase-duration limit (defaults to 1h) which meant that we had to perform the initialization step all over again on the shards (this was addressed in: #19044).

In this PR we address this by NOT treating the workflow lock wait timeout as any other kind of retryable error, and instead retrying to acquire the lock indefinitely, with an exponential backoff, until we are able to acquire it. This prevents the repeated 30 second delay and the repeated thundering herd that was always coming to get the lock when the VDiff was retried/resumed.

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

AI Disclosure

My man Claude helped write the bulk of the unit tests.

Signed-off-by: Matt Lord <mattalord@gmail.com>
@vitess-bot
Copy link
Copy Markdown
Contributor

vitess-bot bot commented Dec 5, 2025

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Dec 5, 2025
@github-actions github-actions bot added this to the v24.0.0 milestone Dec 5, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 5, 2025

Codecov Report

❌ Patch coverage is 58.06452% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.89%. Comparing base (32b8bd8) to head (93f8d38).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vttablet/tabletmanager/vdiff/table_differ.go 58.06% 13 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #18998      +/-   ##
==========================================
- Coverage   69.89%   69.89%   -0.01%     
==========================================
  Files        1612     1612              
  Lines      215826   215847      +21     
==========================================
+ Hits       150857   150862       +5     
- Misses      64969    64985      +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 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.

@mattlord mattlord added Component: VReplication Type: Enhancement Logical improvement (somewhere between a bug and feature) and removed NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Dec 9, 2025
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
@mattlord mattlord force-pushed the vdiff_stream_scheduling branch from 5503604 to b1100bb Compare January 8, 2026 21:37
@mattlord mattlord removed the NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work label Jan 8, 2026
@mattlord mattlord marked this pull request as ready for review January 8, 2026 22:28
@promptless
Copy link
Copy Markdown
Contributor

promptless bot commented Jan 8, 2026

📝 Documentation updates detected!

New suggestion: Add changelog entry for VDiff exponential backoff lock wait

Copy link
Copy Markdown
Collaborator

@mhamza15 mhamza15 left a comment

Choose a reason for hiding this comment

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

Nice! Would some jitter be helpful here as well?

Copy link
Copy Markdown
Collaborator

@nickvanw nickvanw left a comment

Choose a reason for hiding this comment

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

I do think we should add some jitter, but this is better than the status quo even without it.

Comment on lines +147 to +149
if retryDelay < maxRetryDelay {
retryDelay = min(time.Duration(float64(retryDelay)*backoffFactor), maxRetryDelay)
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it'd probably be a good idea to add a jitter factor here, something like:

jitter := time.Duration(rand.Int63n(int64(retryDelay / 4)))
retryDelay = min(time.Duration(float64(retryDelay)*backoffFactor)+jitter, maxRetryDelay)

You could also apply it to the time.After and do something like

case <-time.After(retryDelay + time.Duration(rand.Int63n(int64(retryDelay/4)))):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added here: 24d7d18

Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Copy link
Copy Markdown
Collaborator

@nickvanw nickvanw left a comment

Choose a reason for hiding this comment

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

:shipit: !

Signed-off-by: Matt Lord <mattalord@gmail.com>
@mattlord mattlord merged commit 1fa05c1 into main Jan 10, 2026
100 of 101 checks passed
@mattlord mattlord deleted the vdiff_stream_scheduling branch January 10, 2026 05:45
@promptless
Copy link
Copy Markdown
Contributor

promptless bot commented Jan 10, 2026

📝 Documentation updates detected!

Updated existing suggestion: Add changelog entry for VDiff exponential backoff lock wait

@promptless
Copy link
Copy Markdown
Contributor

promptless bot commented Jan 10, 2026

📝 Documentation updates detected!

New suggestion: Add changelog entry for VDiff exponential backoff lock wait

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

Labels

Component: VReplication Type: Enhancement Logical improvement (somewhere between a bug and feature)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve scheduling VDiff streams to speed up VDiff time.

3 participants