Skip to content

Commit

Permalink
domain lru: support compressed vals (#12025)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Sep 19, 2024
1 parent c7681eb commit 2e4860f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
8 changes: 3 additions & 5 deletions erigon-lib/state/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type DomainGetFromFileCache struct {

// nolint
type domainGetFromFileCacheItem struct {
lvl uint8
v []byte // pointer to `mmap` - if .kv file is not compressed
lvl uint8
exists bool
offset uint64
}

var (
Expand Down Expand Up @@ -76,9 +77,6 @@ func (v *domainVisible) newGetFromFileCache() *DomainGetFromFileCache {
if !domainGetFromFileCacheEnabled {
return nil
}
if v.name == kv.CommitmentDomain {
return nil
}
return v.caches.Get().(*DomainGetFromFileCache)
}
func (v *domainVisible) returnGetFromFileCache(c *DomainGetFromFileCache) {
Expand Down
36 changes: 22 additions & 14 deletions erigon-lib/state/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ func domainReadMetric(name kv.Domain, level int) metrics.Summary {
return mxsKVGet[name][level]
}

func (dt *DomainRoTx) getLatestFromFile(i int, filekey []byte) ([]byte, bool, error) {
func (dt *DomainRoTx) getLatestFromFile(i int, filekey []byte) (v []byte, ok bool, offset uint64, err error) {
if dbg.KVReadLevelledMetrics {
defer domainReadMetric(dt.name, i).ObserveDuration(time.Now())
}
Expand All @@ -754,33 +754,33 @@ func (dt *DomainRoTx) getLatestFromFile(i int, filekey []byte) ([]byte, bool, er
if !(UseBtree || UseBpsTree) {
reader := dt.statelessIdxReader(i)
if reader.Empty() {
return nil, false, nil
return nil, false, 0, nil
}
offset, ok := reader.Lookup(filekey)
if !ok {
return nil, false, nil
return nil, false, 0, nil
}
g.Reset(offset)

k, _ := g.Next(nil)
if !bytes.Equal(filekey, k) {
return nil, false, nil
return nil, false, 0, nil
}
v, _ := g.Next(nil)
return v, true, nil
return v, true, 0, nil
}

_, v, _, ok, err := dt.statelessBtree(i).Get(filekey, g)
_, v, offset, ok, err = dt.statelessBtree(i).Get(filekey, g)
if err != nil || !ok {
return nil, false, err
return nil, false, 0, err
}
//fmt.Printf("getLatestFromBtreeColdFiles key %x shard %d %x\n", filekey, exactColdShard, v)
return v, true, nil
return v, true, offset, nil
}

func (dt *DomainRoTx) DebugKVFilesWithKey(k []byte) (res []string, err error) {
for i := len(dt.files) - 1; i >= 0; i-- {
_, ok, err := dt.getLatestFromFile(i, k)
_, ok, _, err := dt.getLatestFromFile(i, k)
if err != nil {
return res, err
}
Expand Down Expand Up @@ -1421,7 +1421,7 @@ func (dt *DomainRoTx) getFromFiles(filekey []byte) (v []byte, found bool, fileSt
return
}
useExistenceFilter := dt.d.indexList&withExistence != 0
useCache := dt.d.compression&seg.CompressVals == 0 && dt.name != kv.CommitmentDomain
useCache := dt.name != kv.CommitmentDomain

hi, lo := dt.ht.iit.hashKey(filekey)
if useCache && dt.getFromFileCache == nil {
Expand All @@ -1430,7 +1430,14 @@ func (dt *DomainRoTx) getFromFiles(filekey []byte) (v []byte, found bool, fileSt
if dt.getFromFileCache != nil {
cv, ok := dt.getFromFileCache.Get(u128{hi: hi, lo: lo})
if ok {
return cv.v, true, dt.files[cv.lvl].startTxNum, dt.files[cv.lvl].endTxNum, nil
if !cv.exists {
return nil, true, dt.files[cv.lvl].startTxNum, dt.files[cv.lvl].endTxNum, nil
}
g := dt.statelessGetter(int(cv.lvl))
g.Reset(cv.offset)
g.Skip()
v, _ = g.Next(nil) // can be compressed
return v, true, dt.files[cv.lvl].startTxNum, dt.files[cv.lvl].endTxNum, nil
}
}

Expand All @@ -1454,7 +1461,8 @@ func (dt *DomainRoTx) getFromFiles(filekey []byte) (v []byte, found bool, fileSt
}
}

v, found, err = dt.getLatestFromFile(i, filekey)
var offset uint64
v, found, offset, err = dt.getLatestFromFile(i, filekey)
if err != nil {
return nil, false, 0, 0, err
}
Expand All @@ -1469,7 +1477,7 @@ func (dt *DomainRoTx) getFromFiles(filekey []byte) (v []byte, found bool, fileSt
}

if dt.getFromFileCache != nil {
dt.getFromFileCache.Add(u128{hi: hi, lo: lo}, domainGetFromFileCacheItem{lvl: uint8(i), v: v})
dt.getFromFileCache.Add(u128{hi: hi, lo: lo}, domainGetFromFileCacheItem{lvl: uint8(i), offset: offset, exists: true})
}
return v, true, dt.files[i].startTxNum, dt.files[i].endTxNum, nil
}
Expand All @@ -1478,7 +1486,7 @@ func (dt *DomainRoTx) getFromFiles(filekey []byte) (v []byte, found bool, fileSt
}

if dt.getFromFileCache != nil {
dt.getFromFileCache.Add(u128{hi: hi, lo: lo}, domainGetFromFileCacheItem{lvl: 0, v: nil})
dt.getFromFileCache.Add(u128{hi: hi, lo: lo}, domainGetFromFileCacheItem{lvl: 0, offset: 0, exists: false})
}
return nil, false, 0, 0, nil
}
Expand Down

0 comments on commit 2e4860f

Please sign in to comment.