From 02929dd166fe941b33d87044192d804391783139 Mon Sep 17 00:00:00 2001 From: Florian RUEN Date: Wed, 25 Oct 2023 18:32:02 +0200 Subject: [PATCH 1/2] feat: load SectorsSummmary from SectorStats instead of calling API (faster) --- CHANGELOG.md | 1 + node/impl/storminer.go | 14 ++++++-------- storage/pipeline/sealing.go | 4 ++++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32107f76e44..e54438ebae8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ # UNRELEASED - chore: Auto remove local chain data when importing chain file or snapshot ([filecoin-project/lotus#11277](https://github.com/filecoin-project/lotus/pull/11277)) - feat: metric: export Mpool message count ([filecoin-project/lotus#11361](https://github.com/filecoin-project/lotus/pull/11361)) +- feat: sealing: load SectorsSummary from sealing SectorStats instead of calling API each time ([filecoin-project/lotus#11353](https://github.com/filecoin-project/lotus/pull/11353)) ## New features - feat: Added new tracing API (**HIGHLY EXPERIMENTAL**) supporting two RPC methods: `trace_block` and `trace_replayBlockTransactions` ([filecoin-project/lotus#11100](https://github.com/filecoin-project/lotus/pull/11100)) diff --git a/node/impl/storminer.go b/node/impl/storminer.go index 6fd6045b4b6..b152b2ff0ee 100644 --- a/node/impl/storminer.go +++ b/node/impl/storminer.go @@ -336,16 +336,14 @@ func (sm *StorageMinerAPI) SectorsListInStates(ctx context.Context, states []api return sns, nil } +// Use SectorsSummary from stats (prometheus) for faster result func (sm *StorageMinerAPI) SectorsSummary(ctx context.Context) (map[api.SectorState]int, error) { - sectors, err := sm.Miner.ListSectors() - if err != nil { - return nil, err - } - + sectorStats := sm.Miner.SectorsSummary(ctx) out := make(map[api.SectorState]int) - for i := range sectors { - state := api.SectorState(sectors[i].State) - out[state]++ + + for st, count := range sectorStats { + state := api.SectorState(st) + out[state] = int(count) } return out, nil diff --git a/storage/pipeline/sealing.go b/storage/pipeline/sealing.go index 65d3fb14b1e..124dca9fc81 100644 --- a/storage/pipeline/sealing.go +++ b/storage/pipeline/sealing.go @@ -304,6 +304,10 @@ func (m *Sealing) TerminateSector(ctx context.Context, sid abi.SectorNumber) err return m.sectors.Send(uint64(sid), SectorTerminate{}) } +func (m *Sealing) SectorsSummary(ctx context.Context) map[SectorState]int64 { + return m.stats.byState +} + func (m *Sealing) TerminateFlush(ctx context.Context) (*cid.Cid, error) { return m.terminator.Flush(ctx) } From c0c4e6bf35dd4ff74670bd264a7acdf5737fc09f Mon Sep 17 00:00:00 2001 From: Florian RUEN Date: Fri, 3 Nov 2023 11:49:45 +0100 Subject: [PATCH 2/2] fix: add lk.Lock() and move the map copy to avoid miner crash --- node/impl/storminer.go | 10 +--------- storage/pipeline/sealing.go | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/node/impl/storminer.go b/node/impl/storminer.go index b152b2ff0ee..25da71ef0ef 100644 --- a/node/impl/storminer.go +++ b/node/impl/storminer.go @@ -338,15 +338,7 @@ func (sm *StorageMinerAPI) SectorsListInStates(ctx context.Context, states []api // Use SectorsSummary from stats (prometheus) for faster result func (sm *StorageMinerAPI) SectorsSummary(ctx context.Context) (map[api.SectorState]int, error) { - sectorStats := sm.Miner.SectorsSummary(ctx) - out := make(map[api.SectorState]int) - - for st, count := range sectorStats { - state := api.SectorState(st) - out[state] = int(count) - } - - return out, nil + return sm.Miner.SectorsSummary(ctx), nil } func (sm *StorageMinerAPI) StorageLocal(ctx context.Context) (map[storiface.ID]string, error) { diff --git a/storage/pipeline/sealing.go b/storage/pipeline/sealing.go index 124dca9fc81..936bd8b39e1 100644 --- a/storage/pipeline/sealing.go +++ b/storage/pipeline/sealing.go @@ -304,8 +304,18 @@ func (m *Sealing) TerminateSector(ctx context.Context, sid abi.SectorNumber) err return m.sectors.Send(uint64(sid), SectorTerminate{}) } -func (m *Sealing) SectorsSummary(ctx context.Context) map[SectorState]int64 { - return m.stats.byState +func (m *Sealing) SectorsSummary(ctx context.Context) map[api.SectorState]int { + m.stats.lk.Lock() + defer m.stats.lk.Unlock() + + out := make(map[api.SectorState]int) + + for st, count := range m.stats.byState { + state := api.SectorState(st) + out[state] = int(count) + } + + return out } func (m *Sealing) TerminateFlush(ctx context.Context) (*cid.Cid, error) {