Skip to content

Commit

Permalink
feat(shed): lotus-shed sectors dump-sectors
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Nov 19, 2024
1 parent afd45db commit 3a9980f
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 17 deletions.
85 changes: 85 additions & 0 deletions cmd/lotus-shed/sectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (

"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/cli/spcli"
Expand All @@ -49,6 +51,7 @@ var sectorsCmd = &cli.Command{
terminateSectorPenaltyEstimationCmd,
visAllocatedSectorsCmd,
dumpRLESectorCmd,
dumpSectorOnChainInfoCmd,
sectorReadCmd,
sectorDeleteCmd,
},
Expand Down Expand Up @@ -652,6 +655,88 @@ var sectorDeleteCmd = &cli.Command{
},
}

var dumpSectorOnChainInfoCmd = &cli.Command{
Name: "dump-sectors",
Usage: "Dump SectorOnChainInfo in CSV format for all sectors for all miners that have claimed power",
Action: func(cctx *cli.Context) error {
ctx := lcli.ReqContext(cctx)

h, err := loadChainStore(ctx, cctx.String("repo"))
if err != nil {
return xerrors.Errorf("loading chainstore: %w", err)
}
defer h.closer()

ts, err := lcli.LoadTipSet(ctx, cctx, &ChainStoreTipSetResolver{Chain: h.cs})
if err != nil {
return xerrors.Errorf("loading tipset: %w", err)
}

powerActor, err := h.sm.LoadActor(ctx, power.Address, ts)
if err != nil {
return xerrors.Errorf("failed to load power actor: %w", err)
}

powerState, err := power.Load(h.cs.ActorStore(ctx), powerActor)
if err != nil {
return xerrors.Errorf("failed to load power actor state: %w", err)
}

fmt.Fprintf(cctx.App.Writer,

Check failure on line 685 in cmd/lotus-shed/sectors.go

View workflow job for this annotation

GitHub Actions / Check (lint-all)

Error return value of `fmt.Fprintf` is not checked (errcheck)
"Miner,SectorNumber,SealProof,DealIDCount,Activation,Expiration,DealWeight,VerifiedDealWeight,"+
"InitialPledge,ExpectedDayReward,ExpectedStoragePledge,PowerBaseEpoch,Flags\n")

var count int
powerState.ForEachClaim(func(maddr address.Address, claim power.Claim) error {

Check failure on line 690 in cmd/lotus-shed/sectors.go

View workflow job for this annotation

GitHub Actions / Check (lint-all)

Error return value of `powerState.ForEachClaim` is not checked (errcheck)
act, err := h.sm.LoadActorTsk(ctx, maddr, ts.Key())
if err != nil {
return xerrors.Errorf("failed to load miner actor: %w", err)
}

mas, err := miner.Load(h.sm.ChainStore().ActorStore(ctx), act)
if err != nil {
return xerrors.Errorf("failed to load miner actor state: %w", err)
}

soci, err := mas.LoadSectors(nil)
if err != nil {
return xerrors.Errorf("load sectors: %w", err)
}

for _, sector := range soci {
fmt.Fprintf(

Check failure on line 707 in cmd/lotus-shed/sectors.go

View workflow job for this annotation

GitHub Actions / Check (lint-all)

Error return value of `fmt.Fprintf` is not checked (errcheck)
cctx.App.Writer,
"%s,%d,%d,%d,%d,%d,%s,%s,%s,%s,%s,%d,%x\n",
maddr,
sector.SectorNumber,
sector.SealProof,
len(sector.DealIDs),
sector.Activation,
sector.Expiration,
sector.DealWeight,
sector.VerifiedDealWeight,
sector.InitialPledge,
sector.ExpectedDayReward,
sector.ExpectedStoragePledge,
sector.PowerBaseEpoch,
sector.Flags,
)
}

count++
if count%1000 == 0 {
fmt.Fprintf(cctx.App.ErrWriter, "Processed %d miners.\n", count)

Check failure on line 728 in cmd/lotus-shed/sectors.go

View workflow job for this annotation

GitHub Actions / Check (lint-all)

Error return value of `fmt.Fprintf` is not checked (errcheck)
}

return nil
})

fmt.Fprintf(cctx.App.ErrWriter, "Processed %d miners. Complete.\n", count)

return nil
},
}

