Skip to content

vtctl/workflow: stop reverse replication before Complete drops sources#20188

Open
harshit2017 wants to merge 3 commits into
vitessio:mainfrom
harshit2017:fix-complete-reverse-drain
Open

vtctl/workflow: stop reverse replication before Complete drops sources#20188
harshit2017 wants to merge 3 commits into
vitessio:mainfrom
harshit2017:fix-complete-reverse-drain

Conversation

@harshit2017
Copy link
Copy Markdown

Description

MoveTables Complete could rename or drop source tables while reverse vreplication was still applying, causing permanent errno 1146 errors. Validate reverse workflow state, wait for catchup, stop streams, then remove source tables.

Related Issue(s)

Fixes #20135

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

Deployment Notes

AI Disclosure

Tests were written by AI

Copilot AI review requested due to automatic review settings May 26, 2026 18:06
@github-actions github-actions Bot added this to the v25.0.0 milestone May 26, 2026
@vitess-bot vitess-bot Bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work 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 May 26, 2026
@vitess-bot
Copy link
Copy Markdown
Contributor

vitess-bot Bot commented May 26, 2026

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR adds safety checks and shutdown behavior for reverse VReplication during workflow completion to prevent completing (and potentially removing source data) while reverse streams are unhealthy or not drained.

Changes:

  • Validate reverse workflow stream states during doValidateWorkflowHasCompleted.
  • Add stopAndDrainReverseVReplication to stop reverse streams after waiting for them to catch up to target primary positions.
  • Update workflow completion tests to expect reverse stream stop queries and add an error-state test case.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
go/vt/vtctl/workflow/utils.go Adds validation of reverse workflow stream states during completion validation.
go/vt/vtctl/workflow/traffic_switcher.go Implements draining (wait-for-pos) and stopping reverse VReplication streams prior to source removal.
go/vt/vtctl/workflow/switcher_interface.go Extends the switcher interface with reverse drain/stop capability.
go/vt/vtctl/workflow/switcher_dry_run.go Adds dry-run logging for reverse drain/stop behavior.
go/vt/vtctl/workflow/switcher.go Wires switcher call-through to trafficSwitcher implementation.
go/vt/vtctl/workflow/server.go Calls reverse drain/stop before dropping sources (unless ignoreSourceKeyspace).
go/vt/vtctl/workflow/server_test.go Updates expected queries to include stopping reverse streams; adds error-state coverage.
go/vt/vtctl/workflow/framework_test.go Adjusts test TM client behavior to better model reverse workflows.

Comment thread go/vt/vtctl/workflow/utils.go Outdated
Comment on lines +609 to +640
func validateReverseWorkflowForComplete(ctx context.Context, ts *trafficSwitcher, wg *sync.WaitGroup, rec *concurrency.AllErrorRecorder) {
_ = ts.ForAllSources(func(source *MigrationSource) error {
wg.Add(1)
defer wg.Done()
res, err := ts.ws.tmc.ReadVReplicationWorkflow(ctx, source.GetPrimary().Tablet, &tabletmanagerdatapb.ReadVReplicationWorkflowRequest{
Workflow: ts.ReverseWorkflowName(),
})
if err != nil {
rec.RecordError(err)
return nil
}
if res == nil || len(res.Streams) == 0 {
return nil
}
for _, stream := range res.Streams {
switch stream.State {
case binlogdatapb.VReplicationWorkflowState_Running,
binlogdatapb.VReplicationWorkflowState_Stopped:
case binlogdatapb.VReplicationWorkflowState_Error:
rec.RecordError(fmt.Errorf("reverse vreplication stream %d is in error state on tablet %d",
stream.Id, source.GetPrimary().Alias.Uid))
case binlogdatapb.VReplicationWorkflowState_Copying:
rec.RecordError(fmt.Errorf("reverse vreplication stream %d is still copying on tablet %d",
stream.Id, source.GetPrimary().Alias.Uid))
default:
rec.RecordError(fmt.Errorf("reverse vreplication stream %d is in state %s on tablet %d",
stream.Id, stream.State, source.GetPrimary().Alias.Uid))
}
}
return nil
})
}
Comment on lines +1064 to +1091
for _, stream := range res.Streams {
if stream.Bls == nil {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "reverse vreplication stream %d on %s has no binlog source",
stream.Id, topoproto.TabletAliasString(source.GetPrimary().GetAlias()))
}
targetShard := stream.Bls.Shard
pos, ok := targetPositions[targetShard]
if !ok {
return vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION,
"reverse vreplication stream %d on %s reads from unknown target shard %s",
stream.Id, topoproto.TabletAliasString(source.GetPrimary().GetAlias()), targetShard)
}
if stream.State == binlogdatapb.VReplicationWorkflowState_Running {
ts.Logger().Infof("Waiting for reverse stream %d on %s to catch up to target shard %s position %s",
stream.Id, topoproto.TabletAliasString(source.GetPrimary().GetAlias()), targetShard, pos)
if err := ts.TabletManagerClient().VReplicationWaitForPos(ctx, source.GetPrimary().Tablet, stream.Id, pos); err != nil {
return err
}
}
if stream.State != binlogdatapb.VReplicationWorkflowState_Stopped {
ts.Logger().Infof("Stopping reverse stream %d on %s for complete",
stream.Id, topoproto.TabletAliasString(source.GetPrimary().GetAlias()))
if _, err := ts.TabletManagerClient().VReplicationExec(ctx, source.GetPrimary().Tablet,
binlogplayer.StopVReplication(stream.Id, stoppedForComplete)); err != nil {
return err
}
}
}
Comment on lines +78 to +80
// reverseReplicationDrainTimeout bounds how long Complete waits for reverse
// streams to catch up to the target primary position before stopping them.
reverseReplicationDrainTimeout = 30 * time.Second
@promptless
Copy link
Copy Markdown
Contributor

