From 923a3db4b0762f32d4be799612912c7a381b567c Mon Sep 17 00:00:00 2001 From: vyzo Date: Sat, 27 Feb 2021 12:01:55 +0200 Subject: [PATCH] abstract tracking store and live set construction --- chain/store/splitstore/liveset.go | 16 ++++++++++++++++ chain/store/splitstore/liveset_lmdb.go | 18 ++++++++++++++++-- chain/store/splitstore/snoop.go | 11 +++++++++++ chain/store/splitstore/splitstore.go | 13 +++++++------ 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/chain/store/splitstore/liveset.go b/chain/store/splitstore/liveset.go index 5a7c8e6f2..17024bf37 100644 --- a/chain/store/splitstore/liveset.go +++ b/chain/store/splitstore/liveset.go @@ -1,6 +1,9 @@ package splitstore import ( + "fmt" + "path/filepath" + cid "github.com/ipfs/go-cid" ) @@ -11,3 +14,16 @@ type LiveSet interface { } var markBytes = []byte{} + +type LiveSetEnv interface { + NewLiveSet(name string) (LiveSet, error) + Close() error +} + +func NewLiveSetEnv(path string, useLMDB bool) (LiveSetEnv, error) { + if useLMDB { + return NewLMDBLiveSetEnv(filepath.Join(path, "sweep.lmdb")) + } + + return nil, fmt.Errorf("FIXME: non-lmdb livesets") +} diff --git a/chain/store/splitstore/liveset_lmdb.go b/chain/store/splitstore/liveset_lmdb.go index 3eb44dbe1..615605a75 100644 --- a/chain/store/splitstore/liveset_lmdb.go +++ b/chain/store/splitstore/liveset_lmdb.go @@ -12,6 +12,12 @@ import ( var LMDBLiveSetMapSize int64 = 1 << 34 // 16G; TODO grow the map dynamically +type LMDBLiveSetEnv struct { + env *lmdb.Env +} + +var _ LiveSetEnv = (*LMDBLiveSetEnv)(nil) + type LMDBLiveSet struct { env *lmdb.Env db lmdb.DBI @@ -19,7 +25,7 @@ type LMDBLiveSet struct { var _ LiveSet = (*LMDBLiveSet)(nil) -func NewLMDBLiveSetEnv(path string) (*lmdb.Env, error) { +func NewLMDBLiveSetEnv(path string) (*LMDBLiveSetEnv, error) { env, err := lmdb.NewEnv() if err != nil { return nil, xerrors.Errorf("failed to initialize LDMB env: %w", err) @@ -49,7 +55,15 @@ func NewLMDBLiveSetEnv(path string) (*lmdb.Env, error) { return nil, xerrors.Errorf("error opening LMDB database: %w", err) } - return env, nil + return &LMDBLiveSetEnv{env: env}, nil +} + +func (e *LMDBLiveSetEnv) NewLiveSet(name string) (LiveSet, error) { + return NewLMDBLiveSet(e.env, name+".lmdb") +} + +func (e *LMDBLiveSetEnv) Close() error { + return e.env.Close() } func NewLMDBLiveSet(env *lmdb.Env, name string) (*LMDBLiveSet, error) { diff --git a/chain/store/splitstore/snoop.go b/chain/store/splitstore/snoop.go index a762be79c..08faabd6c 100644 --- a/chain/store/splitstore/snoop.go +++ b/chain/store/splitstore/snoop.go @@ -1,6 +1,9 @@ package splitstore import ( + "fmt" + "path/filepath" + "github.com/filecoin-project/go-state-types/abi" cid "github.com/ipfs/go-cid" ) @@ -13,3 +16,11 @@ type TrackingStore interface { ForEach(func(cid.Cid, abi.ChainEpoch) error) error Close() error } + +func NewTrackingStore(path string, useLMDB bool) (TrackingStore, error) { + if useLMDB { + return NewLMDBTrackingStore(filepath.Join(path, "snoop.lmdb")) + } + + return nil, fmt.Errorf("TODO: non-lmdb livesets") +} diff --git a/chain/store/splitstore/splitstore.go b/chain/store/splitstore/splitstore.go index 9c629e46e..680ec2a5b 100644 --- a/chain/store/splitstore/splitstore.go +++ b/chain/store/splitstore/splitstore.go @@ -4,7 +4,6 @@ import ( "context" "encoding/binary" "errors" - "path/filepath" "sync" "sync/atomic" "time" @@ -32,6 +31,8 @@ const ( var baseEpochKey = dstore.NewKey("baseEpoch") +var UseLMDB = true // TODO snake this through DI + var log = logging.Logger("splitstore") func init() { @@ -57,7 +58,7 @@ type SplitStore struct { cold bstore.Blockstore snoop TrackingStore - env *lmdb.Env + env LiveSetEnv } var _ bstore.Blockstore = (*SplitStore)(nil) @@ -67,13 +68,13 @@ var _ bstore.Blockstore = (*SplitStore)(nil) // compaction. func NewSplitStore(path string, ds dstore.Datastore, cold, hot bstore.Blockstore) (*SplitStore, error) { // the tracking store - snoop, err := NewLMDBTrackingStore(filepath.Join(path, "snoop.lmdb")) + snoop, err := NewTrackingStore(path, UseLMDB) if err != nil { return nil, err } // the liveset env - env, err := NewLMDBLiveSetEnv(filepath.Join(path, "sweep.lmdb")) + env, err := NewLiveSetEnv(path, UseLMDB) if err != nil { snoop.Close() //nolint:errcheck return nil, err @@ -322,14 +323,14 @@ func (s *SplitStore) HeadChange(revert, apply []*types.TipSet) error { func (s *SplitStore) compact() { // create two on disk live sets, one for marking the cold finality region // and one for marking the hot region - hotSet, err := NewLMDBLiveSet(s.env, "hot") + hotSet, err := s.env.NewLiveSet("hot") if err != nil { // TODO do something better here panic(err) } defer hotSet.Close() //nolint:errcheck - coldSet, err := NewLMDBLiveSet(s.env, "cold") + coldSet, err := s.env.NewLiveSet("cold") if err != nil { // TODO do something better here panic(err)