Skip to content

Commit

Permalink
Track the last time we got a heartbeat response.
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber committed Jan 31, 2025
1 parent 341c62c commit 7da7d81
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions go/vt/vttablet/tabletserver/repltracker/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type heartbeatReader struct {

lagMu sync.Mutex
lastKnownLag time.Duration
lastKnownTime time.Time
lastKnownError error
}

Expand Down Expand Up @@ -106,6 +107,7 @@ func (r *heartbeatReader) Open() {
r.pool.Open(r.env.Config().DB.AppWithDB(), r.env.Config().DB.DbaWithDB(), r.env.Config().DB.AppDebugWithDB())
r.ticks.Start(func() { r.readHeartbeat() })
r.isOpen = true
r.lastKnownTime = r.now()
}

// Close cancels the watchHeartbeat periodic ticker and closes the db pool.
Expand All @@ -131,6 +133,12 @@ func (r *heartbeatReader) Close() {
func (r *heartbeatReader) Status() (time.Duration, error) {
r.lagMu.Lock()
defer r.lagMu.Unlock()

// Return an error if we didn't receive a heartbeat for more than two seconds
if r.now().Sub(r.lastKnownTime) > 2*r.interval {
return 0, fmt.Errorf("no heartbeat received in over 2x the heartbeat interval")
}

if r.lastKnownError != nil {
return 0, r.lastKnownError
}
Expand Down Expand Up @@ -163,6 +171,7 @@ func (r *heartbeatReader) readHeartbeat() {
reads.Add(1)

r.lagMu.Lock()
r.lastKnownTime = r.now()
r.lastKnownLag = lag
r.lastKnownError = nil
r.lagMu.Unlock()
Expand Down

0 comments on commit 7da7d81

Please sign in to comment.