-
Notifications
You must be signed in to change notification settings - Fork 818
feat(blockdb): add lru cache for block entries #4425
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
Open
DracoLi
wants to merge
8
commits into
master
Choose a base branch
from
dl/block-cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+299
−66
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c6240d3
feat(blockdb): add lru cache for block entries
DracoLi e5336fc
fix: test to ensure testing cache is used
DracoLi 225955e
refactor: entryCache -> blockCache
DracoLi 20b70f1
use cache_db wrapper for caching functionality
DracoLi afa7427
add locking to cached Put
DracoLi d666deb
acceptable limitation
DracoLi 9264d55
Update doc wording
DracoLi 9792f38
clarify docs
DracoLi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package blockdb | ||
|
|
||
| import ( | ||
| "slices" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/ava-labs/avalanchego/cache/lru" | ||
| "github.com/ava-labs/avalanchego/database" | ||
| ) | ||
|
|
||
| var _ database.HeightIndex = (*cacheDB)(nil) | ||
|
|
||
| // cacheDB caches data from the underlying [Database]. | ||
| // | ||
| // Operations (Get, Has, Put) are not atomic with the underlying database. | ||
| // Concurrent writes to the same height can result in cache inconsistencies where | ||
| // the cache and database contain different values. This limitation is acceptable | ||
| // because concurrent writes to the same height are not an intended use case. | ||
| type cacheDB struct { | ||
| db *Database | ||
| cache *lru.Cache[BlockHeight, BlockData] | ||
| } | ||
|
|
||
| func newCacheDB(db *Database, size uint16) *cacheDB { | ||
| return &cacheDB{ | ||
| db: db, | ||
| cache: lru.NewCache[BlockHeight, BlockData](int(size)), | ||
| } | ||
| } | ||
|
|
||
| func (c *cacheDB) Get(height BlockHeight) (BlockData, error) { | ||
| c.db.closeMu.RLock() | ||
| defer c.db.closeMu.RUnlock() | ||
|
|
||
| if c.db.closed { | ||
| c.db.log.Error("Failed Get: database closed", zap.Uint64("height", height)) | ||
| return nil, database.ErrClosed | ||
| } | ||
|
|
||
| if cached, ok := c.cache.Get(height); ok { | ||
| return slices.Clone(cached), nil | ||
| } | ||
| data, err := c.db.getWithoutLock(height) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| c.cache.Put(height, slices.Clone(data)) | ||
| return data, nil | ||
| } | ||
|
|
||
| func (c *cacheDB) Put(height BlockHeight, data BlockData) error { | ||
rrazvan1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := c.db.Put(height, data); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| c.cache.Put(height, slices.Clone(data)) | ||
| return nil | ||
| } | ||
|
|
||
| func (c *cacheDB) Has(height BlockHeight) (bool, error) { | ||
| c.db.closeMu.RLock() | ||
| defer c.db.closeMu.RUnlock() | ||
|
|
||
| if c.db.closed { | ||
| c.db.log.Error("Failed Has: database closed", zap.Uint64("height", height)) | ||
| return false, database.ErrClosed | ||
| } | ||
|
|
||
| if _, ok := c.cache.Get(height); ok { | ||
| return true, nil | ||
| } | ||
| return c.db.hasWithoutLock(height) | ||
| } | ||
|
|
||
| func (c *cacheDB) Close() error { | ||
| if err := c.db.Close(); err != nil { | ||
| return err | ||
| } | ||
| c.cache.Flush() | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package blockdb | ||
|
|
||
| import ( | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCacheOnMiss(t *testing.T) { | ||
| db := newCacheDatabase(t, DefaultConfig()) | ||
| height := uint64(20) | ||
| block := randomBlock(t) | ||
| require.NoError(t, db.Put(height, block)) | ||
|
|
||
| // Evict the entry from cache to simulate a cache miss | ||
| db.cache.Evict(height) | ||
|
|
||
| // Read the block - should populate the cache on cache miss | ||
| _, err := db.Get(height) | ||
| require.NoError(t, err) | ||
|
|
||
| _, ok := db.cache.Get(height) | ||
| require.True(t, ok) | ||
| } | ||
|
|
||
| func TestCacheGet(t *testing.T) { | ||
| db := newCacheDatabase(t, DefaultConfig()) | ||
| height := uint64(30) | ||
| block := randomBlock(t) | ||
|
|
||
| // Populate cache directly without writing to database | ||
| db.cache.Put(height, block) | ||
|
|
||
| // Get should return the block from cache | ||
| data, err := db.Get(height) | ||
| require.NoError(t, err) | ||
| require.Equal(t, block, data) | ||
| } | ||
|
|
||
| func TestCacheHas(t *testing.T) { | ||
| db := newCacheDatabase(t, DefaultConfig()) | ||
| height := uint64(40) | ||
| block := randomBlock(t) | ||
|
|
||
| // Populate cache directly without writing to database | ||
| db.cache.Put(height, block) | ||
|
|
||
| // Has should return true from cache even though block is not in database | ||
| has, err := db.Has(height) | ||
| require.NoError(t, err) | ||
| require.True(t, has) | ||
| } | ||
|
|
||
| func TestCachePutStoresClone(t *testing.T) { | ||
| db := newCacheDatabase(t, DefaultConfig()) | ||
| height := uint64(40) | ||
| block := randomBlock(t) | ||
| clone := slices.Clone(block) | ||
| require.NoError(t, db.Put(height, clone)) | ||
|
|
||
| // Modify the original block after Put | ||
| clone[0] = 99 | ||
|
|
||
| // Cache should have the original unmodified data | ||
| cached, ok := db.cache.Get(height) | ||
| require.True(t, ok) | ||
| require.Equal(t, block, cached) | ||
| } | ||
|
|
||
| func TestCacheGetReturnsClone(t *testing.T) { | ||
DracoLi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| db := newCacheDatabase(t, DefaultConfig()) | ||
| height := uint64(50) | ||
| block := randomBlock(t) | ||
| require.NoError(t, db.Put(height, block)) | ||
|
|
||
| // Get the block and modify the returned data | ||
| data, err := db.Get(height) | ||
| require.NoError(t, err) | ||
| data[0] = 99 | ||
|
|
||
| // Cache should still have the original unmodified data | ||
| cached, ok := db.cache.Get(height) | ||
| require.True(t, ok) | ||
| require.Equal(t, block, cached) | ||
|
|
||
| // Second Get should also return original data | ||
| data, err = db.Get(height) | ||
| require.NoError(t, err) | ||
| require.Equal(t, block, data) | ||
| } | ||
|
|
||
| func TestCachePutOverridesSameHeight(t *testing.T) { | ||
| db := newCacheDatabase(t, DefaultConfig()) | ||
| height := uint64(60) | ||
| b1 := randomBlock(t) | ||
| require.NoError(t, db.Put(height, b1)) | ||
|
|
||
| // Verify first block is in cache | ||
| cached, ok := db.cache.Get(height) | ||
| require.True(t, ok) | ||
| require.Equal(t, b1, cached) | ||
|
|
||
| // Put second block at same height and verify it overrides the first one | ||
| b2 := randomBlock(t) | ||
| require.NoError(t, db.Put(height, b2)) | ||
| cached, ok = db.cache.Get(height) | ||
| require.True(t, ok) | ||
| require.Equal(t, b2, cached) | ||
|
|
||
| // Get should also return the new block | ||
| data, err := db.Get(height) | ||
| require.NoError(t, err) | ||
| require.Equal(t, b2, data) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
concurrent Get/Has to the same height is fine and expected. its the writes to the same height that is not intended usage and will result in access inconsistencies from cached Has and Get
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it sounds wrong to me:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted to the original comment. 9792f38
I think both are right depending on how you interpret it but I think we can just call out the writes.