Skip to content

Commit a76b2c2

Browse files
committed
implement a custom io.Reader for snapshot cars
the snapshot car files are continually appended to while the ExportRangeInternal process executes. if one uses the os.File io.Reader, it will encounter an EOF before the snapshot is complete. our custom io.Reader returns an io.EOF when the ExportRangeInternal method completes
1 parent 0da5925 commit a76b2c2

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

cmd/filecoin-chain-archiver/cmds/create.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,43 +43,46 @@ func Compress(in io.Reader, out io.Writer) error {
4343
return enc.Close()
4444
}
4545

46-
type autocloser struct {
47-
rc io.ReadCloser
48-
}
49-
50-
func (ac autocloser) Read(p []byte) (n int, err error) {
51-
n, err = ac.rc.Read(p)
52-
if err != nil {
53-
_ = ac.rc.Close()
54-
}
55-
return
56-
}
57-
58-
func AutoCloser(rc io.ReadCloser) io.Reader {
59-
return &autocloser{rc}
46+
type snapshotInfo struct {
47+
digest string
48+
size int64
49+
filename string
50+
latestIndex string
51+
latestLocation string
6052
}
6153

62-
type multi struct {
63-
io.Writer
64-
cs []io.Closer
54+
type snapshotReader struct {
55+
data []byte
56+
fileLocation string
57+
errCh chan error
6558
}
6659

67-
func (m *multi) Close() error {
68-
var first error
69-
for _, c := range m.cs {
70-
if err := c.Close(); err != nil && first == nil {
71-
first = err
60+
func (sr *snapshotReader) Read(p []byte) (n int, err error) {
61+
r, err := os.OpenFile(sr.fileLocation, os.O_RDONLY, 444)
62+
if err != nil {
63+
return n, err
64+
}
65+
defer r.Close()
66+
for {
67+
n, err := r.Read(p)
68+
if err != nil && err != io.EOF {
69+
return n, err
70+
}
71+
select {
72+
case err := <- sr.errCh:
73+
if err != nil {
74+
return n, err
75+
}
76+
return n, io.EOF
7277
}
7378
}
74-
return first
7579
}
7680

77-
type snapshotInfo struct {
78-
digest string
79-
size int64
80-
filename string
81-
latestIndex string
82-
latestLocation string
81+
func newSnapshotReader(f string, errChan chan error) *snapshotReader {
82+
return &snapshotReader{
83+
fileLocation: f,
84+
errCh: errChan,
85+
}
8386
}
8487

8588
var cmdCreate = &cli.Command{
@@ -401,15 +404,11 @@ var cmdCreate = &cli.Command{
401404
}
402405
break
403406
}
404-
rr, err := os.OpenFile(rrPath, os.O_RDONLY, 444)
405-
if err != nil {
406-
return err
407-
}
408-
defer rr.Close()
407+
408+
rr := newSnapshotReader(rrPath, errCh)
409409

410410
go func() {
411411
var lastSize int64
412-
fmt.Printf("rrPath: %v", rrPath)
413412
for {
414413
select {
415414
case <-time.After(flagProgressUpdate):

0 commit comments

Comments
 (0)