lotus/extern/storage-sealing/terminate_batch.go

300 lines
7.1 KiB
Go
Raw Normal View History

2021-01-12 23:42:01 +00:00
package sealing
import (
"bytes"
"context"
2021-01-14 11:37:23 +00:00
"sort"
2021-01-12 23:42:01 +00:00
"sync"
"time"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
2021-01-13 21:19:10 +00:00
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/dline"
miner2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/miner"
2021-01-12 23:42:01 +00:00
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
)
var (
// TODO: config
TerminateBatchMax uint64 = 100 // adjust based on real-world gas numbers, actors limit at 10k
TerminateBatchMin uint64 = 1
TerminateBatchWait = 5 * time.Minute
)
type TerminateBatcherApi interface {
StateSectorPartition(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok TipSetToken) (*SectorLocation, error)
SendMsg(ctx context.Context, from, to address.Address, method abi.MethodNum, value, maxFee abi.TokenAmount, params []byte) (cid.Cid, error)
StateMinerInfo(context.Context, address.Address, TipSetToken) (miner.MinerInfo, error)
2021-01-13 21:19:10 +00:00
StateMinerProvingDeadline(context.Context, address.Address, TipSetToken) (*dline.Info, error)
2021-01-12 23:42:01 +00:00
}
type TerminateBatcher struct {
api TerminateBatcherApi
maddr address.Address
mctx context.Context
addrSel AddrSel
feeCfg FeeConfig
todo map[SectorLocation]*bitfield.BitField // MinerSectorLocation -> BitField
waiting map[SectorLocation][]chan cid.Cid
2021-01-13 22:32:04 +00:00
notify, stop, stopped chan struct{}
force chan chan *cid.Cid
lk sync.Mutex
2021-01-12 23:42:01 +00:00
}
func NewTerminationBatcher(mctx context.Context, maddr address.Address, api TerminateBatcherApi, addrSel AddrSel, feeCfg FeeConfig) *TerminateBatcher {
b := &TerminateBatcher{
api: api,
maddr: maddr,
mctx: mctx,
addrSel: addrSel,
feeCfg: feeCfg,
todo: map[SectorLocation]*bitfield.BitField{},
waiting: map[SectorLocation][]chan cid.Cid{},
notify: make(chan struct{}, 1),
2021-01-13 22:32:04 +00:00
force: make(chan chan *cid.Cid),
2021-01-12 23:42:01 +00:00
stop: make(chan struct{}),
stopped: make(chan struct{}),
}
go b.run()
return b
}
func (b *TerminateBatcher) run() {
2021-01-13 22:32:04 +00:00
var forceRes chan *cid.Cid
var lastMsg *cid.Cid
2021-01-12 23:42:01 +00:00
for {
2021-01-13 22:32:04 +00:00
if forceRes != nil {
forceRes <- lastMsg
forceRes = nil
}
lastMsg = nil
2021-01-12 23:42:01 +00:00
var notif, after bool
select {
case <-b.stop:
close(b.stopped)
return
case <-b.notify:
notif = true // send above max
case <-time.After(TerminateBatchWait):
after = true // send above min
2021-01-13 22:32:04 +00:00
case fr := <-b.force: // user triggered
forceRes = fr
2021-01-12 23:42:01 +00:00
}
2021-01-13 21:19:10 +00:00
dl, err := b.api.StateMinerProvingDeadline(b.mctx, b.maddr, nil)
if err != nil {
log.Errorw("TerminateBatcher: getting proving deadline info failed", "error", err)
continue
}
2021-01-12 23:42:01 +00:00
b.lk.Lock()
params := miner2.TerminateSectorsParams{}
2021-01-13 21:19:10 +00:00
var total uint64
2021-01-12 23:42:01 +00:00
for loc, sectors := range b.todo {
n, err := sectors.Count()
if err != nil {
log.Errorw("TerminateBatcher: failed to count sectors to terminate", "deadline", loc.Deadline, "partition", loc.Partition, "error", err)
}
2021-01-13 21:19:10 +00:00
// don't send terminations for currently challenged sectors
if loc.Deadline == dl.Index || (loc.Deadline+1)%miner.WPoStPeriodDeadlines == dl.Index {
2021-01-12 23:42:01 +00:00
continue
}
2021-01-13 21:19:10 +00:00
2021-01-12 23:42:01 +00:00
if n < 1 {
log.Warnw("TerminateBatcher: zero sectors in bucket", "deadline", loc.Deadline, "partition", loc.Partition)
continue
}
2021-01-13 21:19:10 +00:00
total += n
2021-01-12 23:42:01 +00:00
params.Terminations = append(params.Terminations, miner2.TerminationDeclaration{
Deadline: loc.Deadline,
Partition: loc.Partition,
Sectors: *sectors,
})
}
if len(params.Terminations) == 0 {
b.lk.Unlock()
continue // nothing to do
}
2021-01-13 21:19:10 +00:00
if notif && total < TerminateBatchMax {
b.lk.Unlock()
continue
}
if after && total < TerminateBatchMin {
b.lk.Unlock()
continue
}
2021-01-12 23:42:01 +00:00
enc := new(bytes.Buffer)
if err := params.MarshalCBOR(enc); err != nil {
log.Warnw("TerminateBatcher: couldn't serialize TerminateSectors params", "error", err)
b.lk.Unlock()
continue
}
mi, err := b.api.StateMinerInfo(b.mctx, b.maddr, nil)
if err != nil {
log.Warnw("TerminateBatcher: couldn't get miner info", "error", err)
b.lk.Unlock()
continue
}
from, _, err := b.addrSel(b.mctx, mi, api.TerminateSectorsAddr, b.feeCfg.MaxTerminateGasFee, b.feeCfg.MaxTerminateGasFee)
if err != nil {
log.Warnw("TerminateBatcher: no good address found", "error", err)
b.lk.Unlock()
continue
}
mcid, err := b.api.SendMsg(b.mctx, from, b.maddr, miner.Methods.TerminateSectors, big.Zero(), b.feeCfg.MaxTerminateGasFee, enc.Bytes())
if err != nil {
log.Errorw("TerminateBatcher: sending message failed", "error", err)
b.lk.Unlock()
continue
}
2021-01-13 22:32:04 +00:00
lastMsg = &mcid
2021-01-12 23:42:01 +00:00
log.Infow("Sent TerminateSectors message", "cid", mcid, "from", from, "terminations", len(params.Terminations))
for _, t := range params.Terminations {
delete(b.todo, SectorLocation{
Deadline: t.Deadline,
Partition: t.Partition,
})
}
for _, w := range b.waiting {
for _, ch := range w {
ch <- mcid // buffered
}
}
2021-01-13 22:32:04 +00:00
2021-01-12 23:42:01 +00:00
b.waiting = map[SectorLocation][]chan cid.Cid{}
b.lk.Unlock()
}
}
// register termination, wait for batch message, return message CID
func (b *TerminateBatcher) AddTermination(ctx context.Context, s abi.SectorID) (cid.Cid, error) {
maddr, err := address.NewIDAddress(uint64(s.Miner))
if err != nil {
return cid.Undef, err
}
loc, err := b.api.StateSectorPartition(ctx, maddr, s.Number, nil)
if err != nil {
return cid.Undef, xerrors.Errorf("getting sector location: %w", err)
}
if loc == nil {
return cid.Undef, xerrors.New("sector location not found")
}
b.lk.Lock()
bf, ok := b.todo[*loc]
if !ok {
n := bitfield.New()
bf = &n
b.todo[*loc] = bf
}
bf.Set(uint64(s.Number))
sent := make(chan cid.Cid, 1)
b.waiting[*loc] = append(b.waiting[*loc], sent)
select {
case b.notify <- struct{}{}:
default: // already have a pending notification, don't need more
}
b.lk.Unlock()
select {
case c := <-sent:
return c, nil
case <-ctx.Done():
return cid.Undef, ctx.Err()
}
}
2021-01-13 22:32:04 +00:00
func (b *TerminateBatcher) Flush(ctx context.Context) (*cid.Cid, error) {
resCh := make(chan *cid.Cid, 1)
select {
case b.force <- resCh:
select {
case res := <-resCh:
return res, nil
case <-ctx.Done():
return nil, ctx.Err()
}
case <-ctx.Done():
return nil, ctx.Err()
}
}
2021-01-14 11:37:23 +00:00
func (b *TerminateBatcher) Pending(ctx context.Context) ([]abi.SectorID, error) {
b.lk.Lock()
defer b.lk.Unlock()
mid, err := address.IDFromAddress(b.maddr)
if err != nil {
return nil, err
}
res := make([]abi.SectorID, 0)
for _, bf := range b.todo {
err := bf.ForEach(func(id uint64) error {
res = append(res, abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(id),
})
return nil
})
if err != nil {
return nil, err
}
}
sort.Slice(res, func(i, j int) bool {
if res[i].Miner != res[j].Miner {
return res[i].Miner < res[j].Miner
}
return res[i].Number < res[j].Number
})
return res, nil
}
2021-01-12 23:42:01 +00:00
func (b *TerminateBatcher) Stop(ctx context.Context) error {
close(b.stop)
select {
case <-b.stopped:
return nil
case <-ctx.Done():
return ctx.Err()
}
}