Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go/vt/vttablet/tabletserver/repltracker/repltracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (rt *ReplTracker) MakePrimary() {
rt.hw.Open()
}
rt.hw.Open()
replicationLagSeconds.Reset() // we are the primary, we have no lag
}

// MakeNonPrimary must be called if the tablet type becomes non-PRIMARY.
Expand Down Expand Up @@ -130,6 +131,7 @@ func (rt *ReplTracker) Status() (time.Duration, error) {

switch {
case rt.isPrimary || rt.mode == tabletenv.Disable:
replicationLagSeconds.Reset() // we are the primary, we have no lag
return 0, nil
case rt.mode == tabletenv.Heartbeat:
return rt.hr.Status()
Expand Down
49 changes: 49 additions & 0 deletions go/vt/vttablet/tabletserver/repltracker/repltracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,53 @@ func TestReplTracker(t *testing.T) {
assert.False(t, rt.hw.isOpen)
assert.False(t, rt.hr.isOpen)
})
t.Run("metric reset on promotion", func(t *testing.T) {
// Clean up the global metric after test
defer replicationLagSeconds.Reset()

rt := NewReplTracker(env, alias)
rt.InitDBConfig(target, mysqld)

// Start as replica
rt.MakeNonPrimary()
assert.False(t, rt.isPrimary)

// Simulate having lag (would normally be set by poller)
replicationLagSeconds.Set(42)
assert.Equal(t, int64(42), replicationLagSeconds.Get())

// Promote to primary
rt.MakePrimary()
assert.True(t, rt.isPrimary)

// Verify metric is reset
assert.Equal(t, int64(0), replicationLagSeconds.Get())

rt.Close()
})
t.Run("metric reset on status when primary", func(t *testing.T) {
// Clean up the global metric after test
defer replicationLagSeconds.Reset()

rt := NewReplTracker(env, alias)
rt.InitDBConfig(target, mysqld)

// Set as primary
rt.MakePrimary()
assert.True(t, rt.isPrimary)

// Simulate metric having a stale value (shouldn't happen, but be defensive)
replicationLagSeconds.Set(99)
assert.Equal(t, int64(99), replicationLagSeconds.Get())

// Call Status() which should reset the metric
lag, err := rt.Status()
assert.NoError(t, err)
assert.Equal(t, time.Duration(0), lag)

// Verify metric is reset
assert.Equal(t, int64(0), replicationLagSeconds.Get())

rt.Close()
})
}
Loading