Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ffiwrapper: Close readers in AddPiece #9328

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions storage/sealer/ffiwrapper/sealer_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (sb *Sealer) DataCid(ctx context.Context, pieceSize abi.UnpaddedPieceSize,
log.Warnf("DataCid: cannot close pieceData reader %T because it is not an io.Closer", origPieceData)
return
}
closer.Close() //nolint:errcheck
if err := closer.Close(); err != nil {
log.Warnw("closing pieceData in DataCid", "error", err)
}
}()

pieceData = io.LimitReader(io.MultiReader(
Expand Down Expand Up @@ -182,7 +184,19 @@ func (sb *Sealer) DataCid(ctx context.Context, pieceSize abi.UnpaddedPieceSize,
}, nil
}

func (sb *Sealer) AddPiece(ctx context.Context, sector storiface.SectorRef, existingPieceSizes []abi.UnpaddedPieceSize, pieceSize abi.UnpaddedPieceSize, file storiface.Data) (abi.PieceInfo, error) {
func (sb *Sealer) AddPiece(ctx context.Context, sector storiface.SectorRef, existingPieceSizes []abi.UnpaddedPieceSize, pieceSize abi.UnpaddedPieceSize, pieceData storiface.Data) (abi.PieceInfo, error) {
origPieceData := pieceData
defer func() {
closer, ok := origPieceData.(io.Closer)
if !ok {
log.Warnf("AddPiece: cannot close pieceData reader %T because it is not an io.Closer", origPieceData)
return
}
if err := closer.Close(); err != nil {
log.Warnw("closing pieceData in AddPiece", "error", err)
}
}()

// TODO: allow tuning those:
chunk := abi.PaddedPieceSize(4 << 20)
parallel := runtime.NumCPU()
Expand Down Expand Up @@ -248,7 +262,7 @@ func (sb *Sealer) AddPiece(ctx context.Context, sector storiface.SectorRef, exis

pw := fr32.NewPadWriter(w)

pr := io.TeeReader(io.LimitReader(file, int64(pieceSize)), pw)
pr := io.TeeReader(io.LimitReader(pieceData, int64(pieceSize)), pw)

throttle := make(chan []byte, parallel)
piecePromises := make([]func() (abi.PieceInfo, error), 0)
Expand Down
84 changes: 84 additions & 0 deletions storage/sealer/ffiwrapper/sealer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,87 @@ func TestPoStChallengeAssumptions(t *testing.T) {
require.Len(t, c1.Challenges, 3)
}
}

func TestDCAPCloses(t *testing.T) {
sz := abi.PaddedPieceSize(2 << 10).Unpadded()

cdir, err := ioutil.TempDir("", "sbtest-c-")
if err != nil {
t.Fatal(err)
}
miner := abi.ActorID(123)

sp := &basicfs.Provider{
Root: cdir,
}
sb, err := New(sp)
if err != nil {
t.Fatalf("%+v", err)
}
cleanup := func() {
if t.Failed() {
fmt.Printf("not removing %s\n", cdir)
return
}
if err := os.RemoveAll(cdir); err != nil {
t.Error(err)
}
}
t.Cleanup(cleanup)

t.Run("DataCid", func(t *testing.T) {
r := rand.New(rand.NewSource(0x7e5))

clr := &closeAssertReader{
Reader: io.LimitReader(r, int64(sz)),
}

c, err := sb.DataCid(context.TODO(), sz, clr)
if err != nil {
t.Fatal(err)
}

require.Equal(t, "baga6ea4seaqeje7jy4hufnybpo7ckxzujaigqbcxhdjq7ojb4b6xzgqdugkyciq", c.PieceCID.String())
require.True(t, clr.closed)
})

t.Run("AddPiece", func(t *testing.T) {
r := rand.New(rand.NewSource(0x7e5))

clr := &closeAssertReader{
Reader: io.LimitReader(r, int64(sz)),
}

c, err := sb.AddPiece(context.TODO(), storiface.SectorRef{
ID: abi.SectorID{
Miner: miner,
Number: 0,
},
ProofType: abi.RegisteredSealProof_StackedDrg2KiBV1_1,
}, nil, sz, clr)
if err != nil {
t.Fatal(err)
}

require.Equal(t, "baga6ea4seaqeje7jy4hufnybpo7ckxzujaigqbcxhdjq7ojb4b6xzgqdugkyciq", c.PieceCID.String())
require.True(t, clr.closed)
})

}

type closeAssertReader struct {
io.Reader
closed bool
}

func (c *closeAssertReader) Close() error {
if c.closed {
panic("double close")
}

c.closed = true

return nil
}

var _ io.Closer = &closeAssertReader{}