Skip to content

Conversation

@siddharth16396
Copy link
Contributor

@siddharth16396 siddharth16396 commented Jun 23, 2025

This PR implements the fix for the bug mentioned #18391

TL;DR
VStreamRows uses a broken heartbeat mechanism that only sends one heartbeat and then exits. In contrast, VStream (used in normal binlog replication) sends heartbeats continuously. This PR fixes the issue by introducing a proper loop in the rowstreamer.go heartbeat goroutine to ensure heartbeats are sent throughout the copy phase.

We found this out because Some of our long-running schema changes failed due to missing heartbeats during the copy phase of VReplication.

Description

This is already patched in our internal fork of vitess and has been tested to work correctly for all the failing cases we saw.

We've identified a critical architectural inconsistency in Vitess VReplication that explains this seemingly contradictory behavior. The reason some long-running schema changes don't get heartbeats while others do is because different phases of the operation use different heartbeat mechanisms.

The Two Different Heartbeat Systems

1. Working Heartbeat System (vstreamer.go)

Used by normal VReplication (binlog streaming phase):

// vstreamer.go lines 297-360
hbTimer := time.NewTimer(HeartbeatTime) // 900ms
defer hbTimer.Stop()

for {
    hbTimer.Reset(HeartbeatTime)  // ← Proper timer reset in loop
    select {
    case ev, ok := <-throttledEvents:
        // Process events
    case <-hbTimer.C:
        // Send heartbeat every 900ms
        if err := injectHeartbeat(false, ""); err != nil {
            return err
        }
    }
}

This system works correctly - it continuously sends heartbeats every 900ms.

2. Broken Heartbeat System (rowstreamer.go)

Used by table copying phase (VStreamRows):

// rowstreamer.go lines 372-385
go func() {
    select {  // ← NO FOR LOOP! Only executes once!
    case <-rs.ctx.Done():
        return
    case <-heartbeatTicker.C:
        safeSend(&binlogdatapb.VStreamRowsResponse{Heartbeat: true})
    }  // ← Goroutine exits after first heartbeat
}()

This system is broken - it only sends one heartbeat and then the goroutine dies.

Which Operations Use Which System

Operations That DO Get Heartbeats:

  • Normal VReplication workflows (MoveTables, Reshard after copy phase)
  • Catchup phase during onlineDDL
  • Binlog streaming operations
  • Short onlineDDL operations (complete within 10 seconds)

These use vplayer.goVStream()vstreamer.go (working heartbeats)

Operations That DON'T Get Heartbeats:

  • OnlineDDL copy phase on large tables
  • Long-running VStreamRows operations
  • Table copying during MoveTables/Reshard

These use vcopier.goVStreamRows()rowstreamer.go (broken heartbeats)

The Code Flow

Copy Phase (Broken Heartbeats):

vcopier.copyTable() 
  → sourceVStreamer.VStreamRows() 
  → tabletConnector.VStreamRows() 
  → gRPC call to tablet 
  → tabletserver.VStreamRows() 
  → vstreamer.StreamRows() 
  → rowstreamer.streamQuery() 
  → BROKEN heartbeat goroutine

Normal Replication (Working Heartbeats):

vplayer.play() 
  → sourceVStreamer.VStream() 
  → tabletConnector.VStream() 
  → gRPC call to tablet 
  → tabletserver.VStream() 
  → vstreamer.Stream() 
  → WORKING heartbeat timer

Why You See This Pattern

  1. T=0: OnlineDDL starts, enters copy phase using VStreamRows
  2. T=10s: First (and only) heartbeat sent from broken rowstreamer
  3. T=10s+: Heartbeat goroutine exits, no more heartbeats sent
  4. T=180min: Operation fails due to "no heartbeat activity"

The Fix

The immediate fix is to add a for loop to the rowstreamer heartbeat goroutine:

// In rowstreamer.go lines 372-385
go func() {
    for {  // ← Add this loop!
        select {
        case <-rs.ctx.Done():
            return
        case <-heartbeatTicker.C:
            safeSend(&binlogdatapb.VStreamRowsResponse{Heartbeat: true})
        }
    }
}()

This explains the inconsistent behavior we observed - it's not random, it's deterministic based on which code path the operation takes. The bug only affects the table copying component, not the binlog streaming component.

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

@vitess-bot
Copy link
Contributor

vitess-bot bot commented Jun 23, 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 Jun 23, 2025
@github-actions github-actions bot added this to the v23.0.0 milestone Jun 23, 2025
@arthurschreiber
Copy link
Member

Nice, and good catch! Is this an issue in older versions of vitess as well? Do you think there's a test case you could write that fails before / passes after?

@siddharth16396
Copy link
Contributor Author

