Skip to content

Helper files for the flatKV cache implementation#3072

Open
cody-littley wants to merge 2 commits intomainfrom
cjl/cache-auxilery-2
Open

Helper files for the flatKV cache implementation#3072
cody-littley wants to merge 2 commits intomainfrom
cjl/cache-auxilery-2

Conversation

@cody-littley
Copy link
Contributor

Describe your changes and provide context

Replaces PR #3056

Helper files for the FlatKV cache implementation.

Testing performed to validate your change

Local testing, long running benchmark.

@github-actions
Copy link

github-actions bot commented Mar 13, 2026

The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedMar 13, 2026, 4:39 PM

@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 71.53285% with 39 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.36%. Comparing base (837ba92) to head (36d7328).

Files with missing lines Patch % Lines
sei-db/db_engine/dbcache/cached_key_value_db.go 0.00% 39 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3072      +/-   ##
==========================================
+ Coverage   58.35%   58.36%   +0.01%     
==========================================
  Files        2080     2087       +7     
  Lines      171659   171796     +137     
==========================================
+ Hits       100170   100268      +98     
- Misses      62564    62603      +39     
  Partials     8925     8925              
Flag Coverage Δ
sei-chain-pr 71.53% <71.53%> (?)
sei-db 70.41% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sei-db/db_engine/dbcache/cache.go 100.00% <100.00%> (ø)
sei-db/db_engine/dbcache/cached_batch.go 100.00% <100.00%> (ø)
sei-db/db_engine/dbcache/lru_queue.go 100.00% <100.00%> (ø)
sei-db/db_engine/dbcache/noop_cache.go 100.00% <100.00%> (ø)
sei-db/db_engine/dbcache/shard_manager.go 100.00% <100.00%> (ø)
sei-db/db_engine/types/types.go 100.00% <100.00%> (ø)
sei-db/db_engine/dbcache/cached_key_value_db.go 0.00% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

}

func (c *cachedKeyValueDB) Get(key []byte) ([]byte, error) {
val, found, err := c.cache.Get(key, true)
Copy link
Contributor

@yzang2019 yzang2019 Mar 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we don't need to fallback to c.db.get() on cache miss? Usually what we do is:

  1. read from cache
  2. on cache miss -> read from db -> backfill cache

Comment on lines +23 to +28
func (cb *cachedBatch) Set(key, value []byte) error {
cb.pending = append(cb.pending, CacheUpdate{Key: key, Value: value})
return cb.inner.Set(key, value)
}

func (cb *cachedBatch) Delete(key []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set and Delete append CacheUpdate{Key: key, Value: value} directly using the caller's slices. If the caller reuses or mutates those byte slices after calling Set/Delete but before Commit, the pending updates will contain corrupted data?

return err
}
if err := cb.cache.BatchSet(cb.pending); err != nil {
return fmt.Errorf("failed to update cache after commit: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if inner.Commit() succeeds but cache.BatchSet() fails, the method returns an error. Would that cause any confusion to the caller since the data is already persisted? Is cache update best-effort? If so maybe we just return nil but log the error?

}

// Combine a cache and a key-value database to create a new key-value database with caching.
func NewCachedKeyValueDB(db types.KeyValueDB, cache Cache) types.KeyValueDB {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the constructor returns KeyValueDB and func BatchGet is not defined in the interface

return c.readFunc(key)
}

func (c *noOpCache) BatchGet(keys map[string]types.BatchGetResult) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the diff between not-found vs empty-value in BatchGet

lru.totalSize += size
}

// Signal that an entry has been interated with, moving it to the back of the queue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: interacted

) {
if elem, ok := lru.entries[string(key)]; ok {
entry := elem.Value.(*lruQueueEntry)
lru.totalSize += size - entry.size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance this leads to underflow?

cache Cache
}

// Combine a cache and a key-value database to create a new key-value database with caching.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dup comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants