Skip to content

Commit

Permalink
Cleanup hot blockstore domain opening
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenGround0 committed Dec 26, 2024
1 parent a4ac610 commit 4c002ef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 55 deletions.
45 changes: 12 additions & 33 deletions node/modules/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
60 changes: 38 additions & 22 deletions node/repo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down

0 comments on commit 4c002ef

Please sign in to comment.