Skip to content

Commit 9548629

Browse files
authored
feat(shed): lotus-shed sectors dump-sectors (#12703)
1 parent f4dbd88 commit 9548629

File tree

2 files changed

+138
-17
lines changed

2 files changed

+138
-17
lines changed

cmd/lotus-shed/sectors.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929

3030
"github.com/filecoin-project/lotus/api/v0api"
3131
"github.com/filecoin-project/lotus/chain/actors"
32+
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
33+
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
3234
"github.com/filecoin-project/lotus/chain/types"
3335
lcli "github.com/filecoin-project/lotus/cli"
3436
"github.com/filecoin-project/lotus/cli/spcli"
@@ -49,6 +51,7 @@ var sectorsCmd = &cli.Command{
4951
terminateSectorPenaltyEstimationCmd,
5052
visAllocatedSectorsCmd,
5153
dumpRLESectorCmd,
54+
dumpSectorOnChainInfoCmd,
5255
sectorReadCmd,
5356
sectorDeleteCmd,
5457
},
@@ -652,6 +655,92 @@ var sectorDeleteCmd = &cli.Command{
652655
},
653656
}
654657

658+
var dumpSectorOnChainInfoCmd = &cli.Command{
659+
Name: "dump-sectors",
660+
Usage: "Dump SectorOnChainInfo in CSV format for all sectors for all miners that have claimed power",
661+
Action: func(cctx *cli.Context) error {
662+
ctx := lcli.ReqContext(cctx)
663+
664+
h, err := loadChainStore(ctx, cctx.String("repo"))
665+
if err != nil {
666+
return xerrors.Errorf("loading chainstore: %w", err)
667+
}
668+
defer h.closer()
669+
670+
ts, err := lcli.LoadTipSet(ctx, cctx, &ChainStoreTipSetResolver{Chain: h.cs})
671+
if err != nil {
672+
return xerrors.Errorf("loading tipset: %w", err)
673+
}
674+
675+
powerActor, err := h.sm.LoadActor(ctx, power.Address, ts)
676+
if err != nil {
677+
return xerrors.Errorf("failed to load power actor: %w", err)
678+
}
679+
680+
powerState, err := power.Load(h.cs.ActorStore(ctx), powerActor)
681+
if err != nil {
682+
return xerrors.Errorf("failed to load power actor state: %w", err)
683+
}
684+
685+
_, _ = fmt.Fprintf(cctx.App.Writer,
686+
"Miner,SectorNumber,SealProof,DealIDCount,Activation,Expiration,DealWeight,VerifiedDealWeight,"+
687+
"InitialPledge,ExpectedDayReward,ExpectedStoragePledge,PowerBaseEpoch,Flags\n")
688+
689+
var count int
690+
err = powerState.ForEachClaim(func(maddr address.Address, claim power.Claim) error {
691+
act, err := h.sm.LoadActorTsk(ctx, maddr, ts.Key())
692+
if err != nil {
693+
return xerrors.Errorf("failed to load miner actor: %w", err)
694+
}
695+
696+
mas, err := miner.Load(h.sm.ChainStore().ActorStore(ctx), act)
697+
if err != nil {
698+
return xerrors.Errorf("failed to load miner actor state: %w", err)
699+
}
700+
701+
soci, err := mas.LoadSectors(nil)
702+
if err != nil {
703+
return xerrors.Errorf("load sectors: %w", err)
704+
}
705+
706+
for _, sector := range soci {
707+
_, _ = fmt.Fprintf(
708+
cctx.App.Writer,
709+
"%s,%d,%d,%d,%d,%d,%s,%s,%s,%s,%s,%d,%x\n",
710+
maddr,
711+
sector.SectorNumber,
712+
sector.SealProof,
713+
len(sector.DealIDs),
714+
sector.Activation,
715+
sector.Expiration,
716+
sector.DealWeight,
717+
sector.VerifiedDealWeight,
718+
sector.InitialPledge,
719+
sector.ExpectedDayReward,
720+
sector.ExpectedStoragePledge,
721+
sector.PowerBaseEpoch,
722+
sector.Flags,
723+
)
724+
}
725+
726+
count++
727+
if count%1000 == 0 {
728+
_, _ = fmt.Fprintf(cctx.App.ErrWriter, "Processed %d miners.\n", count)
729+
}
730+
731+
return nil
732+
})
733+
734+
if err != nil {
735+
return xerrors.Errorf("iterating over claims: %w", err)
736+
}
737+
738+
_, _ = fmt.Fprintf(cctx.App.ErrWriter, "Processed %d miners. Complete.\n", count)
739+
740+
return nil
741+
},
742+
}
743+
655744
type emptyLocalStorage struct {
656745
}
657746

