lotus/storage/sector/store.go

191 lines
3.8 KiB
Go
Raw Normal View History

2019-08-14 20:27:10 +00:00
package sector
import (
"context"
2019-09-23 10:50:28 +00:00
"github.com/filecoin-project/go-sectorbuilder/sealing_state"
2019-09-19 16:17:49 +00:00
"golang.org/x/xerrors"
2019-08-14 20:27:10 +00:00
"io"
"sync"
2019-08-14 21:33:52 +00:00
"time"
"github.com/filecoin-project/go-lotus/api"
2019-08-14 21:33:52 +00:00
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
logging "github.com/ipfs/go-log"
2019-08-14 20:27:10 +00:00
)
2019-08-14 21:33:52 +00:00
var log = logging.Logger("sectorstore")
2019-08-14 20:27:10 +00:00
// TODO: eventually handle sector storage here instead of in rust-sectorbuilder
type Store struct {
lk sync.Mutex
sb *sectorbuilder.SectorBuilder
waiting map[uint64]chan struct{}
incoming []chan sectorbuilder.SectorSealingStatus
// TODO: outdated chan
2019-08-14 22:17:27 +00:00
closeCh chan struct{}
2019-08-14 20:27:10 +00:00
}
func NewStore(sb *sectorbuilder.SectorBuilder) *Store {
return &Store{
sb: sb,
waiting: map[uint64]chan struct{}{},
2019-08-14 22:17:27 +00:00
closeCh: make(chan struct{}),
2019-08-14 20:27:10 +00:00
}
}
func (s *Store) Service() {
go s.service()
}
2019-08-14 21:33:52 +00:00
func (s *Store) poll() {
2019-09-19 16:17:49 +00:00
log.Debug("polling for sealed sectors...")
2019-08-14 21:33:52 +00:00
// get a list of sectors to poll
s.lk.Lock()
toPoll := make([]uint64, 0, len(s.waiting))
for id := range s.waiting {
toPoll = append(toPoll, id)
}
s.lk.Unlock()
var done []sectorbuilder.SectorSealingStatus
// check status of each
for _, sec := range toPoll {
status, err := s.sb.SealStatus(sec)
if err != nil {
log.Errorf("getting seal status: %s", err)
continue
}
2019-09-23 10:50:28 +00:00
if status.State == sealing_state.Sealed {
2019-08-14 21:33:52 +00:00
done = append(done, status)
}
}
// send updates
s.lk.Lock()
for _, sector := range done {
watch, ok := s.waiting[sector.SectorID]
if ok {
close(watch)
delete(s.waiting, sector.SectorID)
}
for _, c := range s.incoming {
c <- sector // TODO: ctx!
}
}
s.lk.Unlock()
}
2019-08-14 20:27:10 +00:00
func (s *Store) service() {
2019-08-14 21:33:52 +00:00
poll := time.Tick(5 * time.Second)
2019-08-14 20:27:10 +00:00
for {
select {
2019-08-14 21:33:52 +00:00
case <-poll:
s.poll()
2019-08-14 22:17:27 +00:00
case <-s.closeCh:
2019-08-14 20:27:10 +00:00
s.lk.Lock()
for _, c := range s.incoming {
close(c)
}
s.lk.Unlock()
return
}
}
}
2019-08-26 08:02:26 +00:00
func (s *Store) AddPiece(ref string, size uint64, r io.Reader) (sectorID uint64, err error) {
2019-09-23 10:50:28 +00:00
sectorID, err = s.sb.AddPiece(ref, size, r)
2019-08-14 20:27:10 +00:00
if err != nil {
return 0, err
}
2019-08-14 21:33:52 +00:00
2019-08-14 20:27:10 +00:00
s.lk.Lock()
_, exists := s.waiting[sectorID]
2019-08-14 21:33:52 +00:00
if !exists { // pieces can share sectors
2019-08-14 20:27:10 +00:00
s.waiting[sectorID] = make(chan struct{})
}
s.lk.Unlock()
2019-08-14 21:33:52 +00:00
2019-08-14 20:27:10 +00:00
return sectorID, nil
}
func (s *Store) CloseIncoming(c <-chan sectorbuilder.SectorSealingStatus) {
s.lk.Lock()
var at = -1
for i, ch := range s.incoming {
if ch == c {
at = i
}
}
if at == -1 {
s.lk.Unlock()
return
}
if len(s.incoming) > 1 {
2019-08-14 22:17:27 +00:00
last := len(s.incoming) - 1
s.incoming[at] = s.incoming[last]
s.incoming[last] = nil
2019-08-14 20:27:10 +00:00
}
s.incoming = s.incoming[:len(s.incoming)-1]
s.lk.Unlock()
}
func (s *Store) Incoming() <-chan sectorbuilder.SectorSealingStatus {
ch := make(chan sectorbuilder.SectorSealingStatus, 8)
s.lk.Lock()
s.incoming = append(s.incoming, ch)
s.lk.Unlock()
return ch
}
func (s *Store) WaitSeal(ctx context.Context, sector uint64) (sectorbuilder.SectorSealingStatus, error) {
s.lk.Lock()
watch, ok := s.waiting[sector]
s.lk.Unlock()
if ok {
select {
case <-watch:
case <-ctx.Done():
return sectorbuilder.SectorSealingStatus{}, ctx.Err()
}
}
return s.sb.SealStatus(sector)
}
2019-09-18 03:32:52 +00:00
func (s *Store) RunPoSt(ctx context.Context, sectors []*api.SectorInfo, r []byte, faults []uint64) ([]byte, error) {
2019-09-19 16:17:49 +00:00
sbsi := make([]sectorbuilder.SectorInfo, len(sectors))
for k, sector := range sectors {
var commR [sectorbuilder.CommLen]byte
if copy(commR[:], sector.CommR) != sectorbuilder.CommLen {
return nil, xerrors.Errorf("commR too short, %d bytes", len(sector.CommR))
}
sbsi[k] = sectorbuilder.SectorInfo{
SectorID: sector.SectorID,
CommR: commR,
}
}
ssi := sectorbuilder.NewSortedSectorInfo(sbsi)
var seed [sectorbuilder.CommLen]byte
if copy(seed[:], r) != sectorbuilder.CommLen {
return nil, xerrors.Errorf("random seed too short, %d bytes", len(r))
}
return s.sb.GeneratePoSt(ssi, seed, faults)
}
2019-08-14 20:27:10 +00:00
func (s *Store) Stop() {
2019-08-14 22:17:27 +00:00
close(s.closeCh)
2019-08-14 20:27:10 +00:00
}