Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kvcache: optimize View() method and fix redundant atomic store #13906

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 13 additions & 32 deletions erigon-lib/kv/kvcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,10 @@ type Coherent struct {
}

type CoherentRoot struct {
cache *btree2.BTreeG[*Element]
codeCache *btree2.BTreeG[*Element]
ready chan struct{} // close when ready
readyChanClosed atomic.Bool // protecting `ready` field from double-close (on unwind). Consumers don't need check this field.

// Views marked as `Canonical` if it received onNewBlock message
// we may drop `Non-Canonical` views even if they had fresh keys
// keys added to `Non-Canonical` views SHOULD NOT be added to stateEvict
// cache.latestStateView is always `Canonical`
cache *btree2.BTreeG[*Element]
codeCache *btree2.BTreeG[*Element]
ready chan struct{} // close when ready
closeOnce sync.Once // protecting `ready` field from double-close
isCanonical bool
}

Expand Down Expand Up @@ -326,48 +321,34 @@ func (c *Coherent) OnNewBlock(stateChanges *remote.StateChangeBatch) {
}
}

switched := r.readyChanClosed.CompareAndSwap(false, true)
if switched {
r.closeOnce.Do(func() {
close(r.ready) //broadcast
}
})
//log.Info("on new block handled", "viewID", stateChanges.StateVersionID)
}

func (c *Coherent) View(ctx context.Context, tx kv.Tx) (CacheView, error) {
idBytes, err := tx.GetOne(kv.Sequence, kv.PlainStateVersion)
id, err := tx.ReadSequence(string(kv.PlainStateVersion))
if err != nil {
return nil, err
}
var id uint64
if len(idBytes) == 0 {
id = 0
} else {
id = binary.BigEndian.Uint64(idBytes)
}

r := c.selectOrCreateRoot(id)

if !c.cfg.WaitForNewBlock || c.waitExceededCount.Load() >= MAX_WAITS {
return &CoherentView{stateVersionID: id, tx: tx, cache: c}, nil
}

select { // fast non-blocking path
select {
case <-r.ready:
//fmt.Printf("recv broadcast: %d\n", id)
return &CoherentView{stateVersionID: id, tx: tx, cache: c}, nil
default:
}

select { // slow blocking path
case <-r.ready:
//fmt.Printf("recv broadcast2: %d\n", tx.ViewID())
case <-ctx.Done():
return nil, fmt.Errorf("kvcache rootNum=%x, %w", tx.ViewID(), ctx.Err())
case <-time.After(c.cfg.NewBlockWait): //TODO: switch to timer to save resources
case <-time.After(c.cfg.NewBlockWait):
c.timeout.Inc()
c.waitExceededCount.Add(1)
//log.Info("timeout", "db_id", id, "has_btree", r.cache != nil)
return &CoherentView{stateVersionID: id, tx: tx, cache: c}, nil
}
return &CoherentView{stateVersionID: id, tx: tx, cache: c}, nil
}

func (c *Coherent) getFromCache(k []byte, id uint64, code bool) (*Element, *CoherentRoot, error) {
Expand Down Expand Up @@ -524,11 +505,11 @@ func (c *Coherent) ValidateCurrentRoot(ctx context.Context, tx kv.Tx) (*CacheVal
default:
}

idBytes, err := tx.GetOne(kv.Sequence, kv.PlainStateVersion)
stateID, err := tx.ReadSequence(string(kv.PlainStateVersion))
if err != nil {
return nil, err
}
stateID := binary.BigEndian.Uint64(idBytes)

result.LatestStateID = stateID

// if the latest view id in the cache is not the same as the tx or one below it
Expand Down