make repo.Blockstore() idempotent; wrap in IDStore.

This commit is contained in:
Raúl Kripalani 2020-11-01 16:56:20 +00:00
parent d8d85373f5
commit 5a98660d1b
2 changed files with 30 additions and 8 deletions

View File

@ -23,6 +23,7 @@ import (
"github.com/filecoin-project/lotus/extern/sector-storage/fsutil"
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
lblockstore "github.com/filecoin-project/lotus/lib/blockstore"
badgerbs "github.com/filecoin-project/lotus/lib/blockstore/badger"
"github.com/filecoin-project/lotus/chain/types"
@ -259,6 +260,10 @@ type fsLockedRepo struct {
dsErr error
dsOnce sync.Once
bs blockstore.Blockstore
bsErr error
bsOnce sync.Once
storageLk sync.Mutex
configLk sync.Mutex
}
@ -281,6 +286,13 @@ func (fsr *fsLockedRepo) Close() error {
}
}
// type assertion will return ok=false if fsr.bs is nil altogether.
if c, ok := fsr.bs.(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
return err
@ -292,14 +304,24 @@ func (fsr *fsLockedRepo) Blockstore(domain BlockstoreDomain) (blockstore.Blockst
return nil, ErrInvalidBlockstoreDomain
}
path := fsr.join(filepath.Join(fsDatastore, "chain"))
readonly := fsr.readonly
fsr.bsOnce.Do(func() {
path := fsr.join(filepath.Join(fsDatastore, "chain"))
readonly := fsr.readonly
opts, err := BadgerBlockstoreOptions(domain, path, readonly)
if err != nil {
return nil, err
}
return badgerbs.Open(opts)
opts, err := BadgerBlockstoreOptions(domain, path, readonly)
if err != nil {
fsr.bsErr = err
return
}
bs, err := badgerbs.Open(opts)
if err != nil {
fsr.bsErr = err
}
fsr.bs = lblockstore.WrapIDStore(bs)
})
return fsr.bs, fsr.bsErr
}
// join joins path elements with fsr.path

View File

@ -161,7 +161,7 @@ func NewMemory(opts *MemRepoOptions) *MemRepo {
return &MemRepo{
repoLock: make(chan struct{}, 1),
blockstore: lblockstore.NewTemporarySync(),
blockstore: lblockstore.WrapIDStore(lblockstore.NewTemporarySync()),
datastore: opts.Ds,
configF: opts.ConfigF,
keystore: opts.KeyStore,