-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprobe.go
More file actions
177 lines (141 loc) · 3.54 KB
/
probe.go
File metadata and controls
177 lines (141 loc) · 3.54 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import "time"
func (m *MyMembers) probe() {
m.nodeLock.RLock()
numNodes := len(m.nodes)
if numNodes <= 1 {
m.nodeLock.RUnlock()
return
}
// calculate the probe index
if m.probeIndex >= numNodes {
m.nodeLock.RUnlock()
m.nodeLock.Lock()
shuffledNodes(m.nodes)
m.probeIndex = 0
m.nodeLock.Unlock()
m.nodeLock.RLock()
numNodes = len(m.nodes)
}
var target *nodeState
for i := 0; i < numNodes; i++ {
idx := (m.probeIndex + i) % numNodes
ns := m.nodes[idx]
if ns.Name == m.config.Name || ns.DeadOrLeft() {
continue
}
target = ns
m.probeIndex = idx + 1
break
}
m.nodeLock.RUnlock()
if target == nil {
return
}
m.probeNode(target)
}
func (m *MyMembers) probeNode(target *nodeState) {
seqNo := m.nextSeqNo()
ackCh := make(chan ackMessage, m.config.IndirectChecks+1)
m.setProbeChannels(seqNo, ackCh, m.config.ProbeInterval)
advertiseIP, advertisePort, _ := m.transport.GetAdvertiseAddr()
p := ping{
SeqNo: seqNo,
TargetNode: target.Name,
SourceAddr: advertiseIP,
SourcePort: uint16(advertisePort),
SourceNode: m.config.Name,
Broadcasts: m.getBroadcasts(0, m.config.UDPBufferSize),
}
err := m.encodeAndSendMsg(target.Address(), pingMsg, &p)
if err != nil {
m.logger.Printf("[ERR] couldn't send the ping message")
return
}
probeStart := time.Now()
select {
case resp := <-ackCh:
if resp.Complete {
return
}
case <-time.After(m.config.ProbeTimeout):
// Timeout - proceed to indirect ping
case <-m.shutdownCh:
return
}
// Indirect ping Starts
// pick k random peers
m.nodeLock.RLock()
peers := kRandomNodes(m.config.IndirectChecks, m.nodes, func(ns *nodeState) bool {
return ns.Name == m.config.Name || ns.Name == target.Name || ns.DeadOrLeft()
})
m.nodeLock.RUnlock()
// send indirect ping requests to each peer
for _, peer := range peers {
req := indirectPingReq{
SeqNo: seqNo,
TargetNode: target.Name,
TargetNodeIP: target.Addr,
TargetNodePort: target.Port,
SourceAddr: advertiseIP,
SourcePort: uint16(advertisePort),
SourceNode: m.config.Name,
}
err := m.encodeAndSendMsg(peer.Address(), indirectPingMsg, &req)
if err != nil {
m.logger.Printf("[ERR] couldn't send the ping message")
return
}
}
// wait for the remaining time
elapsed := time.Since(probeStart)
remaining := m.config.ProbeInterval - elapsed
if remaining > 0 {
select {
case resp := <-ackCh:
if resp.Complete {
return
}
case <-time.After(remaining):
case <-m.shutdownCh:
return
}
}
// If no ack then mark the target node suspect
m.logger.Printf("[INFO] probe: no ack from %s, suspecting", target.Name)
m.nodeLock.RLock()
s := suspect{
Incarnation: target.Incarnation,
Node: target.Name,
From: m.config.Name,
}
m.nodeLock.RUnlock()
m.suspectNode(&s)
}
type ackMessage struct {
Complete bool
Payload []byte
Timestamp time.Time
}
// =================================== Probing ===================================
func (m *MyMembers) setProbeChannels(seqNo uint32, ackCh chan ackMessage, timeout time.Duration) {
ackFun := func(payload []byte, timestamp time.Time) {
select {
case ackCh <- ackMessage{Complete: true, Payload: payload, Timestamp: timestamp}:
default:
}
}
ah := &ackHandler{ackFn: ackFun, nackFn: nil, timer: nil}
m.ackLock.Lock()
m.ackHandlers[seqNo] = ah
m.ackLock.Unlock()
ah.timer = time.AfterFunc(timeout, func() {
m.ackLock.Lock()
delete(m.ackHandlers, seqNo)
m.ackLock.Unlock()
select {
case ackCh <- ackMessage{Complete: false}:
default:
}
})
}