From d56170d70e19097a5b188be01aea37b3e6605713 Mon Sep 17 00:00:00 2001 From: Dan Shao Date: Sun, 6 Dec 2020 08:51:48 +0800 Subject: [PATCH] Optimize sectors info loading --- api/api_storage.go | 6 +++++ api/apistruct/struct.go | 10 +++++++ cmd/lotus-storage-miner/info.go | 18 +++++-------- cmd/lotus-storage-miner/sectors.go | 23 ++++++++++++++-- node/impl/storminer.go | 43 ++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 13 deletions(-) diff --git a/api/api_storage.go b/api/api_storage.go index a0600a5f4..a71b1a0a2 100644 --- a/api/api_storage.go +++ b/api/api_storage.go @@ -43,6 +43,12 @@ type StorageMiner interface { // List all staged sectors SectorsList(context.Context) ([]abi.SectorNumber, error) + // Get summary info of sectors + SectorsSummary(ctx context.Context) (map[SectorState]int, error) + + // List sectors in particular states + SectorsListInStates(context.Context, []SectorState) ([]abi.SectorNumber, error) + SectorsRefs(context.Context) (map[string][]SealedRef, error) // SectorStartSealing can be called on sectors in Empty or WaitDeals states diff --git a/api/apistruct/struct.go b/api/apistruct/struct.go index cca498bf5..c2b245cf7 100644 --- a/api/apistruct/struct.go +++ b/api/apistruct/struct.go @@ -301,6 +301,8 @@ type StorageMinerStruct struct { SectorsStatus func(ctx context.Context, sid abi.SectorNumber, showOnChainInfo bool) (api.SectorInfo, error) `perm:"read"` SectorsList func(context.Context) ([]abi.SectorNumber, error) `perm:"read"` + SectorsListInStates func(context.Context, []api.SectorState) ([]abi.SectorNumber, error) `perm:"read"` + SectorsSummary func(ctx context.Context) (map[api.SectorState]int, error) `perm:"read"` SectorsRefs func(context.Context) (map[string][]api.SealedRef, error) `perm:"read"` SectorStartSealing func(context.Context, abi.SectorNumber) error `perm:"write"` SectorSetSealDelay func(context.Context, time.Duration) error `perm:"write"` @@ -1249,6 +1251,14 @@ func (c *StorageMinerStruct) SectorsList(ctx context.Context) ([]abi.SectorNumbe return c.Internal.SectorsList(ctx) } +func (c *StorageMinerStruct) SectorsListInStates(ctx context.Context, states []api.SectorState) ([]abi.SectorNumber, error) { + return c.Internal.SectorsListInStates(ctx, states) +} + +func (c *StorageMinerStruct) SectorsSummary(ctx context.Context) (map[api.SectorState]int, error) { + return c.Internal.SectorsSummary(ctx) +} + func (c *StorageMinerStruct) SectorsRefs(ctx context.Context) (map[string][]api.SealedRef, error) { return c.Internal.SectorsRefs(ctx) } diff --git a/cmd/lotus-storage-miner/info.go b/cmd/lotus-storage-miner/info.go index 7bedd2b94..ed74da96b 100644 --- a/cmd/lotus-storage-miner/info.go +++ b/cmd/lotus-storage-miner/info.go @@ -327,22 +327,18 @@ func init() { } func sectorsInfo(ctx context.Context, napi api.StorageMiner) error { - sectors, err := napi.SectorsList(ctx) + summary, err := napi.SectorsSummary(ctx) if err != nil { return err } - buckets := map[sealing.SectorState]int{ - "Total": len(sectors), - } - for _, s := range sectors { - st, err := napi.SectorsStatus(ctx, s, false) - if err != nil { - return err - } - - buckets[sealing.SectorState(st.State)]++ + buckets := make(map[sealing.SectorState]int) + var total int + for s, c := range summary { + buckets[sealing.SectorState(s)] = c + total += c } + buckets["Total"] = total var sorted []stateMeta for state, i := range buckets { diff --git a/cmd/lotus-storage-miner/sectors.go b/cmd/lotus-storage-miner/sectors.go index 37eb06284..1c3e4858c 100644 --- a/cmd/lotus-storage-miner/sectors.go +++ b/cmd/lotus-storage-miner/sectors.go @@ -164,6 +164,10 @@ var sectorsListCmd = &cli.Command{ Name: "seal-time", Usage: "display how long it took for the sector to be sealed", }, + &cli.StringFlag{ + Name: "states", + Usage: "filter sectors by a comma-separated list of states", + }, }, Action: func(cctx *cli.Context) error { color.NoColor = !cctx.Bool("color") @@ -182,7 +186,22 @@ var sectorsListCmd = &cli.Command{ ctx := lcli.ReqContext(cctx) - list, err := nodeApi.SectorsList(ctx) + var list []abi.SectorNumber + + showRemoved := cctx.Bool("show-removed") + states := cctx.String("states") + if len(states) == 0 { + list, err = nodeApi.SectorsList(ctx) + } else { + showRemoved = true + sList := strings.Split(states, ",") + ss := make([]api.SectorState, len(sList)) + for i := range sList { + ss[i] = api.SectorState(sList[i]) + } + list, err = nodeApi.SectorsListInStates(ctx, ss) + } + if err != nil { return err } @@ -244,7 +263,7 @@ var sectorsListCmd = &cli.Command{ continue } - if cctx.Bool("show-removed") || st.State != api.SectorState(sealing.Removed) { + if showRemoved || st.State != api.SectorState(sealing.Removed) { _, inSSet := commitedIDs[s] _, inASet := activeIDs[s] diff --git a/node/impl/storminer.go b/node/impl/storminer.go index 85d76f354..bedff5999 100644 --- a/node/impl/storminer.go +++ b/node/impl/storminer.go @@ -218,6 +218,49 @@ func (sm *StorageMinerAPI) SectorsList(context.Context) ([]abi.SectorNumber, err return out, nil } +func (sm *StorageMinerAPI) SectorsListInStates(ctx context.Context, states []api.SectorState) ([]abi.SectorNumber, error) { + filterStates := make(map[sealing.SectorState]struct{}) + for _, state := range states { + st := sealing.SectorState(state) + if _, ok := sealing.ExistSectorStateList[st]; !ok { + continue + } + filterStates[st] = struct{}{} + } + + var sns []abi.SectorNumber + if len(filterStates) == 0 { + return sns, nil + } + + sectors, err := sm.Miner.ListSectors() + if err != nil { + return nil, err + } + + for i := range sectors { + if _, ok := filterStates[sectors[i].State]; ok { + sns = append(sns, sectors[i].SectorNumber) + } + } + return sns, nil +} + +func (sm *StorageMinerAPI) SectorsSummary(ctx context.Context) (map[api.SectorState]int, error) { + sectors, err := sm.Miner.ListSectors() + if err != nil { + return nil, err + } + + out := make(map[api.SectorState]int) + for i := range sectors { + state := api.SectorState(sectors[i].State) + out[state] += 1 + } + + return out, nil +} + func (sm *StorageMinerAPI) StorageLocal(ctx context.Context) (map[stores.ID]string, error) { return sm.StorageMgr.StorageLocal(ctx) }