-
Notifications
You must be signed in to change notification settings - Fork 199
/
heartbeatMessageInfo.go
223 lines (188 loc) · 6.39 KB
/
heartbeatMessageInfo.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package process
import (
"sync"
"time"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/heartbeat"
)
// heartbeatMessageInfo retain the message info received from another node (identified by a public key)
type heartbeatMessageInfo struct {
maxDurationPeerUnresponsive time.Duration
maxInactiveTime time.Duration
totalUpTime time.Duration
totalDownTime time.Duration
timeStamp time.Time
lastUptimeDowntime time.Time
genesisTime time.Time
versionNumber string
nodeDisplayName string
identity string
peerType string
receivedShardID uint32
computedShardID uint32
updateMutex sync.RWMutex
getTimeHandler func() time.Time
isActive bool
nonce uint64
numInstances uint64
}
// newHeartbeatMessageInfo returns a new instance of a heartbeatMessageInfo
func newHeartbeatMessageInfo(
maxDurationPeerUnresponsive time.Duration,
peerType string,
genesisTime time.Time,
timer heartbeat.Timer,
) (*heartbeatMessageInfo, error) {
if maxDurationPeerUnresponsive == 0 {
return nil, heartbeat.ErrInvalidMaxDurationPeerUnresponsive
}
if check.IfNil(timer) {
return nil, heartbeat.ErrNilTimer
}
hbmi := &heartbeatMessageInfo{
maxDurationPeerUnresponsive: maxDurationPeerUnresponsive,
maxInactiveTime: time.Duration(0),
isActive: false,
receivedShardID: uint32(0),
timeStamp: genesisTime,
lastUptimeDowntime: timer.Now(),
totalUpTime: time.Duration(0),
totalDownTime: time.Duration(0),
versionNumber: "",
nodeDisplayName: "",
identity: "",
peerType: peerType,
genesisTime: genesisTime,
getTimeHandler: timer.Now,
nonce: 0,
numInstances: 0,
}
return hbmi, nil
}
// ComputeActive will update the isActive field
func (hbmi *heartbeatMessageInfo) ComputeActive(crtTime time.Time) {
hbmi.updateMutex.Lock()
defer hbmi.updateMutex.Unlock()
validDuration := computeValidDuration(crtTime, hbmi)
hbmi.isActive = hbmi.isActive && validDuration
hbmi.updateTimes(crtTime)
}
func (hbmi *heartbeatMessageInfo) updateTimes(crtTime time.Time) {
if crtTime.Sub(hbmi.genesisTime) < 0 {
return
}
hbmi.updateMaxInactiveTimeDuration(crtTime)
hbmi.updateUpAndDownTime(crtTime)
}
func computeValidDuration(crtTime time.Time, hbmi *heartbeatMessageInfo) bool {
crtDuration := crtTime.Sub(hbmi.timeStamp)
crtDuration = maxDuration(0, crtDuration)
validDuration := crtDuration <= hbmi.maxDurationPeerUnresponsive
return validDuration
}
// Will update the total time a node was up and down
func (hbmi *heartbeatMessageInfo) updateUpAndDownTime(crtTime time.Time) {
if hbmi.lastUptimeDowntime.Sub(hbmi.genesisTime) < 0 {
hbmi.lastUptimeDowntime = hbmi.genesisTime
}
if crtTime.Sub(hbmi.timeStamp) < 0 {
return
}
lastDuration := crtTime.Sub(hbmi.lastUptimeDowntime)
lastDuration = maxDuration(0, lastDuration)
if lastDuration == 0 {
return
}
uptime, downTime := hbmi.computeUptimeDowntime(crtTime, lastDuration)
hbmi.totalUpTime += uptime
hbmi.totalDownTime += downTime
hbmi.isActive = uptime == lastDuration
hbmi.lastUptimeDowntime = crtTime
}
func (hbmi *heartbeatMessageInfo) computeUptimeDowntime(
crtTime time.Time,
lastDuration time.Duration,
) (time.Duration, time.Duration) {
durationSinceLastHeartbeat := crtTime.Sub(hbmi.timeStamp)
insideActiveWindowAfterHeartheat := durationSinceLastHeartbeat <= hbmi.maxDurationPeerUnresponsive
noHeartbeatReceived := hbmi.timeStamp == hbmi.genesisTime && !hbmi.isActive
outSideActiveWindowAfterHeartbeat := durationSinceLastHeartbeat-lastDuration > hbmi.maxDurationPeerUnresponsive
uptime := time.Duration(0)
downtime := time.Duration(0)
if noHeartbeatReceived || outSideActiveWindowAfterHeartbeat {
downtime = lastDuration
return uptime, downtime
}
if insideActiveWindowAfterHeartheat {
uptime = lastDuration
return uptime, downtime
}
downtime = durationSinceLastHeartbeat - hbmi.maxDurationPeerUnresponsive
uptime = lastDuration - downtime
return uptime, downtime
}
// HeartbeatReceived processes a new message arrived from a peer
func (hbmi *heartbeatMessageInfo) HeartbeatReceived(
computedShardID uint32,
receivedShardID uint32,
version string,
nodeDisplayName string,
identity string,
peerType string,
nonce uint64,
numInstances uint64,
) {
hbmi.updateMutex.Lock()
defer hbmi.updateMutex.Unlock()
crtTime := hbmi.getTimeHandler()
hbmi.computedShardID = computedShardID
hbmi.receivedShardID = receivedShardID
hbmi.versionNumber = version
hbmi.nodeDisplayName = nodeDisplayName
hbmi.identity = identity
hbmi.peerType = peerType
hbmi.updateTimes(crtTime)
hbmi.timeStamp = crtTime
hbmi.isActive = true
hbmi.nonce = nonce
hbmi.numInstances = numInstances
}
// UpdateShardAndPeerType - updates the shard and peerType only for a heartbeat message info
func (hbmi *heartbeatMessageInfo) UpdateShardAndPeerType(
computedShardID uint32,
peerType string,
) {
hbmi.updateMutex.Lock()
defer hbmi.updateMutex.Unlock()
hbmi.computedShardID = computedShardID
hbmi.peerType = peerType
}
func (hbmi *heartbeatMessageInfo) updateMaxInactiveTimeDuration(currentTime time.Time) {
crtDuration := currentTime.Sub(hbmi.timeStamp)
crtDuration = maxDuration(0, crtDuration)
greaterDurationThanMax := hbmi.maxInactiveTime < crtDuration
currentTimeAfterGenesis := hbmi.genesisTime.Sub(currentTime) < 0
if greaterDurationThanMax && currentTimeAfterGenesis {
hbmi.maxInactiveTime = crtDuration
}
}
func maxDuration(first, second time.Duration) time.Duration {
if first > second {
return first
}
return second
}
// GetIsActive will return true if the peer is set as active
func (hbmi *heartbeatMessageInfo) GetIsActive() bool {
hbmi.updateMutex.Lock()
defer hbmi.updateMutex.Unlock()
isActive := hbmi.isActive
return isActive
}
// GetIsValidator will return true is the peer is a validator
func (hbmi *heartbeatMessageInfo) GetIsValidator() bool {
hbmi.updateMutex.Lock()
defer hbmi.updateMutex.Unlock()
return hbmi.peerType == string(core.EligibleList) || hbmi.peerType == string(core.WaitingList)
}