Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snake a context through the Chain-blockstore creation #5282

Merged
merged 6 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chain/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ 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)
}

bs, err := lr.Blockstore(repo.BlockstoreChain)
bs, err := lr.Blockstore(context.TODO(), repo.BlockstoreChain)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions chain/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/lotus-seal-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/lotus-shed/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/lotus-shed/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/lotus-shed/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/lotus-shed/import-car.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/hex"
"fmt"
"io"
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/lotus-shed/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/lotus-storage-miner/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/lotus-storage-miner/init_restore.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/lotus-wallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/lotus/backup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"os"

dstore "github.com/ipfs/go-datastore"
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/lotus/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,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") {
Expand Down Expand Up @@ -389,7 +389,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://") {
Expand Down Expand Up @@ -432,12 +432,12 @@ 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)
}

mds, err := lr.Datastore("/metadata")
mds, err := lr.Datastore(context.TODO(), "/metadata")
if err != nil {
return err
}
Expand Down Expand Up @@ -473,7 +473,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
}
Expand All @@ -487,13 +487,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
}

Expand Down
2 changes: 1 addition & 1 deletion node/modules/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions node/modules/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions node/modules/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions node/modules/storageminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc for the Datastore says that ctx is only for startup, here we are passing a lifecycle context from start to the end of Lotus runtime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Datastore() constructor should not retain the context. It should only be used for the start operation, and this is the only context that we have that is cancelled if the startup is cancelled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we should be using the context from OnStart here, but it makes everything way harder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be unnecessarily complex IMO.

if err != nil {
return nil, xerrors.Errorf("getting datastore out of reop: %w", err)
}
Expand All @@ -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
}
Expand Down
Loading