2020-08-17 13:26:18 +00:00
|
|
|
package sectorstorage
|
2020-03-23 11:40:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-03-23 11:40:02 +00:00
|
|
|
|
2020-11-04 20:29:08 +00:00
|
|
|
"github.com/filecoin-project/specs-storage/storage"
|
2020-03-27 20:08:06 +00:00
|
|
|
|
2020-08-17 13:26:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
|
2020-09-30 17:32:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
|
2020-03-23 11:40:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type readonlyProvider struct {
|
2021-07-27 03:15:53 +00:00
|
|
|
index stores.SectorIndex
|
|
|
|
stor *stores.Local
|
|
|
|
remote *stores.Remote
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:29:08 +00:00
|
|
|
func (l *readonlyProvider) AcquireSector(ctx context.Context, id storage.SectorRef, existing storiface.SectorFileType, allocate storiface.SectorFileType, sealing storiface.PathType) (storiface.SectorPaths, func(), error) {
|
2020-09-06 16:54:00 +00:00
|
|
|
if allocate != storiface.FTNone {
|
|
|
|
return storiface.SectorPaths{}, nil, xerrors.New("read-only storage")
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 20:00:34 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-06-08 16:57:56 +00:00
|
|
|
|
|
|
|
// use TryLock to avoid blocking
|
2020-11-04 20:29:08 +00:00
|
|
|
locked, err := l.index.StorageTryLock(ctx, id.ID, existing, storiface.FTNone)
|
2020-06-08 16:57:56 +00:00
|
|
|
if err != nil {
|
2020-06-09 09:14:25 +00:00
|
|
|
cancel()
|
2020-09-06 16:54:00 +00:00
|
|
|
return storiface.SectorPaths{}, nil, xerrors.Errorf("acquiring sector lock: %w", err)
|
2020-06-03 20:00:34 +00:00
|
|
|
}
|
2020-06-08 16:57:56 +00:00
|
|
|
if !locked {
|
2020-06-09 09:14:25 +00:00
|
|
|
cancel()
|
2020-09-06 16:54:00 +00:00
|
|
|
return storiface.SectorPaths{}, nil, xerrors.Errorf("failed to acquire sector lock")
|
2020-06-08 16:57:56 +00:00
|
|
|
}
|
2020-06-03 20:00:34 +00:00
|
|
|
|
2020-11-04 20:29:08 +00:00
|
|
|
p, _, err := l.stor.AcquireSector(ctx, id, existing, allocate, sealing, storiface.AcquireMove)
|
2020-03-23 11:40:02 +00:00
|
|
|
|
2020-06-04 19:00:16 +00:00
|
|
|
return p, cancel, err
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
2021-07-27 03:15:53 +00:00
|
|
|
|
|
|
|
func (l *readonlyProvider) AcquireSectorPaths(ctx context.Context, id storage.SectorRef, existing storiface.SectorFileType, sealing storiface.PathType) (storiface.SectorPaths, func(), error) {
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
// use TryLock to avoid blocking
|
|
|
|
locked, err := l.index.StorageTryLock(ctx, id.ID, existing, storiface.FTNone)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return storiface.SectorPaths{}, nil, xerrors.Errorf("acquiring sector lock: %w", err)
|
|
|
|
}
|
|
|
|
if !locked {
|
|
|
|
cancel()
|
|
|
|
return storiface.SectorPaths{}, nil, xerrors.Errorf("failed to acquire sector lock")
|
|
|
|
}
|
|
|
|
|
|
|
|
p, _, err := l.remote.AcquireSectorPaths(ctx, id, existing, storiface.FTNone, sealing)
|
|
|
|
|
|
|
|
return p, cancel, err
|
|
|
|
}
|