Skip to content

Commit

Permalink
Merge pull request #284 from qianbin/db-caching
Browse files Browse the repository at this point in the history
Db caching
  • Loading branch information
qianbin authored Aug 6, 2019
2 parents bcd10f8 + 4d240b0 commit 48c1764
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 120 deletions.
12 changes: 12 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ ignored = []
[[constraint]]
name = "gopkg.in/cheggaaa/pb.v1"
version = "1.0.28"

[[constraint]]
name = "github.com/allegro/bigcache"
version = "2.0.0"
56 changes: 4 additions & 52 deletions chain/ancestor_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package chain
import (
"encoding/binary"

lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
"github.com/vechain/thor/block"
"github.com/vechain/thor/kv"
Expand All @@ -21,14 +20,13 @@ const rootCacheLimit = 2048
type ancestorTrie struct {
kv kv.GetPutter
rootsCache *cache
trieCache *trieCache
}

func newAncestorTrie(kv kv.GetPutter) *ancestorTrie {
rootsCache := newCache(rootCacheLimit, func(key interface{}) (interface{}, error) {
return loadBlockNumberIndexTrieRoot(kv, key.(thor.Bytes32))
})
return &ancestorTrie{kv, rootsCache, newTrieCache()}
return &ancestorTrie{kv, rootsCache}
}

func numberAsKey(num uint32) []byte {
Expand All @@ -48,7 +46,7 @@ func (at *ancestorTrie) Update(w kv.Putter, id, parentID thor.Bytes32) error {
parentRoot = root.(thor.Bytes32)
}

tr, err := at.trieCache.Get(parentRoot, at.kv, true)
tr, err := trie.New(parentRoot, at.kv)
if err != nil {
return err
}
Expand All @@ -64,7 +62,7 @@ func (at *ancestorTrie) Update(w kv.Putter, id, parentID thor.Bytes32) error {
if err := saveBlockNumberIndexTrieRoot(w, id, root); err != nil {
return err
}
at.trieCache.Add(root, tr, at.kv)

at.rootsCache.Add(id, root)
return nil
}
Expand All @@ -81,7 +79,7 @@ func (at *ancestorTrie) GetAncestor(descendantID thor.Bytes32, ancestorNum uint3
if err != nil {
return thor.Bytes32{}, errors.WithMessage(err, "load index root")
}
tr, err := at.trieCache.Get(root.(thor.Bytes32), at.kv, false)
tr, err := trie.New(root.(thor.Bytes32), at.kv)
if err != nil {
return thor.Bytes32{}, err
}
Expand All @@ -92,49 +90,3 @@ func (at *ancestorTrie) GetAncestor(descendantID thor.Bytes32, ancestorNum uint3
}
return thor.BytesToBytes32(id), nil
}

///
type trieCache struct {
cache *lru.Cache
}

type trieCacheEntry struct {
trie *trie.Trie
kv kv.GetPutter
}

func newTrieCache() *trieCache {
cache, _ := lru.New(16)
return &trieCache{cache: cache}
}

// to get a trie for writing, copy should be set to true
func (tc *trieCache) Get(root thor.Bytes32, kv kv.GetPutter, copy bool) (*trie.Trie, error) {

if v, ok := tc.cache.Get(root); ok {
entry := v.(*trieCacheEntry)
if entry.kv == kv {
if copy {
cpy := *entry.trie
return &cpy, nil
}
return entry.trie, nil
}
}
tr, err := trie.New(root, kv)
if err != nil {
return nil, err
}
tr.SetCacheLimit(16)
tc.cache.Add(root, &trieCacheEntry{tr, kv})
if copy {
cpy := *tr
return &cpy, nil
}
return tr, nil
}

func (tc *trieCache) Add(root thor.Bytes32, trie *trie.Trie, kv kv.GetPutter) {
cpy := *trie
tc.cache.Add(root, &trieCacheEntry{&cpy, kv})
}
5 changes: 5 additions & 0 deletions cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@ var (
Usage: "verify log db at startup",
Hidden: true,
}
cacheFlag = cli.IntFlag{
Name: "cache",
Usage: "megabytes of ram allocated to internal caching",
Value: 2048,
}
)
1 change: 1 addition & 0 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func main() {
networkFlag,
configDirFlag,
dataDirFlag,
cacheFlag,
beneficiaryFlag,
targetGasLimitFlag,
apiAddrFlag,
Expand Down
9 changes: 8 additions & 1 deletion cmd/thor/must.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/vechain/thor/p2psrv"
"github.com/vechain/thor/state"
"github.com/vechain/thor/thor"
"github.com/vechain/thor/trie"
"github.com/vechain/thor/tx"
"github.com/vechain/thor/txpool"
cli "gopkg.in/urfave/cli.v1"
Expand Down Expand Up @@ -126,6 +127,11 @@ func makeInstanceDir(ctx *cli.Context, gene *genesis.Genesis) string {
}

func openMainDB(ctx *cli.Context, dataDir string) *lvldb.LevelDB {
cacheMB := ctx.Int(cacheFlag.Name)
if cacheMB < 128 {
cacheMB = 128
}

limit, err := fdlimit.Current()
if err != nil {
fatal("failed to get fd limit:", err)
Expand All @@ -141,12 +147,13 @@ func openMainDB(ctx *cli.Context, dataDir string) *lvldb.LevelDB {

dir := filepath.Join(dataDir, "main.db")
db, err := lvldb.New(dir, lvldb.Options{
CacheSize: 256,
CacheSize: cacheMB / 2,
OpenFilesCacheCapacity: fileCache,
})
if err != nil {
fatal(fmt.Sprintf("open chain database [%v]: %v", dir, err))
}
trie.SetCache(trie.NewCache(cacheMB / 2))
return db
}

Expand Down
2 changes: 1 addition & 1 deletion lvldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func New(path string, opts Options) (*LevelDB, error) {
CompactionTableSizeMultiplier: 2,
OpenFilesCacheCapacity: opts.OpenFilesCacheCapacity,
BlockCacheCapacity: opts.CacheSize / 2 * opt.MiB,
WriteBuffer: opts.CacheSize / 4 * opt.MiB, // Two of these are used internally
WriteBuffer: 32 * opt.MiB, // Two of these are used internally
Filter: filter.NewBloomFilter(10),
})

Expand Down
3 changes: 2 additions & 1 deletion state/cached_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
lru "github.com/hashicorp/golang-lru"
"github.com/vechain/thor/kv"
"github.com/vechain/thor/thor"
"github.com/vechain/thor/trie"
)

var codeCache, _ = lru.New(512)
Expand Down Expand Up @@ -37,7 +38,7 @@ func (co *cachedObject) getOrCreateStorageTrie() (trieReader, error) {

root := thor.BytesToBytes32(co.data.StorageRoot)

trie, err := trCache.Get(root, co.kv, false)
trie, err := trie.NewSecure(root, co.kv, 0)
if err != nil {
return nil, err
}
Expand Down
9 changes: 3 additions & 6 deletions state/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type codeWithHash struct {

func newStage(root thor.Bytes32, kv kv.GetPutter, changes map[thor.Address]*changedObject) *Stage {

accountTrie, err := trCache.Get(root, kv, true)
accountTrie, err := trie.NewSecure(root, kv, 0)
if err != nil {
return &Stage{err: err}
}
Expand All @@ -48,7 +48,7 @@ func newStage(root thor.Bytes32, kv kv.GetPutter, changes map[thor.Address]*chan
// skip storage changes if account is empty
if !dataCpy.IsEmpty() {
if len(obj.storage) > 0 {
strie, err := trCache.Get(thor.BytesToBytes32(dataCpy.StorageRoot), kv, true)
strie, err := trie.NewSecure(thor.BytesToBytes32(dataCpy.StorageRoot), kv, 0)
if err != nil {
return &Stage{err: err}
}
Expand Down Expand Up @@ -97,11 +97,10 @@ func (s *Stage) Commit() (thor.Bytes32, error) {

// commit storage tries
for _, strie := range s.storageTries {
root, err := strie.CommitTo(batch)
_, err := strie.CommitTo(batch)
if err != nil {
return thor.Bytes32{}, err
}
trCache.Add(root, strie, s.kv)
}

// commit accounts trie
Expand All @@ -114,7 +113,5 @@ func (s *Stage) Commit() (thor.Bytes32, error) {
return thor.Bytes32{}, err
}

trCache.Add(root, s.accountTrie, s.kv)

return root, nil
}
4 changes: 2 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type trieWriter interface {

// New create an state object.
func New(root thor.Bytes32, kv kv.GetPutter) (*State, error) {
trie, err := trCache.Get(root, kv, false)
trie, err := trie.NewSecure(root, kv, 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -372,7 +372,7 @@ func (s *State) BuildStorageTrie(addr thor.Address) (*trie.SecureTrie, error) {
root := thor.BytesToBytes32(acc.StorageRoot)

// retrieve a copied trie
trie, err := trCache.Get(root, s.kv, true)
trie, err := trie.NewSecure(root, s.kv, 0)
if err != nil {
return nil, err
}
Expand Down
56 changes: 0 additions & 56 deletions state/trie_cache.go

This file was deleted.

Loading

0 comments on commit 48c1764

Please sign in to comment.