Skip to content

Commit 8fc596b

Browse files
authored
repltracker: reset replica lag when we are primary (#18800)
Signed-off-by: Nick Van Wiggeren <[email protected]>
1 parent d02d40c commit 8fc596b

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

go/vt/vttablet/tabletserver/repltracker/repltracker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func (rt *ReplTracker) MakePrimary() {
9696
rt.hw.Open()
9797
}
9898
rt.hw.Open()
99+
replicationLagSeconds.Reset() // we are the primary, we have no lag
99100
}
100101

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

131132
switch {
132133
case rt.isPrimary || rt.mode == tabletenv.Disable:
134+
replicationLagSeconds.Reset() // we are the primary, we have no lag
133135
return 0, nil
134136
case rt.mode == tabletenv.Heartbeat:
135137
return rt.hr.Status()

go/vt/vttablet/tabletserver/repltracker/repltracker_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,53 @@ func TestReplTracker(t *testing.T) {
136136
assert.False(t, rt.hw.isOpen)
137137
assert.False(t, rt.hr.isOpen)
138138
})
139+
t.Run("metric reset on promotion", func(t *testing.T) {
140+
// Clean up the global metric after test
141+
defer replicationLagSeconds.Reset()
142+
143+
rt := NewReplTracker(env, alias)
144+
rt.InitDBConfig(target, mysqld)
145+
146+
// Start as replica
147+
rt.MakeNonPrimary()
148+
assert.False(t, rt.isPrimary)
149+
150+
// Simulate having lag (would normally be set by poller)
151+
replicationLagSeconds.Set(42)
152+
assert.Equal(t, int64(42), replicationLagSeconds.Get())
153+
154+
// Promote to primary
155+
rt.MakePrimary()
156+
assert.True(t, rt.isPrimary)
157+
158+
// Verify metric is reset
159+
assert.Equal(t, int64(0), replicationLagSeconds.Get())
160+
161+
rt.Close()
162+
})
163+
t.Run("metric reset on status when primary", func(t *testing.T) {
164+
// Clean up the global metric after test
165+
defer replicationLagSeconds.Reset()
166+
167+
rt := NewReplTracker(env, alias)
168+
rt.InitDBConfig(target, mysqld)
169+
170+
// Set as primary
171+
rt.MakePrimary()
172+
assert.True(t, rt.isPrimary)
173+
174+
// Simulate metric having a stale value (shouldn't happen, but be defensive)
175+
replicationLagSeconds.Set(99)
176+
assert.Equal(t, int64(99), replicationLagSeconds.Get())
177+
178+
// Call Status() which should reset the metric
179+
lag, err := rt.Status()
180+
assert.NoError(t, err)
181+
assert.Equal(t, time.Duration(0), lag)
182+
183+
// Verify metric is reset
184+
assert.Equal(t, int64(0), replicationLagSeconds.Get())
185+
186+
rt.Close()
187+
})
139188
}

0 commit comments

Comments
 (0)