-
Notifications
You must be signed in to change notification settings - Fork 210
/
fetch_queue.go
345 lines (297 loc) · 8.43 KB
/
fetch_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
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
package sync
import (
"errors"
"fmt"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"runtime"
"runtime/debug"
"sync"
)
type fetchPoetProofFunc func(poetProofRef []byte) error
type sValidateAtxFunc func(atx *types.ActivationTx) error
type sFetchAtxFunc func(atx *types.ActivationTx) error
type checkLocalFunc func(ids []types.Hash32) (map[types.Hash32]item, map[types.Hash32]item, []types.Hash32)
type item interface {
Hash32() types.Hash32
ShortString() string
}
type fetchJob struct {
items []item
ids []types.Hash32
}
//todo make the queue generic
type fetchQueue struct {
log.Log
batchRequestFactory
*sync.Mutex
workerInfra networker
pending map[types.Hash32][]chan bool
handleFetch func(fj fetchJob)
checkLocal checkLocalFunc
queue chan []types.Hash32 //types.TransactionID //todo make buffered
name string
}
func (fq *fetchQueue) Close() {
close(fq.queue)
fq.Info("done")
}
func concatShortIds(items []types.Hash32) string {
str := ""
for i, h := range items {
str += h.ShortString()
if i < len(items)-1 {
str += " "
}
}
return str
}
func (fq *fetchQueue) shutdownRecover() {
r := recover()
if r != nil {
fq.Info("%s shut down ", fq.name)
fq.Error("stacktrace from panic: \n" + string(debug.Stack()))
}
}
//todo batches
func (fq *fetchQueue) work() error {
defer fq.shutdownRecover()
parallelWorkers := runtime.NumCPU()
output := fetchWithFactory(newFetchWorker(fq.workerInfra, runtime.NumCPU(), fq.batchRequestFactory, fq.queue, fq.name))
wg := sync.WaitGroup{}
wg.Add(parallelWorkers)
for i := 0; i < parallelWorkers; i++ {
go func() {
fq.Info("running work")
for out := range output {
fq.Info("new batch out of queue")
if out == nil {
fq.Info("close queue")
break
}
bjb, ok := out.(fetchJob)
if !ok {
fq.Error(fmt.Sprintf("Type assertion err %v", out))
continue
}
if len(bjb.ids) == 0 {
break //fmt.Errorf("channel closed")
}
fq.Info("fetched %ss %s", fq.name, concatShortIds(bjb.ids))
fq.handleFetch(bjb)
fq.Info("next batch")
}
wg.Done()
}()
}
wg.Wait()
return nil
}
func (fq *fetchQueue) addToPendingGetCh(ids []types.Hash32) chan bool {
return getDoneChan(fq.addToPending(ids))
}
func (fq *fetchQueue) addToPending(ids []types.Hash32) []chan bool {
//defer fq.shutdownRecover()
fq.Lock()
deps := make([]chan bool, 0, len(ids))
for _, id := range ids {
ch := make(chan bool, 1)
deps = append(deps, ch)
fq.pending[id] = append(fq.pending[id], ch)
}
fq.Unlock()
if len(ids) > 0 {
fq.queue <- ids
}
return deps
}
func (fq *fetchQueue) invalidate(id types.Hash32, valid bool) {
fq.Lock()
deps := fq.pending[id]
delete(fq.pending, id)
fq.Unlock()
for _, dep := range deps {
dep <- valid
}
fq.Debug("invalidated %v %v", id.ShortString(), valid)
}
//returns items out of itemIds that are not in the local database
func (fq *fetchQueue) handle(itemIds []types.Hash32) (map[types.Hash32]item, error) {
if len(itemIds) == 0 {
fq.Debug("handle empty item ids slice")
return nil, nil
}
unprocessedItems, _, missing := fq.checkLocal(itemIds)
if len(missing) > 0 {
output := fq.addToPendingGetCh(missing)
if success := <-output; !success {
return nil, fmt.Errorf("could not fetch all items")
}
unprocessedItems, _, missing = fq.checkLocal(itemIds)
if len(missing) > 0 {
return nil, errors.New("could not find all items even though fetch was successful")
}
}
return unprocessedItems, nil
}
//todo make the queue generic
type txQueue struct {
fetchQueue
}
func newTxQueue(s *Syncer) *txQueue {
//todo buffersize
q := &txQueue{
fetchQueue: fetchQueue{
Log: s.Log.WithName("txFetchQueue"),
workerInfra: s.net,
Mutex: &sync.Mutex{},
batchRequestFactory: txFetchReqFactory,
checkLocal: s.txCheckLocal,
pending: make(map[types.Hash32][]chan bool),
queue: make(chan []types.Hash32, 10000),
name: "Tx",
},
}
q.handleFetch = updateTxDependencies(q.invalidate, s.txpool)
go q.work()
return q
}
//we could get rid of this if we had a unified id type
func (tx txQueue) HandleTxs(txids []types.TransactionID) ([]*types.Transaction, error) {
txItems := make([]types.Hash32, 0, len(txids))
for _, i := range txids {
txItems = append(txItems, i.Hash32())
}
txres, err := tx.handle(txItems)
if err != nil {
return nil, err
}
txs := make([]*types.Transaction, 0, len(txres))
for _, i := range txres {
txs = append(txs, i.(*types.Transaction))
}
return txs, nil
}
func updateTxDependencies(invalidate func(id types.Hash32, valid bool), txpool txMemPool) func(fj fetchJob) {
return func(fj fetchJob) {
mp := map[types.Hash32]*types.Transaction{}
for _, item := range fj.items {
mp[item.Hash32()] = item.(*types.Transaction)
}
for _, id := range fj.ids {
if item, ok := mp[id]; ok {
txpool.Put(types.TransactionID(id), item)
invalidate(id, true)
} else {
invalidate(id, false)
}
}
}
}
type atxQueue struct {
fetchQueue
}
func newAtxQueue(s *Syncer, fetchPoetProof fetchPoetProofFunc) *atxQueue {
//todo buffersize
q := &atxQueue{
fetchQueue: fetchQueue{
Log: s.Log.WithName("atxFetchQueue"),
workerInfra: s.net,
batchRequestFactory: atxFetchReqFactory,
Mutex: &sync.Mutex{},
checkLocal: s.atxCheckLocal,
pending: make(map[types.Hash32][]chan bool),
queue: make(chan []types.Hash32, 10000),
name: "Atx",
},
}
q.handleFetch = updateAtxDependencies(q.invalidate, s.SyntacticallyValidateAtx, s.FetchAtxReferences, s.atxDb, fetchPoetProof, q.Log)
go q.work()
return q
}
//we could get rid of this if we had a unified id type
func (atx atxQueue) HandleAtxs(atxids []types.ATXID) ([]*types.ActivationTx, error) {
atx.Log.Debug("going to fetch %v", atxids)
atxItems := make([]types.Hash32, 0, len(atxids))
for _, i := range atxids {
atxItems = append(atxItems, i.Hash32())
}
atxres, err := atx.handle(atxItems)
if err != nil {
atx.Log.Error("cannot fetch all atxs for block %v", err)
return nil, err
}
atxs := make([]*types.ActivationTx, 0, len(atxres))
for _, i := range atxres {
atxs = append(atxs, i.(*types.ActivationTx))
}
return atxs, nil
}
func updateAtxDependencies(invalidate func(id types.Hash32, valid bool), sValidateAtx sValidateAtxFunc, fetchAtxRefs sFetchAtxFunc, atxDB atxDB, fetchProof fetchPoetProofFunc, logger log.Log) func(fj fetchJob) {
return func(fj fetchJob) {
fetchProofCalcID(fetchProof, fj)
mp := map[types.Hash32]*types.ActivationTx{}
for _, item := range fj.items {
tmp := item.(*types.ActivationTx)
mp[item.Hash32()] = tmp
}
for _, id := range fj.ids {
if atx, ok := mp[id]; ok {
logger.Info("atx queue work item %v atx %v", id.String(), atx.ShortString())
err := fetchAtxRefs(atx)
if err != nil {
logger.Warning("failed to fetch referenced atxs of %s %s", id.ShortString(), err)
invalidate(id, false)
continue
}
err = sValidateAtx(atx)
if err != nil {
logger.Warning("failed to validate atx %v job %s %s", atx.ShortString(), id.ShortString(), err)
invalidate(id, false)
continue
}
err = atxDB.ProcessAtx(atx)
if err != nil {
logger.Warning("failed to add atx to db %s %s", id.ShortString(), err)
invalidate(id, false)
continue
}
logger.Info("atx queue work item ok %v atx %v", id.String(), atx.ShortString())
invalidate(id, true)
} else {
logger.Error("job returned with no response %v", id.String())
invalidate(id, false)
}
}
}
}
func getDoneChan(deps []chan bool) chan bool {
doneChan := make(chan bool)
//fan in
go func() {
alldone := true
for _, c := range deps {
if done := <-c; !done {
alldone = false
break
}
}
doneChan <- alldone
close(doneChan)
}()
return doneChan
}
func fetchProofCalcID(fetchPoetProof fetchPoetProofFunc, fj fetchJob) {
itemsWithProofs := make([]item, 0, len(fj.items))
for _, item := range fj.items {
atx := item.(*types.ActivationTx)
atx.CalcAndSetID() //todo put it somewhere that will cause less confusion
if err := fetchPoetProof(atx.GetPoetProofRef()); err != nil {
log.Error("received atx (%v) with syntactically invalid or missing PoET proof (%x): %v",
atx.ShortString(), atx.GetShortPoetProofRef(), err)
continue
}
itemsWithProofs = append(itemsWithProofs, atx)
}
fj.items = itemsWithProofs
}