promptless Bot commented May 26, 2026

Promptless prepared a documentation update related to this change.

Triggered by PR #20188

This PR adds validation of reverse workflow state before MoveTables Complete proceeds. The documentation update explains the new safety behavior: Complete now validates that reverse replication streams are not in Error or Copying state, waits for them to catch up, and stops them before removing source tables.

Review: Document reverse workflow validation in MoveTables Complete

MoveTables Complete could rename or drop source tables while reverse
vreplication was still applying, causing permanent errno 1146 errors.
Validate reverse workflow state, wait for catchup, stop streams, then
remove source tables.

Fixes vitessio#20135

Signed-off-by: Harshit Katyal <harshit1121998@gmail.com>
Signed-off-by: Harshit <harshit1121998@gmail.com>
@harshit2017 harshit2017 force-pushed the fix-complete-reverse-drain branch from d02e4a8 to 33916df Compare May 26, 2026 18:25
…sCompleted

Remove the redundant outer WaitGroup; ForAllSources and ForAllTargets
already run callbacks concurrently and wait internally. Propagate
ForAllSources aggregate errors into the error recorder instead of
discarding them, and return the error from validateReverseWorkflowForComplete.

Signed-off-by: Harshit <harshit1121998@gmail.com>
Copilot AI review requested due to automatic review settings May 26, 2026 18:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Comment thread go/vt/vtctl/workflow/utils.go Outdated
Comment on lines +626 to +633
rec.RecordError(fmt.Errorf("reverse vreplication stream %d is in error state on tablet %d",
stream.Id, source.GetPrimary().Alias.Uid))
case binlogdatapb.VReplicationWorkflowState_Copying:
rec.RecordError(fmt.Errorf("reverse vreplication stream %d is still copying on tablet %d",
stream.Id, source.GetPrimary().Alias.Uid))
default:
rec.RecordError(fmt.Errorf("reverse vreplication stream %d is in state %s on tablet %d",
stream.Id, stream.State, source.GetPrimary().Alias.Uid))
Comment on lines +611 to +617
res, err := ts.ws.tmc.ReadVReplicationWorkflow(ctx, source.GetPrimary().Tablet, &tabletmanagerdatapb.ReadVReplicationWorkflowRequest{
Workflow: ts.ReverseWorkflowName(),
})
if err != nil {
rec.RecordError(err)
return nil
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

Comment on lines +1038 to +1040
func (ts *trafficSwitcher) stopAndDrainReverseVReplication(ctx context.Context, waitTime time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, waitTime)
defer cancel()
Comment on lines +1065 to +1075
if stream.Bls == nil {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "reverse vreplication stream %d on %s has no binlog source",
stream.Id, topoproto.TabletAliasString(source.GetPrimary().GetAlias()))
}
targetShard := stream.Bls.Shard
pos, ok := targetPositions[targetShard]
if !ok {
return vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION,
"reverse vreplication stream %d on %s reads from unknown target shard %s",
stream.Id, topoproto.TabletAliasString(source.GetPrimary().GetAlias()), targetShard)
}
Comment on lines +1079 to +1081
if err := ts.TabletManagerClient().VReplicationWaitForPos(ctx, source.GetPrimary().Tablet, stream.Id, pos); err != nil {
return err
}
Signed-off-by: Harshit <harshit1121998@gmail.com>
},
expectedSourceQueries: []*queryResult{
{
expectedSourceQueries: append(slices.Clone(stopReverseStreamQueries),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are these changes necessary? It's best to eliminate unnecessary changes if we can.

}

func (ts *trafficSwitcher) stopAndDrainReverseVReplication(ctx context.Context, waitTime time.Duration) error {
targetPositions := make(map[string]string)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think that we should do all of this work to "drain" the stream. The tables are normally deleted. In this case we are renaming them. But new data is ONLY in the keyspace where they were moved. We are holding onto locks and can potentially introduce a whole new set of edge cases and failure scenarios by doing this work and having it take a long time, time out, or error.

I think that we should simply stop the workflow / delete the record for the reverse workflow. i.e. I think it's better to rename the function to just stop, and then only do that, stop it.

return nil
}

func validateReverseWorkflowForComplete(ctx context.Context, ts *trafficSwitcher, rec *concurrency.AllErrorRecorder) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need to validate it? Why can't we simply stop it / clean it up?

@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

❌ Patch coverage is 54.25532% with 43 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.66%. Comparing base (70c7a72) to head (091b5b8).
⚠️ Report is 277 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vtctl/workflow/traffic_switcher.go 56.60% 23 Missing ⚠️
go/vt/vtctl/workflow/utils.go 53.12% 15 Missing ⚠️
go/vt/vtctl/workflow/switcher_dry_run.go 0.00% 4 Missing ⚠️
go/vt/vtctl/workflow/server.go 66.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main   #20188       +/-   ##
===========================================
+ Coverage   69.67%   72.66%    +2.99%     
===========================================
  Files        1614       22     -1592     
  Lines      216793     7742   -209051     
===========================================
- Hits       151044     5626   -145418     
+ Misses      65749     2116    -63633     
Flag Coverage Δ
partial 72.66% <54.25%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: race condition in MoveTables ... Complete --rename-tables

3 participants