forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbStateCatchup.go
786 lines (684 loc) · 19 KB
/
dbStateCatchup.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package state
import (
"container/list"
"reflect"
"sync"
"time"
"github.com/FactomProject/factomd/common/messages"
)
type GenericListItem interface {
Height() uint32
}
func getHeightSafe(i GenericListItem) int {
if i == nil || reflect.ValueOf(i).IsNil() {
return -1
}
return int(i.Height())
}
func waitForLoaded(s *State) {
// Don't start until the db is finished loading.
for !s.DBFinished {
time.Sleep(1 * time.Second)
}
if s.highestKnown < s.DBHeightAtBoot {
s.highestKnown = s.DBHeightAtBoot + 1 // Make sure we ask for the next block after the database at startup.
}
}
// TODO: Redesign Catchup. Some assumptions were made that made this more
// TODO: complex than it needed to be.
func (list *DBStateList) Catchup() {
missing := list.State.StatesMissing
waiting := list.State.StatesWaiting
received := list.State.StatesReceived
factomSecond := list.State.FactomSecond()
requestTimeout := time.Duration(list.State.RequestTimeout) * factomSecond
requestLimit := list.State.RequestLimit
// Wait for db to be loaded
waitForLoaded(list.State)
// keep the lists up to date with the saved states.
go func() {
// Notify missing will add the height to the missing
// if it is not received and not already requested.
notifyMissing := func(n uint32) bool {
if !waiting.Has(n) {
list.State.LogPrintf("dbstatecatchup", "{actual} notify missing %d", n)
missing.Add(n)
return true
}
return false
}
var hs, hk uint32
hsf := func() (rval uint32) {
defer func() {
if hs != rval {
list.State.LogPrintf("dbstatecatchup", "HS = %d", rval)
}
}()
// Sets the floor for what we will be requesting
// AKA : What we have. In reality the receivedlist should
// indicate that we have it, however, because a dbstate
// is not fully validated before we get it, we cannot
// assume that.
floor := uint32(0)
// Once it is in the db, we can assume it's all good.
if d, err := list.State.DB.FetchDBlockHead(); err == nil && d != nil {
floor = d.GetDatabaseHeight() // If it is in our db, let's make sure to stop asking
}
list.State.LogPrintf("dbstatecatchup", "Floor diff %d / %d", list.State.GetHighestSavedBlk(), floor)
// get the hightest block in the database at boot
b := list.State.GetDBHeightAtBoot()
// don't request states that are in the database at boot time
if b > floor {
return b
}
return floor
}
// get the height of the known blocks
hkf := func() (rval uint32) {
a := list.State.GetHighestAck()
k := list.State.GetHighestKnownBlock()
defer func() {
if hk != rval {
list.State.LogPrintf("dbstatecatchup", "HK = %d", rval)
}
}()
// check that known is more than 2 ahead of acknowledged to make
// sure not to ask for blocks that haven't finished
if k > a+2 {
return k - 2
}
if a < 2 {
return a
}
return a - 2 // Acks are for height + 1 (sometimes +2 in min 0)
}
hs = hsf()
hk = hkf()
list.State.LogPrintf("dbstatecatchup", "Start with hs = %d hk = %d", hs, hk)
for {
start := time.Now()
// get the height of the saved blocks
hs = hsf()
hk = hkf()
// The base means anything below we can toss
base := received.Base()
if base < hs {
list.State.LogPrintf("dbstatecatchup", "Received base set to %d", hs)
received.SetBase(hs)
base = hs
}
receivedSlice := received.ListAsSlice()
// When we pull the slice, we might be able to trim the receivedSlice for the next loop
sliceKeep := 0
// TODO: Rewrite to stop redundant looping over missing/waiting list
// TODO: for each delete. It shouldn't be too bad atm, as most things are in order.
for i, h := range receivedSlice {
list.State.LogPrintf("dbstatecatchup", "missing & waiting delete %d", h)
// remove any states from the missing list that have been saved.
missing.LockAndDelete(h)
// remove any states from the waiting list that have been saved.
waiting.LockAndDelete(h)
// Clean our our received list as well.
if h <= base {
sliceKeep = i
received.LockAndDelete(h)
}
}
// find gaps in the received list
// we can start at `sliceKeep` because everything below it was removed
for i := sliceKeep; i < len(receivedSlice)-1; i++ {
h := receivedSlice[i]
// if the height of the next received state is not equal to the
// height of the current received state plus one then there is a
// gap in the received state list.
for n := h; n+1 < receivedSlice[i+1]; n++ {
// missing.Notify <- NewMissingState(n + 1)
r := notifyMissing(n + 1)
list.State.LogPrintf("dbstatecatchup", "{gf} notify missing %d [%t]", n, r)
}
}
// TODO: Better limit the number of asks based on what we already asked for.
// TODO: If we implement that, ensure that we don't drop anything, as this covers any holes
// TODO: that might be made
max := 3000 // Limit the number of new asks we will add for each iteration
// add all known states after the last received to the missing list
for n := received.Heighestreceived() + 1; n <= hk && max > 0; n++ {
max--
// missing.Notify <- NewMissingState(n)
r := notifyMissing(n)
list.State.LogPrintf("dbstatecatchup", "{hf (%d, %d)} notify missing %d [%t]", hk, max, n, r)
}
list.State.LogPrintf("dbstatecatchup", "height update took %s. Base:%d/%d/%d, Miss[v%d, ^_, T%d], Wait [v_, ^%d, T%d], Rec[v%d, ^%d, T%d]",
time.Since(start),
received.Base(), hs, list.State.GetDBHeightAtBoot(),
getHeightSafe(missing.GetFront()), missing.Len(),
getHeightSafe(waiting.GetEnd()), waiting.Len(),
received.Base(), received.Heighestreceived(), received.List.Len())
time.Sleep(factomSecond)
}
}()
// watch the waiting list and move any requests that have timed out back
// into the missing list.
go func() {
for {
base := received.Base()
waitingSlice := waiting.ListAsSlice()
//for e := waiting.List.Front(); e != nil; e = e.Next() {
for _, s := range waitingSlice {
// Instead of choosing if to ask for it, just remove it
if s.Height() <= base {
waiting.LockAndDelete(s.Height())
continue
}
if s.RequestAge() > requestTimeout {
waiting.LockAndDelete(s.Height())
if received.Get(s.Height()) == nil {
list.State.LogPrintf("dbstatecatchup", "request timeout : waiting -> missing %d", s.Height())
missing.Add(s.Height())
}
}
}
time.Sleep(requestTimeout)
}
}()
// manage received dbstates
go func() {
for {
select {
case m := <-received.Notify:
s := NewReceivedState(m)
if s != nil {
list.State.LogPrintf("dbstatecatchup", "dbstate received : missing & waiting delete, received add %d", s.Height())
missing.LockAndDelete(s.Height())
waiting.LockAndDelete(s.Height())
received.Add(s.Height(), s.Message())
}
}
}
}()
// request missing states from the network
go func() {
for {
if waiting.Len() < requestLimit {
// TODO: the batch limit should probably be set by a configuration variable
b, e := missing.NextConsecutiveMissing(10)
list.State.LogPrintf("dbstatecatchup", "dbstate requesting from %d to %d", b, e)
if b == 0 && e == 0 {
time.Sleep(1 * time.Second)
continue
}
// make sure the end doesn't come before the beginning
if e < b {
e = b
}
msg := messages.NewDBStateMissing(list.State, b, e)
msg.SendOut(list.State, msg)
list.State.DBStateAskCnt += 1 // Total number of dbstates requests
for i := b; i <= e; i++ {
list.State.LogPrintf("dbstatecatchup", "\tdbstate requested : missing -> waiting %d", i)
missing.LockAndDelete(i)
waiting.Add(i)
}
} else {
// if the next missing state is a lower height than the last waiting
// state prune the waiting list
m := missing.GetFront()
w := waiting.GetEnd()
if m != nil && w != nil {
if m.Height() < w.Height() {
list.State.LogPrintf("dbstatecatchup", "waiting delete, cleanup %d", w.Height())
waiting.LockAndDelete(w.Height())
}
}
time.Sleep(50 * time.Millisecond)
}
}
}()
}
// MissingState is information about a DBState that is known to exist but is not
// available on the current node.
type MissingState struct {
height uint32
}
// NewMissingState creates a new MissingState for the DBState at a specific
// height.
func NewMissingState(height uint32) *MissingState {
s := new(MissingState)
s.height = height
return s
}
func (s *MissingState) Height() uint32 {
return s.height
}
type StatesMissing struct {
List *list.List
// Notify chan *MissingState
lock *sync.Mutex
}
// NewStatesMissing creates a new list of missing DBStates.
func NewStatesMissing() *StatesMissing {
l := new(StatesMissing)
l.List = list.New()
// l.Notify = make(chan *MissingState)
l.lock = new(sync.Mutex)
return l
}
// Add adds a new MissingState to the list.
func (l *StatesMissing) Add(height uint32) {
l.lock.Lock()
defer l.lock.Unlock()
for e := l.List.Back(); e != nil; e = e.Prev() {
s := e.Value.(*MissingState)
if height > s.Height() {
l.List.InsertAfter(NewMissingState(height), e)
return
} else if height == s.Height() {
return
}
}
l.List.PushFront(NewMissingState(height))
}
// LockAndDelete removes a MissingState from the list.
func (l *StatesMissing) LockAndDelete(height uint32) {
l.lock.Lock()
defer l.lock.Unlock()
l.DeleteLockless(height)
}
func (l *StatesMissing) DeleteLockless(height uint32) {
// DeleteLockless does not lock the mutex, if called from another top level func
if l == nil {
return
}
for e := l.List.Front(); e != nil; e = e.Next() {
if e.Value.(*MissingState).Height() == height {
l.List.Remove(e)
break
}
}
}
func (l *StatesMissing) Get(height uint32) *MissingState {
// We want to lock here, as something can be deleted/added as we are iterating
// and mess up our for loop
l.lock.Lock()
defer l.lock.Unlock()
for e := l.List.Front(); e != nil; e = e.Next() {
s := e.Value.(*MissingState)
if s.Height() == height {
return s
}
}
return nil
}
func (l *StatesMissing) GetFront() *MissingState {
// We want to lock here, as we first check the length, then grab the root.
// the root could be deleted after we checked the len.
l.lock.Lock()
defer l.lock.Unlock()
e := l.List.Front()
if e != nil {
s := e.Value.(*MissingState)
if s != nil {
return s
}
}
return nil
}
func (l *StatesMissing) Len() int {
return l.List.Len()
}
// NextConsecutiveMissing returns the heights of the the next n or fewer
// consecutive missing states
func (l *StatesMissing) NextConsecutiveMissing(n int) (uint32, uint32) {
// We want to lock here, as something can be deleted/added as we are iterating
// and mess up our for loop
l.lock.Lock()
defer l.lock.Unlock()
f := l.List.Front()
if f == nil {
return 0, 0
}
beg := f.Value.(*MissingState).Height()
end := beg
c := 0
for e := f.Next(); e != nil; e = e.Next() {
h := e.Value.(*MissingState).Height()
// We are looking to see if the consecutive height
// sequence is broken. Easy to check if h != the next one
// we are expecting.
if h != end+1 {
break
}
end++
c++
// TODO: the batch limit should probably be set as a configuration variable
if c == n {
break
}
}
return beg, end
}
// GetNext pops the next MissingState from the list.
func (l *StatesMissing) GetNext() *MissingState {
// We want to lock here, as we first check the length, then grab the root.
// the root could be deleted after we checked the len.
l.lock.Lock()
defer l.lock.Unlock()
e := l.List.Front()
if e != nil {
s := e.Value.(*MissingState)
l.DeleteLockless(s.Height())
return s
}
return nil
}
type WaitingState struct {
height uint32
requestedTime time.Time
}
func NewWaitingState(height uint32) *WaitingState {
s := new(WaitingState)
s.height = height
s.requestedTime = time.Now()
return s
}
func (s *WaitingState) Height() uint32 {
return s.height
}
func (s *WaitingState) RequestAge() time.Duration {
return time.Since(s.requestedTime)
}
func (s *WaitingState) ResetRequestAge() {
s.requestedTime = time.Now()
}
type StatesWaiting struct {
List *list.List
// Notify chan *WaitingState
lock *sync.Mutex
}
func NewStatesWaiting() *StatesWaiting {
l := new(StatesWaiting)
l.List = list.New()
// l.Notify = make(chan *WaitingState)
l.lock = new(sync.Mutex)
return l
}
func (l *StatesWaiting) ListAsSlice() []*WaitingState {
// Lock as we are iterating
l.lock.Lock()
defer l.lock.Unlock()
slice := make([]*WaitingState, l.List.Len())
i := 0
for e := l.List.Front(); e != nil; e = e.Next() {
slice[i] = e.Value.(*WaitingState)
i++
}
return slice
}
func (l *StatesWaiting) Add(height uint32) {
l.lock.Lock()
defer l.lock.Unlock()
for e := l.List.Back(); e != nil; e = e.Prev() {
s := e.Value.(*WaitingState)
if s == nil {
n := NewWaitingState(height)
l.List.InsertAfter(n, e)
return
} else if height > s.Height() {
n := NewWaitingState(height)
l.List.InsertAfter(n, e)
return
} else if height == s.Height() {
return
}
}
l.List.PushFront(NewWaitingState(height))
}
func (l *StatesWaiting) LockAndDelete(height uint32) {
l.lock.Lock()
defer l.lock.Unlock()
for e := l.List.Front(); e != nil; e = e.Next() {
s := e.Value.(*WaitingState)
if s.Height() == height {
l.List.Remove(e)
break
}
}
}
func (l *StatesWaiting) Get(height uint32) *WaitingState {
// We want to lock here, as something can be deleted/added as we are iterating
// and mess up our for loop
l.lock.Lock()
defer l.lock.Unlock()
for e := l.List.Front(); e != nil; e = e.Next() {
s := e.Value.(*WaitingState)
if s.Height() == height {
return s
}
}
return nil
}
func (l *StatesWaiting) GetEnd() *WaitingState {
// We want to lock here, as check the length then grab the root.
// The root could be deleted after we checked for the length
l.lock.Lock()
defer l.lock.Unlock()
e := l.List.Back()
if e != nil {
s := e.Value.(*WaitingState)
if s != nil {
return s
}
}
return nil
}
func (l *StatesWaiting) Has(height uint32) bool {
// We want to lock here, as something can be deleted/added as we are iterating
// and mess up our for loop
l.lock.Lock()
defer l.lock.Unlock()
for e := l.List.Front(); e != nil; e = e.Next() {
s := e.Value.(*WaitingState)
if s.Height() == height {
return true
}
}
return false
}
func (l *StatesWaiting) Len() int {
return l.List.Len()
}
// ReceivedState represents a DBStateMsg received from the network
type ReceivedState struct {
height uint32
msg *messages.DBStateMsg
}
// NewReceivedState creates a new member for the StatesReceived list
func NewReceivedState(msg *messages.DBStateMsg) *ReceivedState {
if msg == nil {
return nil
}
s := new(ReceivedState)
s.height = msg.DirectoryBlock.GetHeader().GetDBHeight()
s.msg = msg
return s
}
// Height returns the block height of the received state
func (s *ReceivedState) Height() uint32 {
return s.height
}
// Message returns the DBStateMsg received from the network.
func (s *ReceivedState) Message() *messages.DBStateMsg {
return s.msg
}
// StatesReceived is the list of DBStates received from the network. "base"
// represents the height of known saved states.
type StatesReceived struct {
List *list.List
Notify chan *messages.DBStateMsg
base uint32
lock *sync.Mutex
}
func NewStatesReceived() *StatesReceived {
l := new(StatesReceived)
l.List = list.New()
l.Notify = make(chan *messages.DBStateMsg)
l.lock = new(sync.Mutex)
return l
}
// Base returns the base height of the StatesReceived list
func (l *StatesReceived) Base() uint32 {
return l.base
}
func (l *StatesReceived) SetBase(height uint32) {
l.lock.Lock()
defer l.lock.Unlock()
l.SetBaseLockless(height)
}
func (l *StatesReceived) SetBaseLockless(height uint32) {
l.base = height
for e := l.List.Front(); e != nil; e = e.Next() {
switch v := e.Value.(*ReceivedState).Height(); {
case v < l.base:
l.List.Remove(e)
case v == l.base:
l.List.Remove(e)
break
case v > l.base:
break
}
}
}
// Heighestreceived returns the height of the last member in StatesReceived
func (l *StatesReceived) Heighestreceived() uint32 {
// We want to lock here, as we first check the length, then grab the root.
// the root could be deleted after we checked the len.
l.lock.Lock()
defer l.lock.Unlock()
height := uint32(0)
s := l.List.Back()
if s != nil {
height = s.Value.(*ReceivedState).Height()
}
if l.Base() > height {
return l.Base()
}
return height
}
// ListAsSlice will return the list as a slice
// to be iterated over in a threadsafe manner.
func (l *StatesReceived) ListAsSlice() []uint32 {
// Lock as we are iterating
l.lock.Lock()
defer l.lock.Unlock()
slice := make([]uint32, l.List.Len())
i := 0
for e := l.List.Front(); e != nil; e = e.Next() {
slice[i] = e.Value.(*ReceivedState).Height()
i++
}
return slice
}
// Add adds a new received state to the list.
func (l *StatesReceived) Add(height uint32, msg *messages.DBStateMsg) {
if msg == nil {
return
}
l.lock.Lock()
defer l.lock.Unlock()
if height < l.base {
// We already know we had this height
// This should really never happen
return
}
for e := l.List.Back(); e != nil; e = e.Prev() {
s := e.Value.(*ReceivedState)
if s == nil {
n := NewReceivedState(msg)
l.List.InsertAfter(n, e)
return
} else if height > s.Height() {
n := NewReceivedState(msg)
l.List.InsertAfter(n, e)
return
} else if height == s.Height() {
return
}
}
l.List.PushFront(NewReceivedState(msg))
}
// LockAndDelete removes a state from the StatesReceived list
func (l *StatesReceived) LockAndDelete(height uint32) {
l.lock.Lock()
defer l.lock.Unlock()
for e := l.List.Back(); e != nil; e = e.Prev() {
s := e.Value.(*ReceivedState)
if s == nil {
break
} else if s.Height() == height {
l.List.Remove(e)
break
}
}
}
// Get returns a member from the StatesReceived list
func (l *StatesReceived) Get(height uint32) *ReceivedState {
// We want to lock here, as something can be deleted/added as we are iterating
// and mess up our for loop
l.lock.Lock()
defer l.lock.Unlock()
for e := l.List.Back(); e != nil; e = e.Prev() {
s := e.Value.(*ReceivedState)
if height > s.Height() {
}
if s.Height() == height {
return s
}
}
return nil
}
func (l *StatesReceived) Has(height uint32) bool {
// We want to lock here, as something can be deleted/added as we are iterating
// and mess up our for loop
l.lock.Lock()
defer l.lock.Unlock()
if height <= l.Base() {
return true
}
for e := l.List.Front(); e != nil; e = e.Next() {
s := e.Value.(*ReceivedState)
if s == nil {
return false
}
if s.Height() == height {
return true
}
}
return false
}
func (l *StatesReceived) GetNext() *ReceivedState {
l.lock.Lock()
defer l.lock.Unlock()
if l.List.Len() == 0 {
return nil
}
e := l.List.Front()
if e != nil {
s := e.Value.(*ReceivedState)
if s == nil {
l.List.Remove(e)
return nil
}
if s.Height() == l.Base()+1 {
l.SetBaseLockless(s.Height())
l.List.Remove(e)
return s
}
if s.Height() <= l.Base() {
l.List.Remove(e)
}
}
return nil
}