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
367 lines (315 loc) · 9.44 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
package session
import (
"context"
"time"
bsgetter "github.com/ipfs/go-bitswap/getter"
notifications "github.com/ipfs/go-bitswap/notifications"
bssd "github.com/ipfs/go-bitswap/sessiondata"
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"
loggables "github.com/libp2p/go-libp2p-loggables"
)
const (
broadcastLiveWantsLimit = 4
targetedLiveWantsLimit = 32
)
// WantManager is an interface that can be used to request blocks
// from given peers.
type WantManager interface {
WantBlocks(ctx context.Context, ks []cid.Cid, peers []peer.ID, ses uint64)
CancelWants(ctx context.Context, ks []cid.Cid, peers []peer.ID, ses uint64)
}
// PeerManager provides an interface for tracking and optimize peers, and
// requesting more when neccesary.
type PeerManager interface {
FindMorePeers(context.Context, cid.Cid)
GetOptimizedPeers() []bssd.OptimizedPeer
RecordPeerRequests([]peer.ID, []cid.Cid)
RecordPeerResponse(peer.ID, []cid.Cid)
RecordCancels([]cid.Cid)
}
// RequestSplitter provides an interface for splitting
// a request for Cids up among peers.
type RequestSplitter interface {
SplitRequest([]bssd.OptimizedPeer, []cid.Cid) []bssd.PartialRequest
RecordDuplicateBlock()
RecordUniqueBlock()
}
type opType int
const (
opReceive opType = iota
opWant
opCancel
)
type op struct {
op opType
from peer.ID
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
wm WantManager
pm PeerManager
srs RequestSplitter
sw sessionWants
// channels
incoming chan op
latencyReqs chan chan time.Duration
tickDelayReqs chan time.Duration
// do not touch outside run loop
idleTick *time.Timer
periodicSearchTimer *time.Timer
baseTickDelay time.Duration
latTotal time.Duration
fetchcnt int
consecutiveTicks int
initialSearchDelay time.Duration
periodicSearchDelay delay.D
// identifiers
notif notifications.PubSub
uuid logging.Loggable
id uint64
}
// New creates a new bitswap session whose lifetime is bounded by the
// given context.
func New(ctx context.Context,
id uint64,
wm WantManager,
pm PeerManager,
srs RequestSplitter,
notif notifications.PubSub,
initialSearchDelay time.Duration,
periodicSearchDelay delay.D) *Session {
s := &Session{
sw: sessionWants{
toFetch: newCidQueue(),
liveWants: make(map[cid.Cid]time.Time),
pastWants: cid.NewSet(),
},
latencyReqs: make(chan chan time.Duration),
tickDelayReqs: make(chan time.Duration),
ctx: ctx,
wm: wm,
pm: pm,
srs: srs,
incoming: make(chan op, 16),
notif: notif,
uuid: loggables.Uuid("GetBlockRequest"),
baseTickDelay: time.Millisecond * 500,
id: id,
initialSearchDelay: initialSearchDelay,
periodicSearchDelay: periodicSearchDelay,
}
go s.run(ctx)
return s
}
// ReceiveFrom receives incoming blocks from the given peer.
func (s *Session) ReceiveFrom(from peer.ID, ks []cid.Cid) {
interested := s.sw.FilterInteresting(ks)
if len(interested) == 0 {
return
}
select {
case s.incoming <- op{op: opReceive, from: from, keys: interested}:
case <-s.ctx.Done():
}
}
// IsWanted returns true if this session is waiting to receive the given Cid.
func (s *Session) IsWanted(c cid.Cid) bool {
return s.sw.IsWanted(c)
}
// GetBlock fetches a single block.
func (s *Session) GetBlock(parent context.Context, k cid.Cid) (blocks.Block, error) {
return bsgetter.SyncGetBlock(parent, 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 = logging.ContextWithLoggable(ctx, s.uuid)
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():
}
},
)
}
// GetAverageLatency returns the average latency for block requests.
func (s *Session) GetAverageLatency() time.Duration {
resp := make(chan time.Duration)
select {
case s.latencyReqs <- resp:
case <-s.ctx.Done():
return -1 * time.Millisecond
}
select {
case latency := <-resp:
return latency
case <-s.ctx.Done():
return -1 * time.Millisecond
}
}
// SetBaseTickDelay changes the rate at which ticks happen.
func (s *Session) SetBaseTickDelay(baseTickDelay time.Duration) {
select {
case s.tickDelayReqs <- baseTickDelay:
case <-s.ctx.Done():
}
}
// Session run loop -- everything function below here should not be called
// of this loop
func (s *Session) run(ctx context.Context) {
s.idleTick = time.NewTimer(s.initialSearchDelay)
s.periodicSearchTimer = time.NewTimer(s.periodicSearchDelay.NextWaitTime())
for {
select {
case oper := <-s.incoming:
switch oper.op {
case opReceive:
s.handleReceive(ctx, oper.from, oper.keys)
case opWant:
s.wantBlocks(ctx, oper.keys)
case opCancel:
s.sw.CancelPending(oper.keys)
default:
panic("unhandled operation")
}
case <-s.idleTick.C:
s.handleIdleTick(ctx)
case <-s.periodicSearchTimer.C:
s.handlePeriodicSearch(ctx)
case resp := <-s.latencyReqs:
resp <- s.averageLatency()
case baseTickDelay := <-s.tickDelayReqs:
s.baseTickDelay = baseTickDelay
case <-ctx.Done():
s.handleShutdown()
return
}
}
}
func (s *Session) handleIdleTick(ctx context.Context) {
live := s.sw.PrepareBroadcast()
// Broadcast these keys to everyone we're connected to
s.pm.RecordPeerRequests(nil, live)
s.wm.WantBlocks(ctx, live, nil, s.id)
// do no find providers on consecutive ticks
// -- just rely on periodic search widening
if len(live) > 0 && (s.consecutiveTicks == 0) {
s.pm.FindMorePeers(ctx, live[0])
}
s.resetIdleTick()
if s.sw.HasLiveWants() {
s.consecutiveTicks++
}
}
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.pm.FindMorePeers(ctx, randomWant)
s.wm.WantBlocks(ctx, []cid.Cid{randomWant}, nil, s.id)
s.periodicSearchTimer.Reset(s.periodicSearchDelay.NextWaitTime())
}
func (s *Session) handleShutdown() {
s.idleTick.Stop()
live := s.sw.LiveWants()
s.wm.CancelWants(s.ctx, live, nil, s.id)
}
func (s *Session) handleReceive(ctx context.Context, from peer.ID, keys []cid.Cid) {
// Record statistics only if the blocks came from the network
// (blocks can also be received from the local node)
if from != "" {
s.updateReceiveCounters(ctx, from, keys)
}
// Update the want list
wanted, totalLatency := s.sw.BlocksReceived(keys)
if len(wanted) == 0 {
return
}
// We've received the blocks so we can cancel any outstanding wants for them
s.cancelIncoming(ctx, wanted)
s.idleTick.Stop()
// Process the received blocks
s.processReceive(ctx, wanted, totalLatency)
s.resetIdleTick()
}
func (s *Session) updateReceiveCounters(ctx context.Context, from peer.ID, keys []cid.Cid) {
// Record unique vs duplicate blocks
s.sw.ForEachUniqDup(keys, s.srs.RecordUniqueBlock, s.srs.RecordDuplicateBlock)
// Record response (to be able to time latency)
if len(keys) > 0 {
s.pm.RecordPeerResponse(from, keys)
}
}
func (s *Session) cancelIncoming(ctx context.Context, ks []cid.Cid) {
s.pm.RecordCancels(ks)
s.wm.CancelWants(s.ctx, ks, nil, s.id)
}
func (s *Session) processReceive(ctx context.Context, ks []cid.Cid, totalLatency time.Duration) {
// Keep track of the total number of blocks received and total latency
s.fetchcnt += len(ks)
s.latTotal += totalLatency
// We've received new wanted blocks, so reset the number of ticks
// that have occurred since the last new block
s.consecutiveTicks = 0
s.wantBlocks(ctx, nil)
}
func (s *Session) wantBlocks(ctx context.Context, newks []cid.Cid) {
// Given the want limit and any newly received blocks, get as many wants as
// we can to send out
ks := s.sw.GetNextWants(s.wantLimit(), newks)
if len(ks) == 0 {
return
}
peers := s.pm.GetOptimizedPeers()
if len(peers) > 0 {
splitRequests := s.srs.SplitRequest(peers, ks)
for _, splitRequest := range splitRequests {
s.pm.RecordPeerRequests(splitRequest.Peers, splitRequest.Keys)
s.wm.WantBlocks(ctx, splitRequest.Keys, splitRequest.Peers, s.id)
}
} else {
s.pm.RecordPeerRequests(nil, ks)
s.wm.WantBlocks(ctx, ks, nil, s.id)
}
}
func (s *Session) averageLatency() time.Duration {
return s.latTotal / time.Duration(s.fetchcnt)
}
func (s *Session) resetIdleTick() {
var tickDelay time.Duration
if s.latTotal == 0 {
tickDelay = s.initialSearchDelay
} else {
avLat := s.averageLatency()
tickDelay = s.baseTickDelay + (3 * avLat)
}
tickDelay = tickDelay * time.Duration(1+s.consecutiveTicks)
s.idleTick.Reset(tickDelay)
}
func (s *Session) wantLimit() int {
if len(s.pm.GetOptimizedPeers()) > 0 {
return targetedLiveWantsLimit
}
return broadcastLiveWantsLimit
}