This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathsession.go
508 lines (445 loc) · 15.2 KB
/
session.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
package session
import (
"context"
"time"
"github.com/ipfs/go-bitswap/client/internal"
bsbpm "github.com/ipfs/go-bitswap/client/internal/blockpresencemanager"
bsgetter "github.com/ipfs/go-bitswap/client/internal/getter"
notifications "github.com/ipfs/go-bitswap/client/internal/notifications"
bspm "github.com/ipfs/go-bitswap/client/internal/peermanager"
bssim "github.com/ipfs/go-bitswap/client/internal/sessioninterestmanager"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
delay "github.com/ipfs/go-ipfs-delay"
logging "github.com/ipfs/go-log"
peer "github.com/libp2p/go-libp2p/core/peer"
"go.uber.org/zap"
)
var log = logging.Logger("bs:sess")
var sflog = log.Desugar()
const (
broadcastLiveWantsLimit = 64
)
// PeerManager keeps track of which sessions are interested in which peers
// and takes care of sending wants for the sessions
type PeerManager interface {
// RegisterSession tells the PeerManager that the session is interested
// in a peer's connection state
RegisterSession(peer.ID, bspm.Session)
// UnregisterSession tells the PeerManager that the session is no longer
// interested in a peer's connection state
UnregisterSession(uint64)
// SendWants tells the PeerManager to send wants to the given peer
SendWants(ctx context.Context, peerId peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid)
// BroadcastWantHaves sends want-haves to all connected peers (used for
// session discovery)
BroadcastWantHaves(context.Context, []cid.Cid)
// SendCancels tells the PeerManager to send cancels to all peers
SendCancels(context.Context, []cid.Cid)
}
// SessionManager manages all the sessions
type SessionManager interface {
// Remove a session (called when the session shuts down)
RemoveSession(sesid uint64)
// Cancel wants (called when a call to GetBlocks() is cancelled)
CancelSessionWants(sid uint64, wants []cid.Cid)
}
// SessionPeerManager keeps track of peers in the session
type SessionPeerManager interface {
// PeersDiscovered indicates if any peers have been discovered yet
PeersDiscovered() bool
// Shutdown the SessionPeerManager
Shutdown()
// Adds a peer to the session, returning true if the peer is new
AddPeer(peer.ID) bool
// Removes a peer from the session, returning true if the peer existed
RemovePeer(peer.ID) bool
// All peers in the session
Peers() []peer.ID
// Whether there are any peers in the session
HasPeers() bool
// Protect connection from being pruned by the connection manager
ProtectConnection(peer.ID)
}
// ProviderFinder is used to find providers for a given key
type ProviderFinder interface {
// FindProvidersAsync searches for peers that provide the given CID
FindProvidersAsync(ctx context.Context, k cid.Cid) <-chan peer.ID
}
// opType is the kind of operation that is being processed by the event loop
type opType int
const (
// Receive blocks
opReceive opType = iota
// Want blocks
opWant
// Cancel wants
opCancel
// Broadcast want-haves
opBroadcast
// Wants sent to peers
opWantsSent
)
type op struct {
op opType
keys []cid.Cid
}
// Session holds state for an individual bitswap transfer operation.
// This allows bitswap to make smarter decisions about who to send wantlist
// info to, and who to request blocks from.
type Session struct {
// dependencies
ctx context.Context
shutdown func()
sm SessionManager
pm PeerManager
sprm SessionPeerManager
providerFinder ProviderFinder
sim *bssim.SessionInterestManager
sw sessionWants
sws sessionWantSender
latencyTrkr latencyTracker
// channels
incoming chan op
tickDelayReqs chan time.Duration
// do not touch outside run loop
idleTick *time.Timer
periodicSearchTimer *time.Timer
baseTickDelay time.Duration
consecutiveTicks int
initialSearchDelay time.Duration
periodicSearchDelay delay.D
// identifiers
notif notifications.PubSub
id uint64
self peer.ID
}
// New creates a new bitswap session whose lifetime is bounded by the
// given context.
func New(
ctx context.Context,
sm SessionManager,
id uint64,
sprm SessionPeerManager,
providerFinder ProviderFinder,
sim *bssim.SessionInterestManager,
pm PeerManager,
bpm *bsbpm.BlockPresenceManager,
notif notifications.PubSub,
initialSearchDelay time.Duration,
periodicSearchDelay delay.D,
self peer.ID) *Session {
ctx, cancel := context.WithCancel(ctx)
s := &Session{
sw: newSessionWants(broadcastLiveWantsLimit),
tickDelayReqs: make(chan time.Duration),
ctx: ctx,
shutdown: cancel,
sm: sm,
pm: pm,
sprm: sprm,
providerFinder: providerFinder,
sim: sim,
incoming: make(chan op, 128),
latencyTrkr: latencyTracker{},
notif: notif,
baseTickDelay: time.Millisecond * 500,
id: id,
initialSearchDelay: initialSearchDelay,
periodicSearchDelay: periodicSearchDelay,
self: self,
}
s.sws = newSessionWantSender(id, pm, sprm, sm, bpm, s.onWantsSent, s.onPeersExhausted)
go s.run(ctx)
return s
}
func (s *Session) ID() uint64 {
return s.id
}
func (s *Session) Shutdown() {
s.shutdown()
}
// ReceiveFrom receives incoming blocks from the given peer.
func (s *Session) ReceiveFrom(from peer.ID, ks []cid.Cid, haves []cid.Cid, dontHaves []cid.Cid) {
// The SessionManager tells each Session about all keys that it may be
// interested in. Here the Session filters the keys to the ones that this
// particular Session is interested in.
interestedRes := s.sim.FilterSessionInterested(s.id, ks, haves, dontHaves)
ks = interestedRes[0]
haves = interestedRes[1]
dontHaves = interestedRes[2]
s.logReceiveFrom(from, ks, haves, dontHaves)
// Inform the session want sender that a message has been received
s.sws.Update(from, ks, haves, dontHaves)
if len(ks) == 0 {
return
}
// Inform the session that blocks have been received
select {
case s.incoming <- op{op: opReceive, keys: ks}:
case <-s.ctx.Done():
}
}
func (s *Session) logReceiveFrom(from peer.ID, interestedKs []cid.Cid, haves []cid.Cid, dontHaves []cid.Cid) {
// Save some CPU cycles if log level is higher than debug
if ce := sflog.Check(zap.DebugLevel, "Bitswap <- rcv message"); ce == nil {
return
}
for _, c := range interestedKs {
log.Debugw("Bitswap <- block", "local", s.self, "from", from, "cid", c, "session", s.id)
}
for _, c := range haves {
log.Debugw("Bitswap <- HAVE", "local", s.self, "from", from, "cid", c, "session", s.id)
}
for _, c := range dontHaves {
log.Debugw("Bitswap <- DONT_HAVE", "local", s.self, "from", from, "cid", c, "session", s.id)
}
}
// GetBlock fetches a single block.
func (s *Session) GetBlock(ctx context.Context, k cid.Cid) (blocks.Block, error) {
ctx, span := internal.StartSpan(ctx, "Session.GetBlock")
defer span.End()
return bsgetter.SyncGetBlock(ctx, k, s.GetBlocks)
}
// GetBlocks fetches a set of blocks within the context of this session and
// returns a channel that found blocks will be returned on. No order is
// guaranteed on the returned blocks.
func (s *Session) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks.Block, error) {
ctx, span := internal.StartSpan(ctx, "Session.GetBlocks")
defer span.End()
return bsgetter.AsyncGetBlocks(ctx, s.ctx, keys, s.notif,
func(ctx context.Context, keys []cid.Cid) {
select {
case s.incoming <- op{op: opWant, keys: keys}:
case <-ctx.Done():
case <-s.ctx.Done():
}
},
func(keys []cid.Cid) {
select {
case s.incoming <- op{op: opCancel, keys: keys}:
case <-s.ctx.Done():
}
},
)
}
// SetBaseTickDelay changes the rate at which ticks happen.
func (s *Session) SetBaseTickDelay(baseTickDelay time.Duration) {
select {
case s.tickDelayReqs <- baseTickDelay:
case <-s.ctx.Done():
}
}
// onWantsSent is called when wants are sent to a peer by the session wants sender
func (s *Session) onWantsSent(p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) {
allBlks := append(wantBlocks[:len(wantBlocks):len(wantBlocks)], wantHaves...)
s.nonBlockingEnqueue(op{op: opWantsSent, keys: allBlks})
}
// onPeersExhausted is called when all available peers have sent DONT_HAVE for
// a set of cids (or all peers become unavailable)
func (s *Session) onPeersExhausted(ks []cid.Cid) {
s.nonBlockingEnqueue(op{op: opBroadcast, keys: ks})
}
// We don't want to block the sessionWantSender if the incoming channel
// is full. So if we can't immediately send on the incoming channel spin
// it off into a go-routine.
func (s *Session) nonBlockingEnqueue(o op) {
select {
case s.incoming <- o:
default:
go func() {
select {
case s.incoming <- o:
case <-s.ctx.Done():
}
}()
}
}
// Session run loop -- everything in this function should not be called
// outside of this loop
func (s *Session) run(ctx context.Context) {
go s.sws.Run()
s.idleTick = time.NewTimer(s.initialSearchDelay)
s.periodicSearchTimer = time.NewTimer(s.periodicSearchDelay.NextWaitTime())
for {
select {
case oper := <-s.incoming:
switch oper.op {
case opReceive:
// Received blocks
s.handleReceive(oper.keys)
case opWant:
// Client wants blocks
s.wantBlocks(ctx, oper.keys)
case opCancel:
// Wants were cancelled
s.sw.CancelPending(oper.keys)
s.sws.Cancel(oper.keys)
case opWantsSent:
// Wants were sent to a peer
s.sw.WantsSent(oper.keys)
case opBroadcast:
// Broadcast want-haves to all peers
s.broadcast(ctx, oper.keys)
default:
panic("unhandled operation")
}
case <-s.idleTick.C:
// The session hasn't received blocks for a while, broadcast
s.broadcast(ctx, nil)
case <-s.periodicSearchTimer.C:
// Periodically search for a random live want
s.handlePeriodicSearch(ctx)
case baseTickDelay := <-s.tickDelayReqs:
// Set the base tick delay
s.baseTickDelay = baseTickDelay
case <-ctx.Done():
// Shutdown
s.handleShutdown()
return
}
}
}
// Called when the session hasn't received any blocks for some time, or when
// all peers in the session have sent DONT_HAVE for a particular set of CIDs.
// Send want-haves to all connected peers, and search for new peers with the CID.
func (s *Session) broadcast(ctx context.Context, wants []cid.Cid) {
// If this broadcast is because of an idle timeout (we haven't received
// any blocks for a while) then broadcast all pending wants
if wants == nil {
wants = s.sw.PrepareBroadcast()
}
// Broadcast a want-have for the live wants to everyone we're connected to
s.broadcastWantHaves(ctx, wants)
// do not find providers on consecutive ticks
// -- just rely on periodic search widening
if len(wants) > 0 && (s.consecutiveTicks == 0) {
// Search for providers who have the first want in the list.
// Typically if the provider has the first block they will have
// the rest of the blocks also.
log.Debugw("FindMorePeers", "session", s.id, "cid", wants[0], "pending", len(wants))
s.findMorePeers(ctx, wants[0])
}
s.resetIdleTick()
// If we have live wants record a consecutive tick
if s.sw.HasLiveWants() {
s.consecutiveTicks++
}
}
// handlePeriodicSearch is called periodically to search for providers of a
// randomly chosen CID in the sesssion.
func (s *Session) handlePeriodicSearch(ctx context.Context) {
randomWant := s.sw.RandomLiveWant()
if !randomWant.Defined() {
return
}
// TODO: come up with a better strategy for determining when to search
// for new providers for blocks.
s.findMorePeers(ctx, randomWant)
s.broadcastWantHaves(ctx, []cid.Cid{randomWant})
s.periodicSearchTimer.Reset(s.periodicSearchDelay.NextWaitTime())
}
// findMorePeers attempts to find more peers for a session by searching for
// providers for the given Cid
func (s *Session) findMorePeers(ctx context.Context, c cid.Cid) {
go func(k cid.Cid) {
for p := range s.providerFinder.FindProvidersAsync(ctx, k) {
// When a provider indicates that it has a cid, it's equivalent to
// the providing peer sending a HAVE
s.sws.Update(p, nil, []cid.Cid{c}, nil)
}
}(c)
}
// handleShutdown is called when the session shuts down
func (s *Session) handleShutdown() {
// Stop the idle timer
s.idleTick.Stop()
// Shut down the session peer manager
s.sprm.Shutdown()
// Shut down the sessionWantSender (blocks until sessionWantSender stops
// sending)
s.sws.Shutdown()
// Signal to the SessionManager that the session has been shutdown
// and can be cleaned up
s.sm.RemoveSession(s.id)
}
// handleReceive is called when the session receives blocks from a peer
func (s *Session) handleReceive(ks []cid.Cid) {
// Record which blocks have been received and figure out the total latency
// for fetching the blocks
wanted, totalLatency := s.sw.BlocksReceived(ks)
if len(wanted) == 0 {
return
}
// Record latency
s.latencyTrkr.receiveUpdate(len(wanted), totalLatency)
// Inform the SessionInterestManager that this session is no longer
// expecting to receive the wanted keys
s.sim.RemoveSessionWants(s.id, wanted)
s.idleTick.Stop()
// We've received new wanted blocks, so reset the number of ticks
// that have occurred since the last new block
s.consecutiveTicks = 0
s.resetIdleTick()
}
// wantBlocks is called when blocks are requested by the client
func (s *Session) wantBlocks(ctx context.Context, newks []cid.Cid) {
if len(newks) > 0 {
// Inform the SessionInterestManager that this session is interested in the keys
s.sim.RecordSessionInterest(s.id, newks)
// Tell the sessionWants tracker that that the wants have been requested
s.sw.BlocksRequested(newks)
// Tell the sessionWantSender that the blocks have been requested
s.sws.Add(newks)
}
// If we have discovered peers already, the sessionWantSender will
// send wants to them
if s.sprm.PeersDiscovered() {
return
}
// No peers discovered yet, broadcast some want-haves
ks := s.sw.GetNextWants()
if len(ks) > 0 {
log.Infow("No peers - broadcasting", "session", s.id, "want-count", len(ks))
s.broadcastWantHaves(ctx, ks)
}
}
// Send want-haves to all connected peers
func (s *Session) broadcastWantHaves(ctx context.Context, wants []cid.Cid) {
log.Debugw("broadcastWantHaves", "session", s.id, "cids", wants)
s.pm.BroadcastWantHaves(ctx, wants)
}
// The session will broadcast if it has outstanding wants and doesn't receive
// any blocks for some time.
// The length of time is calculated
// - initially
// as a fixed delay
// - once some blocks are received
// from a base delay and average latency, with a backoff
func (s *Session) resetIdleTick() {
var tickDelay time.Duration
if !s.latencyTrkr.hasLatency() {
tickDelay = s.initialSearchDelay
} else {
avLat := s.latencyTrkr.averageLatency()
tickDelay = s.baseTickDelay + (3 * avLat)
}
tickDelay = tickDelay * time.Duration(1+s.consecutiveTicks)
s.idleTick.Reset(tickDelay)
}
// latencyTracker keeps track of the average latency between sending a want
// and receiving the corresponding block
type latencyTracker struct {
totalLatency time.Duration
count int
}
func (lt *latencyTracker) hasLatency() bool {
return lt.totalLatency > 0 && lt.count > 0
}
func (lt *latencyTracker) averageLatency() time.Duration {
return lt.totalLatency / time.Duration(lt.count)
}
func (lt *latencyTracker) receiveUpdate(count int, totalLatency time.Duration) {
lt.totalLatency += totalLatency
lt.count += count
}