From 4c002ef3b8150bd0a42bfcae6a58174b2e57525d Mon Sep 17 00:00:00 2001 From: zenground0 Date: Thu, 26 Dec 2024 11:05:53 +0530 Subject: [PATCH] Cleanup hot blockstore domain opening --- node/modules/blockstore.go | 45 ++++++++-------------------- node/repo/fsrepo.go | 60 ++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 55 deletions(-) diff --git a/node/modules/blockstore.go b/node/modules/blockstore.go index 4fbc4dfa9b..2ff18f956e 100644 --- a/node/modules/blockstore.go +++ b/node/modules/blockstore.go @@ -2,16 +2,12 @@ package modules import ( "context" - "io" - "os" - "path/filepath" bstore "github.com/ipfs/boxo/blockstore" "go.uber.org/fx" "golang.org/x/xerrors" "github.com/filecoin-project/lotus/blockstore" - badgerbs "github.com/filecoin-project/lotus/blockstore/badger" "github.com/filecoin-project/lotus/blockstore/splitstore" "github.com/filecoin-project/lotus/node/config" "github.com/filecoin-project/lotus/node/modules/dtypes" @@ -23,17 +19,16 @@ import ( // chain data and state data. It can be backed by a blockstore directly // (e.g. Badger), or by a Splitstore. func UniversalBlockstore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.UniversalBlockstore, error) { - bs, _, err := r.Blockstore(helpers.LifecycleCtx(mctx, lc), repo.UniversalBlockstore) + bs, closer, err := r.Blockstore(helpers.LifecycleCtx(mctx, lc), repo.UniversalBlockstore) if err != nil { return nil, err } - if c, ok := bs.(io.Closer); ok { - lc.Append(fx.Hook{ - OnStop: func(_ context.Context) error { - return c.Close() - }, - }) - } + lc.Append(fx.Hook{ + OnStop: func(_ context.Context) error { + return closer() + }, + }) + return bs, err } @@ -45,32 +40,16 @@ func DiscardColdBlockstore(lc fx.Lifecycle, bs dtypes.UniversalBlockstore) (dtyp return blockstore.NewDiscardStore(bs), nil } -func BadgerHotBlockstore(lc fx.Lifecycle, r repo.LockedRepo) (dtypes.HotBlockstore, error) { - path, err := r.SplitstorePath() - if err != nil { - return nil, err - } - - path = filepath.Join(path, "hot.badger") - if err := os.MkdirAll(path, 0755); err != nil { - return nil, err - } - - opts, err := repo.BadgerBlockstoreOptions(repo.HotBlockstore, path, r.Readonly()) - if err != nil { - return nil, err - } - - bs, err := badgerbs.Open(opts) +func BadgerHotBlockstore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.HotBlockstore, error) { + bs, closer, err := r.Blockstore(helpers.LifecycleCtx(mctx, lc), repo.HotBlockstore) if err != nil { return nil, err } - lc.Append(fx.Hook{ OnStop: func(_ context.Context) error { - return bs.Close() - }}) - + return closer() + }, + }) return bs, nil } diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index b769108d72..07fbb95bef 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -419,6 +419,11 @@ func (fsr *fsLockedRepo) Close() error { return xerrors.Errorf("could not close blockstore: %w", err) } } + if c, ok := fsr.bsHot.(io.Closer); ok && c != nil { + if err := c.Close(); err != nil { + return xerrors.Errorf("could not close blockstore: %w", err) + } + } err = fsr.closer.Close() fsr.closer = nil @@ -430,12 +435,14 @@ func (fsr *fsLockedRepo) Blockstore(ctx context.Context, domain BlockstoreDomain if domain != UniversalBlockstore && domain != HotBlockstore { return nil, nil, ErrInvalidBlockstoreDomain } + var bs *badgerbs.Blockstore + var err error if domain == UniversalBlockstore { fsr.bsOnce.Do(func() { path := fsr.join(filepath.Join(fsDatastore, "chain")) readonly := fsr.readonly - if err := os.MkdirAll(path, 0755); err != nil { + if err = os.MkdirAll(path, 0755); err != nil { fsr.bsErr = err return } @@ -450,35 +457,44 @@ func (fsr *fsLockedRepo) Blockstore(ctx context.Context, domain BlockstoreDomain opts.SyncWrites = false } - bs, err := badgerbs.Open(opts) + bs, err = badgerbs.Open(opts) if err != nil { fsr.bsErr = err return } fsr.bs = blockstore.WrapIDStore(bs) }) - return fsr.bs, nil, fsr.bsErr - } - // else domain == HotBlockstore { - path, err := fsr.SplitstorePath() - if err != nil { - return nil, nil, err - } - path = filepath.Join(path, "hot.badger") - if err := os.MkdirAll(path, 0755); err != nil { - return nil, nil, err - } - readonly := fsr.readonly - opts, err := BadgerBlockstoreOptions(HotBlockstore, path, readonly) - if err != nil { - return nil, nil, err - } - bs, err := badgerbs.Open(opts) - if err != nil { - return nil, nil, err + err = fsr.bsErr + } else { + // else domain == HotBlockstore { + fsr.bsHotOnce.Do(func() { + path, err := fsr.SplitstorePath() + if err != nil { + fsr.bsHotErr = err + return + } + path = filepath.Join(path, "hot.badger") + if err := os.MkdirAll(path, 0755); err != nil { + fsr.bsHotErr = err + return + } + readonly := fsr.readonly + opts, err := BadgerBlockstoreOptions(HotBlockstore, path, readonly) + if err != nil { + fsr.bsHotErr = err + return + } + bs, err = badgerbs.Open(opts) + if err != nil { + fsr.bsHotErr = err + return + } + fsr.bsHot = bs + }) + err = fsr.bsHotErr } - return bs, bs.Close, nil + return bs, bs.Close, err } func (fsr *fsLockedRepo) SplitstorePath() (string, error) {