-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd-fetch.go
361 lines (325 loc) · 12 KB
/
cmd-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
package main
// CREDIT: from https://github.com/filecoin-project/lassie/blob/main/cmd/lassie/fetch.go
// The MIT License (MIT)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import (
"fmt"
"io"
"net/url"
"os"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/filecoin-project/lassie/pkg/events"
"github.com/filecoin-project/lassie/pkg/indexerlookup"
"github.com/filecoin-project/lassie/pkg/lassie"
"github.com/filecoin-project/lassie/pkg/net/host"
"github.com/filecoin-project/lassie/pkg/retriever"
"github.com/filecoin-project/lassie/pkg/storage"
"github.com/filecoin-project/lassie/pkg/types"
"github.com/ipfs/go-cid"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/urfave/cli/v2"
"k8s.io/klog/v2"
)
var fetchProviderAddrInfos []peer.AddrInfo
var lassieFetchFlags = []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "the CAR file to write to, may be an existing or a new CAR, or use '-' to write to stdout",
TakesFile: true,
},
&cli.DurationFlag{
Name: "provider-timeout",
Aliases: []string{"pt"},
Usage: "consider it an error after not receiving a response from a storage provider after this amount of time",
Value: 20 * time.Second,
},
&cli.DurationFlag{
Name: "global-timeout",
Aliases: []string{"gt"},
Usage: "consider it an error after not completing the retrieval after this amount of time",
Value: 0,
},
&cli.BoolFlag{
Name: "progress",
Aliases: []string{"p"},
Usage: "print progress output",
},
&cli.StringFlag{
Name: "dag-scope",
Usage: "describes the fetch behavior at the end of the traversal path. Valid values include [all, entity, block].",
DefaultText: "defaults to all, the entire DAG at the end of the path will be fetched",
Value: "all",
Action: func(cctx *cli.Context, v string) error {
switch v {
case string(types.DagScopeAll):
case string(types.DagScopeEntity):
case string(types.DagScopeBlock):
default:
return fmt.Errorf("invalid dag-scope parameter, must be of value [all, entity, block]")
}
return nil
},
},
&cli.StringFlag{
Name: "providers",
Aliases: []string{"provider"},
DefaultText: "Providers will be discovered automatically",
Usage: "Addresses of providers, including peer IDs, to use instead of automatic discovery, seperated by a comma. All protocols will be attempted when connecting to these providers. Example: /ip4/1.2.3.4/tcp/1234/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4",
Action: func(cctx *cli.Context, v string) error {
// Do nothing if given an empty string
if v == "" {
return nil
}
var err error
fetchProviderAddrInfos, err = types.ParseProviderStrings(v)
return err
},
},
&cli.StringFlag{
Name: "ipni-endpoint",
Aliases: []string{"ipni"},
DefaultText: "Defaults to https://cid.contact",
Usage: "HTTP endpoint of the IPNI instance used to discover providers.",
},
FlagEventRecorderAuth,
FlagEventRecorderInstanceId,
FlagEventRecorderUrl,
FlagVerbose,
FlagVeryVerbose,
FlagProtocols,
FlagExcludeProviders,
FlagTempDir,
FlagBitswapConcurrency,
}
var fetchCmd = &cli.Command{
Name: "fetch",
Usage: "Fetches content from the IPFS and Filecoin network",
Before: before,
Action: Fetch,
Flags: lassieFetchFlags,
}
func Fetch(cctx *cli.Context) error {
if cctx.Args().Len() != 1 {
return fmt.Errorf("usage: lassie fetch [-o <CAR file>] [-t <timeout>] <CID>[/path/to/content]")
}
ctx := cctx.Context
msgWriter := cctx.App.ErrWriter
dataWriter := cctx.App.Writer
progress := cctx.Bool("progress")
providerTimeout := cctx.Duration("provider-timeout")
globalTimeout := cctx.Duration("global-timeout")
dagScope := cctx.String("dag-scope")
tempDir := cctx.String("tempdir")
bitswapConcurrency := cctx.Int("bitswap-concurrency")
eventRecorderURL := cctx.String("event-recorder-url")
authToken := cctx.String("event-recorder-auth")
instanceID := cctx.String("event-recorder-instance-id")
rootCid, path, err := parseCidPath(cctx.Args().Get(0))
if err != nil {
return err
}
providerTimeoutOpt := lassie.WithProviderTimeout(providerTimeout)
host, err := host.InitHost(ctx, []libp2p.Option{})
if err != nil {
return err
}
hostOpt := lassie.WithHost(host)
lassieOpts := []lassie.LassieOption{providerTimeoutOpt, hostOpt}
if len(fetchProviderAddrInfos) > 0 {
finderOpt := lassie.WithFinder(retriever.NewDirectCandidateFinder(host, fetchProviderAddrInfos))
if cctx.IsSet("ipni-endpoint") {
klog.Warning("Ignoring ipni-endpoint flag since direct provider is specified")
}
lassieOpts = append(lassieOpts, finderOpt)
} else if cctx.IsSet("ipni-endpoint") {
endpoint := cctx.String("ipni-endpoint")
endpointUrl, err := url.Parse(endpoint)
if err != nil {
klog.Error("Failed to parse IPNI endpoint as URL", "err", err)
return fmt.Errorf("cannot parse given IPNI endpoint %s as valid URL: %w", endpoint, err)
}
finder, err := indexerlookup.NewCandidateFinder(indexerlookup.WithHttpEndpoint(endpointUrl))
if err != nil {
klog.Error("Failed to instantiate IPNI candidate finder", "err", err)
return err
}
lassieOpts = append(lassieOpts, lassie.WithFinder(finder))
klog.Info("Using explicit IPNI endpoint to find candidates", "endpoint", endpoint)
}
if len(providerBlockList) > 0 {
lassieOpts = append(lassieOpts, lassie.WithProviderBlockList(providerBlockList))
}
if len(protocols) > 0 {
lassieOpts = append(lassieOpts, lassie.WithProtocols(protocols))
}
if globalTimeout > 0 {
lassieOpts = append(lassieOpts, lassie.WithGlobalTimeout(globalTimeout))
}
if tempDir != "" {
lassieOpts = append(lassieOpts, lassie.WithTempDir(tempDir))
} else {
tempDir = os.TempDir()
}
if bitswapConcurrency > 0 {
lassieOpts = append(lassieOpts, lassie.WithBitswapConcurrency(bitswapConcurrency))
}
lassie, err := lassie.NewLassie(ctx, lassieOpts...)
if err != nil {
return err
}
// create and subscribe an event recorder API if configured
setupLassieEventRecorder(ctx, eventRecorderURL, authToken, instanceID, lassie)
if len(fetchProviderAddrInfos) == 0 {
fmt.Fprintf(msgWriter, "Fetching %s", rootCid.String()+path)
} else {
fmt.Fprintf(msgWriter, "Fetching %s from %v", rootCid.String()+path, fetchProviderAddrInfos)
}
if progress {
fmt.Fprintln(msgWriter)
pp := &progressPrinter{writer: msgWriter}
lassie.RegisterSubscriber(pp.subscriber)
}
outfile := fmt.Sprintf("%s.car", rootCid)
if cctx.IsSet("output") {
outfile = cctx.String("output")
}
var carWriter *storage.DeferredCarWriter
if outfile == "-" { // stdout
// we need the onlyWriter because stdout is presented as an os.File, and
// therefore pretend to support seeks, so feature-checking in go-car
// will make bad assumptions about capabilities unless we hide it
carWriter = storage.NewDeferredCarWriterForStream(rootCid, &onlyWriter{dataWriter})
} else {
carWriter = storage.NewDeferredCarWriterForPath(rootCid, outfile)
}
tempStore := storage.NewDeferredStorageCar(tempDir)
carStore := storage.NewCachingTempStore(carWriter.BlockWriteOpener(), tempStore)
defer carStore.Close()
var blockCount int
var byteLength uint64
carWriter.OnPut(func(putBytes int) {
blockCount++
byteLength += uint64(putBytes)
if !progress {
fmt.Fprint(msgWriter, ".")
} else {
fmt.Fprintf(msgWriter, "\rReceived %d blocks / %s...", blockCount, humanize.IBytes(byteLength))
}
}, false)
request, err := types.NewRequestForPath(carStore, rootCid, path, types.DagScope(dagScope))
if err != nil {
return err
}
// setup preload storage for bitswap, the temporary CAR store can set up a
// separate preload space in its storage
request.PreloadLinkSystem = cidlink.DefaultLinkSystem()
preloadStore := carStore.PreloadStore()
request.PreloadLinkSystem.SetReadStorage(preloadStore)
request.PreloadLinkSystem.SetWriteStorage(preloadStore)
request.PreloadLinkSystem.TrustedStorage = true
stats, err := lassie.Fetch(ctx, request, func(types.RetrievalEvent) {})
if err != nil {
fmt.Fprintln(msgWriter)
return err
}
fmt.Fprintf(msgWriter, "\nFetched [%s] from [%s]:\n"+
"\tDuration: %s\n"+
"\t Blocks: %d\n"+
"\t Bytes: %s\n",
rootCid,
stats.StorageProviderId,
stats.Duration,
blockCount,
humanize.IBytes(stats.Size),
)
return nil
}
func parseCidPath(cpath string) (cid.Cid, string, error) {
cstr := strings.Split(cpath, "/")[0]
path := strings.TrimPrefix(cpath, cstr)
rootCid, err := cid.Parse(cstr)
if err != nil {
return cid.Undef, "", err
}
return rootCid, path, nil
}
type progressPrinter struct {
candidatesFound int
writer io.Writer
}
func (pp *progressPrinter) subscriber(event types.RetrievalEvent) {
switch ret := event.(type) {
case events.RetrievalEventStarted:
switch ret.Phase() {
case types.IndexerPhase:
fmt.Fprintf(pp.writer, "\rQuerying indexer for %s...\n", ret.PayloadCid())
case types.QueryPhase:
fmt.Fprintf(pp.writer, "\rQuerying [%s] (%s)...\n", types.Identifier(ret), ret.Code())
case types.RetrievalPhase:
fmt.Fprintf(pp.writer, "\rRetrieving from [%s] (%s)...\n", types.Identifier(ret), ret.Code())
}
case events.RetrievalEventConnected:
switch ret.Phase() {
case types.QueryPhase:
fmt.Fprintf(pp.writer, "\rQuerying [%s] (%s)...\n", types.Identifier(ret), ret.Code())
case types.RetrievalPhase:
fmt.Fprintf(pp.writer, "\rRetrieving from [%s] (%s)...\n", types.Identifier(ret), ret.Code())
}
case events.RetrievalEventProposed:
fmt.Fprintf(pp.writer, "\rRetrieving from [%s] (%s)...\n", types.Identifier(ret), ret.Code())
case events.RetrievalEventAccepted:
fmt.Fprintf(pp.writer, "\rRetrieving from [%s] (%s)...\n", types.Identifier(ret), ret.Code())
case events.RetrievalEventFirstByte:
fmt.Fprintf(pp.writer, "\rRetrieving from [%s] (%s)...\n", types.Identifier(ret), ret.Code())
case events.RetrievalEventCandidatesFound:
pp.candidatesFound = len(ret.Candidates())
case events.RetrievalEventCandidatesFiltered:
num := "all of them"
if pp.candidatesFound != len(ret.Candidates()) {
num = fmt.Sprintf("%d of them", len(ret.Candidates()))
} else if pp.candidatesFound == 1 {
num = "it"
}
if len(fetchProviderAddrInfos) > 0 {
fmt.Fprintf(pp.writer, "Found %d storage providers candidates from the indexer, querying %s:\n", pp.candidatesFound, num)
} else {
fmt.Fprintf(pp.writer, "Using the explicitly specified storage provider(s), querying %s:\n", num)
}
for _, candidate := range ret.Candidates() {
fmt.Fprintf(pp.writer, "\r\t%s, Protocols: %v\n", candidate.MinerPeer.ID, candidate.Metadata.Protocols())
}
case events.RetrievalEventFailed:
if ret.Phase() == types.IndexerPhase {
fmt.Fprintf(pp.writer, "\rRetrieval failure from indexer: %s\n", ret.ErrorMessage())
} else {
fmt.Fprintf(pp.writer, "\rRetrieval failure for [%s]: %s\n", types.Identifier(ret), ret.ErrorMessage())
}
case events.RetrievalEventSuccess:
// noop, handled at return from Retrieve()
}
}
type onlyWriter struct {
w io.Writer
}
func (ow *onlyWriter) Write(p []byte) (n int, err error) {
return ow.w.Write(p)
}