-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_queue.go
161 lines (140 loc) · 3.1 KB
/
badger_queue.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
package storage
import (
"sync"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
)
type Queue struct {
mutex *sync.Mutex
cacheRing *RingBuffer
finalRing *RingBuffer
finalSet map[crypto.Hash]bool
cacheSet map[crypto.Hash]bool
}
type PeerSnapshot struct {
key crypto.Hash
PeerId crypto.Hash
Snapshot *common.Snapshot
}
func (ps *PeerSnapshot) cacheKey() crypto.Hash {
hash := ps.Snapshot.PayloadHash()
hash = hash.ForNetwork(ps.PeerId)
for _, sig := range ps.Snapshot.Signatures {
hash = crypto.NewHash(append(hash[:], sig[:]...))
}
return hash
}
func (ps *PeerSnapshot) finalKey() crypto.Hash {
hash := ps.Snapshot.PayloadHash()
return hash.ForNetwork(ps.PeerId)
}
func NewQueue() *Queue {
return &Queue{
mutex: new(sync.Mutex),
finalSet: make(map[crypto.Hash]bool),
cacheSet: make(map[crypto.Hash]bool),
cacheRing: NewRingBuffer(1024 * 1024),
finalRing: NewRingBuffer(1024 * 1024 * 16),
}
}
func (q *Queue) Dispose() {
q.finalRing.Dispose()
q.cacheRing.Dispose()
}
func (q *Queue) PutFinal(ps *PeerSnapshot) error {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.finalSet[ps.key] {
return nil
}
q.finalSet[ps.key] = true
for {
put, err := q.finalRing.Offer(ps)
if err != nil || put {
return err
}
time.Sleep(100 * time.Millisecond)
}
}
func (q *Queue) PopFinal() (*PeerSnapshot, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
item, err := q.finalRing.Poll(false)
if err != nil || item == nil {
return nil, err
}
ps := item.(*PeerSnapshot)
delete(q.finalSet, ps.key)
return ps, nil
}
func (q *Queue) PutCache(ps *PeerSnapshot) error {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.cacheSet[ps.key] {
return nil
}
q.cacheSet[ps.key] = true
for {
put, err := q.cacheRing.Offer(ps)
if err != nil || put {
return err
}
time.Sleep(100 * time.Millisecond)
}
}
func (q *Queue) PopCache() (*PeerSnapshot, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
item, err := q.cacheRing.Poll(false)
if err != nil || item == nil {
return nil, err
}
ps := item.(*PeerSnapshot)
delete(q.cacheSet, ps.key)
return ps, nil
}
func (s *BadgerStore) QueueInfo() (uint64, uint64, error) {
return s.queue.finalRing.Len(), s.queue.cacheRing.Len(), nil
}
func (s *BadgerStore) QueueAppendSnapshot(peerId crypto.Hash, snap *common.Snapshot, finalized bool) error {
ps := &PeerSnapshot{
PeerId: peerId,
Snapshot: snap,
}
if finalized {
ps.key = ps.finalKey()
return s.queue.PutFinal(ps)
}
ps.key = ps.cacheKey()
return s.queue.PutCache(ps)
}
func (s *BadgerStore) QueuePollSnapshots(hook func(peerId crypto.Hash, snap *common.Snapshot) error) {
for !s.closing {
time.Sleep(1 * time.Millisecond)
final, cache := 0, 0
for i := 0; i < 10; i++ {
ps, err := s.queue.PopFinal()
if err != nil {
break
}
if ps != nil {
hook(ps.PeerId, ps.Snapshot)
final++
}
}
for i := 0; i < 2; i++ {
ps, err := s.queue.PopCache()
if err != nil {
break
}
if ps != nil {
hook(ps.PeerId, ps.Snapshot)
cache++
}
}
if cache < 1 && final < 1 {
time.Sleep(100 * time.Millisecond)
}
}
}