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] 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 9ef959336..78be7dfff 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 d41d7ac44..5723b1380 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 c748e47c4..1ee415727 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 8726a6e0d..8a17a10a3 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 591917bd6..140effb3d 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 8cdc1630c..ce152d882 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 83567add3..4820381b5 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 5def0c6b2..c7fc97c30 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 008b2ea15..596af96d5 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 83a9ad87c..9591129b8 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 25b89eb9d..cabb604c9 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 5517bd9f4..d41e0c098 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 4cc07f903..4226c33f7 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 fcc93fb40..8ba0d6a8d 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 9c1a18368..c0e9192f5 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 30f84aeaf..d49159e90 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 433ddb9b8..09fb85460 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 b86b6f43f..b58168ecf 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 06937c052..a202c0b80 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 f6599cf23..f2de49904 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 8651d97f9..6dcf0442c 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 }