cmd/lotus-shed/state-stats.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"io"
8+
"os"
99
"path"
10+
"path/filepath"
1011
"reflect"
1112
"sort"
1213
"sync"
@@ -29,6 +30,8 @@ import (
2930

3031
"github.com/filecoin-project/lotus/api"
3132
"github.com/filecoin-project/lotus/blockstore"
33+
badgerbs "github.com/filecoin-project/lotus/blockstore/badger"
34+
"github.com/filecoin-project/lotus/blockstore/splitstore"
3235
"github.com/filecoin-project/lotus/chain/actors"
3336
"github.com/filecoin-project/lotus/chain/actors/builtin"
3437
"github.com/filecoin-project/lotus/chain/consensus"
@@ -226,41 +229,70 @@ func loadChainStore(ctx context.Context, repoPath string) (*StoreHandle, error)
226229
return nil, xerrors.Errorf("lotus repo doesn't exist")
227230
}
228231

229-
lr, err := r.Lock(repo.FullNode)
232+
lkrepo, err := r.Lock(repo.FullNode)
230233
if err != nil {
231234
return nil, err
232235
}
233236

234-
bs, err := lr.Blockstore(ctx, repo.UniversalBlockstore)
237+
cold, err := lkrepo.Blockstore(ctx, repo.UniversalBlockstore)
235238
if err != nil {
236-
return nil, fmt.Errorf("failed to open blockstore: %w", err)
239+
return nil, xerrors.Errorf("failed to open universal blockstore %w", err)
237240
}
238241

242+
path, err := lkrepo.SplitstorePath()
243+
if err != nil {
244+
return nil, xerrors.Errorf("failed to get splitstore path: %w", err)
245+
}
246+
247+
path = filepath.Join(path, "hot.badger")
248+
if err := os.MkdirAll(path, 0755); err != nil {
249+
return nil, xerrors.Errorf("failed to create hot.badger dir: %w", err)
250+
}
251+
252+
opts, err := repo.BadgerBlockstoreOptions(repo.HotBlockstore, path, lkrepo.Readonly())
253+
if err != nil {
254+
return nil, xerrors.Errorf("failed to get badger blockstore options: %w", err)
255+
}
256+
257+
hot, err := badgerbs.Open(opts)
258+
if err != nil {
259+
return nil, xerrors.Errorf("failed to open badger blockstore: %w", err)
260+
}
261+
262+
mds, err := lkrepo.Datastore(context.Background(), "/metadata")
263+
if err != nil {
264+
return nil, xerrors.Errorf("failed to open metadata datastore: %w", err)
265+
}
266+
267+
cfg := &splitstore.Config{
268+
MarkSetType: "map",
269+
DiscardColdBlocks: true,
270+
}
271+
ss, err := splitstore.Open(path, mds, hot, cold, cfg)
272+
if err != nil {
273+
return nil, xerrors.Errorf("failed to open splitstore: %w", err)
274+
}
275+
276+
bs := ss
277+
239278
closer := func() {
240-
if err := lr.Close(); err != nil {
279+
if err := lkrepo.Close(); err != nil {
241280
log.Warnf("failed to close locked repo: %s", err)
242281
}
243-
if c, ok := bs.(io.Closer); ok {
244-
if err := c.Close(); err != nil {
245-
log.Warnf("failed to close blockstore: %s", err)
246-
}
282+
if err := ss.Close(); err != nil {
283+
log.Warnf("failed to close blockstore: %s", err)
247284
}
248285
}
249286

250-
mds, err := lr.Datastore(context.Background(), "/metadata")
251-
if err != nil {
252-
return nil, err
253-
}
254-
255-
cs := store.NewChainStore(bs, bs, mds, nil, nil)
287+
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
256288
if err := cs.Load(ctx); err != nil {
257-
return nil, fmt.Errorf("failed to load chain store: %w", err)
289+
return nil, xerrors.Errorf("failed to load chain store: %w", err)
258290
}
259291

260292
tsExec := consensus.NewTipSetExecutor(filcns.RewardFunc)
261293
sm, err := stmgr.NewStateManager(cs, tsExec, vm.Syscalls(proofsffi.ProofVerifier), filcns.DefaultUpgradeSchedule(), nil, mds, nil)
262294
if err != nil {
263-
return nil, fmt.Errorf("failed to open state manager: %w", err)
295+
return nil, xerrors.Errorf("failed to open state manager: %w", err)
264296
}
265297
handle := StoreHandle{
266298
bs: bs,

0 commit comments

Comments
 (0)