-
Notifications
You must be signed in to change notification settings - Fork 211
/
fetch.go
666 lines (594 loc) · 17.3 KB
/
fetch.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
// Package fetch contains mechanism to fetch Data from remote peers
package fetch
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/protocol"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/p2p/server"
"github.com/spacemeshos/go-spacemesh/system"
)
const (
atxProtocol = "ax/1"
lyrDataProtocol = "ld/1"
lyrOpnsProtocol = "lp/1"
hashProtocol = "hs/1"
meshHashProtocol = "mh/1"
malProtocol = "ml/1"
OpnProtocol = "lp/2"
cacheSize = 1000
)
var (
// errExceedMaxRetries is returned when MaxRetriesForRequest attempts has been made to fetch data for a hash and failed.
errExceedMaxRetries = errors.New("fetch failed after max retries for request")
errValidatorsNotSet = errors.New("validators not set")
)
// request contains all relevant Data for a single request for a specified hash.
type request struct {
ctx context.Context
hash types.Hash32 // hash is the hash of the Data requested
hint datastore.Hint // the hint from which database to fetch this hash
validator dataReceiver
promise *promise
retries int
}
type promise struct {
completed chan struct{}
err error
}
type batchInfo struct {
RequestBatch
peer p2p.Peer
}
// setID calculates the hash of all requests and sets it as this batches ID.
func (b *batchInfo) setID() {
bts, err := codec.EncodeSlice(b.Requests)
if err != nil {
return
}
b.ID = types.CalcHash32(bts)
}
func (b *batchInfo) toMap() map[types.Hash32]RequestMessage {
m := make(map[types.Hash32]RequestMessage)
for _, r := range b.Requests {
m[r.Hash] = r
}
return m
}
// Config is the configuration file of the Fetch component.
type Config struct {
BatchTimeout time.Duration // in milliseconds
MaxRetriesForPeer int
BatchSize, QueueSize int
RequestTimeout time.Duration // in seconds
MaxRetriesForRequest int
ServeNewProtocol bool `mapstructure:"serve-new-opn"`
}
// DefaultConfig is the default config for the fetch component.
func DefaultConfig() Config {
return Config{
BatchTimeout: time.Millisecond * time.Duration(50),
MaxRetriesForPeer: 2,
QueueSize: 20,
BatchSize: 20,
RequestTimeout: time.Second * time.Duration(10),
MaxRetriesForRequest: 100,
ServeNewProtocol: true,
}
}
// randomPeer returns a random peer from current peer list.
func randomPeer(peers []p2p.Peer) p2p.Peer {
return peers[rand.Intn(len(peers))]
}
// Option is a type to configure a fetcher.
type Option func(*Fetch)
// WithContext configures the shutdown context for the fetcher.
func WithContext(c context.Context) Option {
return func(f *Fetch) {
f.shutdownCtx, f.cancel = context.WithCancel(c)
}
}
// WithConfig configures the config for the fetcher.
func WithConfig(c Config) Option {
return func(f *Fetch) {
f.cfg = c
}
}
// WithLogger configures logger for the fetcher.
func WithLogger(log log.Log) Option {
return func(f *Fetch) {
f.logger = log
}
}
func withServers(s map[string]requester) Option {
return func(f *Fetch) {
f.servers = s
}
}
func withHost(h host) Option {
return func(f *Fetch) {
f.host = h
}
}
// Fetch is the main struct that contains network peers and logic to batch and dispatch hash fetch requests.
type Fetch struct {
cfg Config
logger log.Log
bs *datastore.BlobStore
host host
servers map[string]requester
validators *dataValidators
// unprocessed contains requests that are not processed
unprocessed map[types.Hash32]*request
// ongoing contains requests that have been processed and are waiting for responses
ongoing map[types.Hash32]*request
// batched contains batched ongoing requests.
batched map[types.Hash32]*batchInfo
batchTimeout *time.Ticker
mu sync.Mutex
onlyOnce sync.Once
hashToPeers *HashPeersCache
shutdownCtx context.Context
cancel context.CancelFunc
eg errgroup.Group
}
// NewFetch creates a new Fetch struct.
func NewFetch(cdb *datastore.CachedDB, msh meshProvider, b system.BeaconGetter, host *p2p.Host, opts ...Option) *Fetch {
bs := datastore.NewBlobStore(cdb.Database)
f := &Fetch{
cfg: DefaultConfig(),
logger: log.NewNop(),
bs: bs,
host: host,
servers: map[string]requester{},
unprocessed: make(map[types.Hash32]*request),
ongoing: make(map[types.Hash32]*request),
batched: make(map[types.Hash32]*batchInfo),
hashToPeers: NewHashPeersCache(cacheSize),
}
for _, opt := range opts {
opt(f)
}
f.batchTimeout = time.NewTicker(f.cfg.BatchTimeout)
srvOpts := []server.Opt{
server.WithTimeout(f.cfg.RequestTimeout),
server.WithLog(f.logger),
}
if len(f.servers) == 0 {
h := newHandler(cdb, bs, msh, b, f.cfg.ServeNewProtocol, f.logger)
f.servers[atxProtocol] = server.New(host, atxProtocol, h.handleEpochInfoReq, srvOpts...)
f.servers[lyrDataProtocol] = server.New(host, lyrDataProtocol, h.handleLayerDataReq, srvOpts...)
f.servers[lyrOpnsProtocol] = server.New(host, lyrOpnsProtocol, h.handleLayerOpinionsReq, srvOpts...)
f.servers[hashProtocol] = server.New(host, hashProtocol, h.handleHashReq, srvOpts...)
f.servers[meshHashProtocol] = server.New(host, meshHashProtocol, h.handleMeshHashReq, srvOpts...)
f.servers[malProtocol] = server.New(host, malProtocol, h.handleMaliciousIDsReq, srvOpts...)
if f.cfg.ServeNewProtocol {
f.servers[OpnProtocol] = server.New(host, OpnProtocol, h.handleLayerOpinionsReq2, srvOpts...)
}
}
return f
}
type dataValidators struct {
atx SyncValidator
poet SyncValidator
ballot SyncValidator
activeset SyncValidator
block SyncValidator
proposal SyncValidator
txBlock SyncValidator
txProposal SyncValidator
malfeasance SyncValidator
}
// SetValidators sets the handlers to validate various mesh data fetched from peers.
func (f *Fetch) SetValidators(
atx SyncValidator,
poet SyncValidator,
ballot SyncValidator,
activeset SyncValidator,
block SyncValidator,
prop SyncValidator,
txBlock SyncValidator,
txProposal SyncValidator,
mal SyncValidator,
) {
f.validators = &dataValidators{
atx: atx,
poet: poet,
ballot: ballot,
activeset: activeset,
block: block,
proposal: prop,
txBlock: txBlock,
txProposal: txProposal,
malfeasance: mal,
}
}
// Start starts handling fetch requests.
func (f *Fetch) Start() error {
if f.validators == nil {
return errValidatorsNotSet
}
f.onlyOnce.Do(func() {
f.eg.Go(func() error {
f.loop()
return nil
})
})
return nil
}
// Stop stops handling fetch requests.
func (f *Fetch) Stop() {
f.logger.Info("stopping fetch")
f.batchTimeout.Stop()
f.cancel()
if err := f.host.Close(); err != nil {
f.logger.With().Warning("error closing host", log.Err(err))
}
f.mu.Lock()
for _, req := range f.unprocessed {
close(req.promise.completed)
}
for _, req := range f.ongoing {
close(req.promise.completed)
}
f.mu.Unlock()
_ = f.eg.Wait()
f.logger.Info("stopped fetch")
}
// stopped returns if we should stop.
func (f *Fetch) stopped() bool {
select {
case <-f.shutdownCtx.Done():
return true
default:
return false
}
}
// here we receive all requests for hashes for all DBs and batch them together before we send the request to peer
// there can be a priority request that will not be batched.
func (f *Fetch) loop() {
f.logger.Info("starting fetch main loop")
for {
select {
case <-f.batchTimeout.C:
f.eg.Go(func() error {
f.requestHashBatchFromPeers() // Process the batch.
return nil
})
case <-f.shutdownCtx.Done():
return
}
}
}
// receive Data from message server and call response handlers accordingly.
func (f *Fetch) receiveResponse(data []byte) {
if f.stopped() {
return
}
var response ResponseBatch
if err := codec.Decode(data, &response); err != nil {
f.logger.With().Warning("failed to decode batch response", log.Err(err))
return
}
f.logger.With().Debug("received batch response",
log.Stringer("batch_hash", response.ID),
log.Int("num_hashes", len(response.Responses)),
)
f.mu.Lock()
batch, ok := f.batched[response.ID]
delete(f.batched, response.ID)
f.mu.Unlock()
if !ok {
f.logger.With().Warning("unknown batch response received, or already received",
log.Stringer("batch_hash", response.ID))
return
}
batchMap := batch.toMap()
// iterate all hash Responses
for _, resp := range response.Responses {
f.logger.With().Debug("received response for hash", log.Stringer("hash", resp.Hash))
f.mu.Lock()
req, ok := f.ongoing[resp.Hash]
f.mu.Unlock()
if !ok {
f.logger.With().Warning("response received for unknown hash",
log.Stringer("hash", resp.Hash))
continue
}
rsp := resp
f.eg.Go(func() error {
// validation fetch data recursively. offload to another goroutine
f.hashValidationDone(rsp.Hash, req.validator(req.ctx, rsp.Hash, batch.peer, rsp.Data))
return nil
})
delete(batchMap, resp.Hash)
}
// iterate all requests that didn't return value from peer and notify
// they will be retried for MaxRetriesForRequest
for h, r := range batchMap {
f.logger.With().Debug("hash not found in response from peer",
log.String("hint", string(r.Hint)),
log.Stringer("hash", h),
log.Stringer("peer", batch.peer),
)
f.failAfterRetry(r.Hash)
}
}
func (f *Fetch) hashValidationDone(hash types.Hash32, err error) {
f.mu.Lock()
defer f.mu.Unlock()
req, ok := f.ongoing[hash]
if !ok {
f.logger.With().Error("validation ran for unknown hash", log.Stringer("hash", hash))
return
}
if err != nil {
req.promise.err = err
} else {
f.logger.WithContext(req.ctx).With().Debug("hash request done",
log.Stringer("hash", hash))
}
close(req.promise.completed)
delete(f.ongoing, hash)
}
func (f *Fetch) failAfterRetry(hash types.Hash32) {
f.mu.Lock()
defer f.mu.Unlock()
req, ok := f.ongoing[hash]
if !ok {
f.logger.With().Error("hash missing from ongoing requests", log.Stringer("hash", hash))
return
}
// first check if we have it locally from gossips
if _, err := f.bs.Get(req.hint, hash.Bytes()); err == nil {
close(req.promise.completed)
delete(f.ongoing, hash)
return
}
req.retries++
if req.retries > f.cfg.MaxRetriesForRequest {
f.logger.WithContext(req.ctx).With().Warning("gave up on hash after max retries",
log.Stringer("hash", req.hash),
log.Int("retries", req.retries),
)
req.promise.err = errExceedMaxRetries
close(req.promise.completed)
} else {
// put the request back to the unprocessed list
f.unprocessed[req.hash] = req
}
delete(f.ongoing, hash)
}
// this is the main function that sends the hash request to the peer.
func (f *Fetch) requestHashBatchFromPeers() {
requestList := f.getUnprocessed()
f.send(requestList)
}
func (f *Fetch) getUnprocessed() []RequestMessage {
f.mu.Lock()
defer f.mu.Unlock()
var requestList []RequestMessage
// only send one request per hash
for hash, req := range f.unprocessed {
f.logger.WithContext(req.ctx).With().Debug("processing hash request", log.Stringer("hash", hash))
requestList = append(requestList, RequestMessage{Hash: hash, Hint: req.hint})
// move the processed requests to pending
f.ongoing[hash] = req
delete(f.unprocessed, hash)
}
return requestList
}
func (f *Fetch) send(requests []RequestMessage) {
if len(requests) == 0 {
return
}
if f.stopped() {
return
}
peer2batches := f.organizeRequests(requests)
for peer, peerBatches := range peer2batches {
for _, reqs := range peerBatches {
batch := &batchInfo{
RequestBatch: RequestBatch{
Requests: reqs,
},
peer: peer,
}
batch.setID()
_ = f.sendBatch(peer, batch)
}
}
}
func (f *Fetch) organizeRequests(requests []RequestMessage) map[p2p.Peer][][]RequestMessage {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
peer2requests := make(map[p2p.Peer][]RequestMessage)
peers := f.host.GetPeers()
if len(peers) == 0 {
f.logger.Info("cannot send batch: no peers found")
f.mu.Lock()
defer f.mu.Unlock()
errNoPeer := errors.New("no peers")
for _, msg := range requests {
if req, ok := f.ongoing[msg.Hash]; ok {
req.promise.err = errNoPeer
close(req.promise.completed)
delete(f.ongoing, req.hash)
} else {
f.logger.With().Error("ongoing request missing",
log.Stringer("hash", msg.Hash),
log.String("hint", string(msg.Hint)),
)
}
}
return nil
}
for _, req := range requests {
target := p2p.NoPeer
hashPeers := f.hashToPeers.GetRandom(req.Hash, req.Hint, rng)
for _, p := range hashPeers {
if f.host.Connected(p) {
target = p
break
}
}
if target == p2p.NoPeer {
target = randomPeer(peers)
}
_, ok := peer2requests[target]
if !ok {
peer2requests[target] = []RequestMessage{req}
} else {
peer2requests[target] = append(peer2requests[target], req)
}
}
// split every peer's requests into batches of f.cfg.BatchSize each
result := make(map[p2p.Peer][][]RequestMessage)
for peer, reqs := range peer2requests {
if len(reqs) < f.cfg.BatchSize {
result[peer] = [][]RequestMessage{
reqs,
}
continue
}
for i := 0; i < len(reqs); i += f.cfg.BatchSize {
j := i + f.cfg.BatchSize
if j > len(reqs) {
j = len(reqs)
}
result[peer] = append(result[peer], reqs[i:j])
}
}
return result
}
// sendBatch dispatches batched request messages to provided peer.
func (f *Fetch) sendBatch(p p2p.Peer, batch *batchInfo) error {
f.mu.Lock()
f.batched[batch.ID] = batch
f.mu.Unlock()
f.logger.With().Debug("sending batch request",
log.Stringer("batch_hash", batch.ID),
log.Stringer("peer", batch.peer))
// timeout function will be called if no response was received for the hashes sent
errorFunc := func(err error) {
f.logger.With().Warning("failed to send batch",
log.Stringer("batch_hash", batch.ID),
log.Err(err))
f.handleHashError(batch.ID, err)
}
bytes, err := codec.Encode(&batch.RequestBatch)
if err != nil {
f.logger.With().Panic("failed to encode batch", log.Err(err))
}
// try sending batch to provided peer
retries := 0
for {
if f.stopped() {
return nil
}
f.logger.With().Debug("sending batched request to peer",
log.Stringer("batch_hash", batch.ID),
log.Int("num_requests", len(batch.Requests)),
log.Stringer("peer", p))
err = f.servers[hashProtocol].Request(f.shutdownCtx, p, bytes, f.receiveResponse, errorFunc)
if err == nil {
break
}
retries++
if retries > f.cfg.MaxRetriesForPeer {
f.handleHashError(batch.ID, fmt.Errorf("batched request failed w retries: %w", err))
break
}
}
return err
}
// handleHashError is called when an error occurred processing batches of the following hashes.
func (f *Fetch) handleHashError(batchHash types.Hash32, err error) {
f.mu.Lock()
defer f.mu.Unlock()
f.logger.With().Debug("failed batch fetch", log.Stringer("batch_hash", batchHash), log.Err(err))
batch, ok := f.batched[batchHash]
if !ok {
f.logger.With().Error("batch not found", log.Stringer("batch_hash", batchHash))
return
}
for _, br := range batch.Requests {
req, ok := f.ongoing[br.Hash]
if !ok {
f.logger.With().Warning("hash missing from ongoing requests", log.Stringer("hash", br.Hash))
continue
}
f.logger.WithContext(req.ctx).With().Warning("hash request failed",
log.Stringer("hash", req.hash),
log.Err(err))
req.promise.err = err
peerErrors.WithLabelValues(string(req.hint)).Inc()
close(req.promise.completed)
delete(f.ongoing, req.hash)
}
delete(f.batched, batchHash)
}
// getHash is the regular buffered call to get a specific hash, using provided hash, h as hint the receiving end will
// know where to look for the hash, this function returns HashDataPromiseResult channel that will hold Data received or error.
func (f *Fetch) getHash(ctx context.Context, hash types.Hash32, h datastore.Hint, receiver dataReceiver) (*promise, error) {
if f.stopped() {
return nil, f.shutdownCtx.Err()
}
// check if we already have this hash locally
if _, err := f.bs.Get(h, hash.Bytes()); err == nil {
return nil, nil
}
f.mu.Lock()
defer f.mu.Unlock()
if _, ok := f.ongoing[hash]; ok {
f.logger.WithContext(ctx).With().Debug("request ongoing", log.Stringer("hash", hash))
return f.ongoing[hash].promise, nil
}
if _, ok := f.unprocessed[hash]; !ok {
f.unprocessed[hash] = &request{
ctx: ctx,
hash: hash,
hint: h,
validator: receiver,
promise: &promise{
completed: make(chan struct{}, 1),
},
}
f.logger.WithContext(ctx).With().Debug("hash request added to queue",
log.Stringer("hash", hash),
log.Int("queued", len(f.unprocessed)))
} else {
f.logger.WithContext(ctx).With().Debug("hash request already in queue",
log.Stringer("hash", hash),
log.Int("retries", f.unprocessed[hash].retries),
log.Int("queued", len(f.unprocessed)))
}
if len(f.unprocessed) >= f.cfg.QueueSize {
f.eg.Go(func() error {
f.requestHashBatchFromPeers() // Process the batch.
return nil
})
}
return f.unprocessed[hash].promise, nil
}
// RegisterPeerHashes registers provided peer for a list of hashes.
func (f *Fetch) RegisterPeerHashes(peer p2p.Peer, hashes []types.Hash32) {
if peer == f.host.ID() {
return
}
f.hashToPeers.RegisterPeerHashes(peer, hashes)
}
func (f *Fetch) GetPeers() []p2p.Peer {
return f.host.GetPeers()
}
func (f *Fetch) PeerProtocols(p p2p.Peer) ([]protocol.ID, error) {
return f.host.PeerProtocols(p)
}