2019-10-31 18:46:53 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-04 00:44:29 +00:00
|
|
|
"fmt"
|
2019-11-07 18:22:59 +00:00
|
|
|
"io"
|
2019-12-08 15:14:40 +00:00
|
|
|
"math"
|
2019-11-01 22:05:05 +00:00
|
|
|
|
2019-12-19 21:06:25 +00:00
|
|
|
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
|
2019-11-01 22:05:05 +00:00
|
|
|
xerrors "golang.org/x/xerrors"
|
2019-11-07 18:22:59 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-12-02 12:51:16 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/padreader"
|
2019-10-31 18:46:53 +00:00
|
|
|
)
|
|
|
|
|
2019-12-08 16:06:08 +00:00
|
|
|
const NonceIncrement = math.MaxUint64
|
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
type sectorUpdate struct {
|
|
|
|
newState api.SectorState
|
|
|
|
id uint64
|
|
|
|
err error
|
2019-12-08 15:14:40 +00:00
|
|
|
nonce uint64
|
2019-11-01 13:58:48 +00:00
|
|
|
mut func(*SectorInfo)
|
|
|
|
}
|
2019-10-31 18:46:53 +00:00
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
func (u *sectorUpdate) fatal(err error) *sectorUpdate {
|
2019-12-08 15:14:40 +00:00
|
|
|
u.newState = api.FailedUnrecoverable
|
|
|
|
u.err = err
|
|
|
|
return u
|
2019-12-03 23:42:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:59:40 +00:00
|
|
|
func (u *sectorUpdate) error(err error) *sectorUpdate {
|
2019-12-08 15:14:40 +00:00
|
|
|
u.err = err
|
|
|
|
return u
|
2019-12-03 23:59:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
func (u *sectorUpdate) state(m func(*SectorInfo)) *sectorUpdate {
|
2019-12-08 15:14:40 +00:00
|
|
|
u.mut = m
|
|
|
|
return u
|
2019-12-03 23:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *sectorUpdate) to(newState api.SectorState) *sectorUpdate {
|
2019-12-09 13:32:58 +00:00
|
|
|
u.newState = newState
|
2019-12-08 15:14:40 +00:00
|
|
|
return u
|
2019-12-03 23:42:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 15:14:40 +00:00
|
|
|
func (u *sectorUpdate) setNonce(nc uint64) *sectorUpdate {
|
|
|
|
u.nonce = nc
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) UpdateSectorState(ctx context.Context, sector uint64, snonce uint64, state api.SectorState) error {
|
2019-12-05 04:43:54 +00:00
|
|
|
select {
|
|
|
|
case m.sectorUpdated <- sectorUpdate{
|
|
|
|
newState: state,
|
2019-12-08 15:14:40 +00:00
|
|
|
nonce: snonce,
|
2019-12-05 04:43:54 +00:00
|
|
|
id: sector,
|
|
|
|
}:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 12:12:05 +00:00
|
|
|
func (m *Miner) sectorStateLoop(ctx context.Context) error {
|
2019-11-29 19:11:01 +00:00
|
|
|
trackedSectors, err := m.ListSectors()
|
2019-11-17 12:12:05 +00:00
|
|
|
if err != nil {
|
2019-12-11 14:17:38 +00:00
|
|
|
log.Errorf("loading sector list: %+v", err)
|
2019-11-17 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2019-11-29 19:11:01 +00:00
|
|
|
for _, si := range trackedSectors {
|
2019-11-17 12:12:05 +00:00
|
|
|
select {
|
|
|
|
case m.sectorUpdated <- sectorUpdate{
|
|
|
|
newState: si.State,
|
2019-12-08 15:14:40 +00:00
|
|
|
nonce: si.Nonce,
|
2019-11-17 12:12:05 +00:00
|
|
|
id: si.SectorID,
|
|
|
|
err: nil,
|
|
|
|
mut: nil,
|
|
|
|
}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Warn("didn't restart processing for all sectors: ", ctx.Err())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-10-31 18:46:53 +00:00
|
|
|
|
2019-11-29 19:11:01 +00:00
|
|
|
{
|
|
|
|
// verify on-chain state
|
|
|
|
trackedByID := map[uint64]*SectorInfo{}
|
|
|
|
for _, si := range trackedSectors {
|
2019-12-02 12:51:16 +00:00
|
|
|
i := si
|
|
|
|
trackedByID[si.SectorID] = &i
|
2019-11-29 19:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
curTs, err := m.api.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting chain head: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := m.api.StateMinerProvingSet(ctx, m.maddr, curTs)
|
|
|
|
if err != nil {
|
2019-12-09 15:11:54 +00:00
|
|
|
return xerrors.Errorf("getting miner proving set: %w", err)
|
2019-11-29 19:11:01 +00:00
|
|
|
}
|
|
|
|
for _, ocs := range ps {
|
|
|
|
if _, ok := trackedByID[ocs.SectorID]; ok {
|
|
|
|
continue // TODO: check state
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: attempt recovery
|
|
|
|
log.Warnf("untracked sector %d found on chain", ocs.SectorID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
go func() {
|
|
|
|
defer log.Warn("quitting deal provider loop")
|
|
|
|
defer close(m.stopped)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case sector := <-m.sectorIncoming:
|
|
|
|
m.onSectorIncoming(sector)
|
|
|
|
case update := <-m.sectorUpdated:
|
|
|
|
m.onSectorUpdated(ctx, update)
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-11-17 12:12:05 +00:00
|
|
|
|
|
|
|
return nil
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
2019-10-31 18:46:53 +00:00
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
func (m *Miner) onSectorIncoming(sector *SectorInfo) {
|
2019-11-07 14:45:53 +00:00
|
|
|
has, err := m.sectors.Has(sector.SectorID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if has {
|
2019-11-07 18:22:59 +00:00
|
|
|
log.Warnf("SealPiece called more than once for sector %d", sector.SectorID)
|
2019-11-07 14:45:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
if err := m.sectors.Begin(sector.SectorID, sector); err != nil {
|
2019-12-03 23:42:22 +00:00
|
|
|
log.Errorf("sector tracking failed: %s", err)
|
2019-11-01 13:58:48 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-31 18:46:53 +00:00
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
go func() {
|
2019-11-01 22:05:05 +00:00
|
|
|
select {
|
|
|
|
case m.sectorUpdated <- sectorUpdate{
|
2019-11-06 23:09:48 +00:00
|
|
|
newState: api.Packing,
|
2019-11-01 13:58:48 +00:00
|
|
|
id: sector.SectorID,
|
2019-11-01 22:05:05 +00:00
|
|
|
}:
|
|
|
|
case <-m.stop:
|
|
|
|
log.Warn("failed to send incoming sector update, miner shutting down")
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-10-31 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
func (m *Miner) onSectorUpdated(ctx context.Context, update sectorUpdate) {
|
2019-12-03 23:42:22 +00:00
|
|
|
log.Infof("Sector %d updated state to %s", update.id, api.SectorStates[update.newState])
|
2019-11-01 13:58:48 +00:00
|
|
|
var sector SectorInfo
|
2020-01-14 02:04:33 +00:00
|
|
|
err := m.sectors.Get(update.id).Mutate(func(s *SectorInfo) error {
|
2019-12-08 15:14:40 +00:00
|
|
|
if update.nonce < s.Nonce {
|
|
|
|
return xerrors.Errorf("update nonce too low, ignoring (%d < %d)", update.nonce, s.Nonce)
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:06:08 +00:00
|
|
|
if update.nonce != NonceIncrement {
|
2019-12-08 15:14:40 +00:00
|
|
|
s.Nonce = update.nonce
|
|
|
|
} else {
|
|
|
|
s.Nonce++ // forced update
|
|
|
|
}
|
2019-11-01 13:58:48 +00:00
|
|
|
s.State = update.newState
|
2019-12-04 00:44:29 +00:00
|
|
|
if update.err != nil {
|
2019-12-06 00:27:32 +00:00
|
|
|
if s.LastErr != "" {
|
|
|
|
s.LastErr += "---------\n\n"
|
|
|
|
}
|
|
|
|
s.LastErr += fmt.Sprintf("entering state %s: %+v", api.SectorStates[update.newState], update.err)
|
2019-12-04 00:44:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 22:44:55 +00:00
|
|
|
if update.mut != nil {
|
|
|
|
update.mut(s)
|
|
|
|
}
|
2019-11-01 13:58:48 +00:00
|
|
|
sector = *s
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if update.err != nil {
|
2019-12-03 23:42:22 +00:00
|
|
|
log.Errorf("sector %d failed: %+v", update.id, update.err)
|
2019-10-31 18:46:53 +00:00
|
|
|
}
|
2019-11-01 13:58:48 +00:00
|
|
|
if err != nil {
|
2019-12-08 15:14:40 +00:00
|
|
|
log.Errorf("sector %d update error: %+v", update.id, err)
|
2019-11-01 03:57:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
/*
|
|
|
|
|
2019-12-08 16:06:08 +00:00
|
|
|
* Empty
|
|
|
|
| |
|
|
|
|
| v
|
|
|
|
*<- Packing <- incoming
|
|
|
|
| |
|
|
|
|
| v
|
|
|
|
*<- Unsealed <--> SealFailed
|
|
|
|
| |
|
|
|
|
| v
|
|
|
|
* PreCommitting <--> PreCommitFailed
|
|
|
|
| | ^
|
|
|
|
| v |
|
|
|
|
*<- PreCommitted ------/
|
|
|
|
| |||
|
|
|
|
| vvv v--> SealCommitFailed
|
|
|
|
*<- Committing
|
|
|
|
| | ^--> CommitFailed
|
|
|
|
| v ^
|
|
|
|
*<- CommitWait ---/
|
|
|
|
| |
|
|
|
|
| v
|
|
|
|
*<- Proving
|
|
|
|
|
|
|
|
|
v
|
|
|
|
FailedUnrecoverable
|
|
|
|
|
|
|
|
UndefinedSectorState <- ¯\_(ツ)_/¯
|
|
|
|
| ^
|
|
|
|
*---------------------/
|
2019-12-03 23:42:22 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
switch update.newState {
|
2019-12-03 23:59:40 +00:00
|
|
|
// Happy path
|
2019-11-06 23:09:48 +00:00
|
|
|
case api.Packing:
|
2019-12-03 23:42:22 +00:00
|
|
|
m.handleSectorUpdate(ctx, sector, m.handlePacking)
|
2019-11-01 13:58:48 +00:00
|
|
|
case api.Unsealed:
|
2019-12-03 23:42:22 +00:00
|
|
|
m.handleSectorUpdate(ctx, sector, m.handleUnsealed)
|
2019-11-01 13:58:48 +00:00
|
|
|
case api.PreCommitting:
|
2019-12-03 23:42:22 +00:00
|
|
|
m.handleSectorUpdate(ctx, sector, m.handlePreCommitting)
|
2019-11-01 13:58:48 +00:00
|
|
|
case api.PreCommitted:
|
2019-12-03 23:42:22 +00:00
|
|
|
m.handleSectorUpdate(ctx, sector, m.handlePreCommitted)
|
2019-11-01 13:58:48 +00:00
|
|
|
case api.Committing:
|
2019-12-03 23:42:22 +00:00
|
|
|
m.handleSectorUpdate(ctx, sector, m.handleCommitting)
|
2019-12-06 02:40:57 +00:00
|
|
|
case api.CommitWait:
|
|
|
|
m.handleSectorUpdate(ctx, sector, m.handleCommitWait)
|
2019-11-13 14:48:57 +00:00
|
|
|
case api.Proving:
|
|
|
|
// TODO: track sector health / expiration
|
|
|
|
log.Infof("Proving sector %d", update.id)
|
2019-12-03 23:59:40 +00:00
|
|
|
|
|
|
|
// Handled failure modes
|
|
|
|
case api.SealFailed:
|
2019-12-09 15:42:47 +00:00
|
|
|
log.Warnf("sector %d entered unimplemented state 'SealFailed'", update.id)
|
2019-12-03 23:59:40 +00:00
|
|
|
case api.PreCommitFailed:
|
2019-12-09 15:42:47 +00:00
|
|
|
log.Warnf("sector %d entered unimplemented state 'PreCommitFailed'", update.id)
|
2019-12-03 23:59:40 +00:00
|
|
|
case api.SealCommitFailed:
|
2019-12-09 15:42:47 +00:00
|
|
|
log.Warnf("sector %d entered unimplemented state 'SealCommitFailed'", update.id)
|
2019-12-03 23:59:40 +00:00
|
|
|
case api.CommitFailed:
|
2019-12-09 15:42:47 +00:00
|
|
|
log.Warnf("sector %d entered unimplemented state 'CommitFailed'", update.id)
|
2019-12-03 23:59:40 +00:00
|
|
|
|
2019-12-06 14:06:42 +00:00
|
|
|
// Faults
|
|
|
|
case api.Faulty:
|
|
|
|
m.handleSectorUpdate(ctx, sector, m.handleFaulty)
|
|
|
|
case api.FaultReported:
|
|
|
|
m.handleSectorUpdate(ctx, sector, m.handleFaultReported)
|
|
|
|
|
2019-12-03 23:59:40 +00:00
|
|
|
// Fatal errors
|
2019-12-03 23:42:22 +00:00
|
|
|
case api.UndefinedSectorState:
|
|
|
|
log.Error("sector update with undefined state!")
|
|
|
|
case api.FailedUnrecoverable:
|
|
|
|
log.Errorf("sector %d failed unrecoverably", update.id)
|
2019-11-08 23:20:42 +00:00
|
|
|
default:
|
2019-11-09 00:18:15 +00:00
|
|
|
log.Errorf("unexpected sector update state: %d", update.newState)
|
2019-10-31 18:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 17:58:31 +00:00
|
|
|
func (m *Miner) AllocatePiece(size uint64) (sectorID uint64, offset uint64, err error) {
|
|
|
|
if padreader.PaddedSize(size) != size {
|
|
|
|
return 0, 0, xerrors.Errorf("cannot allocate unpadded piece")
|
|
|
|
}
|
2019-11-01 22:44:55 +00:00
|
|
|
|
2019-11-07 18:22:59 +00:00
|
|
|
sid, err := m.sb.AcquireSectorId() // TODO: Put more than one thing in a sector
|
|
|
|
if err != nil {
|
2019-12-01 17:58:31 +00:00
|
|
|
return 0, 0, xerrors.Errorf("acquiring sector ID: %w", err)
|
2019-11-07 18:22:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:58:31 +00:00
|
|
|
// offset hard-coded to 0 since we only put one thing in a sector for now
|
|
|
|
return sid, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) SealPiece(ctx context.Context, size uint64, r io.Reader, sectorID uint64, dealID uint64) error {
|
|
|
|
log.Infof("Seal piece for deal %d", dealID)
|
|
|
|
|
|
|
|
ppi, err := m.sb.AddPiece(size, sectorID, r, []uint64{})
|
2019-11-07 18:22:59 +00:00
|
|
|
if err != nil {
|
2019-12-01 17:58:31 +00:00
|
|
|
return xerrors.Errorf("adding piece to sector: %w", err)
|
2019-11-07 18:22:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:58:31 +00:00
|
|
|
return m.newSector(ctx, sectorID, dealID, ppi)
|
2019-11-07 18:22:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:58:31 +00:00
|
|
|
func (m *Miner) newSector(ctx context.Context, sid uint64, dealID uint64, ppi sectorbuilder.PublicPieceInfo) error {
|
2019-11-01 22:05:05 +00:00
|
|
|
si := &SectorInfo{
|
2019-11-01 13:58:48 +00:00
|
|
|
SectorID: sid,
|
2019-11-07 18:22:59 +00:00
|
|
|
|
|
|
|
Pieces: []Piece{
|
|
|
|
{
|
|
|
|
DealID: dealID,
|
|
|
|
|
2019-11-07 18:43:15 +00:00
|
|
|
Size: ppi.Size,
|
|
|
|
CommP: ppi.CommP[:],
|
2019-11-07 18:22:59 +00:00
|
|
|
},
|
|
|
|
},
|
2019-11-01 22:05:05 +00:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case m.sectorIncoming <- si:
|
2019-11-01 13:58:48 +00:00
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
2019-11-01 22:05:05 +00:00
|
|
|
return xerrors.Errorf("failed to submit sector for sealing, queue full: %w", ctx.Err())
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
2019-10-31 18:46:53 +00:00
|
|
|
}
|