forked from ipfs-cluster/ipfs-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetter.go
435 lines (391 loc) · 11.2 KB
/
getter.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
package ipfscluster
import (
"bufio"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"sync"
"time"
ec "github.com/ipfs-cluster/ipfs-cluster/adder/erasure"
"github.com/ipfs-cluster/ipfs-cluster/adder/sharding"
"github.com/ipfs-cluster/ipfs-cluster/api"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/ipld/merkledag"
unixfile "github.com/ipfs/boxo/ipld/unixfs/file"
uio "github.com/ipfs/boxo/ipld/unixfs/io"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
ipldlegacy "github.com/ipfs/go-ipld-legacy"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/codec/raw"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/multicodec"
"github.com/ipld/go-ipld-prime/node/basicnode"
)
var ipldDecoder *ipldlegacy.Decoder
// create an ipld registry specific to this package
func init() {
mcReg := multicodec.Registry{}
mcReg.RegisterDecoder(cid.DagProtobuf, dagpb.Decode)
mcReg.RegisterDecoder(cid.Raw, raw.Decode)
mcReg.RegisterDecoder(cid.DagCBOR, dagcbor.Decode)
ls := cidlink.LinkSystemUsingMulticodecRegistry(mcReg)
ipldDecoder = ipldlegacy.NewDecoderWithLS(ls)
ipldDecoder.RegisterCodec(cid.DagProtobuf, dagpb.Type.PBNode, merkledag.ProtoNodeConverter)
ipldDecoder.RegisterCodec(cid.Raw, basicnode.Prototype.Bytes, merkledag.RawNodeConverter)
}
// simple dag service, just for erasure coding file get
type dagSession struct {
ctx context.Context
dataShards [][]byte
cached bool
bmeta map[string]sharding.ECBlockMeta
blockGet func(ctx context.Context, ci api.Cid) ([]byte, error)
}
func NewDagGetter(ctx context.Context, bg func(ctx context.Context, ci api.Cid) ([]byte, error)) *dagSession {
return &dagSession{
bmeta: make(map[string]sharding.ECBlockMeta),
ctx: ctx,
blockGet: bg,
}
}
func (ds *dagSession) SetCache(dataShards [][]byte, bmeta map[int][]sharding.ECBlockMeta) {
ds.dataShards = dataShards
for _, meta := range bmeta {
for _, m := range meta {
ds.bmeta[m.Cid] = m
}
}
}
func (ds *dagSession) GetRawFile(ctx context.Context, ci cid.Cid) ([]byte, error) {
root, err := ds.Get(ctx, ci)
if err != nil {
return nil, err
}
r, err := uio.NewDagReader(ctx, root, ds)
if err != nil {
return nil, err
}
blockSize := 256 * 1024
b := make([]byte, 0, blockSize)
for {
n, err := r.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return b, err
}
if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
}
}
func (ds *dagSession) GetArchivedFile(ctx context.Context, ci cid.Cid, name string, out chan<- []byte) error {
root, err := ds.Get(ctx, ci)
if err != nil {
close(out)
return err
}
f, err := unixfile.NewUnixfsFile(ctx, ds, root)
if err != nil {
close(out)
return err
}
return fileArchive(f, name, out)
}
func (ds *dagSession) Get(ctx context.Context, ci cid.Cid) (format.Node, error) {
if ds.cached {
m := ds.bmeta[ci.String()]
b := ds.dataShards[m.ShardNo][m.Offset : m.Offset+m.Size]
return ds.decode(ctx, b, ci)
}
b, err := ds.blockGet(ctx, api.NewCid(ci))
if err != nil {
logger.Infof("Failed to get block %s, err: %s", ci, err)
return nil, err
}
return ds.decode(ctx, b, ci)
}
func (ds *dagSession) GetMany(ctx context.Context, keys []cid.Cid) <-chan *format.NodeOption {
out := make(chan *format.NodeOption, len(keys))
go func() {
defer close(out)
wg := sync.WaitGroup{}
wg.Add(len(keys))
for _, ci := range keys {
go func(ci cid.Cid) {
defer wg.Done()
nd, err := ds.Get(ctx, ci)
if err != nil {
out <- &format.NodeOption{Err: fmt.Errorf("cannot get all blocks: %s", err)}
}
no := &format.NodeOption{Node: nd, Err: err}
select {
case out <- no:
case <-ctx.Done():
out <- &format.NodeOption{Err: fmt.Errorf("GetMany context timeout: %s", err)}
return
}
}(ci)
}
wg.Wait()
}()
return out
}
func (ds *dagSession) decode(ctx context.Context, rawb []byte, ci cid.Cid) (format.Node, error) {
b, err := blocks.NewBlockWithCid(rawb, ci)
if err != nil {
return nil, fmt.Errorf("cannot parse raw data with cid")
}
nd, err := ipldDecoder.DecodeNode(ctx, b)
if err != nil {
logger.Errorf("failed to decode block: %s (len:%d)", err, len(rawb))
return nil, err
}
return nd, err
}
// ECGetBatches use links get all shards and batch send them to RS decode
func (ds *dagSession) ECGetBatches(ctx context.Context, links []*format.Link, batchDataShards, batchIdx int, shardCh chan<- ec.Shard) (error, ec.Batch) {
defer close(shardCh)
vects := make([][]byte, len(links))
wg := sync.WaitGroup{}
wg.Add(len(links))
errCh := make(chan error, len(links))
for i, sh := range links {
go func(i int, sh *format.Link) {
defer wg.Done()
start := time.Now()
vect, err := ds.ECLink2Raw(ctx, sh, i < batchDataShards)
if err != nil {
errCh <- err
return
}
fetchTime := time.Since(start)
shardCh <- ec.Shard{Idx: i, RawData: vect}
vects[i] = vect
typ := "data"
if i >= batchDataShards {
typ = "parity"
}
logger.Infof("use %s fetch %d-batch-%d-shard(%s) %s successfully, size:%d", fetchTime, batchIdx, i, typ, sh.Cid, len(vect))
}(i, sh)
}
wg.Wait()
close(errCh)
var errs error
for err := range errCh {
errs = errors.Join(errs, err)
}
return errs, ec.Batch{Idx: batchIdx, Shards: vects}
}
func (ds *dagSession) ECGetShard2Batch(ctx context.Context, ci api.Cid, d, p int, dShardSize []int, out chan<- ec.Batch) error {
timeout := 5 * time.Minute
links, err := ds.ResolveCborLinks(ctx, ci) // get sorted shards
if err != nil {
logger.Error(err)
return err
}
batchNum := (len(dShardSize) + d - 1) / d
errCh := make(chan error, batchNum)
wg := sync.WaitGroup{}
wg.Add(batchNum)
for i := 0; i < batchNum; i++ {
func(i int) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
defer wg.Done()
shardCh := make(chan ec.Shard, d)
batchResultCh := make(chan ec.Batch)
// split links to batch, links like [d0,d1,d2,d3,d4,p0,p1,p2,p3]
dl, dr := d*i, min((i+1)*d, len(dShardSize))
pl, pr := len(dShardSize)+i*p, len(dShardSize)+(i+1)*p
batchLinks := append(links[dl:dr:dr], links[pl:pr:pr]...)
// batchDataShardSize used to record
batchDataShardSize := dShardSize[dl:dr:dr]
go func() {
err, batch := ds.ECGetBatches(ctx, batchLinks, len(batchDataShardSize), i, shardCh)
if err != nil {
logger.Errorf("directly get %dbatch shards fail:%v", i, err)
return
}
batchResultCh <- batch
}()
go func() {
err, batch := ec.New(ctx, d, p, 0).BatchRecon(ctx, i, batchDataShardSize, shardCh)
if err != nil {
logger.Errorf("recon %dbatch shards fail:%v", i, err)
return
}
batchResultCh <- batch
}()
select {
case <-ctx.Done():
errCh <- fmt.Errorf("cannot get %dth batch: timeout %v", i, 5*time.Minute)
case batch := <-batchResultCh:
out <- batch
}
}(i)
}
wg.Wait()
close(errCh)
var errs error
for err := range errCh {
errs = errors.Join(errs, err)
}
return errs
}
// ResolveCborLinks get sorted block links
func (ds *dagSession) ResolveCborLinks(ctx context.Context, shard api.Cid) ([]*format.Link, error) {
clusterDAGBlock, err := ds.blockGet(ctx, shard)
if err != nil {
return nil, err
}
clusterDAGNode, err := sharding.CborDataToNode(clusterDAGBlock, "cbor")
if err != nil {
return nil, err
}
blks := clusterDAGNode.Links()
links := make([]*format.Link, 0, len(blks))
var errs error
// traverse shard in order
// blks -> 0,cid0; 1,cid1
for i := 0; i < len(blks); i++ {
sh, _, err := clusterDAGNode.ResolveLink([]string{fmt.Sprintf("%d", i)})
if err != nil {
err = fmt.Errorf("cannot resolve %dst shard: %s", i, err)
}
errs = errors.Join(errs, err)
links = append(links, sh)
}
return links, errs
}
// convert shard to []byte
func (ds *dagSession) ECLink2Raw(ctx context.Context, sh *format.Link, isData bool) ([]byte, error) {
if isData {
links, err := ds.ResolveCborLinks(ctx, api.NewCid(sh.Cid)) // get sorted shards
if err != nil {
return nil, fmt.Errorf("cannot resolve shard(%s): %s", sh.Cid, err)
}
vect := make([][]byte, len(links)) // estimate size
errCh := make(chan error, len(links))
wg := sync.WaitGroup{}
wg.Add(len(links))
for i, link := range links {
go func(i int, ci api.Cid) {
defer wg.Done()
vect[i], err = ds.blockGet(ctx, ci)
if err != nil {
errCh <- err
}
}(i, api.NewCid(link.Cid))
}
wg.Wait()
close(errCh)
var errs error
for err := range errCh {
errs = errors.Join(errs, err)
}
if errs != nil {
return nil, errs
}
b := make([]byte, 0, len(vect)*len(vect[0]))
for _, v := range vect {
b = append(b, v...)
}
return b, nil
}
// directly get parity shard
return ds.GetRawFile(ctx, sh.Cid)
}
func (ds *dagSession) Add(ctx context.Context, node format.Node) error {
panic("unreachable code")
}
func (ds *dagSession) AddMany(ctx context.Context, nodes []format.Node) error {
panic("unreachable code")
}
func (ds *dagSession) Remove(ctx context.Context, c cid.Cid) error {
panic("unreachable code")
}
func (ds *dagSession) RemoveMany(ctx context.Context, cids []cid.Cid) error {
panic("unreachable code")
}
// https://github.com/ipfs/kubo/blob/a7c65184976e8717ac23d7efaa5b0d477ad15deb/core/commands/get.go#L93
func fileArchive(f files.Node, name string, out chan<- []byte) error {
logger.Infof("archiving file %s", name)
defer close(out)
compression := gzip.NoCompression // compression seems only has little effect, so tar is used anyway as a transport format
// need to connect a writer to a reader
piper, pipew := io.Pipe()
checkErrAndClosePipe := func(err error) bool {
if err != nil {
_ = pipew.CloseWithError(err)
return true
}
return false
}
// use a buffered writer to parallelize task
DefaultBufSize := 1048576
bufw := bufio.NewWriterSize(pipew, DefaultBufSize)
// compression determines whether to use gzip compression.
maybeGzw, err := newMaybeGzWriter(bufw, compression)
if checkErrAndClosePipe(err) {
return err
}
closeGzwAndPipe := func() {
if err := maybeGzw.Close(); checkErrAndClosePipe(err) {
return
}
if err := bufw.Flush(); checkErrAndClosePipe(err) {
return
}
pipew.Close() // everything seems to be ok.
}
// construct the tar writer
w, err := files.NewTarWriter(maybeGzw)
if checkErrAndClosePipe(err) {
return err
}
go func() {
// write all the nodes recursively
if err := w.WriteFile(f, name); checkErrAndClosePipe(err) {
return
}
w.Close() // close tar writer
closeGzwAndPipe() // everything seems to be ok
}()
for {
b := make([]byte, DefaultBufSize)
n, err := piper.Read(b)
if n != 0 {
out <- b[:n:n]
}
if err != nil {
if err == io.EOF {
err = nil
}
return err
}
}
}
func newMaybeGzWriter(w io.Writer, compression int) (io.WriteCloser, error) {
if compression != gzip.NoCompression {
return gzip.NewWriterLevel(w, compression)
}
return &identityWriteCloser{w}, nil
}
type identityWriteCloser struct {
w io.Writer
}
func (i *identityWriteCloser) Write(p []byte) (int, error) {
return i.w.Write(p)
}
func (i *identityWriteCloser) Close() error {
return nil
}