-
Notifications
You must be signed in to change notification settings - Fork 4
/
event_timer.go
450 lines (363 loc) · 10.8 KB
/
event_timer.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright (C) 2018 The ZeepinChain Authors
* This file is part of The ZeepinChain library.
*
* The ZeepinChain is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ZeepinChain is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ZeepinChain. If not, see <http://www.gnu.org/licenses/>.
*/
package vbft
import (
"container/heap"
"fmt"
"math/rand"
"sync"
"time"
"github.com/zeepin/ZeepinChain/common/log"
)
type TimerEventType int
const (
EventProposeBlockTimeout TimerEventType = iota
EventProposalBackoff
EventRandomBackoff
EventPropose2ndBlockTimeout
EventEndorseBlockTimeout
EventEndorseEmptyBlockTimeout
EventCommitBlockTimeout
EventPeerHeartbeat
EventTxPool
EventTxBlockTimeout
EventMax
)
var (
makeProposalTimeout = 300 * time.Millisecond
make2ndProposalTimeout = 300 * time.Millisecond
endorseBlockTimeout = 100 * time.Millisecond
commitBlockTimeout = 200 * time.Millisecond
peerHandshakeTimeout = 10 * time.Second
txPooltimeout = 1 * time.Second
zeroTxBlockTimeout = 10 * time.Second
)
type SendMsgEvent struct {
ToPeer uint32 // peer index
Msg ConsensusMsg
}
type TimerEvent struct {
evtType TimerEventType
blockNum uint32
msg ConsensusMsg
}
type perBlockTimer map[uint32]*time.Timer
type EventTimer struct {
lock sync.Mutex
server *Server
C chan *TimerEvent
//timerQueue TimerQueue
// bft timers
eventTimers map[TimerEventType]perBlockTimer
// peer heartbeat tickers
peerTickers map[uint32]*time.Timer
// other timers
normalTimers map[uint32]*time.Timer
}
func NewEventTimer(server *Server) *EventTimer {
timer := &EventTimer{
server: server,
C: make(chan *TimerEvent, 64),
eventTimers: make(map[TimerEventType]perBlockTimer),
peerTickers: make(map[uint32]*time.Timer),
normalTimers: make(map[uint32]*time.Timer),
}
for i := 0; i < int(EventMax); i++ {
timer.eventTimers[TimerEventType(i)] = make(map[uint32]*time.Timer)
}
return timer
}
func stopAllTimers(timers map[uint32]*time.Timer) {
for _, t := range timers {
t.Stop()
}
}
func (self *EventTimer) stop() {
self.lock.Lock()
defer self.lock.Unlock()
// clear timers by event timer
for i := 0; i < int(EventMax); i++ {
stopAllTimers(self.eventTimers[TimerEventType(i)])
self.eventTimers[TimerEventType(i)] = make(map[uint32]*time.Timer)
}
// clear normal timers
stopAllTimers(self.normalTimers)
self.normalTimers = make(map[uint32]*time.Timer)
}
func (self *EventTimer) StartTimer(Idx uint32, timeout time.Duration) error {
self.lock.Lock()
defer self.lock.Unlock()
if t, present := self.normalTimers[Idx]; present {
t.Stop()
log.Infof("timer for %d got reset", Idx)
}
self.normalTimers[Idx] = time.AfterFunc(timeout, func() {
// remove timer from map
self.lock.Lock()
defer self.lock.Unlock()
delete(self.normalTimers, Idx)
self.C <- &TimerEvent{
evtType: EventMax,
blockNum: Idx,
}
})
return nil
}
func (self *EventTimer) CancelTimer(idx uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
if t, present := self.normalTimers[idx]; present {
t.Stop()
delete(self.normalTimers, idx)
}
return nil
}
func (self *EventTimer) getEventTimeout(evtType TimerEventType) time.Duration {
switch evtType {
case EventProposeBlockTimeout:
return makeProposalTimeout
case EventPropose2ndBlockTimeout:
return make2ndProposalTimeout
case EventEndorseBlockTimeout:
return endorseBlockTimeout
case EventEndorseEmptyBlockTimeout:
return endorseBlockTimeout
case EventCommitBlockTimeout:
return commitBlockTimeout
case EventPeerHeartbeat:
return peerHandshakeTimeout
case EventProposalBackoff:
rank := self.server.getProposerRank(self.server.GetCurrentBlockNo(), self.server.Index)
if rank >= 0 {
d := int64(rank+1) * int64(make2ndProposalTimeout) / 3
return time.Duration(d)
}
return time.Duration(100 * time.Second)
case EventRandomBackoff:
d := (rand.Int63n(100) + 50) * int64(endorseBlockTimeout) / 100
return time.Duration(d)
case EventTxPool:
return txPooltimeout
case EventTxBlockTimeout:
return zeroTxBlockTimeout
}
return 0
}
//
// internal helper, should call with lock held
//
func (self *EventTimer) startEventTimer(evtType TimerEventType, blockNum uint32) error {
timers := self.eventTimers[evtType]
if t, present := timers[blockNum]; present {
t.Stop()
delete(timers, blockNum)
log.Infof("timer (type: %d) for %d got reset", evtType, blockNum)
}
timeout := self.getEventTimeout(evtType)
if timeout == 0 {
panic(fmt.Errorf("invalid timeout for event %d, blkNum %d", evtType, blockNum))
}
timers[blockNum] = time.AfterFunc(timeout, func() {
self.C <- &TimerEvent{
evtType: evtType,
blockNum: blockNum,
}
})
return nil
}
//
// internal helper, should call with lock held
//
func (self *EventTimer) cancelEventTimer(evtType TimerEventType, blockNum uint32) error {
timers := self.eventTimers[evtType]
if t, present := timers[blockNum]; present {
t.Stop()
delete(timers, blockNum)
}
return nil
}
func (self *EventTimer) StartProposalTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
log.Infof("server %d started proposal timer for blk %d", self.server.Index, blockNum)
return self.startEventTimer(EventProposeBlockTimeout, blockNum)
}
func (self *EventTimer) CancelProposalTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventProposeBlockTimeout, blockNum)
}
func (self *EventTimer) StartEndorsingTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
log.Infof("server %d started endorsing timer for blk %d", self.server.Index, blockNum)
return self.startEventTimer(EventEndorseBlockTimeout, blockNum)
}
func (self *EventTimer) CancelEndorseMsgTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventEndorseBlockTimeout, blockNum)
}
func (self *EventTimer) StartEndorseEmptyBlockTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
log.Infof("server %d started empty endorsing timer for blk %d", self.server.Index, blockNum)
return self.startEventTimer(EventEndorseEmptyBlockTimeout, blockNum)
}
func (self *EventTimer) CancelEndorseEmptyBlockTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventEndorseEmptyBlockTimeout, blockNum)
}
func (self *EventTimer) StartCommitTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
log.Infof("server %d started commit timer for blk %d", self.server.Index, blockNum)
return self.startEventTimer(EventCommitBlockTimeout, blockNum)
}
func (self *EventTimer) CancelCommitMsgTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventCommitBlockTimeout, blockNum)
}
func (self *EventTimer) StartProposalBackoffTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.startEventTimer(EventProposalBackoff, blockNum)
}
func (self *EventTimer) CancelProposalBackoffTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventProposalBackoff, blockNum)
}
func (self *EventTimer) StartBackoffTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.startEventTimer(EventRandomBackoff, blockNum)
}
func (self *EventTimer) CancelBackoffTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventRandomBackoff, blockNum)
}
func (self *EventTimer) Start2ndProposalTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.startEventTimer(EventPropose2ndBlockTimeout, blockNum)
}
func (self *EventTimer) Cancel2ndProposalTimer(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventPropose2ndBlockTimeout, blockNum)
}
func (self *EventTimer) onBlockSealed(blockNum uint32) {
self.lock.Lock()
defer self.lock.Unlock()
// clear event timers
for i := 0; i < int(EventMax); i++ {
if err := self.cancelEventTimer(TimerEventType(i), blockNum); err != nil {
log.Errorf("server %d, failed to stop timer %d on sealing",
self.server.Index, i)
}
}
}
func (self *EventTimer) StartTxBlockTimeout(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.startEventTimer(EventTxBlockTimeout, blockNum)
}
func (self *EventTimer) CancelTxBlockTimeout(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventTxBlockTimeout, blockNum)
}
func (self *EventTimer) startPeerTicker(peerIdx uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
if p, present := self.peerTickers[peerIdx]; present {
p.Stop()
log.Infof("ticker for %d got reset", peerIdx)
}
timeout := self.getEventTimeout(EventPeerHeartbeat)
self.peerTickers[peerIdx] = time.AfterFunc(timeout, func() {
self.C <- &TimerEvent{
evtType: EventPeerHeartbeat,
blockNum: peerIdx,
}
self.peerTickers[peerIdx].Reset(timeout)
})
return nil
}
func (self *EventTimer) stopPeerTicker(peerIdx uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
if p, present := self.peerTickers[peerIdx]; present {
p.Stop()
delete(self.peerTickers, peerIdx)
}
return nil
}
func (self *EventTimer) startTxTicker(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.startEventTimer(EventTxPool, blockNum)
}
func (self *EventTimer) stopTxTicker(blockNum uint32) error {
self.lock.Lock()
defer self.lock.Unlock()
return self.cancelEventTimer(EventTxPool, blockNum)
}
///////////////////////////////////////////////////////////
//
// timer queue
//
///////////////////////////////////////////////////////////
type TimerItem struct {
due time.Time
evt *TimerEvent
index int
}
type TimerQueue []*TimerItem
func (tq TimerQueue) Len() int {
return len(tq)
}
func (tq TimerQueue) Less(i, j int) bool {
return tq[j].due.After(tq[i].due)
}
func (tq TimerQueue) Swap(i, j int) {
tq[i], tq[j] = tq[j], tq[i]
tq[i].index = i
tq[j].index = j
}
func (tq *TimerQueue) Push(x interface{}) {
item := x.(*TimerItem)
item.index = len(*tq)
*tq = append(*tq, item)
}
func (tq *TimerQueue) Pop() interface{} {
old := *tq
n := len(old)
item := old[n-1]
item.index = -1
*tq = old[0 : n-1]
return item
}
func (tq *TimerQueue) update(item *TimerItem, due time.Time) {
item.due = due
heap.Fix(tq, item.index)
}