type emptyLocalStorage struct {
}

Expand Down
66 changes: 49 additions & 17 deletions cmd/lotus-shed/state-stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"reflect"
"sort"
"sync"
Expand All @@ -29,6 +30,8 @@ import (

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/blockstore"
badgerbs "github.com/filecoin-project/lotus/blockstore/badger"
"github.com/filecoin-project/lotus/blockstore/splitstore"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/consensus"
Expand Down Expand Up @@ -226,41 +229,70 @@ func loadChainStore(ctx context.Context, repoPath string) (*StoreHandle, error)
return nil, xerrors.Errorf("lotus repo doesn't exist")
}

lr, err := r.Lock(repo.FullNode)
lkrepo, err := r.Lock(repo.FullNode)
if err != nil {
return nil, err
}

bs, err := lr.Blockstore(ctx, repo.UniversalBlockstore)
cold, err := lkrepo.Blockstore(ctx, repo.UniversalBlockstore)
if err != nil {
return nil, fmt.Errorf("failed to open blockstore: %w", err)
return nil, xerrors.Errorf("failed to open universal blockstore %w", err)
}

path, err := lkrepo.SplitstorePath()
if err != nil {
return nil, xerrors.Errorf("failed to get splitstore path: %w", err)
}

path = filepath.Join(path, "hot.badger")
if err := os.MkdirAll(path, 0755); err != nil {
return nil, xerrors.Errorf("failed to create hot.badger dir: %w", err)
}

opts, err := repo.BadgerBlockstoreOptions(repo.HotBlockstore, path, lkrepo.Readonly())
if err != nil {
return nil, xerrors.Errorf("failed to get badger blockstore options: %w", err)
}

hot, err := badgerbs.Open(opts)
if err != nil {
return nil, xerrors.Errorf("failed to open badger blockstore: %w", err)
}

mds, err := lkrepo.Datastore(context.Background(), "/metadata")
if err != nil {
return nil, xerrors.Errorf("failed to open metadata datastore: %w", err)
}

cfg := &splitstore.Config{
MarkSetType: "map",
DiscardColdBlocks: true,
}
ss, err := splitstore.Open(path, mds, hot, cold, cfg)
if err != nil {
return nil, xerrors.Errorf("failed to open splitstore: %w", err)
}

bs := ss

closer := func() {
if err := lr.Close(); err != nil {
if err := lkrepo.Close(); err != nil {
log.Warnf("failed to close locked repo: %s", err)
}
if c, ok := bs.(io.Closer); ok {
if err := c.Close(); err != nil {
log.Warnf("failed to close blockstore: %s", err)
}
if err := ss.Close(); err != nil {
log.Warnf("failed to close blockstore: %s", err)
}
}

mds, err := lr.Datastore(context.Background(), "/metadata")
if err != nil {
return nil, err
}

cs := store.NewChainStore(bs, bs, mds, nil, nil)
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
if err := cs.Load(ctx); err != nil {
return nil, fmt.Errorf("failed to load chain store: %w", err)
return nil, xerrors.Errorf("failed to load chain store: %w", err)
}

tsExec := consensus.NewTipSetExecutor(filcns.RewardFunc)
sm, err := stmgr.NewStateManager(cs, tsExec, vm.Syscalls(proofsffi.ProofVerifier), filcns.DefaultUpgradeSchedule(), nil, mds, nil)
if err != nil {
return nil, fmt.Errorf("failed to open state manager: %w", err)
return nil, xerrors.Errorf("failed to open state manager: %w", err)
}
handle := StoreHandle{
bs: bs,
Expand Down

0 comments on commit 3a9980f

Please sign in to comment.