Skip to content

tabletmanager: detach DemotePrimary rollback contexts#20343

Draft
mhamza15 wants to merge 4 commits into
vitessio:mainfrom
mhamza15:defer-demote-primary-ctx
Draft

tabletmanager: detach DemotePrimary rollback contexts#20343
mhamza15 wants to merge 4 commits into
vitessio:mainfrom
mhamza15:defer-demote-primary-ctx

Conversation

@mhamza15

Copy link
Copy Markdown
Collaborator

Description

DemotePrimary rollback defers reused the caller context after partial demotion failures. If that context was canceled or timed out, rollback could fail before restoring read-write state or primary-side semi-sync.

This gives each rollback operation its own bounded context detached from caller cancellation.

Related Issue(s)

Fixes #20342

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

No deployment notes.

AI Disclosure

This PR was written primarily by GPT-5 Codex under user direction.

mhamza15 added 2 commits June 16, 2026 12:53
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
DemotePrimary rollback defers reused the caller context after partial demotion failures. If that context was canceled or timed out, rollback could fail before restoring read-write state or primary-side semi-sync. This gives each rollback operation its own bounded context detached from caller cancellation.

Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Copilot AI review requested due to automatic review settings June 16, 2026 17:00
@github-actions github-actions Bot added this to the v25.0.0 milestone Jun 16, 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 Jun 16, 2026
@vitess-bot

vitess-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

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.

Copilot AI 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.

Pull request overview

This PR fixes a failure-mode in TabletManager.demotePrimary() where rollback defers could reuse a canceled/timed-out caller context, causing rollback operations (restoring read-write state and/or primary-side semi-sync) to fail prematurely. The change ensures rollback actions run under their own bounded context detached from caller cancellation.

Changes:

  • Use context.WithoutCancel(ctx) + context.WithTimeout(..., topo.RemoteOperationTimeout) for rollback operations in demotePrimary() so rollback isn’t short-circuited by caller cancellation.
  • Add a unit test intended to validate rollback behavior under a canceled demotion context.
  • Introduce a generated GoMock MysqlDaemon mock to support the new test.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
go/vt/vttablet/tabletmanager/rpc_replication.go Detaches rollback contexts for semi-sync and read-only restoration paths during partial demotion failures.
go/vt/vttablet/tabletmanager/rpc_replication_test.go Adds a regression test for the rollback-context behavior (currently needs strengthening to assert the intended property).
go/vt/mysqlctl/mock/mysqldaemon.go Adds generated GoMock implementation for mysqlctl.MysqlDaemon to enable targeted unit testing.
Files not reviewed (1)
  • go/vt/mysqlctl/mock/mysqldaemon.go: Generated file

Comment on lines +312 to +326
mockMysqlDaemon.EXPECT().SetSemiSyncEnabled(gomock.Any(), true, true).DoAndReturn(
func(ctx context.Context, primary bool, replica bool) error {
return ctx.Err()
},
),
mockMysqlDaemon.EXPECT().SetSuperReadOnly(gomock.Any(), false).DoAndReturn(
func(ctx context.Context, on bool) (mysqlctl.ResetSuperReadOnlyFunc, error) {
return nil, ctx.Err()
},
),
mockMysqlDaemon.EXPECT().SetReadOnly(gomock.Any(), false).DoAndReturn(
func(ctx context.Context, on bool) error {
return ctx.Err()
},
),
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 12.17839% with 512 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.21%. Comparing base (70c7a72) to head (bd4bf65).
⚠️ Report is 341 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/mysqlctl/mock/mysqldaemon.go 11.61% 510 Missing ⚠️
go/vt/vttablet/tabletmanager/rpc_replication.go 66.66% 2 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (70c7a72) and HEAD (bd4bf65). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (70c7a72) HEAD (bd4bf65)
1 0
Additional details and impacted files
@@             Coverage Diff             @@
##             main   #20343       +/-   ##
===========================================
- Coverage   69.67%   63.21%    -6.46%     
===========================================
  Files        1614       60     -1554     
  Lines      216793    11293   -205500     
===========================================
- Hits       151044     7139   -143905     
+ Misses      65749     4154    -61595     
Flag Coverage Δ
partial 63.21% <12.17%> (?)

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

☔ View full report in Codecov by Harness.
📢 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.

@mhamza15 mhamza15 self-assigned this Jun 18, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The regression test needed specific ordering and conditions that were simpler to organize with a gomock version of mysqldaemon rather than the existing fake.

mhamza15 added 2 commits June 18, 2026 10:20
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Copilot AI review requested due to automatic review settings June 18, 2026 14:22

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 3 changed files in this pull request and generated no new comments.

Files not reviewed (1)
  • go/vt/mysqlctl/mock/mysqldaemon.go: Generated file

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

Labels

Component: TabletManager Component: VTTablet 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: DemotePrimary rollback can reuse canceled caller context

2 participants