@arthurschreiber : Yes this seems to be an issue on atleast v20,21,22 (so i'm guessing its on all)

WRT the test cases, i'll try to take a stab at it, since it was just a very minor change i didn't bother diving into it.

@timvaillancourt
Copy link
Contributor

@siddharth16396 thanks for the fix. Is there a way we can ensure a stream gets continuous heartbeats in a unit test? I'm hoping there is an existing test we can add more validation to

@siddharth16396
Copy link
Contributor Author

@timvaillancourt @arthurschreiber : I've added a UT which should work.

@timvaillancourt timvaillancourt added Type: Bug Component: VReplication Component: Online DDL Online DDL (vitess/native/gh-ost/pt-osc) Component: VTTablet and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request labels Jun 23, 2025
Copy link
Contributor

@timvaillancourt timvaillancourt left a comment

Choose a reason for hiding this comment

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

@siddharth16396 thanks for the new test. LGTM

@rohit-nayak-ps rohit-nayak-ps added Backport to: release-20.0 Backport to: release-22.0 Needs to be backport to release-22.0 and removed NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jun 23, 2025
Copy link
Member

@rohit-nayak-ps rohit-nayak-ps left a comment

Choose a reason for hiding this comment

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

Nice catch!

@rohit-nayak-ps rohit-nayak-ps merged commit 460fffe into vitessio:main Jun 23, 2025
105 of 106 checks passed
vitess-bot pushed a commit that referenced this pull request Jun 23, 2025
vitess-bot pushed a commit that referenced this pull request Jun 23, 2025
vitess-bot pushed a commit that referenced this pull request Jun 23, 2025
timvaillancourt pushed a commit that referenced this pull request Jun 23, 2025
…) (#18397)

Signed-off-by: siddharth16396 <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
timvaillancourt pushed a commit to slackhq/vitess that referenced this pull request Jun 26, 2025
timvaillancourt pushed a commit to slackhq/vitess that referenced this pull request Jun 26, 2025
timvaillancourt pushed a commit to slackhq/vitess that referenced this pull request Jun 26, 2025
rohit-nayak-ps pushed a commit that referenced this pull request Jul 1, 2025
…) (#18398)

Signed-off-by: siddharth16396 <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
morgo added a commit to morgo/vitess that referenced this pull request Jul 7, 2025
…tests

* origin/master: (32 commits)
  test: Fix race condition in TestStreamRowsHeartbeat (vitessio#18414)
  VReplication: Improve permission check logic on external tablets on SwitchTraffic (vitessio#18348)
  Perform post copy actions in atomic copy (vitessio#18411)
  Update `operator.yaml` (vitessio#18364)
  Feature(onlineddl): Add shard-specific completion to online ddl (vitessio#18331)
  Set parsed comments in operator for subqueries (vitessio#18369)
  `vtorc`: move shard primary timestamp to time type (vitessio#18401)
  `vtorc`: rename `isClusterWideRecovery` -> `isShardWideRecovery` (vitessio#18351)
  `vtorc`: remove dupe keyspace/shard in replication analysis (vitessio#18395)
  Topo: Add NamedLock test for zk2 and consul and get them passing (vitessio#18407)
  Handle MySQL 9.x as New Flavor in getFlavor() (vitessio#18399)
  Add support for sending grpc server backend metrics via ORCA (vitessio#18282)
  asthelpergen: add design documentation (vitessio#18403)
  `vtorc`: add keyspace/shard labels to recoveries stats (vitessio#18304)
  `vtorc`: cleanup `database_instance` location fields (vitessio#18339)
  avoid derived tables for UNION when possible (vitessio#18393)
  [Bugfix] Broken Heartbeat system in Row Streamer (vitessio#18390)
  Update MAINTAINERS.md (vitessio#18394)
  move vmg to emeritus (vitessio#18388)
  Make sure to check if the server is closed in etcd2topo (vitessio#18352)
  ...
shlomi-noach added a commit that referenced this pull request Jul 17, 2025
…) (#18396)

Signed-off-by: siddharth16396 <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Shlomi Noach <[email protected]>
tanjinx pushed a commit to slackhq/vitess that referenced this pull request Aug 6, 2025
tanjinx added a commit to slackhq/vitess that referenced this pull request Aug 6, 2025
* [Bugfix] Broken Heartbeat system in Row Streamer (vitessio#18390)

Signed-off-by: siddharth16396 <[email protected]>
Signed-off-by: Tanjin Xu <[email protected]>

* test: Fix race condition in TestStreamRowsHeartbeat (vitessio#18414)

Signed-off-by: siddharth16396 <[email protected]>
Signed-off-by: Tanjin Xu <[email protected]>

* fix spaces

Signed-off-by: Tanjin Xu <[email protected]>

* undo golangci

Signed-off-by: Tanjin Xu <[email protected]>

* undo golangci

Signed-off-by: Tanjin Xu <[email protected]>

---------

Signed-off-by: siddharth16396 <[email protected]>
Signed-off-by: Tanjin Xu <[email protected]>
Co-authored-by: siddharth16396 <[email protected]>
tanjinx pushed a commit to slackhq/vitess that referenced this pull request Nov 4, 2025
tanjinx added a commit to slackhq/vitess that referenced this pull request Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backport to: release-22.0 Needs to be backport to release-22.0 Component: Online DDL Online DDL (vitess/native/gh-ost/pt-osc) Component: VReplication Component: VTTablet Type: Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants