forked from hashicorp/nomad
/
heartbeat.go
148 lines (128 loc) · 3.95 KB
/
heartbeat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package nomad
import (
"time"
"github.com/armon/go-metrics"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/nomad/nomad/structs"
)
// initializeHeartbeatTimers is used when a leader is newly elected to create
// a new map to track heartbeat expiration and to reset all the timers from
// the previously known set of timers.
func (s *Server) initializeHeartbeatTimers() error {
// Scan all nodes and reset their timer
snap, err := s.fsm.State().Snapshot()
if err != nil {
return err
}
// Get an iterator over nodes
iter, err := snap.Nodes()
if err != nil {
return err
}
s.heartbeatTimersLock.Lock()
defer s.heartbeatTimersLock.Unlock()
// Handle each node
for {
raw := iter.Next()
if raw == nil {
break
}
node := raw.(*structs.Node)
if node.TerminalStatus() {
continue
}
s.resetHeartbeatTimerLocked(node.ID, s.config.FailoverHeartbeatTTL)
}
return nil
}
// resetHeartbeatTimer is used to reset the TTL of a heartbeat.
// This can be used for new heartbeats and existing ones.
func (s *Server) resetHeartbeatTimer(id string) (time.Duration, error) {
s.heartbeatTimersLock.Lock()
defer s.heartbeatTimersLock.Unlock()
// Compute the target TTL value
n := len(s.heartbeatTimers)
ttl := lib.RateScaledInterval(s.config.MaxHeartbeatsPerSecond, s.config.MinHeartbeatTTL, n)
ttl += lib.RandomStagger(ttl)
// Reset the TTL
s.resetHeartbeatTimerLocked(id, ttl+s.config.HeartbeatGrace)
return ttl, nil
}
// resetHeartbeatTimerLocked is used to reset a heartbeat timer
// assuming the heartbeatTimerLock is already held
func (s *Server) resetHeartbeatTimerLocked(id string, ttl time.Duration) {
// Ensure a timer map exists
if s.heartbeatTimers == nil {
s.heartbeatTimers = make(map[string]*time.Timer)
}
// Renew the heartbeat timer if it exists
if timer, ok := s.heartbeatTimers[id]; ok {
timer.Reset(ttl)
return
}
// Create a new timer to track expiration of this heartbeat
timer := time.AfterFunc(ttl, func() {
s.invalidateHeartbeat(id)
})
s.heartbeatTimers[id] = timer
}
// invalidateHeartbeat is invoked when a heartbeat TTL is reached and we
// need to invalidate the heartbeat.
func (s *Server) invalidateHeartbeat(id string) {
defer metrics.MeasureSince([]string{"nomad", "heartbeat", "invalidate"}, time.Now())
// Clear the heartbeat timer
s.heartbeatTimersLock.Lock()
delete(s.heartbeatTimers, id)
s.heartbeatTimersLock.Unlock()
s.logger.Printf("[WARN] nomad.heartbeat: node '%s' TTL expired", id)
// Make a request to update the node status
req := structs.NodeUpdateStatusRequest{
NodeID: id,
Status: structs.NodeStatusDown,
WriteRequest: structs.WriteRequest{
Region: s.config.Region,
},
}
var resp structs.NodeUpdateResponse
if err := s.endpoints.Node.UpdateStatus(&req, &resp); err != nil {
s.logger.Printf("[ERR] nomad.heartbeat: update status failed: %v", err)
}
}
// clearHeartbeatTimer is used to clear the heartbeat time for
// a single heartbeat. This is used when a heartbeat is destroyed
// explicitly and no longer needed.
func (s *Server) clearHeartbeatTimer(id string) error {
s.heartbeatTimersLock.Lock()
defer s.heartbeatTimersLock.Unlock()
if timer, ok := s.heartbeatTimers[id]; ok {
timer.Stop()
delete(s.heartbeatTimers, id)
}
return nil
}
// clearAllHeartbeatTimers is used when a leader is stepping
// down and we no longer need to track any heartbeat timers.
func (s *Server) clearAllHeartbeatTimers() error {
s.heartbeatTimersLock.Lock()
defer s.heartbeatTimersLock.Unlock()
for _, t := range s.heartbeatTimers {
t.Stop()
}
s.heartbeatTimers = nil
return nil
}
// heartbeatStats is a long running routine used to capture
// the number of active heartbeats being tracked
func (s *Server) heartbeatStats() {
for {
select {
case <-time.After(5 * time.Second):
s.heartbeatTimersLock.Lock()
num := len(s.heartbeatTimers)
s.heartbeatTimersLock.Unlock()
metrics.SetGauge([]string{"nomad", "heartbeat", "active"}, float32(num))
case <-s.shutdownCh:
return
}
}
}