From 9334e733969021610b669665a4150443fd7714a1 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Wed, 30 Dec 2020 10:04:00 +0000 Subject: [PATCH 1/4] Snake a context through the Chain-blockstore creation --- chain/gen/gen.go | 2 +- cmd/lotus-shed/balances.go | 4 ++-- cmd/lotus-shed/export.go | 2 +- cmd/lotus-shed/import-car.go | 9 +++++++-- cmd/lotus-shed/pruning.go | 2 +- cmd/lotus/daemon.go | 12 ++++++------ node/modules/chain.go | 2 +- node/repo/fsrepo.go | 3 ++- node/repo/interface.go | 3 ++- node/repo/memrepo.go | 3 ++- 10 files changed, 25 insertions(+), 17 deletions(-) diff --git a/chain/gen/gen.go b/chain/gen/gen.go index 8e749095cf3..9ef95933621 100644 --- a/chain/gen/gen.go +++ b/chain/gen/gen.go @@ -139,7 +139,7 @@ func NewGeneratorWithSectors(numSectors int) (*ChainGen, error) { return nil, xerrors.Errorf("failed to get metadata datastore: %w", err) } - bs, err := lr.Blockstore(repo.BlockstoreChain) + bs, err := lr.Blockstore(context.TODO(), repo.BlockstoreChain) if err != nil { return nil, err } diff --git a/cmd/lotus-shed/balances.go b/cmd/lotus-shed/balances.go index 3280a63e5f2..591917bd648 100644 --- a/cmd/lotus-shed/balances.go +++ b/cmd/lotus-shed/balances.go @@ -175,7 +175,7 @@ var chainBalanceStateCmd = &cli.Command{ defer lkrepo.Close() //nolint:errcheck - bs, err := lkrepo.Blockstore(repo.BlockstoreChain) + bs, err := lkrepo.Blockstore(ctx, repo.BlockstoreChain) if err != nil { return fmt.Errorf("failed to open blockstore: %w", err) } @@ -396,7 +396,7 @@ var chainPledgeCmd = &cli.Command{ defer lkrepo.Close() //nolint:errcheck - bs, err := lkrepo.Blockstore(repo.BlockstoreChain) + bs, err := lkrepo.Blockstore(ctx, repo.BlockstoreChain) if err != nil { return xerrors.Errorf("failed to open blockstore: %w", err) } diff --git a/cmd/lotus-shed/export.go b/cmd/lotus-shed/export.go index 9b07034456b..83567add390 100644 --- a/cmd/lotus-shed/export.go +++ b/cmd/lotus-shed/export.go @@ -72,7 +72,7 @@ var exportChainCmd = &cli.Command{ defer fi.Close() //nolint:errcheck - bs, err := lr.Blockstore(repo.BlockstoreChain) + bs, err := lr.Blockstore(ctx, repo.BlockstoreChain) if err != nil { return fmt.Errorf("failed to open blockstore: %w", err) } diff --git a/cmd/lotus-shed/import-car.go b/cmd/lotus-shed/import-car.go index 9fa8537284f..7f3fa7c8953 100644 --- a/cmd/lotus-shed/import-car.go +++ b/cmd/lotus-shed/import-car.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/hex" "fmt" "io" @@ -24,6 +25,8 @@ var importCarCmd = &cli.Command{ return xerrors.Errorf("opening fs repo: %w", err) } + ctx := context.TODO() + exists, err := r.Exists() if err != nil { return err @@ -44,7 +47,7 @@ var importCarCmd = &cli.Command{ return xerrors.Errorf("opening the car file: %w", err) } - bs, err := lr.Blockstore(repo.BlockstoreChain) + bs, err := lr.Blockstore(ctx, repo.BlockstoreChain) if err != nil { return err } @@ -99,6 +102,8 @@ var importObjectCmd = &cli.Command{ return xerrors.Errorf("opening fs repo: %w", err) } + ctx := context.TODO() + exists, err := r.Exists() if err != nil { return err @@ -113,7 +118,7 @@ var importObjectCmd = &cli.Command{ } defer lr.Close() //nolint:errcheck - bs, err := lr.Blockstore(repo.BlockstoreChain) + bs, err := lr.Blockstore(ctx, repo.BlockstoreChain) if err != nil { return fmt.Errorf("failed to open blockstore: %w", err) } diff --git a/cmd/lotus-shed/pruning.go b/cmd/lotus-shed/pruning.go index 8728163c9ad..5def0c6b2da 100644 --- a/cmd/lotus-shed/pruning.go +++ b/cmd/lotus-shed/pruning.go @@ -131,7 +131,7 @@ var stateTreePruneCmd = &cli.Command{ defer lkrepo.Close() //nolint:errcheck - bs, err := lkrepo.Blockstore(repo.BlockstoreChain) + bs, err := lkrepo.Blockstore(ctx, repo.BlockstoreChain) if err != nil { return fmt.Errorf("failed to open blockstore: %w", err) } diff --git a/cmd/lotus/daemon.go b/cmd/lotus/daemon.go index 581238d4ba5..006da822a36 100644 --- a/cmd/lotus/daemon.go +++ b/cmd/lotus/daemon.go @@ -235,7 +235,7 @@ var DaemonCmd = &cli.Command{ issnapshot = true } - if err := ImportChain(r, chainfile, issnapshot); err != nil { + if err := ImportChain(ctx, r, chainfile, issnapshot); err != nil { return err } if cctx.Bool("halt-after-import") { @@ -370,7 +370,7 @@ func importKey(ctx context.Context, api api.FullNode, f string) error { return nil } -func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) { +func ImportChain(ctx context.Context, r repo.Repo, fname string, snapshot bool) (err error) { var rd io.Reader var l int64 if strings.HasPrefix(fname, "http://") || strings.HasPrefix(fname, "https://") { @@ -413,7 +413,7 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) { } defer lr.Close() //nolint:errcheck - bs, err := lr.Blockstore(repo.BlockstoreChain) + bs, err := lr.Blockstore(ctx, repo.BlockstoreChain) if err != nil { return xerrors.Errorf("failed to open blockstore: %w", err) } @@ -454,7 +454,7 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) { return xerrors.Errorf("flushing validation cache failed: %w", err) } - gb, err := cst.GetTipsetByHeight(context.TODO(), 0, ts, true) + gb, err := cst.GetTipsetByHeight(ctx, 0, ts, true) if err != nil { return err } @@ -468,13 +468,13 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) { if !snapshot { log.Infof("validating imported chain...") - if err := stm.ValidateChain(context.TODO(), ts); err != nil { + if err := stm.ValidateChain(ctx, ts); err != nil { return xerrors.Errorf("chain validation failed: %w", err) } } log.Infof("accepting %s as new head", ts.Cids()) - if err := cst.ForceHeadSilent(context.Background(), ts); err != nil { + if err := cst.ForceHeadSilent(ctx, ts); err != nil { return err } diff --git a/node/modules/chain.go b/node/modules/chain.go index 095bb501cc5..782e0b32f02 100644 --- a/node/modules/chain.go +++ b/node/modules/chain.go @@ -77,7 +77,7 @@ func MessagePool(lc fx.Lifecycle, sm *stmgr.StateManager, ps *pubsub.PubSub, ds } func ChainRawBlockstore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.ChainRawBlockstore, error) { - bs, err := r.Blockstore(repo.BlockstoreChain) + bs, err := r.Blockstore(helpers.LifecycleCtx(mctx, lc), repo.BlockstoreChain) if err != nil { return nil, err } diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index e4d9b32391f..b57fb64afcb 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -2,6 +2,7 @@ package repo import ( "bytes" + "context" "encoding/json" "fmt" "io" @@ -299,7 +300,7 @@ func (fsr *fsLockedRepo) Close() error { } // Blockstore returns a blockstore for the provided data domain. -func (fsr *fsLockedRepo) Blockstore(domain BlockstoreDomain) (blockstore.Blockstore, error) { +func (fsr *fsLockedRepo) Blockstore(ctx context.Context, domain BlockstoreDomain) (blockstore.Blockstore, error) { if domain != BlockstoreChain { return nil, ErrInvalidBlockstoreDomain } diff --git a/node/repo/interface.go b/node/repo/interface.go index 4ae68f880a4..564fc46c95f 100644 --- a/node/repo/interface.go +++ b/node/repo/interface.go @@ -1,6 +1,7 @@ package repo import ( + "context" "errors" "github.com/filecoin-project/lotus/lib/blockstore" @@ -54,7 +55,7 @@ type LockedRepo interface { Datastore(namespace string) (datastore.Batching, error) // Blockstore returns an IPLD blockstore for the requested domain. - Blockstore(domain BlockstoreDomain) (blockstore.Blockstore, error) + Blockstore(ctx context.Context, domain BlockstoreDomain) (blockstore.Blockstore, error) // Returns config in this repo Config() (interface{}, error) diff --git a/node/repo/memrepo.go b/node/repo/memrepo.go index 88d4eccd32f..06937c052f1 100644 --- a/node/repo/memrepo.go +++ b/node/repo/memrepo.go @@ -1,6 +1,7 @@ package repo import ( + "context" "encoding/json" "io/ioutil" "os" @@ -244,7 +245,7 @@ func (lmem *lockedMemRepo) Datastore(ns string) (datastore.Batching, error) { return namespace.Wrap(lmem.mem.datastore, datastore.NewKey(ns)), nil } -func (lmem *lockedMemRepo) Blockstore(domain BlockstoreDomain) (blockstore.Blockstore, error) { +func (lmem *lockedMemRepo) Blockstore(ctx context.Context, domain BlockstoreDomain) (blockstore.Blockstore, error) { if domain != BlockstoreChain { return nil, ErrInvalidBlockstoreDomain } From 446a24ef3fbb2c8864c14400e4f7863f498c2bc4 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Mon, 4 Jan 2021 15:36:36 +0100 Subject: [PATCH 2/4] Fix forgotten test-call --- chain/store/store_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chain/store/store_test.go b/chain/store/store_test.go index 5f1f336f3ed..d41d7ac44c2 100644 --- a/chain/store/store_test.go +++ b/chain/store/store_test.go @@ -52,7 +52,7 @@ func BenchmarkGetRandomness(b *testing.B) { b.Fatal(err) } - bs, err := lr.Blockstore(repo.BlockstoreChain) + bs, err := lr.Blockstore(context.TODO(), repo.BlockstoreChain) if err != nil { b.Fatal(err) } From 47141042b6f2c0412fb75f9c3272a5dfbdf91878 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Tue, 12 Jan 2021 18:11:18 +0100 Subject: [PATCH 3/4] Add comment Co-authored-by: raulk --- node/repo/interface.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/node/repo/interface.go b/node/repo/interface.go index 564fc46c95f..b86b6f43fd2 100644 --- a/node/repo/interface.go +++ b/node/repo/interface.go @@ -55,6 +55,9 @@ type LockedRepo interface { Datastore(namespace string) (datastore.Batching, error) // Blockstore returns an IPLD blockstore for the requested domain. + // The supplied context must only be used to initialize the blockstore. + // The implementation should not retain the context for usage throughout + // the lifecycle. Blockstore(ctx context.Context, domain BlockstoreDomain) (blockstore.Blockstore, error) // Returns config in this repo From a1da1dab854fd85e7b332d1024bf8abff9f3f842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Tue, 26 Jan 2021 10:25:34 +0000 Subject: [PATCH 4/4] add context to LockedRepo#Datastore(). --- chain/gen/gen.go | 2 +- chain/store/store_test.go | 2 +- cli/backup.go | 2 +- cmd/lotus-seal-worker/main.go | 4 ++-- cmd/lotus-shed/balances.go | 4 ++-- cmd/lotus-shed/datastore.go | 5 +++-- cmd/lotus-shed/export.go | 2 +- cmd/lotus-shed/pruning.go | 2 +- cmd/lotus-storage-miner/init.go | 2 +- cmd/lotus-storage-miner/init_restore.go | 3 ++- cmd/lotus-wallet/main.go | 2 +- cmd/lotus/backup.go | 3 ++- cmd/lotus/daemon.go | 2 +- node/modules/client.go | 6 ++++-- node/modules/storage.go | 6 ++++-- node/modules/storageminer.go | 10 ++++++---- node/repo/fsrepo_ds.go | 3 ++- node/repo/interface.go | 5 ++++- node/repo/memrepo.go | 2 +- node/test/builder.go | 2 +- testplans/lotus-soup/testkit/role_miner.go | 2 +- 21 files changed, 42 insertions(+), 29 deletions(-) diff --git a/chain/gen/gen.go b/chain/gen/gen.go index 9ef95933621..78be7dfff5f 100644 --- a/chain/gen/gen.go +++ b/chain/gen/gen.go @@ -134,7 +134,7 @@ func NewGeneratorWithSectors(numSectors int) (*ChainGen, error) { return nil, xerrors.Errorf("taking mem-repo lock failed: %w", err) } - ds, err := lr.Datastore("/metadata") + ds, err := lr.Datastore(context.TODO(), "/metadata") if err != nil { return nil, xerrors.Errorf("failed to get metadata datastore: %w", err) } diff --git a/chain/store/store_test.go b/chain/store/store_test.go index d41d7ac44c2..5723b1380e4 100644 --- a/chain/store/store_test.go +++ b/chain/store/store_test.go @@ -65,7 +65,7 @@ func BenchmarkGetRandomness(b *testing.B) { } }() - mds, err := lr.Datastore("/metadata") + mds, err := lr.Datastore(context.Background(), "/metadata") if err != nil { b.Fatal(err) } diff --git a/cli/backup.go b/cli/backup.go index c748e47c438..1ee4157278c 100644 --- a/cli/backup.go +++ b/cli/backup.go @@ -46,7 +46,7 @@ func BackupCmd(repoFlag string, rt repo.RepoType, getApi BackupApiFn) *cli.Comma } defer lr.Close() // nolint:errcheck - mds, err := lr.Datastore("/metadata") + mds, err := lr.Datastore(context.TODO(), "/metadata") if err != nil { return xerrors.Errorf("getting metadata datastore: %w", err) } diff --git a/cmd/lotus-seal-worker/main.go b/cmd/lotus-seal-worker/main.go index 8726a6e0da2..8a17a10a3c6 100644 --- a/cmd/lotus-seal-worker/main.go +++ b/cmd/lotus-seal-worker/main.go @@ -308,7 +308,7 @@ var runCmd = &cli.Command{ { // init datastore for r.Exists - _, err := lr.Datastore("/metadata") + _, err := lr.Datastore(context.Background(), "/metadata") if err != nil { return err } @@ -327,7 +327,7 @@ var runCmd = &cli.Command{ log.Error("closing repo", err) } }() - ds, err := lr.Datastore("/metadata") + ds, err := lr.Datastore(context.Background(), "/metadata") if err != nil { return err } diff --git a/cmd/lotus-shed/balances.go b/cmd/lotus-shed/balances.go index 591917bd648..140effb3d83 100644 --- a/cmd/lotus-shed/balances.go +++ b/cmd/lotus-shed/balances.go @@ -188,7 +188,7 @@ var chainBalanceStateCmd = &cli.Command{ } }() - mds, err := lkrepo.Datastore("/metadata") + mds, err := lkrepo.Datastore(context.Background(), "/metadata") if err != nil { return err } @@ -409,7 +409,7 @@ var chainPledgeCmd = &cli.Command{ } }() - mds, err := lkrepo.Datastore("/metadata") + mds, err := lkrepo.Datastore(context.Background(), "/metadata") if err != nil { return err } diff --git a/cmd/lotus-shed/datastore.go b/cmd/lotus-shed/datastore.go index 8cdc1630cb5..ce152d8829c 100644 --- a/cmd/lotus-shed/datastore.go +++ b/cmd/lotus-shed/datastore.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "context" "encoding/json" "fmt" "io" @@ -75,7 +76,7 @@ var datastoreListCmd = &cli.Command{ } defer lr.Close() //nolint:errcheck - ds, err := lr.Datastore(datastore.NewKey(cctx.Args().First()).String()) + ds, err := lr.Datastore(context.Background(), datastore.NewKey(cctx.Args().First()).String()) if err != nil { return err } @@ -141,7 +142,7 @@ var datastoreGetCmd = &cli.Command{ } defer lr.Close() //nolint:errcheck - ds, err := lr.Datastore(datastore.NewKey(cctx.Args().First()).String()) + ds, err := lr.Datastore(context.Background(), datastore.NewKey(cctx.Args().First()).String()) if err != nil { return err } diff --git a/cmd/lotus-shed/export.go b/cmd/lotus-shed/export.go index 83567add390..4820381b5c6 100644 --- a/cmd/lotus-shed/export.go +++ b/cmd/lotus-shed/export.go @@ -85,7 +85,7 @@ var exportChainCmd = &cli.Command{ } }() - mds, err := lr.Datastore("/metadata") + mds, err := lr.Datastore(context.Background(), "/metadata") if err != nil { return err } diff --git a/cmd/lotus-shed/pruning.go b/cmd/lotus-shed/pruning.go index 5def0c6b2da..c7fc97c30da 100644 --- a/cmd/lotus-shed/pruning.go +++ b/cmd/lotus-shed/pruning.go @@ -151,7 +151,7 @@ var stateTreePruneCmd = &cli.Command{ return fmt.Errorf("only badger blockstores are supported") } - mds, err := lkrepo.Datastore("/metadata") + mds, err := lkrepo.Datastore(context.Background(), "/metadata") if err != nil { return err } diff --git a/cmd/lotus-storage-miner/init.go b/cmd/lotus-storage-miner/init.go index 008b2ea157e..596af96d5ea 100644 --- a/cmd/lotus-storage-miner/init.go +++ b/cmd/lotus-storage-miner/init.go @@ -416,7 +416,7 @@ func storageMinerInit(ctx context.Context, cctx *cli.Context, api lapi.FullNode, return xerrors.Errorf("peer ID from private key: %w", err) } - mds, err := lr.Datastore("/metadata") + mds, err := lr.Datastore(context.TODO(), "/metadata") if err != nil { return err } diff --git a/cmd/lotus-storage-miner/init_restore.go b/cmd/lotus-storage-miner/init_restore.go index 83a9ad87c53..9591129b8d6 100644 --- a/cmd/lotus-storage-miner/init_restore.go +++ b/cmd/lotus-storage-miner/init_restore.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "io/ioutil" "os" @@ -190,7 +191,7 @@ var initRestoreCmd = &cli.Command{ log.Info("Restoring metadata backup") - mds, err := lr.Datastore("/metadata") + mds, err := lr.Datastore(context.TODO(), "/metadata") if err != nil { return err } diff --git a/cmd/lotus-wallet/main.go b/cmd/lotus-wallet/main.go index 25b89eb9d91..cabb604c9ab 100644 --- a/cmd/lotus-wallet/main.go +++ b/cmd/lotus-wallet/main.go @@ -118,7 +118,7 @@ var runCmd = &cli.Command{ var w api.WalletAPI = lw if cctx.Bool("ledger") { - ds, err := lr.Datastore("/metadata") + ds, err := lr.Datastore(context.Background(), "/metadata") if err != nil { return err } diff --git a/cmd/lotus/backup.go b/cmd/lotus/backup.go index 5517bd9f446..d41e0c098bf 100644 --- a/cmd/lotus/backup.go +++ b/cmd/lotus/backup.go @@ -1,6 +1,7 @@ package main import ( + "context" "os" dstore "github.com/ipfs/go-datastore" @@ -87,7 +88,7 @@ func restore(cctx *cli.Context, r repo.Repo) error { log.Info("Restoring metadata backup") - mds, err := lr.Datastore("/metadata") + mds, err := lr.Datastore(context.TODO(), "/metadata") if err != nil { return err } diff --git a/cmd/lotus/daemon.go b/cmd/lotus/daemon.go index 4cc07f903d5..4226c33f775 100644 --- a/cmd/lotus/daemon.go +++ b/cmd/lotus/daemon.go @@ -437,7 +437,7 @@ func ImportChain(ctx context.Context, r repo.Repo, fname string, snapshot bool) return xerrors.Errorf("failed to open blockstore: %w", err) } - mds, err := lr.Datastore("/metadata") + mds, err := lr.Datastore(context.TODO(), "/metadata") if err != nil { return err } diff --git a/node/modules/client.go b/node/modules/client.go index fcc93fb4072..8ba0d6a8d09 100644 --- a/node/modules/client.go +++ b/node/modules/client.go @@ -39,6 +39,7 @@ import ( "github.com/filecoin-project/lotus/node/impl/full" payapi "github.com/filecoin-project/lotus/node/impl/paych" "github.com/filecoin-project/lotus/node/modules/dtypes" + "github.com/filecoin-project/lotus/node/modules/helpers" "github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/node/repo/importmgr" "github.com/filecoin-project/lotus/node/repo/retrievalstoremgr" @@ -78,8 +79,9 @@ func HandleMigrateClientFunds(lc fx.Lifecycle, ds dtypes.MetadataDS, wallet full }) } -func ClientMultiDatastore(lc fx.Lifecycle, r repo.LockedRepo) (dtypes.ClientMultiDstore, error) { - ds, err := r.Datastore("/client") +func ClientMultiDatastore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.ClientMultiDstore, error) { + ctx := helpers.LifecycleCtx(mctx, lc) + ds, err := r.Datastore(ctx, "/client") if err != nil { return nil, xerrors.Errorf("getting datastore out of reop: %w", err) } diff --git a/node/modules/storage.go b/node/modules/storage.go index 9c1a18368ce..c0e9192f57e 100644 --- a/node/modules/storage.go +++ b/node/modules/storage.go @@ -8,6 +8,7 @@ import ( "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/lib/backupds" "github.com/filecoin-project/lotus/node/modules/dtypes" + "github.com/filecoin-project/lotus/node/modules/helpers" "github.com/filecoin-project/lotus/node/repo" ) @@ -27,8 +28,9 @@ func KeyStore(lr repo.LockedRepo) (types.KeyStore, error) { return lr.KeyStore() } -func Datastore(r repo.LockedRepo) (dtypes.MetadataDS, error) { - mds, err := r.Datastore("/metadata") +func Datastore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.MetadataDS, error) { + ctx := helpers.LifecycleCtx(mctx, lc) + mds, err := r.Datastore(ctx, "/metadata") if err != nil { return nil, err } diff --git a/node/modules/storageminer.go b/node/modules/storageminer.go index 30f84aeaff8..d49159e90a7 100644 --- a/node/modules/storageminer.go +++ b/node/modules/storageminer.go @@ -357,8 +357,9 @@ func NewProviderPieceStore(lc fx.Lifecycle, ds dtypes.MetadataDS) (dtypes.Provid return ps, nil } -func StagingMultiDatastore(lc fx.Lifecycle, r repo.LockedRepo) (dtypes.StagingMultiDstore, error) { - ds, err := r.Datastore("/staging") +func StagingMultiDatastore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.StagingMultiDstore, error) { + ctx := helpers.LifecycleCtx(mctx, lc) + ds, err := r.Datastore(ctx, "/staging") if err != nil { return nil, xerrors.Errorf("getting datastore out of reop: %w", err) } @@ -379,8 +380,9 @@ func StagingMultiDatastore(lc fx.Lifecycle, r repo.LockedRepo) (dtypes.StagingMu // StagingBlockstore creates a blockstore for staging blocks for a miner // in a storage deal, prior to sealing -func StagingBlockstore(r repo.LockedRepo) (dtypes.StagingBlockstore, error) { - stagingds, err := r.Datastore("/staging") +func StagingBlockstore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.StagingBlockstore, error) { + ctx := helpers.LifecycleCtx(mctx, lc) + stagingds, err := r.Datastore(ctx, "/staging") if err != nil { return nil, err } diff --git a/node/repo/fsrepo_ds.go b/node/repo/fsrepo_ds.go index 433ddb9b8aa..09fb854606f 100644 --- a/node/repo/fsrepo_ds.go +++ b/node/repo/fsrepo_ds.go @@ -1,6 +1,7 @@ package repo import ( + "context" "os" "path/filepath" @@ -67,7 +68,7 @@ func (fsr *fsLockedRepo) openDatastores(readonly bool) (map[string]datastore.Bat return out, nil } -func (fsr *fsLockedRepo) Datastore(ns string) (datastore.Batching, error) { +func (fsr *fsLockedRepo) Datastore(_ context.Context, ns string) (datastore.Batching, error) { fsr.dsOnce.Do(func() { fsr.ds, fsr.dsErr = fsr.openDatastores(fsr.readonly) }) diff --git a/node/repo/interface.go b/node/repo/interface.go index b86b6f43fd2..b58168ecfaa 100644 --- a/node/repo/interface.go +++ b/node/repo/interface.go @@ -52,7 +52,10 @@ type LockedRepo interface { Close() error // Returns datastore defined in this repo. - Datastore(namespace string) (datastore.Batching, error) + // The supplied context must only be used to initialize the datastore. + // The implementation should not retain the context for usage throughout + // the lifecycle. + Datastore(ctx context.Context, namespace string) (datastore.Batching, error) // Blockstore returns an IPLD blockstore for the requested domain. // The supplied context must only be used to initialize the blockstore. diff --git a/node/repo/memrepo.go b/node/repo/memrepo.go index 06937c052f1..a202c0b8017 100644 --- a/node/repo/memrepo.go +++ b/node/repo/memrepo.go @@ -237,7 +237,7 @@ func (lmem *lockedMemRepo) Close() error { } -func (lmem *lockedMemRepo) Datastore(ns string) (datastore.Batching, error) { +func (lmem *lockedMemRepo) Datastore(_ context.Context, ns string) (datastore.Batching, error) { if err := lmem.checkToken(); err != nil { return nil, err } diff --git a/node/test/builder.go b/node/test/builder.go index f6599cf23cd..f2de499040d 100644 --- a/node/test/builder.go +++ b/node/test/builder.go @@ -77,7 +77,7 @@ func CreateTestStorageNode(ctx context.Context, t *testing.T, waddr address.Addr }) require.NoError(t, err) - ds, err := lr.Datastore("/metadata") + ds, err := lr.Datastore(context.TODO(), "/metadata") require.NoError(t, err) err = ds.Put(datastore.NewKey("miner-address"), act.Bytes()) require.NoError(t, err) diff --git a/testplans/lotus-soup/testkit/role_miner.go b/testplans/lotus-soup/testkit/role_miner.go index 8651d97f9ec..6dcf0442cef 100644 --- a/testplans/lotus-soup/testkit/role_miner.go +++ b/testplans/lotus-soup/testkit/role_miner.go @@ -165,7 +165,7 @@ func PrepareMiner(t *TestEnvironment) (*LotusMiner, error) { return nil, err } - ds, err := lr.Datastore("/metadata") + ds, err := lr.Datastore(context.Background(), "/metadata") if err != nil { return nil, err }