2022-06-14 18:03:38 +00:00
|
|
|
package sealer
|
2021-05-18 07:32:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"io"
|
2023-05-23 14:45:46 +00:00
|
|
|
"sync"
|
2021-05-18 07:32:30 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2023-05-23 09:59:40 +00:00
|
|
|
pool "github.com/libp2p/go-buffer-pool"
|
2021-05-18 07:32:30 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2021-12-08 22:16:27 +00:00
|
|
|
"github.com/filecoin-project/dagstore/mount"
|
2021-05-18 07:32:30 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
|
2022-06-14 18:25:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/paths"
|
2022-06-14 18:03:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/fr32"
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2021-05-18 07:32:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Unsealer interface {
|
2021-05-18 11:35:25 +00:00
|
|
|
// SectorsUnsealPiece will Unseal a Sealed sector file for the given sector.
|
2022-06-17 11:31:05 +00:00
|
|
|
SectorsUnsealPiece(ctx context.Context, sector storiface.SectorRef, offset storiface.UnpaddedByteIndex, size abi.UnpaddedPieceSize, randomness abi.SealRandomness, commd *cid.Cid) error
|
2021-05-18 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PieceProvider interface {
|
2021-05-18 11:35:25 +00:00
|
|
|
// ReadPiece is used to read an Unsealed piece at the given offset and of the given size from a Sector
|
2021-11-26 16:40:43 +00:00
|
|
|
// pieceOffset + pieceSize specify piece bounds for unsealing (note: with SDR the entire sector will be unsealed by
|
|
|
|
// default in most cases, but this might matter with future PoRep)
|
|
|
|
// startOffset is added to the pieceOffset to get the starting reader offset.
|
|
|
|
// The number of bytes that can be read is pieceSize-startOffset
|
2022-06-17 11:31:05 +00:00
|
|
|
ReadPiece(ctx context.Context, sector storiface.SectorRef, pieceOffset storiface.UnpaddedByteIndex, pieceSize abi.UnpaddedPieceSize, ticket abi.SealRandomness, unsealed cid.Cid) (mount.Reader, bool, error)
|
|
|
|
IsUnsealed(ctx context.Context, sector storiface.SectorRef, offset storiface.UnpaddedByteIndex, size abi.UnpaddedPieceSize) (bool, error)
|
2021-05-18 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 17:10:21 +00:00
|
|
|
var _ PieceProvider = &pieceProvider{}
|
|
|
|
|
2021-05-18 07:32:30 +00:00
|
|
|
type pieceProvider struct {
|
2022-06-14 18:25:52 +00:00
|
|
|
storage *paths.Remote
|
|
|
|
index paths.SectorIndex
|
2021-05-18 07:32:30 +00:00
|
|
|
uns Unsealer
|
|
|
|
}
|
|
|
|
|
2022-06-14 18:25:52 +00:00
|
|
|
func NewPieceProvider(storage *paths.Remote, index paths.SectorIndex, uns Unsealer) PieceProvider {
|
2021-05-18 07:32:30 +00:00
|
|
|
return &pieceProvider{
|
|
|
|
storage: storage,
|
|
|
|
index: index,
|
|
|
|
uns: uns,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 17:10:21 +00:00
|
|
|
// IsUnsealed checks if we have the unsealed piece at the given offset in an already
|
|
|
|
// existing unsealed file either locally or on any of the workers.
|
2022-06-17 11:31:05 +00:00
|
|
|
func (p *pieceProvider) IsUnsealed(ctx context.Context, sector storiface.SectorRef, offset storiface.UnpaddedByteIndex, size abi.UnpaddedPieceSize) (bool, error) {
|
2021-05-22 17:10:21 +00:00
|
|
|
if err := offset.Valid(); err != nil {
|
|
|
|
return false, xerrors.Errorf("offset is not valid: %w", err)
|
|
|
|
}
|
|
|
|
if err := size.Validate(); err != nil {
|
|
|
|
return false, xerrors.Errorf("size is not a valid piece size: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 04:57:43 +00:00
|
|
|
ctxLock, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := p.index.StorageLock(ctxLock, sector.ID, storiface.FTUnsealed, storiface.FTNone); err != nil {
|
2021-05-22 17:10:21 +00:00
|
|
|
return false, xerrors.Errorf("acquiring read sector lock: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 04:57:43 +00:00
|
|
|
return p.storage.CheckIsUnsealed(ctxLock, sector, abi.PaddedPieceSize(offset.Padded()), size.Padded())
|
2021-05-22 17:10:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 11:35:25 +00:00
|
|
|
// tryReadUnsealedPiece will try to read the unsealed piece from an existing unsealed sector file for the given sector from any worker that has it.
|
|
|
|
// It will NOT try to schedule an Unseal of a sealed sector file for the read.
|
|
|
|
//
|
2021-05-19 05:47:56 +00:00
|
|
|
// Returns a nil reader if the piece does NOT exist in any unsealed file or there is no unsealed file for the given sector on any of the workers.
|
2023-05-19 12:33:35 +00:00
|
|
|
func (p *pieceProvider) tryReadUnsealedPiece(ctx context.Context, pc cid.Cid, sector storiface.SectorRef, pieceOffset storiface.UnpaddedByteIndex, pieceSize abi.UnpaddedPieceSize) (mount.Reader, error) {
|
2021-05-18 07:32:30 +00:00
|
|
|
// acquire a lock purely for reading unsealed sectors
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
if err := p.index.StorageLock(ctx, sector.ID, storiface.FTUnsealed, storiface.FTNone); err != nil {
|
|
|
|
cancel()
|
2021-12-08 22:16:27 +00:00
|
|
|
return nil, xerrors.Errorf("acquiring read sector lock: %w", err)
|
2021-05-18 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 13:52:33 +00:00
|
|
|
// Reader returns a reader getter for an unsealed piece at the given offset in the given sector.
|
|
|
|
// The returned reader will be nil if none of the workers has an unsealed sector file containing
|
|
|
|
// the unsealed piece.
|
2023-05-25 13:55:12 +00:00
|
|
|
readerGetter, err := p.storage.Reader(ctx, sector, abi.PaddedPieceSize(pieceOffset.Padded()), pieceSize.Padded())
|
2021-12-09 13:52:33 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
log.Debugf("did not get storage reader;sector=%+v, err:%s", sector.ID, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-25 13:55:12 +00:00
|
|
|
if readerGetter == nil {
|
2021-12-09 13:52:33 +00:00
|
|
|
cancel()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:16:27 +00:00
|
|
|
pr, err := (&pieceReader{
|
2023-05-25 13:55:12 +00:00
|
|
|
getReader: func(startOffset, readSize uint64) (io.ReadCloser, error) {
|
|
|
|
// The request is for unpadded bytes, at any offset.
|
|
|
|
// storage.Reader readers give us fr32-padded bytes, so we need to
|
|
|
|
// do the unpadding here.
|
|
|
|
|
|
|
|
startOffsetAligned := storiface.UnpaddedFloor(startOffset)
|
2023-05-19 12:33:35 +00:00
|
|
|
startOffsetDiff := int(startOffset - uint64(startOffsetAligned))
|
2021-12-08 22:16:27 +00:00
|
|
|
|
2023-05-19 12:33:35 +00:00
|
|
|
endOffset := startOffset + readSize
|
2023-05-25 13:55:12 +00:00
|
|
|
endOffsetAligned := storiface.UnpaddedCeil(endOffset)
|
2023-05-19 12:33:35 +00:00
|
|
|
|
2023-05-25 13:55:12 +00:00
|
|
|
r, err := readerGetter(startOffsetAligned.Padded(), endOffsetAligned.Padded())
|
2021-12-08 22:16:27 +00:00
|
|
|
if err != nil {
|
2021-12-09 13:52:33 +00:00
|
|
|
return nil, xerrors.Errorf("getting reader at +%d: %w", startOffsetAligned, err)
|
2021-12-08 22:16:27 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 16:47:51 +00:00
|
|
|
buf := pool.Get(fr32.BufSize(pieceSize.Padded()))
|
|
|
|
|
2023-05-19 12:33:35 +00:00
|
|
|
upr, err := fr32.NewUnpadReaderBuf(r, pieceSize.Padded(), buf)
|
2021-12-08 22:16:27 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Close() // nolint
|
|
|
|
return nil, xerrors.Errorf("creating unpadded reader: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bir := bufio.NewReaderSize(upr, 127)
|
|
|
|
if startOffset > uint64(startOffsetAligned) {
|
2023-05-19 12:33:35 +00:00
|
|
|
if _, err := bir.Discard(startOffsetDiff); err != nil {
|
2021-12-09 14:49:37 +00:00
|
|
|
r.Close() // nolint
|
2021-12-08 22:16:27 +00:00
|
|
|
return nil, xerrors.Errorf("discarding bytes for startOffset: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 14:45:46 +00:00
|
|
|
var closeOnce sync.Once
|
|
|
|
|
2021-12-08 22:16:27 +00:00
|
|
|
return struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
}{
|
|
|
|
Reader: bir,
|
2021-12-09 14:49:37 +00:00
|
|
|
Closer: funcCloser(func() error {
|
2023-05-23 14:45:46 +00:00
|
|
|
closeOnce.Do(func() {
|
|
|
|
pool.Put(buf)
|
|
|
|
})
|
2021-12-09 14:49:37 +00:00
|
|
|
return r.Close()
|
|
|
|
}),
|
2021-12-08 22:16:27 +00:00
|
|
|
}, nil
|
|
|
|
},
|
2023-05-19 12:33:35 +00:00
|
|
|
len: pieceSize,
|
2021-12-08 22:16:27 +00:00
|
|
|
onClose: cancel,
|
|
|
|
pieceCid: pc,
|
2023-05-25 13:55:12 +00:00
|
|
|
}).init(ctx)
|
2021-12-08 22:16:27 +00:00
|
|
|
if err != nil || pr == nil { // pr == nil to make sure we don't return typed nil
|
2021-05-18 07:32:30 +00:00
|
|
|
cancel()
|
2021-12-08 22:16:27 +00:00
|
|
|
return nil, err
|
2021-05-18 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 22:16:27 +00:00
|
|
|
return pr, err
|
2021-05-18 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 14:49:37 +00:00
|
|
|
type funcCloser func() error
|
|
|
|
|
|
|
|
func (f funcCloser) Close() error {
|
|
|
|
return f()
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.Closer = funcCloser(nil)
|
|
|
|
|
2021-05-18 11:35:25 +00:00
|
|
|
// ReadPiece is used to read an Unsealed piece at the given offset and of the given size from a Sector
|
|
|
|
// If an Unsealed sector file exists with the Piece Unsealed in it, we'll use that for the read.
|
|
|
|
// Otherwise, we will Unseal a Sealed sector file for the given sector and read the Unsealed piece from it.
|
2021-05-21 13:45:05 +00:00
|
|
|
// If we do NOT have an existing unsealed file containing the given piece thus causing us to schedule an Unseal,
|
|
|
|
// the returned boolean parameter will be set to true.
|
|
|
|
// If we have an existing unsealed file containing the given piece, the returned boolean will be set to false.
|
2022-06-17 11:31:05 +00:00
|
|
|
func (p *pieceProvider) ReadPiece(ctx context.Context, sector storiface.SectorRef, pieceOffset storiface.UnpaddedByteIndex, size abi.UnpaddedPieceSize, ticket abi.SealRandomness, unsealed cid.Cid) (mount.Reader, bool, error) {
|
2021-11-26 16:40:43 +00:00
|
|
|
if err := pieceOffset.Valid(); err != nil {
|
|
|
|
return nil, false, xerrors.Errorf("pieceOffset is not valid: %w", err)
|
2021-05-18 07:32:30 +00:00
|
|
|
}
|
|
|
|
if err := size.Validate(); err != nil {
|
|
|
|
return nil, false, xerrors.Errorf("size is not a valid piece size: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:16:27 +00:00
|
|
|
r, err := p.tryReadUnsealedPiece(ctx, unsealed, sector, pieceOffset, size)
|
2021-05-21 09:46:20 +00:00
|
|
|
|
2021-05-18 07:32:30 +00:00
|
|
|
if xerrors.Is(err, storiface.ErrSectorNotFound) {
|
2021-11-26 16:40:43 +00:00
|
|
|
log.Debugf("no unsealed sector file with unsealed piece, sector=%+v, pieceOffset=%d, size=%d", sector, pieceOffset, size)
|
2021-05-18 07:32:30 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-05-21 13:31:17 +00:00
|
|
|
log.Errorf("returning error from ReadPiece:%s", err)
|
2021-05-18 07:32:30 +00:00
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var uns bool
|
2021-05-19 06:06:37 +00:00
|
|
|
|
2021-05-18 07:32:30 +00:00
|
|
|
if r == nil {
|
2021-05-19 06:06:37 +00:00
|
|
|
// a nil reader means that none of the workers has an unsealed sector file
|
|
|
|
// containing the unsealed piece.
|
|
|
|
// we now need to unseal a sealed sector file for the given sector to read the unsealed piece from it.
|
2021-05-18 07:32:30 +00:00
|
|
|
uns = true
|
|
|
|
commd := &unsealed
|
|
|
|
if unsealed == cid.Undef {
|
|
|
|
commd = nil
|
|
|
|
}
|
2021-11-26 16:40:43 +00:00
|
|
|
if err := p.uns.SectorsUnsealPiece(ctx, sector, pieceOffset, size, ticket, commd); err != nil {
|
2021-05-21 13:31:17 +00:00
|
|
|
log.Errorf("failed to SectorsUnsealPiece: %s", err)
|
2021-05-18 07:32:30 +00:00
|
|
|
return nil, false, xerrors.Errorf("unsealing piece: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-26 16:40:43 +00:00
|
|
|
log.Debugf("unsealed a sector file to read the piece, sector=%+v, pieceOffset=%d, size=%d", sector, pieceOffset, size)
|
2021-05-18 11:35:25 +00:00
|
|
|
|
2021-12-08 22:16:27 +00:00
|
|
|
r, err = p.tryReadUnsealedPiece(ctx, unsealed, sector, pieceOffset, size)
|
2021-05-18 07:32:30 +00:00
|
|
|
if err != nil {
|
2021-05-21 13:31:17 +00:00
|
|
|
log.Errorf("failed to tryReadUnsealedPiece after SectorsUnsealPiece: %s", err)
|
2021-05-18 07:32:30 +00:00
|
|
|
return nil, true, xerrors.Errorf("read after unsealing: %w", err)
|
|
|
|
}
|
|
|
|
if r == nil {
|
2021-05-21 13:31:17 +00:00
|
|
|
log.Errorf("got no reader after unsealing piece")
|
2021-05-18 07:32:30 +00:00
|
|
|
return nil, true, xerrors.Errorf("got no reader after unsealing piece")
|
|
|
|
}
|
2021-11-26 16:40:43 +00:00
|
|
|
log.Debugf("got a reader to read unsealed piece, sector=%+v, pieceOffset=%d, size=%d", sector, pieceOffset, size)
|
2021-05-18 11:35:25 +00:00
|
|
|
} else {
|
2021-11-26 16:40:43 +00:00
|
|
|
log.Debugf("unsealed piece already exists, no need to unseal, sector=%+v, pieceOffset=%d, size=%d", sector, pieceOffset, size)
|
2021-05-18 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 22:16:27 +00:00
|
|
|
log.Debugf("returning reader to read unsealed piece, sector=%+v, pieceOffset=%d, size=%d", sector, pieceOffset, size)
|
2021-05-18 07:32:30 +00:00
|
|
|
|
2021-12-08 22:16:27 +00:00
|
|
|
return r, uns, nil
|
2021-05-18 07:32:30 +00:00
|
|
|
}
|