-
Notifications
You must be signed in to change notification settings - Fork 5
/
subscriber.go
1085 lines (949 loc) · 33.2 KB
/
subscriber.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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dagsync
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/gammazero/channelqueue"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
"github.com/ipni/go-libipni/announce"
"github.com/ipni/go-libipni/dagsync/ipnisync"
"github.com/ipni/go-libipni/mautil"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
"github.com/multiformats/go-multiaddr"
)
var log = logging.Logger("dagsync")
const (
tempAddrTTL = 24 * time.Hour // must be long enough for ad chain to sync
)
// BlockHookFunc is the signature of a function that is called when a received.
type BlockHookFunc func(peer.ID, cid.Cid, SegmentSyncActions)
// Subscriber reads chains of advertisements from index-providers (publishers)
// and keeps track of the latest advertisement read from each publisher.
// Advertisements are read when explicitly requested, or in response to
// announcement messages if an announcement receiver is configured.
//
// An announcement receiver can receive announcements that are broadcast over
// libp2p gossip pubsub, and sent directly over HTTP. A receiver can be given
// an optional externally-defined function to determines whether to allow or
// deny messages from specific peers.
//
// A stateful message handler is maintained for each advertisement source peer.
// Messages from separate peers are handled concurrently, and multiple messages
// from the same peer are handled serially. If a handler is busy handling a
// message, and more messages arrive from the same peer, then the last message
// replaces the previous unhandled message to avoid having to maintain queues
// of messages. Handlers do not have persistent goroutines, but start a new
// goroutine to handle a single message.
type Subscriber struct {
host host.Host
addrTTL time.Duration
handlers map[peer.ID]*handler
handlersMutex sync.Mutex
// A map of block hooks to call for a specific peer id if the
// generalBlockHook is overridden within a sync via ScopedBlockHook sync
// option.
scopedBlockHook map[peer.ID]func(peer.ID, cid.Cid)
scopedBlockHookMutex *sync.RWMutex
generalBlockHook BlockHookFunc
// inEvents is used to send a SyncFinished from a peer handler to the
// distributeEvents goroutine.
inEvents chan SyncFinished
addEventChan chan chan<- SyncFinished
rmEventChan chan chan<- SyncFinished
// closing signals that the Subscriber is closing.
closing chan struct{}
// closeOnce ensures that the Close only happens once.
closeOnce sync.Once
// watchDone signals that the watch function exited.
watchDone chan struct{}
asyncWG sync.WaitGroup
ipniSync *ipnisync.Sync
// A separate peerstore is used to store HTTP addresses. This is necessary
// when peers have both libp2p and HTTP addresses, and a sync is requested
// over a libp2p transport. Since libp2p transports do not use an explicit
// multiaddr and depend on the libp2p peerstore, the HTTP addresses cannot
// be stored in the libp2p peerstore as those are not usable by the libp2p
// transport.
httpPeerstore peerstore.Peerstore
idleHandlerTTL time.Duration
latestSyncHandler latestSyncHandler
lastKnownSync LastKnownSyncFunc
// syncSem is a counting semaphore that limits the number of concurrent
// async syncs.
syncSem chan struct{}
adsDepthLimit selector.RecursionLimit
firstSyncDepth int64
segDepthLimit int64
receiver *announce.Receiver
// Track explicit Sync calls in progress and allow them to complete before
// closing subscriber.
expSyncClosed bool
expSyncMutex sync.Mutex
expSyncWG sync.WaitGroup
// selector sequence for advertisements
//
// This selector sequence is wrapped with selector logic that stops
// traversal when the latest synced link is reached. So, this only
// specifies the selection sequence itself.
adsSelectorSeq ipld.Node
// selectorOne selects one multihash entries or HAMT block.
selectorOne ipld.Node
// selectorAll selects all multihash HAMT blocks.
selectorAll ipld.Node
// selectEnts selects multihash entries blocks up to depth limit.
selectorEnts ipld.Node
}
// SyncFinished notifies an OnSyncFinished reader that a specified peer
// completed a sync. The channel receives events from providers that are
// manually synced to the latest, as well as those auto-discovered.
type SyncFinished struct {
// Cid is the CID identifying the link that finished and is now the latest
// sync for a specific peer.
Cid cid.Cid
// PeerID identifies the peer this SyncFinished event pertains to. This is
// the publisher of the advertisement chain.
PeerID peer.ID
// Count is the number of CIDs synced.
Count int
// Err is used to return a failure to complete an asynchronous sync in
// response to an announcement.
Err error
}
// handler holds state that is specific to a peer
type handler struct {
subscriber *Subscriber
// asyncMutex serializes the handling of individual asymchronous as chain
// syncs started by announce message. It protects the starting of new async
// goroutines and the counting of running async syncs.
asyncMutex sync.Mutex
// syncMutex serializes the handling of individual syncs. This should only
// guard the actual handling of a sync, nothing else.
syncMutex sync.Mutex
// peerID is the ID of the peer this handler is responsible for. This is
// the publisher of an advertisement chain.
peerID peer.ID
// pendingMsg is an announce message queued for async handling.
pendingMsg atomic.Pointer[announce.Announce]
// expires is the time the handler is removed if it remains idle.
expires time.Time
// syncer is a sync client for this handler's peer.
syncer Syncer
}
// wrapBlockHook wraps a possibly nil block hook func to allow dispatching to a
// blockhook func that is scoped within a .Sync call.
func wrapBlockHook() (*sync.RWMutex, map[peer.ID]func(peer.ID, cid.Cid), func(peer.ID, cid.Cid)) {
var scopedBlockHookMutex sync.RWMutex
scopedBlockHook := make(map[peer.ID]func(peer.ID, cid.Cid))
return &scopedBlockHookMutex, scopedBlockHook, func(peerID peer.ID, cid cid.Cid) {
scopedBlockHookMutex.RLock()
f, ok := scopedBlockHook[peerID]
scopedBlockHookMutex.RUnlock()
if ok {
f(peerID, cid)
}
}
}
// NewSubscriber creates a new Subscriber that processes pubsub messages and
// syncs dags advertised using the specified selector.
func NewSubscriber(host host.Host, lsys ipld.LinkSystem, options ...Option) (*Subscriber, error) {
opts, err := getOpts(options)
if err != nil {
return nil, err
}
scopedBlockHookMutex, scopedBlockHook, blockHook := wrapBlockHook()
httpPeerstore, err := pstoremem.NewPeerstore()
if err != nil {
return nil, err
}
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
all := ssb.ExploreAll(ssb.ExploreRecursiveEdge())
ipniSync := ipnisync.NewSync(lsys, blockHook,
ipnisync.ClientStreamHost(host),
ipnisync.ClientHTTPTimeout(opts.httpTimeout),
ipnisync.ClientHTTPRetry(opts.httpRetryMax, opts.httpRetryWaitMin, opts.httpRetryWaitMax))
s := &Subscriber{
host: host,
addrTTL: opts.addrTTL,
closing: make(chan struct{}),
handlers: make(map[peer.ID]*handler),
inEvents: make(chan SyncFinished, 1),
addEventChan: make(chan chan<- SyncFinished),
rmEventChan: make(chan chan<- SyncFinished),
ipniSync: ipniSync,
httpPeerstore: httpPeerstore,
scopedBlockHookMutex: scopedBlockHookMutex,
scopedBlockHook: scopedBlockHook,
generalBlockHook: opts.blockHook,
idleHandlerTTL: opts.idleHandlerTTL,
latestSyncHandler: latestSyncHandler{},
lastKnownSync: opts.lastKnownSync,
adsDepthLimit: recursionLimit(opts.adsDepthLimit),
firstSyncDepth: opts.firstSyncDepth,
segDepthLimit: opts.segDepthLimit,
selectorOne: ssb.ExploreRecursive(selector.RecursionLimitDepth(0), all).Node(),
selectorAll: ssb.ExploreRecursive(selector.RecursionLimitNone(), all).Node(),
selectorEnts: ssb.ExploreRecursive(recursionLimit(opts.entriesDepthLimit),
ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
efsb.Insert("Next", ssb.ExploreRecursiveEdge()) // Next field in EntryChunk
})).Node(),
}
if opts.strictAdsSelSeq {
s.adsSelectorSeq = ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
efsb.Insert("PreviousID", ssb.ExploreRecursiveEdge())
}).Node()
} else {
s.adsSelectorSeq = ssb.ExploreAll(ssb.ExploreRecursiveEdge()).Node()
}
if opts.hasRcvr {
if opts.maxAsyncSyncs > 0 {
s.syncSem = make(chan struct{}, opts.maxAsyncSyncs)
}
s.receiver, err = announce.NewReceiver(host, opts.rcvrTopic, opts.rcvrOpts...)
if err != nil {
return nil, fmt.Errorf("failed to create announcement receiver: %w", err)
}
s.watchDone = make(chan struct{})
// Start watcher to read announce messages.
go s.watch()
}
// Start distributor to send SyncFinished messages to interested parties.
go s.distributeEvents()
// Start goroutine to remove idle publisher handlers.
go s.idleHandlerCleaner()
return s, nil
}
// HttpPeerStore returns the subscriber's HTTP peer store.
func (s *Subscriber) HttpPeerStore() peerstore.Peerstore {
return s.httpPeerstore
}
// GetLatestSync returns the latest synced CID for the specified peer.
func (s *Subscriber) GetLatestSync(peerID peer.ID) ipld.Link {
c, ok := s.latestSyncHandler.getLatestSync(peerID)
if ok && c != cid.Undef {
return cidlink.Link{Cid: c}
}
if s.lastKnownSync != nil {
c, ok = s.lastKnownSync(peerID)
if ok && c != cid.Undef {
s.latestSyncHandler.setLatestSync(peerID, c)
return cidlink.Link{Cid: c}
}
}
return nil
}
// SetLatestSync sets the latest synced CID for a specified peer.
func (s *Subscriber) SetLatestSync(peerID peer.ID, latestSync cid.Cid) error {
if latestSync == cid.Undef {
return errors.New("cannot set latest sync to undefined value")
}
s.latestSyncHandler.setLatestSync(peerID, latestSync)
return nil
}
// Close shuts down the Subscriber.
func (s *Subscriber) Close() error {
var err error
s.closeOnce.Do(func() {
err = s.doClose()
})
return err
}
func (s *Subscriber) doClose() error {
// Cancel idle handler cleaner.
close(s.closing)
// Block any additional explicit Sync calls.
s.expSyncMutex.Lock()
s.expSyncClosed = true
s.expSyncMutex.Unlock()
// Wait for explicit Syncs calls to finish.
s.expSyncWG.Wait()
var err error
if s.receiver != nil {
// Close receiver and wait for watch to exit.
err = s.receiver.Close()
if err != nil {
err = fmt.Errorf("error closing receiver: %w", err)
}
<-s.watchDone
}
// Wait for any syncs to complete.
s.asyncWG.Wait()
// Stop the distribution goroutine.
close(s.inEvents)
s.httpPeerstore.Close()
return err
}
// OnSyncFinished creates a channel that receives change notifications, and
// adds that channel to the list of notification channels.
//
// Calling the returned cancel function removes the notification channel from
// the list of channels to be notified on changes, and it closes the channel to
// allow any reading goroutines to stop waiting on the channel.
func (s *Subscriber) OnSyncFinished() (<-chan SyncFinished, context.CancelFunc) {
// Channel is buffered to prevent distribute() from blocking if a reader is
// not reading the channel immediately.
cq := channelqueue.New[SyncFinished](-1)
ch := cq.In()
s.addEventChan <- ch
cncl := func() {
if ch == nil {
return
}
select {
case s.rmEventChan <- ch:
case <-s.closing:
}
ch = nil
}
log.Debug("Subscriber OnSyncFinished configured")
return cq.Out(), cncl
}
// RemoveHandler removes a handler for a publisher.
func (s *Subscriber) RemoveHandler(peerID peer.ID) bool {
s.handlersMutex.Lock()
defer s.handlersMutex.Unlock()
// Check for existing handler, remove if found.
if _, ok := s.handlers[peerID]; !ok {
return false
}
log.Infow("Removing sync handler", "peer", peerID)
delete(s.handlers, peerID)
return true
}
// SyncAdChain performs a one-off explicit sync with the given peer (publisher)
// for an advertisement chain, and updates the latest synced link to it.
//
// The latest root CID is queried from the peer directly. In the event where
// there is no latest root, i.e. querying the latest CID returns cid.Undef,
// this function returns cid.Undef with nil error.
//
// The latest synced CID is returned when this sync is complete. Any
// OnSyncFinished readers will also get a SyncFinished when the sync succeeds.
//
// It is the responsibility of the caller to make sure the given CID appears
// after the latest sync in order to avid re-syncing of content that may have
// previously been synced.
func (s *Subscriber) SyncAdChain(ctx context.Context, peerInfo peer.AddrInfo, options ...SyncOption) (cid.Cid, error) {
s.expSyncMutex.Lock()
if s.expSyncClosed {
s.expSyncMutex.Unlock()
return cid.Undef, errors.New("shutdown")
}
s.expSyncWG.Add(1)
s.expSyncMutex.Unlock()
defer s.expSyncWG.Done()
opts := getSyncOpts(options)
if opts.blockHook == nil {
opts.blockHook = s.generalBlockHook
}
peerInfo = mautil.CleanPeerAddrInfo(peerInfo)
var err error
peerInfo, err = removeIDFromAddrs(peerInfo)
if err != nil {
return cid.Undef, err
}
log := log.With("peer", peerInfo.ID)
hnd := s.getOrCreateHandler(peerInfo.ID)
syncer, updatePeerstore, err := hnd.makeSyncer(peerInfo, true)
if err != nil {
return cid.Undef, err
}
// Set depth limit to ads depth limit unless scoped depth is non-zero.
depthLimit := s.adsDepthLimit
if opts.depthLimit != 0 {
depthLimit = recursionLimit(opts.depthLimit)
}
// If traversal should not stop at the latest synced, then construct
// advertisement chain selector to behave accordingly.
var stopLnk ipld.Link
if opts.resync {
if opts.stopAdCid != cid.Undef {
stopLnk = cidlink.Link{Cid: opts.stopAdCid}
}
} else {
if opts.stopAdCid != cid.Undef {
stopLnk = cidlink.Link{Cid: opts.stopAdCid}
} else {
stopLnk = s.GetLatestSync(peerInfo.ID)
}
}
var updateLatest bool
nextCid := opts.headAdCid
if nextCid == cid.Undef {
// Query the peer for the latest CID
nextCid, err = syncer.GetHead(ctx)
if err != nil {
return cid.Undef, fmt.Errorf("cannot query head for sync: %w", err)
}
// Check if there is a latest CID.
if nextCid == cid.Undef {
// There is no head; nothing to sync.
log.Info("No head to sync")
return cid.Undef, nil
}
// Update the latest unless a specific CID to sync was given.
updateLatest = true
}
var stopAtCid cid.Cid
if stopLnk != nil {
stopAtCid = stopLnk.(cidlink.Link).Cid
if stopAtCid == nextCid {
log.Infow("cid to sync to is the stop node. Nothing to do")
return nextCid, nil
}
} else if s.firstSyncDepth != 0 && opts.depthLimit == 0 {
depthLimit = recursionLimit(s.firstSyncDepth)
}
log = log.With("cid", nextCid)
log.Debug("Start advertisement chain sync")
if ctx.Err() != nil {
return cid.Undef, fmt.Errorf("sync canceled: %w", ctx.Err())
}
segdl := s.segDepthLimit
if opts.segDepthLimit != 0 {
segdl = opts.segDepthLimit
}
sel := ExploreRecursiveWithStopNode(depthLimit, s.adsSelectorSeq, stopLnk)
syncCount, err := hnd.handle(ctx, nextCid, sel, syncer, opts.blockHook, segdl, stopAtCid)
if err != nil {
return cid.Undef, fmt.Errorf("sync handler failed: %w", err)
}
// The sync succeeded, so remember this address in the appropriate
// peerstore. Add to peerstore before sending the SyncFinished event so
// that the address is present before anything triggered by the event is
// run.
updatePeerstore()
if updateLatest {
hnd.sendSyncFinishedEvent(nextCid, syncCount)
}
return nextCid, nil
}
func (s *Subscriber) SyncOneEntry(ctx context.Context, peerInfo peer.AddrInfo, entCid cid.Cid) error {
return s.syncEntries(ctx, peerInfo, entCid, s.selectorOne, s.generalBlockHook, -1)
}
// SyncEntries syncs the entries chain starting with block identified by entCid.
func (s *Subscriber) SyncEntries(ctx context.Context, peerInfo peer.AddrInfo, entCid cid.Cid, options ...SyncOption) error {
opts := getSyncOpts(options)
if opts.blockHook == nil {
opts.blockHook = s.generalBlockHook
}
// If a scoped depth limit is specified, then create a new entries selector
// for that depth limit. Otherwise, use the existing entries selector that
// has the entries depth limit built in.
selectorEnts := s.selectorEnts
if opts.depthLimit != 0 {
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
selectorEnts = ssb.ExploreRecursive(recursionLimit(opts.depthLimit),
ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
efsb.Insert("Next", ssb.ExploreRecursiveEdge())
})).Node()
}
return s.syncEntries(ctx, peerInfo, entCid, selectorEnts, opts.blockHook, s.segDepthLimit)
}
func (s *Subscriber) SyncHAMTEntries(ctx context.Context, peerInfo peer.AddrInfo, entCid cid.Cid, options ...SyncOption) error {
opts := getSyncOpts(options)
if opts.blockHook == nil {
opts.blockHook = s.generalBlockHook
}
return s.syncEntries(ctx, peerInfo, entCid, s.selectorAll, opts.blockHook, -1)
}
func (s *Subscriber) syncEntries(ctx context.Context, peerInfo peer.AddrInfo, entCid cid.Cid, sel ipld.Node, bh BlockHookFunc, segdl int64) error {
if entCid == cid.Undef {
log.Infow("No entries to sync", "peer", peerInfo.ID)
return nil
}
s.expSyncMutex.Lock()
if s.expSyncClosed {
s.expSyncMutex.Unlock()
return errors.New("shutdown")
}
s.expSyncWG.Add(1)
s.expSyncMutex.Unlock()
defer s.expSyncWG.Done()
peerInfo = mautil.CleanPeerAddrInfo(peerInfo)
peerInfo, err := removeIDFromAddrs(peerInfo)
if err != nil {
return err
}
hnd := s.getOrCreateHandler(peerInfo.ID)
syncer, _, err := hnd.makeSyncer(peerInfo, false)
if err != nil {
return err
}
log.Debugw("Start entries sync", "peer", peerInfo.ID, "cid", entCid)
_, err = hnd.handle(ctx, entCid, sel, syncer, bh, segdl, cid.Undef)
if err != nil {
return fmt.Errorf("sync handler failed: %w", err)
}
return nil
}
func removeIDFromAddrs(peerInfo peer.AddrInfo) (peer.AddrInfo, error) {
// Chop off the p2p ID from any of the addresses.
for i, peerAddr := range peerInfo.Addrs {
peerAddr, pid := peer.SplitAddr(peerAddr)
if pid != "" {
peerInfo.Addrs[i] = peerAddr
if peerInfo.ID == "" {
peerInfo.ID = pid
}
}
}
if peerInfo.ID == "" {
return peerInfo, errors.New("empty peer id")
}
return peerInfo, nil
}
// distributeEvents reads a SyncFinished, sent by a peer handler, and copies
// the even to all channels in outEventsChans. This delivers the SyncFinished
// to all OnSyncFinished channel readers.
func (s *Subscriber) distributeEvents() {
var outEventsChans []chan<- SyncFinished
for {
select {
case event, ok := <-s.inEvents:
if !ok {
// Dismiss any event readers.
for _, ch := range outEventsChans {
close(ch)
}
return
}
// Send update to all change notification channels.
for _, ch := range outEventsChans {
ch <- event
}
case ch := <-s.addEventChan:
outEventsChans = append(outEventsChans, ch)
case ch := <-s.rmEventChan:
for i, ca := range outEventsChans {
if ca == ch {
outEventsChans[i] = outEventsChans[len(outEventsChans)-1]
outEventsChans[len(outEventsChans)-1] = nil
outEventsChans = outEventsChans[:len(outEventsChans)-1]
close(ch)
break
}
}
}
}
}
// getOrCreateHandler returns an existing handler or creates a new one for the
// specified peer (publisher).
func (s *Subscriber) getOrCreateHandler(peerID peer.ID) *handler {
expires := time.Now().Add(s.idleHandlerTTL)
s.handlersMutex.Lock()
defer s.handlersMutex.Unlock()
// Check for existing handler, return if found.
hnd, ok := s.handlers[peerID]
if ok {
hnd.expires = expires
} else {
hnd = &handler{
subscriber: s,
peerID: peerID,
expires: expires,
}
s.handlers[peerID] = hnd
}
return hnd
}
// idleHandlerCleaner periodically looks for idle handlers to remove. This
// prevents accumulation of handlers that are no longer in use.
func (s *Subscriber) idleHandlerCleaner() {
t := time.NewTimer(s.idleHandlerTTL)
for {
select {
case now := <-t.C:
s.handlersMutex.Lock()
for pid, hnd := range s.handlers {
if now.After(hnd.expires) {
delete(s.handlers, pid)
log.Debugw("Removed idle handler", "peer", pid)
}
}
s.handlersMutex.Unlock()
t.Reset(s.idleHandlerTTL)
case <-s.closing:
t.Stop()
return
}
}
}
// watch fetches announce messages from the Reciever.
func (s *Subscriber) watch() {
defer close(s.watchDone)
// Cancel any pending messages if this function exits.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for {
amsg, err := s.receiver.Next(context.Background())
if err != nil {
// This is a normal result of shutting down the Receiver.
log.Infow("Done handling announce messages", "reason", err)
break
}
hnd := s.getOrCreateHandler(amsg.PeerID)
// Set the message to be handled by the waiting goroutine.
oldMsg := hnd.pendingMsg.Swap(&amsg)
// If rhw previous pending message was not nil, then there is an
// existing request to sync the ad chain.
if oldMsg != nil {
log.Infow("Pending announce replaced by new", "previous_cid", oldMsg.Cid, "new_cid", amsg.Cid, "peer", hnd.peerID)
continue
}
// If old message is nil, then any previous message is already handled.
// Start a new goroutine to handle this message.
s.asyncWG.Add(1)
go func() {
// Wait for any previous asyncSyncAdChain to finish before removing the
// latest pending messaged and reducing the available items in the sync
// semaphore.
hnd.asyncMutex.Lock()
defer hnd.asyncMutex.Unlock()
if s.syncSem != nil {
select {
case s.syncSem <- struct{}{}:
defer func() {
<-s.syncSem
}()
case <-ctx.Done():
}
}
hnd.asyncSyncAdChain(ctx)
s.asyncWG.Done()
}()
}
}
// Announce handles a direct announce message, that was not received over
// pubsub. The message is resent over pubsub, if the Receiver is configured to
// do so. The peerID and addrs are those of the advertisement publisher, since
// an announce message announces the availability of an advertisement and where
// to retrieve it from.
func (s *Subscriber) Announce(ctx context.Context, nextCid cid.Cid, peerInfo peer.AddrInfo) error {
if s.receiver == nil {
return nil
}
return s.receiver.Direct(ctx, nextCid, peerInfo)
}
// delNotPresent removes from the peerStore the peer's multiaddrs that are not
// present in the specified addrs slice.
func delNotPresent(peerStore peerstore.Peerstore, peerID peer.ID, addrs []multiaddr.Multiaddr) {
var del []multiaddr.Multiaddr
oldAddrs := peerStore.Addrs(peerID)
for _, old := range oldAddrs {
keep := false
for _, new := range addrs {
if old.Equal(new) {
keep = true
break
}
}
if !keep {
del = append(del, old)
}
}
if len(del) != 0 {
peerStore.SetAddrs(peerID, del, 0)
}
}
func (h *handler) makeSyncer(peerInfo peer.AddrInfo, doUpdate bool) (Syncer, func(), error) {
s := h.subscriber
// Check for an HTTP address in peerAddrs, or if not given, in the http
// peerstore.
var httpAddrs []multiaddr.Multiaddr
if len(peerInfo.Addrs) == 0 {
httpAddrs = s.httpPeerstore.Addrs(peerInfo.ID)
} else {
httpAddrs = mautil.FindHTTPAddrs(peerInfo.Addrs)
if doUpdate && len(httpAddrs) != 0 {
delNotPresent(s.httpPeerstore, peerInfo.ID, httpAddrs)
}
}
var update func()
if len(httpAddrs) != 0 {
// Store this http address so that future calls to sync will work without a
// peerAddr (given that it happens within the TTL)
s.httpPeerstore.AddAddrs(peerInfo.ID, httpAddrs, tempAddrTTL)
httpPeerInfo := peer.AddrInfo{
ID: peerInfo.ID,
Addrs: httpAddrs,
}
if h.syncer == nil || !h.syncer.SameAddrs(httpAddrs) {
syncer, err := s.ipniSync.NewSyncer(httpPeerInfo)
if err != nil {
return nil, nil, fmt.Errorf("cannot create ipni-sync handler: %w", err)
}
h.syncer = syncer
}
if doUpdate {
update = func() {
// Store http address so that future calls to sync will work
// without a peerAddr (given that it happens within the TTL)
s.httpPeerstore.AddAddrs(peerInfo.ID, httpAddrs, s.addrTTL)
}
}
return h.syncer, update, nil
}
if doUpdate {
peerStore := s.host.Peerstore()
if peerStore != nil && len(peerInfo.Addrs) != 0 {
delNotPresent(peerStore, peerInfo.ID, peerInfo.Addrs)
// Add it to peerstore with a small TTL first, and extend it if/when
// sync with it completes. In case the peerstore already has this
// address and the existing TTL is greater than this temp one, this is
// a no-op. In other words, the TTL is never decreased here.
peerStore.AddAddrs(peerInfo.ID, peerInfo.Addrs, tempAddrTTL)
update = func() {
peerStore.AddAddrs(peerInfo.ID, peerInfo.Addrs, s.addrTTL)
}
} else {
update = func() {}
}
}
if h.syncer == nil || !h.syncer.SameAddrs(peerInfo.Addrs) {
syncer, err := s.ipniSync.NewSyncer(peerInfo)
if err != nil {
if errors.Is(err, ipnisync.ErrNoHTTPServer) {
err = fmt.Errorf("data-transfer sync is not supported: %w", err)
}
return nil, nil, fmt.Errorf("cannot create ipni-sync handler: %w", err)
}
h.syncer = syncer
}
return h.syncer, update, nil
}
// asyncSyncAdChain processes the latest announce message received over pubsub
// or HTTP. This functions runs in an independent goroutine, with no more than
// one goroutine per advertisement publisher.
func (h *handler) asyncSyncAdChain(ctx context.Context) {
if ctx.Err() != nil {
log.Warnw("Abandoned pending sync", "err", ctx.Err(), "peer", h.peerID)
return
}
// Get the latest pending message.
amsg := h.pendingMsg.Swap(nil)
adsDepthLimit := h.subscriber.adsDepthLimit
nextCid := amsg.Cid
latestSyncLink := h.subscriber.GetLatestSync(h.peerID)
var stopAtCid cid.Cid
if latestSyncLink != nil {
stopAtCid = latestSyncLink.(cidlink.Link).Cid
if stopAtCid == nextCid {
log.Infow("CID to sync to is the stop node. Nothing to do.", "peer", h.peerID)
return
}
} else if h.subscriber.firstSyncDepth != 0 {
// If nothing synced yet, use first sync depth if configured.
adsDepthLimit = recursionLimit(h.subscriber.firstSyncDepth)
}
peerInfo := peer.AddrInfo{
ID: amsg.PeerID,
Addrs: amsg.Addrs,
}
syncer, updatePeerstore, err := h.makeSyncer(peerInfo, true)
if err != nil {
log.Errorw("Cannot make syncer for announce", "err", err, "peer", h.peerID)
return
}
sel := ExploreRecursiveWithStopNode(adsDepthLimit, h.subscriber.adsSelectorSeq, latestSyncLink)
syncCount, err := h.handle(ctx, nextCid, sel, syncer, h.subscriber.generalBlockHook, h.subscriber.segDepthLimit, stopAtCid)
if err != nil {
// Failed to handle the sync, so allow another announce for the same CID.
if h.subscriber.receiver != nil {
h.subscriber.receiver.UncacheCid(nextCid)
}
log.Errorw("Cannot process message", "err", err, "peer", h.peerID)
h.subscriber.inEvents <- SyncFinished{
Cid: nextCid,
PeerID: h.peerID,
Err: err,
}
return
}
updatePeerstore()
h.sendSyncFinishedEvent(nextCid, syncCount)
}
var _ SegmentSyncActions = (*segmentedSync)(nil)
type (
// SegmentSyncActions allows the user to control the flow of segmented sync
// by either choosing which CID should be synced in the next sync cycle or
// setting the error that should mark the sync as failed.
SegmentSyncActions interface {
// SetNextSyncCid sets the cid that will be synced in the next
// segmented sync. Note that the last call to this function during a
// segmented sync cycle dictates which CID will be synced in the next
// cycle.
//
// At least one call to this function must be made for the segmented
// sync cycles to continue. Because, otherwise the CID that should be
// used in the next segmented sync cycle cannot be known.
//
// If no calls are made to this function or next CID is set to
// cid.Undef, the sync will terminate and any CIDs that are synced so
// far will be included in a SyncFinished event.
SetNextSyncCid(cid.Cid)
// FailSync fails the sync and returns the given error as soon as the
// current segment sync finishes. The last call to this function during
// a segmented sync cycle dictates the error value. Passing nil as
// error will cancel sync failure.
FailSync(error)
}
// SegmentBlockHookFunc is called for each synced block, similarly to
// BlockHookFunc. Except that it provides SegmentSyncActions to the hook
// allowing the user to control the flow of segmented sync by determining
// which CID should be used in the next segmented sync cycle by decoding
// the synced block.
//
// SegmentSyncActions also allows the user to signal any errors that may
// occur during the hook execution to terminate the sync and mark it as
// failed.
SegmentBlockHookFunc func(peer.ID, cid.Cid, SegmentSyncActions)
segmentedSync struct {
nextSyncCid *cid.Cid
err error
}
)
// SetNextSyncCid sets the CID that will be synced in the next segmented sync.
func (ss *segmentedSync) SetNextSyncCid(c cid.Cid) {
ss.nextSyncCid = &c
}
// FailSync fails the sync and returns the given error when the current segment
// sync finishes.
func (ss *segmentedSync) FailSync(err error) {
ss.err = err
}
func (ss *segmentedSync) reset() {
ss.nextSyncCid = nil
ss.err = nil
}
func (h *handler) sendSyncFinishedEvent(c cid.Cid, count int) {
h.subscriber.latestSyncHandler.setLatestSync(h.peerID, c)
h.subscriber.inEvents <- SyncFinished{
Cid: c,
PeerID: h.peerID,
Count: count,
}
}
// handle processes a message from the peer that the handler is responsible for.
func (h *handler) handle(ctx context.Context, nextCid cid.Cid, sel ipld.Node, syncer Syncer, bh BlockHookFunc, segdl int64, stopAtCid cid.Cid) (int, error) {
log := log.With("cid", nextCid, "peer", h.peerID)
segSync := &segmentedSync{
nextSyncCid: &nextCid,
}
var syncedCount int
hook := func(p peer.ID, c cid.Cid) {
syncedCount++
if bh != nil {
bh(p, c, segSync)
}
}
// Wait for any previous sync for this peer ID to finish. This is necessary
// to protect the scopedBlockHook map from having having another hook
// mapped to this peer ID.
h.syncMutex.Lock()
h.subscriber.scopedBlockHookMutex.Lock()
h.subscriber.scopedBlockHook[h.peerID] = hook
h.subscriber.scopedBlockHookMutex.Unlock()
defer func() {
h.subscriber.scopedBlockHookMutex.Lock()
delete(h.subscriber.scopedBlockHook, h.peerID)
h.subscriber.scopedBlockHookMutex.Unlock()
h.syncMutex.Unlock()
}()
var syncBySegment bool
var origLimit selector.RecursionLimit
// Only attempt to detect recursion limit in original selector if maximum
// segment depth is larger than zero and there is a block hook set; either
// general or scoped.
//
// Not that we need at least one block hook to let the caller decide which
// CID to sync in next segment. Therefore, it has to be set for segmented
// sync to function correctly.