abstract tracking store and live set construction

This commit is contained in:
vyzo 2021-02-27 12:01:55 +02:00
parent 8f0ddac41a
commit 923a3db4b0
4 changed files with 50 additions and 8 deletions

View File

@ -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")
}

View File

@ -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) {

View File

@ -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")
}

View File

@ -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)