-
Notifications
You must be signed in to change notification settings - Fork 18
/
bitswapretriever.go
352 lines (319 loc) · 12.2 KB
/
bitswapretriever.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
package retriever
import (
"bytes"
"context"
"fmt"
"io"
"sync"
"sync/atomic"
"time"
"github.com/benbjohnson/clock"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lassie/pkg/events"
"github.com/filecoin-project/lassie/pkg/retriever/bitswaphelpers"
"github.com/filecoin-project/lassie/pkg/retriever/bitswaphelpers/groupworkpool"
"github.com/filecoin-project/lassie/pkg/types"
"github.com/ipfs/boxo/bitswap/client"
"github.com/ipfs/boxo/bitswap/network"
"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/linking/preload"
"github.com/ipld/go-trustless-utils/traversal"
"github.com/ipni/go-libipni/metadata"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multicodec"
)
// IndexerRouting are the required methods to track indexer routing
type IndexerRouting interface {
AddProviders(types.RetrievalID, []types.RetrievalCandidate)
RemoveProviders(types.RetrievalID)
}
// MultiBlockstore are the require methods to track linksystems
type MultiBlockstore interface {
AddLinkSystem(id types.RetrievalID, lsys *linking.LinkSystem) error
RemoveLinkSystem(id types.RetrievalID)
}
type InProgressCids interface {
Inc(cid.Cid, types.RetrievalID)
Dec(cid.Cid, types.RetrievalID)
}
// BitswapRetriever uses bitswap to retrieve data
// BitswapRetriever retrieves using a combination of a go-bitswap client specially configured per retrieval,
// underneath a blockservice and a go-fetcher Fetcher.
// Selectors are used to travers the dag to make sure the CARs for bitswap match graphsync
// Note: this is a tradeoff over go-merkledag for traversal, cause selector execution is slow. But the solution
// is to improve selector execution, not introduce unpredictable encoding.
type BitswapRetriever struct {
bstore MultiBlockstore
inProgressCids InProgressCids
routing IndexerRouting
blockService blockservice.BlockService
clock clock.Clock
cfg BitswapConfig
// this is purely for testing purposes, to ensure that we receive all candidates
awaitReceivedCandidates chan<- struct{}
groupWorkPool groupworkpool.GroupWorkPool
}
const shortenedDelay = 4 * time.Millisecond
// BitswapConfig contains configurable parameters for bitswap fetching
type BitswapConfig struct {
BlockTimeout time.Duration
Concurrency int
ConcurrencyPerRetrieval int
}
// NewBitswapRetrieverFromHost constructs a new bitswap retriever for the given libp2p host
func NewBitswapRetrieverFromHost(
ctx context.Context,
host host.Host,
cfg BitswapConfig,
) *BitswapRetriever {
bstore := bitswaphelpers.NewMultiblockstore()
inProgressCids := bitswaphelpers.NewInProgressCids()
routing := bitswaphelpers.NewIndexerRouting(inProgressCids.Get)
bsnet := network.NewFromIpfsHost(host, routing)
bitswap := client.New(ctx, bsnet, bstore, client.ProviderSearchDelay(shortenedDelay))
bsnet.Start(bitswap)
bsrv := blockservice.New(bstore, bitswap)
go func() {
<-ctx.Done()
bsnet.Stop()
}()
return NewBitswapRetrieverFromDeps(ctx, bsrv, routing, inProgressCids, bstore, cfg, clock.New(), nil)
}
// NewBitswapRetrieverFromDeps is primarily for testing, constructing behavior from direct dependencies
func NewBitswapRetrieverFromDeps(
ctx context.Context,
bsrv blockservice.BlockService,
routing IndexerRouting,
inProgressCids InProgressCids,
bstore MultiBlockstore,
cfg BitswapConfig,
clock clock.Clock,
awaitReceivedCandidates chan<- struct{},
) *BitswapRetriever {
gwp := groupworkpool.New(cfg.Concurrency, cfg.ConcurrencyPerRetrieval)
gwp.Start(ctx)
return &BitswapRetriever{
bstore: bstore,
inProgressCids: inProgressCids,
routing: routing,
blockService: bsrv,
clock: clock,
cfg: cfg,
awaitReceivedCandidates: awaitReceivedCandidates,
groupWorkPool: gwp,
}
}
// Retrieve initializes a new bitswap session
func (br *BitswapRetriever) Retrieve(
ctx context.Context,
request types.RetrievalRequest,
events func(types.RetrievalEvent),
) types.CandidateRetrieval {
return &bitswapRetrieval{
BitswapRetriever: br,
bsGetter: blockservice.NewSession(ctx, br.blockService),
ctx: ctx,
request: request,
events: events,
}
}
type bitswapRetrieval struct {
*BitswapRetriever
bsGetter blockservice.BlockGetter
ctx context.Context
request types.RetrievalRequest
events func(types.RetrievalEvent)
}
// RetrieveFromCandidates retrieves via bitswap, with providers fed from the
// given asyncCandidates.
func (br *bitswapRetrieval) RetrieveFromAsyncCandidates(ayncCandidates types.InboundAsyncCandidates) (*types.RetrievalStats, error) {
ctx, cancelCtx := context.WithCancel(br.ctx)
defer cancelCtx()
// Perform the retrieval in a goroutine and use retrievalShared{} to handle
// event collection and dispatching on this goroutine; a bitswap traversal
// can involve many, separate goroutines, all emitting events that need to
// be collected and dispatched on this goroutine.
shared := newRetrievalShared(nil)
go br.runRetrieval(ctx, ayncCandidates, shared)
return collectResults(ctx, shared, br.events)
}
func (br *bitswapRetrieval) runRetrieval(ctx context.Context, ayncCandidates types.InboundAsyncCandidates, shared *retrievalShared) {
selector := br.request.GetSelector()
startTime := br.clock.Now()
// this is a hack cause we aren't able to track bitswap fetches per peer for now, so instead we just create a single peer for all events
bitswapCandidate := types.NewRetrievalCandidate(peer.ID(""), nil, br.request.Root, metadata.Bitswap{})
logger.Debugw("Starting bitswap retrieval", "retrievalID", br.request.RetrievalID, "root", br.request.Root)
// setup the linksystem to record bytes & blocks written -- since this isn't automatic w/o go-data-transfer
retrievalCtx, cancel := context.WithCancel(ctx)
var lastBytesReceivedTimer *clock.Timer
var doneLk sync.Mutex
var timedOut bool
if br.cfg.BlockTimeout != 0 {
lastBytesReceivedTimer = br.clock.AfterFunc(br.cfg.BlockTimeout, func() {
cancel()
doneLk.Lock()
timedOut = true
doneLk.Unlock()
})
}
totalWritten := atomic.Uint64{}
blockCount := atomic.Uint64{}
blockWrittenCb := func(from *peer.ID, bytesWritten uint64) {
// record first byte received
if totalWritten.Load() == 0 {
shared.sendEvent(ctx, events.FirstByte(br.clock.Now(), br.request.RetrievalID, bitswapCandidate, br.clock.Since(startTime), multicodec.TransportBitswap))
}
totalWritten.Add(bytesWritten)
blockCount.Add(1)
if from != nil {
shared.sendEvent(ctx, events.BlockReceived(
br.clock.Now(),
br.request.RetrievalID,
types.RetrievalCandidate{RootCid: br.request.Root, MinerPeer: peer.AddrInfo{ID: *from}},
multicodec.TransportBitswap,
bytesWritten,
))
}
// reset the timer
if bytesWritten > 0 && lastBytesReceivedTimer != nil {
lastBytesReceivedTimer.Reset(br.cfg.BlockTimeout)
}
}
// setup providers for this retrieval
hasCandidates, nextCandidates, err := ayncCandidates.Next(retrievalCtx)
if !hasCandidates || err != nil {
cancel()
// we never received any candidates, so we give up on bitswap retrieval
logger.Debugw("No bitswap candidates received", "retrievalID", br.request.RetrievalID, "root", br.request.Root)
shared.sendResult(ctx, retrievalResult{Err: ErrNoCandidates, AllFinished: true})
return
}
shared.sendEvent(ctx, events.StartedRetrieval(br.clock.Now(), br.request.RetrievalID, bitswapCandidate, multicodec.TransportBitswap))
// set initial providers, then start a goroutine to add more as they come in
br.routing.AddProviders(br.request.RetrievalID, nextCandidates)
logger.Debugf("Adding %d initial bitswap provider(s)", len(nextCandidates))
go func() {
for {
hasCandidates, nextCandidates, err := ayncCandidates.Next(retrievalCtx)
if !hasCandidates || err != nil {
if br.awaitReceivedCandidates != nil {
select {
case <-ctx.Done():
case br.awaitReceivedCandidates <- struct{}{}:
}
}
return
}
br.routing.AddProviders(br.request.RetrievalID, nextCandidates)
logger.Debugf("Adding %d more bitswap provider(s)", len(nextCandidates))
}
}()
// set up the storage system, including the preloader if configured
var preloader preload.Loader
traversalLinkSys := br.request.LinkSystem
loader := br.loader(ctx, shared)
if br.request.HasPreloadLinkSystem() {
var err error
storage, err := bitswaphelpers.NewPreloadCachingStorage(
br.request.LinkSystem,
br.request.PreloadLinkSystem,
loader,
br.groupWorkPool.AddGroup(retrievalCtx),
)
if err != nil {
cancel()
shared.sendResult(ctx, retrievalResult{Err: err, AllFinished: true})
return
}
preloader = storage.Preloader
traversalLinkSys = *storage.TraversalLinkSystem
br.bstore.AddLinkSystem(
br.request.RetrievalID,
bitswaphelpers.NewByteCountingLinkSystem(storage.BitswapLinkSystem, blockWrittenCb),
)
} else {
br.bstore.AddLinkSystem(
br.request.RetrievalID,
bitswaphelpers.NewByteCountingLinkSystem(&br.request.LinkSystem, blockWrittenCb),
)
traversalLinkSys.StorageReadOpener = loader
}
// run the retrieval
_, err = traversal.Config{
Root: br.request.Root,
Selector: selector,
MaxBlocks: br.request.MaxBlocks,
}.Traverse(retrievalCtx, traversalLinkSys, preloader)
cancel()
// unregister relevant provider records & LinkSystem
br.routing.RemoveProviders(br.request.RetrievalID)
br.bstore.RemoveLinkSystem(br.request.RetrievalID)
if err != nil {
logger.Debugw("Traversal/retrieval error, cleaning up", "err", err)
// check for timeout on the local context
// TODO: post 1.19 replace timedOut and doneLk with WithCancelCause and use DeadlineExceeded cause:
// if errors.Is(retrievalCtx.Err(), context.Canceled) && errors.Is(context.Cause(retrievalCtx), context.DeadlineExceeded) {
doneLk.Lock()
if timedOut {
err = fmt.Errorf("%w; %w after %s", ErrRetrievalFailed, ErrRetrievalTimedOut, br.cfg.BlockTimeout)
}
doneLk.Unlock()
// exclude the case where the context was cancelled by the parent, which likely means that
// another protocol has succeeded.
if br.ctx.Err() == nil {
shared.sendEvent(ctx, events.FailedRetrieval(br.clock.Now(), br.request.RetrievalID, bitswapCandidate, multicodec.TransportBitswap, err.Error()))
shared.sendResult(ctx, retrievalResult{Err: err, AllFinished: true})
}
return
}
duration := br.clock.Since(startTime)
speed := uint64(float64(totalWritten.Load()) / duration.Seconds())
logger.Debugw("Bitswap retrieval success", "retrievalID", br.request.RetrievalID, "root", br.request.Root, "duration", duration, "bytes", totalWritten.Load(), "blocks", blockCount.Load(), "speed", speed)
// record success
shared.sendEvent(ctx, events.Success(
br.clock.Now(),
br.request.RetrievalID,
bitswapCandidate,
totalWritten.Load(),
blockCount.Load(),
duration,
multicodec.TransportBitswap,
))
shared.sendResult(ctx, retrievalResult{Stats: &types.RetrievalStats{
StorageProviderId: peer.ID(""),
RootCid: br.request.Root,
Size: totalWritten.Load(),
Blocks: blockCount.Load(),
Duration: duration,
AverageSpeed: speed,
TotalPayment: big.Zero(),
NumPayments: 0,
AskPrice: big.Zero(),
}})
}
func (br *bitswapRetrieval) loader(ctx context.Context, shared *retrievalShared) func(lctx linking.LinkContext, lnk datamodel.Link) (io.Reader, error) {
return func(lctx linking.LinkContext, lnk datamodel.Link) (io.Reader, error) {
cidLink, ok := lnk.(cidlink.Link)
if !ok {
return nil, fmt.Errorf("invalid link type for loading: %v", lnk)
}
br.inProgressCids.Inc(cidLink.Cid, br.request.RetrievalID)
select {
case <-lctx.Ctx.Done():
return nil, lctx.Ctx.Err()
default:
}
blk, err := br.blockService.GetBlock(lctx.Ctx, cidLink.Cid)
br.inProgressCids.Dec(cidLink.Cid, br.request.RetrievalID)
if err != nil {
return nil, err
}
logger.Debugw("Got block from bitswap", "retrievalID", br.request.RetrievalID, "root", br.request.Root, "block", cidLink.Cid, "size", len(blk.RawData()))
return bytes.NewReader(blk.RawData()), nil
}
}