From 40be53c9f96925b3099c2428f2543c645e4411bd Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Wed, 4 Dec 2019 20:43:54 -0800 Subject: [PATCH 1/2] add command for power users to manually update sector state --- api/api_storage.go | 2 ++ api/struct.go | 6 +++++ cmd/lotus-storage-miner/sectors.go | 41 ++++++++++++++++++++++++++++++ node/impl/storminer.go | 15 +++++++---- storage/sectors.go | 12 +++++++++ 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/api/api_storage.go b/api/api_storage.go index fcb4e7aee..56b718420 100644 --- a/api/api_storage.go +++ b/api/api_storage.go @@ -67,6 +67,8 @@ type StorageMiner interface { SectorsRefs(context.Context) (map[string][]SealedRef, error) + SectorsUpdate(context.Context, uint64, SectorState) error + WorkerStats(context.Context) (sectorbuilder.WorkerStats, error) // WorkerQueue registers a remote worker diff --git a/api/struct.go b/api/struct.go index 3c91518bb..8a52c105b 100644 --- a/api/struct.go +++ b/api/struct.go @@ -2,6 +2,7 @@ package api import ( "context" + "github.com/filecoin-project/lotus/lib/sectorbuilder" "github.com/ipfs/go-cid" @@ -138,6 +139,7 @@ type StorageMinerStruct struct { SectorsStatus func(context.Context, uint64) (SectorInfo, error) `perm:"read"` SectorsList func(context.Context) ([]uint64, error) `perm:"read"` SectorsRefs func(context.Context) (map[string][]SealedRef, error) `perm:"read"` + SectorsUpdate func(context.Context, uint64, SectorState) error `perm:"write"` WorkerStats func(context.Context) (sectorbuilder.WorkerStats, error) `perm:"read"` @@ -512,6 +514,10 @@ func (c *StorageMinerStruct) SectorsRefs(ctx context.Context) (map[string][]Seal return c.Internal.SectorsRefs(ctx) } +func (c *StorageMinerStruct) SectorsUpdate(ctx context.Context, id uint64, state SectorState) error { + return c.Internal.SectorsUpdate(ctx, id, state) +} + func (c *StorageMinerStruct) WorkerStats(ctx context.Context) (sectorbuilder.WorkerStats, error) { return c.Internal.WorkerStats(ctx) } diff --git a/cmd/lotus-storage-miner/sectors.go b/cmd/lotus-storage-miner/sectors.go index 774b29bd3..355aadace 100644 --- a/cmd/lotus-storage-miner/sectors.go +++ b/cmd/lotus-storage-miner/sectors.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" + "golang.org/x/xerrors" "gopkg.in/urfave/cli.v2" "github.com/filecoin-project/lotus/api" @@ -32,6 +33,7 @@ var sectorsCmd = &cli.Command{ sectorsStatusCmd, sectorsListCmd, sectorsRefsCmd, + sectorsUpdateCmd, }, } @@ -173,6 +175,45 @@ var sectorsRefsCmd = &cli.Command{ }, } +var sectorsUpdateCmd = &cli.Command{ + Name: "update-state", + Usage: "ADVANCED: manually update the state of a sector, this may aid in error recovery", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "really-do-it", + Usage: "pass this flag if you know what you are doing", + }, + }, + Action: func(cctx *cli.Context) error { + if !cctx.Bool("really-do-it") { + return xerrors.Errorf("this is a command for advanced users, only us it if you are sure of what you are doing") + } + nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx) + if err != nil { + return err + } + defer closer() + ctx := lcli.ReqContext(cctx) + if cctx.Args().Len() < 2 { + return xerrors.Errorf("must pass sector ID and new state") + } + + id, err := strconv.ParseUint(cctx.Args().Get(0), 10, 64) + if err != nil { + return xerrors.Errorf("could not parse sector ID: %w", err) + } + + var st api.SectorState + for i, s := range api.SectorStates { + if cctx.Args().Get(1) == s { + st = api.SectorState(i) + } + } + + return nodeApi.SectorsUpdate(ctx, id, st) + }, +} + func yesno(b bool) string { if b { return "YES" diff --git a/node/impl/storminer.go b/node/impl/storminer.go index 25cdf16b9..d79156c0e 100644 --- a/node/impl/storminer.go +++ b/node/impl/storminer.go @@ -3,6 +3,12 @@ package impl import ( "context" "encoding/json" + "io" + "mime" + "net/http" + "os" + "path/filepath" + "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/chain/address" "github.com/filecoin-project/lotus/lib/sectorbuilder" @@ -12,11 +18,6 @@ import ( "github.com/filecoin-project/lotus/storage/sectorblocks" "github.com/gorilla/mux" files "github.com/ipfs/go-ipfs-files" - "io" - "mime" - "net/http" - "os" - "path/filepath" ) type StorageMinerAPI struct { @@ -201,6 +202,10 @@ func (sm *StorageMinerAPI) SectorsRefs(context.Context) (map[string][]api.Sealed return out, nil } +func (sm *StorageMinerAPI) SectorsUpdate(ctx context.Context, id uint64, state api.SectorState) error { + return sm.Miner.UpdateSectorState(ctx, id, state) +} + func (sm *StorageMinerAPI) WorkerQueue(ctx context.Context) (<-chan sectorbuilder.WorkerTask, error) { return sm.SectorBuilder.AddWorker(ctx) } diff --git a/storage/sectors.go b/storage/sectors.go index 929708880..ea9d3dbb9 100644 --- a/storage/sectors.go +++ b/storage/sectors.go @@ -55,6 +55,18 @@ func (u *sectorUpdate) to(newState api.SectorState) *sectorUpdate { } } +func (m *Miner) UpdateSectorState(ctx context.Context, sector uint64, state api.SectorState) error { + select { + case m.sectorUpdated <- sectorUpdate{ + newState: state, + id: sector, + }: + return nil + case <-ctx.Done(): + return ctx.Err() + } +} + func (m *Miner) sectorStateLoop(ctx context.Context) error { trackedSectors, err := m.ListSectors() if err != nil { From 310b07cc9c7c06538b43939182b63c0c927ea97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 5 Dec 2019 12:15:39 +0100 Subject: [PATCH 2/2] Fix typo --- cmd/lotus-storage-miner/sectors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/lotus-storage-miner/sectors.go b/cmd/lotus-storage-miner/sectors.go index 355aadace..31a7ceaa7 100644 --- a/cmd/lotus-storage-miner/sectors.go +++ b/cmd/lotus-storage-miner/sectors.go @@ -186,7 +186,7 @@ var sectorsUpdateCmd = &cli.Command{ }, Action: func(cctx *cli.Context) error { if !cctx.Bool("really-do-it") { - return xerrors.Errorf("this is a command for advanced users, only us it if you are sure of what you are doing") + return xerrors.Errorf("this is a command for advanced users, only use it if you are sure of what you are doing") } nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx) if err != nil {