lotus/storage/sectorstorage/localworker.go

213 lines
5.5 KiB
Go
Raw Normal View History

2020-03-23 11:40:02 +00:00
package sectorstorage
2020-03-05 02:18:22 +00:00
import (
"context"
"io"
2020-03-20 22:30:17 +00:00
"os"
2020-03-05 02:18:22 +00:00
2020-03-20 22:30:17 +00:00
"github.com/elastic/go-sysinfo"
"golang.org/x/xerrors"
ffi "github.com/filecoin-project/filecoin-ffi"
2020-03-05 02:18:22 +00:00
"github.com/filecoin-project/specs-actors/actors/abi"
2020-03-11 05:49:17 +00:00
storage2 "github.com/filecoin-project/specs-storage/storage"
2020-03-26 19:34:38 +00:00
"github.com/filecoin-project/lotus/storage/sectorstorage/ffiwrapper"
2020-03-23 11:40:02 +00:00
"github.com/filecoin-project/lotus/storage/sectorstorage/sealtasks"
"github.com/filecoin-project/lotus/storage/sectorstorage/stores"
2020-03-05 02:18:22 +00:00
)
2020-03-26 02:50:56 +00:00
var pathTypes = []stores.SectorFileType{stores.FTUnsealed, stores.FTSealed, stores.FTCache}
type WorkerConfig struct {
SealProof abi.RegisteredProof
2020-03-23 11:40:02 +00:00
TaskTypes []sealtasks.TaskType
}
2020-03-11 21:23:16 +00:00
type LocalWorker struct {
2020-03-26 02:50:56 +00:00
scfg *ffiwrapper.Config
2020-03-13 11:59:19 +00:00
storage stores.Store
2020-03-13 01:37:38 +00:00
localStore *stores.Local
sindex stores.SectorIndex
2020-03-23 11:40:02 +00:00
acceptTasks map[sealtasks.TaskType]struct{}
2020-03-13 01:37:38 +00:00
}
func NewLocalWorker(wcfg WorkerConfig, store stores.Store, local *stores.Local, sindex stores.SectorIndex) *LocalWorker {
ppt, err := wcfg.SealProof.RegisteredPoStProof()
2020-03-13 01:37:38 +00:00
if err != nil {
panic(err)
}
2020-03-23 11:40:02 +00:00
acceptTasks := map[sealtasks.TaskType]struct{}{}
for _, taskType := range wcfg.TaskTypes {
acceptTasks[taskType] = struct{}{}
}
2020-03-13 01:37:38 +00:00
return &LocalWorker{
2020-03-26 02:50:56 +00:00
scfg: &ffiwrapper.Config{
SealProofType: wcfg.SealProof,
2020-03-13 01:37:38 +00:00
PoStProofType: ppt,
},
storage: store,
localStore: local,
sindex: sindex,
acceptTasks: acceptTasks,
2020-03-13 01:37:38 +00:00
}
2020-03-05 02:18:22 +00:00
}
type localWorkerPathProvider struct {
2020-03-11 21:23:16 +00:00
w *LocalWorker
2020-03-05 02:18:22 +00:00
}
2020-03-26 02:50:56 +00:00
func (l *localWorkerPathProvider) AcquireSector(ctx context.Context, sector abi.SectorID, existing stores.SectorFileType, allocate stores.SectorFileType, sealing bool) (stores.SectorPaths, func(), error) {
paths, storageIDs, done, err := l.w.storage.AcquireSector(ctx, sector, existing, allocate, sealing)
if err != nil {
2020-03-26 02:50:56 +00:00
return stores.SectorPaths{}, nil, err
}
log.Debugf("acquired sector %d (e:%d; a:%d): %v", sector, existing, allocate, paths)
return paths, func() {
done()
for _, fileType := range pathTypes {
if fileType&allocate == 0 {
continue
}
2020-03-26 02:50:56 +00:00
sid := stores.PathByType(storageIDs, fileType)
if err := l.w.sindex.StorageDeclareSector(ctx, stores.ID(sid), sector, fileType); err != nil {
log.Errorf("declare sector error: %+v", err)
}
}
}, nil
2020-03-05 02:18:22 +00:00
}
2020-03-26 19:34:38 +00:00
func (l *LocalWorker) sb() (ffiwrapper.Storage, error) {
2020-03-26 02:50:56 +00:00
return ffiwrapper.New(&localWorkerPathProvider{w: l}, l.scfg)
2020-03-05 02:18:22 +00:00
}
2020-03-17 20:19:52 +00:00
func (l *LocalWorker) NewSector(ctx context.Context, sector abi.SectorID) error {
sb, err := l.sb()
if err != nil {
return err
}
return sb.NewSector(ctx, sector)
}
func (l *LocalWorker) AddPiece(ctx context.Context, sector abi.SectorID, epcs []abi.UnpaddedPieceSize, sz abi.UnpaddedPieceSize, r io.Reader) (abi.PieceInfo, error) {
2020-03-05 02:18:22 +00:00
sb, err := l.sb()
if err != nil {
return abi.PieceInfo{}, err
}
2020-03-17 20:19:52 +00:00
return sb.AddPiece(ctx, sector, epcs, sz, r)
2020-03-05 02:18:22 +00:00
}
2020-03-17 20:19:52 +00:00
func (l *LocalWorker) SealPreCommit1(ctx context.Context, sector abi.SectorID, ticket abi.SealRandomness, pieces []abi.PieceInfo) (out storage2.PreCommit1Out, err error) {
2020-03-05 18:18:33 +00:00
sb, err := l.sb()
if err != nil {
return nil, err
}
2020-03-17 20:19:52 +00:00
return sb.SealPreCommit1(ctx, sector, ticket, pieces)
2020-03-05 02:18:22 +00:00
}
2020-03-17 20:19:52 +00:00
func (l *LocalWorker) SealPreCommit2(ctx context.Context, sector abi.SectorID, phase1Out storage2.PreCommit1Out) (cids storage2.SectorCids, err error) {
2020-03-05 18:18:33 +00:00
sb, err := l.sb()
if err != nil {
2020-03-17 20:19:52 +00:00
return storage2.SectorCids{}, err
2020-03-05 18:18:33 +00:00
}
2020-03-17 20:19:52 +00:00
return sb.SealPreCommit2(ctx, sector, phase1Out)
2020-03-05 02:18:22 +00:00
}
2020-03-17 20:19:52 +00:00
func (l *LocalWorker) SealCommit1(ctx context.Context, sector abi.SectorID, ticket abi.SealRandomness, seed abi.InteractiveSealRandomness, pieces []abi.PieceInfo, cids storage2.SectorCids) (output storage2.Commit1Out, err error) {
2020-03-05 18:18:33 +00:00
sb, err := l.sb()
if err != nil {
return nil, err
}
2020-03-17 20:19:52 +00:00
return sb.SealCommit1(ctx, sector, ticket, seed, pieces, cids)
2020-03-05 02:18:22 +00:00
}
2020-03-17 20:19:52 +00:00
func (l *LocalWorker) SealCommit2(ctx context.Context, sector abi.SectorID, phase1Out storage2.Commit1Out) (proof storage2.Proof, err error) {
2020-03-05 18:18:33 +00:00
sb, err := l.sb()
if err != nil {
return nil, err
}
2020-03-17 20:19:52 +00:00
return sb.SealCommit2(ctx, sector, phase1Out)
2020-03-05 02:18:22 +00:00
}
2020-03-17 20:19:52 +00:00
func (l *LocalWorker) FinalizeSector(ctx context.Context, sector abi.SectorID) error {
2020-03-05 18:18:33 +00:00
sb, err := l.sb()
if err != nil {
return err
}
if err := sb.FinalizeSector(ctx, sector); err != nil {
return xerrors.Errorf("finalizing sector: %w", err)
}
2020-03-26 02:50:56 +00:00
if err := l.storage.Remove(ctx, sector, stores.FTUnsealed); err != nil {
return xerrors.Errorf("removing unsealed data: %w", err)
}
2020-03-26 02:50:56 +00:00
if err := l.storage.MoveStorage(ctx, sector, stores.FTSealed|stores.FTCache); err != nil {
return xerrors.Errorf("moving sealed data to storage: %w", err)
}
return nil
2020-03-05 02:18:22 +00:00
}
2020-03-23 11:40:02 +00:00
func (l *LocalWorker) TaskTypes(context.Context) (map[sealtasks.TaskType]struct{}, error) {
return l.acceptTasks, nil
2020-03-05 02:18:22 +00:00
}
2020-03-19 15:10:19 +00:00
func (l *LocalWorker) Paths(ctx context.Context) ([]stores.StoragePath, error) {
return l.localStore.Local(ctx)
2020-03-05 02:18:22 +00:00
}
func (l *LocalWorker) Info(context.Context) (WorkerInfo, error) {
2020-03-20 22:30:17 +00:00
hostname, err := os.Hostname() // TODO: allow overriding from config
if err != nil {
panic(err)
}
gpus, err := ffi.GetGPUDevices()
if err != nil {
log.Errorf("getting gpu devices failed: %+v", err)
}
h, err := sysinfo.Host()
if err != nil {
return WorkerInfo{}, xerrors.Errorf("getting host info: %w", err)
2020-03-20 22:30:17 +00:00
}
mem, err := h.Memory()
if err != nil {
return WorkerInfo{}, xerrors.Errorf("getting memory info: %w", err)
2020-03-20 22:30:17 +00:00
}
return WorkerInfo{
2020-03-20 22:30:17 +00:00
Hostname: hostname,
Resources: WorkerResources{
2020-03-20 22:30:17 +00:00
MemPhysical: mem.Total,
MemSwap: mem.VirtualTotal,
MemReserved: mem.VirtualUsed + mem.Total - mem.Available, // TODO: sub this process
GPUs: gpus,
},
}, nil
}
2020-03-24 23:49:45 +00:00
func (l *LocalWorker) Close() error {
return nil
}
2020-03-11 21:23:16 +00:00
var _ Worker = &LocalWorker{}