-
Notifications
You must be signed in to change notification settings - Fork 841
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 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 dafe925
fix: cacheDB use its own close mu
DracoLi 253bfa5
remove getWithoutLock
DracoLi 864fa7d
Merge branch 'master' into dl/block-cache
yacovm 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
Some comments aren't visible on the classic Files Changed page.
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
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,119 @@ | ||
| // 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, _ := newTestDatabase(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.entryCache.Evict(height) | ||
|
|
||
| // Read the block - should populate the cache on cache miss | ||
| _, err := db.Get(height) | ||
| require.NoError(t, err) | ||
|
|
||
| _, ok := db.entryCache.Get(height) | ||
| require.True(t, ok) | ||
| } | ||
|
|
||
| func TestCacheGet(t *testing.T) { | ||
| db, _ := newTestDatabase(t, DefaultConfig()) | ||
| height := uint64(30) | ||
| block := randomBlock(t) | ||
|
|
||
| // Populate cache directly without writing to database | ||
| db.entryCache.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, _ := newTestDatabase(t, DefaultConfig()) | ||
| height := uint64(40) | ||
| block := randomBlock(t) | ||
|
|
||
| // Populate cache directly without writing to database | ||
| db.entryCache.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, _ := newTestDatabase(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.entryCache.Get(height) | ||
| require.True(t, ok) | ||
| require.Equal(t, block, cached) | ||
| } | ||
|
|
||
| func TestCacheGetReturnsClone(t *testing.T) { | ||
| db, _ := newTestDatabase(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.entryCache.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, _ := newTestDatabase(t, DefaultConfig()) | ||
| height := uint64(60) | ||
| b1 := randomBlock(t) | ||
| require.NoError(t, db.Put(height, b1)) | ||
|
|
||
| // Verify first block is in cache | ||
| cached, ok := db.entryCache.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.entryCache.Get(height) | ||
| require.True(t, ok) | ||
| require.Equal(t, b2, cached) | ||
| require.NotEqual(t, b1, 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
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.