2019-11-21 14:10:51 +00:00
|
|
|
package main
|
2019-11-21 00:52:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-16 13:49:58 +00:00
|
|
|
"net/http"
|
2019-11-21 14:10:51 +00:00
|
|
|
|
2020-01-08 20:31:35 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
2020-01-02 19:08:49 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-12-06 00:27:32 +00:00
|
|
|
lapi "github.com/filecoin-project/lotus/api"
|
2019-11-21 00:52:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type worker struct {
|
2019-12-06 00:27:32 +00:00
|
|
|
api lapi.StorageMiner
|
2019-11-21 00:52:59 +00:00
|
|
|
minerEndpoint string
|
|
|
|
repo string
|
2019-11-21 18:38:43 +00:00
|
|
|
auth http.Header
|
2019-11-21 00:52:59 +00:00
|
|
|
|
2020-02-04 19:04:49 +00:00
|
|
|
limiter *limits
|
|
|
|
sb *sectorbuilder.SectorBuilder
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 19:04:49 +00:00
|
|
|
func acceptJobs(ctx context.Context, api lapi.StorageMiner, sb *sectorbuilder.SectorBuilder, limiter *limits, endpoint string, auth http.Header, repo string, noprecommit, nocommit bool) error {
|
2019-11-21 00:52:59 +00:00
|
|
|
w := &worker{
|
2019-11-21 14:10:51 +00:00
|
|
|
api: api,
|
|
|
|
minerEndpoint: endpoint,
|
2019-11-21 18:38:43 +00:00
|
|
|
auth: auth,
|
2019-11-21 14:10:51 +00:00
|
|
|
repo: repo,
|
2020-02-04 19:04:49 +00:00
|
|
|
|
|
|
|
limiter: limiter,
|
|
|
|
sb: sb,
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 00:27:32 +00:00
|
|
|
tasks, err := api.WorkerQueue(ctx, sectorbuilder.WorkerCfg{
|
|
|
|
NoPreCommit: noprecommit,
|
|
|
|
NoCommit: nocommit,
|
|
|
|
})
|
2019-11-21 00:52:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:53:32 +00:00
|
|
|
loop:
|
|
|
|
for {
|
2019-12-06 00:27:32 +00:00
|
|
|
log.Infof("Waiting for new task")
|
|
|
|
|
2019-12-04 16:53:32 +00:00
|
|
|
select {
|
|
|
|
case task := <-tasks:
|
2020-02-11 01:10:50 +00:00
|
|
|
log.Infof("New task: %d, sector %d, action: %d", task.TaskID, task.SectorNum, task.Type)
|
2019-11-21 18:38:43 +00:00
|
|
|
|
2019-12-04 16:53:32 +00:00
|
|
|
res := w.processTask(ctx, task)
|
2019-11-21 00:52:59 +00:00
|
|
|
|
2019-12-04 16:53:32 +00:00
|
|
|
log.Infof("Task %d done, err: %+v", task.TaskID, res.GoErr)
|
2019-11-21 18:38:43 +00:00
|
|
|
|
2019-12-04 16:53:32 +00:00
|
|
|
if err := api.WorkerDone(ctx, task.TaskID, res); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
break loop
|
2019-11-21 14:10:51 +00:00
|
|
|
}
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
2019-11-21 14:10:51 +00:00
|
|
|
|
|
|
|
log.Warn("acceptJobs exit")
|
|
|
|
return nil
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) processTask(ctx context.Context, task sectorbuilder.WorkerTask) sectorbuilder.SealRes {
|
|
|
|
switch task.Type {
|
|
|
|
case sectorbuilder.WorkerPreCommit:
|
|
|
|
case sectorbuilder.WorkerCommit:
|
|
|
|
default:
|
|
|
|
return errRes(xerrors.Errorf("unknown task type %d", task.Type))
|
|
|
|
}
|
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
if err := w.fetchSector(task.SectorNum, task.Type); err != nil {
|
2019-11-22 15:48:02 +00:00
|
|
|
return errRes(xerrors.Errorf("fetching sector: %w", err))
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 00:27:32 +00:00
|
|
|
log.Infof("Data fetched, starting computation")
|
|
|
|
|
2019-11-21 00:52:59 +00:00
|
|
|
var res sectorbuilder.SealRes
|
|
|
|
|
|
|
|
switch task.Type {
|
|
|
|
case sectorbuilder.WorkerPreCommit:
|
2020-02-04 19:04:49 +00:00
|
|
|
w.limiter.workLimit <- struct{}{}
|
2020-02-11 01:10:50 +00:00
|
|
|
rspco, err := w.sb.SealPreCommit(ctx, task.SectorNum, task.SealTicket, task.Pieces)
|
2020-02-04 19:04:49 +00:00
|
|
|
<-w.limiter.workLimit
|
|
|
|
|
2019-11-21 00:52:59 +00:00
|
|
|
if err != nil {
|
2019-11-22 15:48:02 +00:00
|
|
|
return errRes(xerrors.Errorf("precomitting: %w", err))
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
2019-11-22 15:48:02 +00:00
|
|
|
res.Rspco = rspco.ToJson()
|
2019-11-21 00:52:59 +00:00
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
if err := w.push("sealed", task.SectorNum); err != nil {
|
2019-11-22 15:48:02 +00:00
|
|
|
return errRes(xerrors.Errorf("pushing precommited data: %w", err))
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
2019-11-30 13:22:50 +00:00
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
if err := w.push("cache", task.SectorNum); err != nil {
|
2019-11-30 13:22:50 +00:00
|
|
|
return errRes(xerrors.Errorf("pushing precommited data: %w", err))
|
|
|
|
}
|
2020-01-15 01:37:25 +00:00
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
if err := w.remove("staging", task.SectorNum); err != nil {
|
2020-01-15 01:37:25 +00:00
|
|
|
return errRes(xerrors.Errorf("cleaning up staged sector: %w", err))
|
|
|
|
}
|
2019-11-21 00:52:59 +00:00
|
|
|
case sectorbuilder.WorkerCommit:
|
2020-02-04 19:04:49 +00:00
|
|
|
w.limiter.workLimit <- struct{}{}
|
2020-02-11 01:10:50 +00:00
|
|
|
proof, err := w.sb.SealCommit(ctx, task.SectorNum, task.SealTicket, task.SealSeed, task.Pieces, task.Rspco)
|
2020-02-04 19:04:49 +00:00
|
|
|
<-w.limiter.workLimit
|
|
|
|
|
2019-11-21 19:51:48 +00:00
|
|
|
if err != nil {
|
2019-11-22 15:48:02 +00:00
|
|
|
return errRes(xerrors.Errorf("comitting: %w", err))
|
2019-11-21 19:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.Proof = proof
|
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
if err := w.push("cache", task.SectorNum); err != nil {
|
2019-11-30 13:22:50 +00:00
|
|
|
return errRes(xerrors.Errorf("pushing precommited data: %w", err))
|
|
|
|
}
|
2020-01-15 01:24:13 +00:00
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
if err := w.remove("sealed", task.SectorNum); err != nil {
|
2020-01-15 01:24:13 +00:00
|
|
|
return errRes(xerrors.Errorf("cleaning up sealed sector: %w", err))
|
|
|
|
}
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func errRes(err error) sectorbuilder.SealRes {
|
2019-11-30 13:22:50 +00:00
|
|
|
return sectorbuilder.SealRes{Err: err.Error(), GoErr: err}
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|