Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
7c5e216
Created a cache for flatKV.
Mar 5, 2026
4a404ee
checkpoint
Mar 5, 2026
d36e825
incremental progress
Mar 5, 2026
2ccbe62
address feedback
Mar 5, 2026
f412e85
more fixes
Mar 5, 2026
e310037
bugfix
Mar 5, 2026
cf1071c
wire in cache
Mar 5, 2026
11232ff
Merge branch 'main' into cjl/flatkv-cache
Mar 6, 2026
a8c1c75
incremental improvements
Mar 6, 2026
221d114
checkin
Mar 6, 2026
8eca079
Moved where the cache sits
Mar 6, 2026
267feae
bugfix
Mar 6, 2026
50b0be6
Batch update the cache
Mar 6, 2026
2ca00d6
Add batch read to cache
Mar 6, 2026
8f8534a
Add batch get to db interface
Mar 6, 2026
23c0277
integrate batch reads
Mar 6, 2026
02d3ca1
wire in cache
Mar 6, 2026
7ee1b08
Introduce work pool, size caches differently
Mar 6, 2026
20c70c3
bugfix
Mar 6, 2026
b714789
Add unit constants
Mar 9, 2026
cc9d41d
refactor threading utils
Mar 9, 2026
53b2bd8
cleanup
Mar 9, 2026
c10e0cd
Cleanup, fix race condition
Mar 9, 2026
04f40fa
cleanup
Mar 9, 2026
b4e4d2c
cleanup
Mar 9, 2026
e53fefa
use pool
Mar 9, 2026
438fc8d
fix ctx lifecycle
Mar 9, 2026
19a8a19
Merge branch 'main' into cjl/flatkv-cache
Mar 9, 2026
23440f6
rename package
Mar 9, 2026
4ecc8fd
Clean up string copies
Mar 9, 2026
7a315c6
simplify gc
Mar 9, 2026
a3f3907
better error handling
Mar 9, 2026
f255b87
use config to configure cache params
Mar 9, 2026
cf0a73d
Allow flatkv config to be set in tests
Mar 9, 2026
0b34737
tweak config
Mar 10, 2026
452aa4d
incremental progress
Mar 10, 2026
1c804a8
move data dir into config
Mar 10, 2026
663b2ea
fix config file
Mar 10, 2026
bb530b5
cleanup
Mar 10, 2026
04daf75
move pebble metrics to proper location
Mar 10, 2026
354818e
clean up metrics
Mar 10, 2026
b1574ac
updated dashboard
Mar 10, 2026
07e071c
fix histograms
Mar 10, 2026
d090796
threading tests
Mar 10, 2026
dfd92c1
test lru queue
Mar 10, 2026
f751a9b
unit tests for shard
Mar 10, 2026
7b5538e
cache tests
Mar 10, 2026
dc8d0c9
moar unit tests
Mar 10, 2026
e9cc9ca
cleanup
Mar 10, 2026
c7a418c
Merge branch 'main' into cjl/flatkv-cache
Mar 10, 2026
087fd0f
Merge branch 'main' into cjl/flatkv-cache
Mar 10, 2026
eb9bc51
Merge branch 'main' into cjl/flatkv-cache
Mar 11, 2026
cea0ebb
unit test fixes
Mar 11, 2026
e58bec2
fix hash bug
Mar 11, 2026
c3f34b1
fixed path bug
Mar 11, 2026
111459f
Helper files for flatKV cache
Mar 11, 2026
d40395f
add missing struct
Mar 11, 2026
c8e85d2
Merge branch 'main' into cjl/cache-auxilery
Mar 12, 2026
ed7e4b6
made suggested changes
Mar 12, 2026
5c46647
fix tests
Mar 12, 2026
be0d4f5
Merge branch 'main' into cjl/flatkv-cache
Mar 12, 2026
9ff2199
Merge branch 'cjl/cache-auxilery' into cjl/flatkv-cache
Mar 12, 2026
bb2fe7e
Maded suggested change to cache structure
Mar 13, 2026
f4b8326
rename cache -> dbcache to avoid gitignore
Mar 13, 2026
4b2247b
Helper files for the flatKV cache implementation
Mar 13, 2026
36d7328
bugfix
Mar 13, 2026
d759a9b
Merge branch 'cjl/cache-auxilery-2' into cjl/flatkv-cache
Mar 16, 2026
4ba242b
fix merge problems
Mar 16, 2026
e19a998
refactor API
Mar 16, 2026
94ae673
made suggested changes
Mar 16, 2026
ed10a26
made suggested changes
Mar 16, 2026
81dfd46
fix bug
Mar 16, 2026
480839d
Merge branch 'main' into cjl/flatkv-cache
Mar 16, 2026
7835683
Implement a standard cache.
Mar 16, 2026
950197c
cleanup
Mar 16, 2026
cff96ab
Merge branch 'main' into cjl/cache-impl
Mar 17, 2026
003fcc9
made suggested changes
Mar 17, 2026
a208a1b
made suggested change
Mar 17, 2026
157a600
made suggested changes
Mar 17, 2026
b41639f
fix unit test
Mar 17, 2026
fe31475
fix unit test
Mar 17, 2026
0702197
Merge branch 'cjl/cache-impl' into cjl/flatkv-cache
Mar 17, 2026
6d435f5
Merge branch 'main' into cjl/flatkv-cache
Mar 20, 2026
64f8530
fixed merge bugs
Mar 20, 2026
d9c5fc1
fix teardown race
Mar 20, 2026
14593ec
Add logging metric, clean up log files before/after run
Mar 20, 2026
2d88076
fix unit test
Mar 20, 2026
ccad074
fix unit tests
Mar 20, 2026
38ffd35
fix unit test
Mar 20, 2026
f143d30
made suggested changes
Mar 25, 2026
a18fd93
config changes
Mar 25, 2026
34e711d
made suggested changes
Mar 25, 2026
b596f89
Merge branch 'main' into cjl/flatkv-cache
Mar 25, 2026
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
1,231 changes: 1,051 additions & 180 deletions docker/monitornode/dashboards/cryptosim-dashboard.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions sei-db/common/metrics/phase_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewPhaseTimerFactory(meter metric.Meter, timerName string) *PhaseTimerFacto
timerName+"_phase_latency_seconds",
metric.WithDescription("Latency per phase (seconds); use for p99, p95, etc."),
metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(LatencyBuckets...),
)
return &PhaseTimerFactory{
phaseDurationTotal: phaseDurationTotal,
Expand Down
4 changes: 4 additions & 0 deletions sei-db/common/threading/fixed_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func NewFixedPool(
queueSize int,
) Pool {

if workers <= 0 {
workers = 1
}

workQueue := make(chan func(), queueSize)
fp := &fixedPool{
workQueue: workQueue,
Expand Down
2 changes: 1 addition & 1 deletion sei-db/config/sc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func DefaultStateCommitConfig() StateCommitConfig {
ReadMode: CosmosOnlyRead,
EnableLatticeHash: false,
MemIAVLConfig: memiavl.DefaultConfig(),
FlatKVConfig: flatkv.DefaultConfig(),
FlatKVConfig: *flatkv.DefaultConfig(),

HistoricalProofMaxInFlight: DefaultSCHistoricalProofMaxInFlight,
HistoricalProofRateLimit: DefaultSCHistoricalProofRateLimit,
Expand Down
23 changes: 4 additions & 19 deletions sei-db/db_engine/dbcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dbcache
import (
"context"
"fmt"
"time"

"github.com/sei-protocol/sei-chain/sei-db/common/threading"
"github.com/sei-protocol/sei-chain/sei-db/db_engine/types"
Expand Down Expand Up @@ -93,32 +92,18 @@ func (u *CacheUpdate) IsDelete() bool {
return u.Value == nil
}

// BuildCache creates a new Cache.
// BuildCache creates a new Cache. When cfg.MaxSize is 0 a no-op (passthrough) cache is returned.
func BuildCache(
ctx context.Context,
shardCount uint64,
maxSize uint64,
cfg *CacheConfig,
readPool threading.Pool,
miscPool threading.Pool,
estimatedOverheadPerEntry uint64,
cacheName string,
metricsScrapeInterval time.Duration,
) (Cache, error) {

if maxSize == 0 {
if cfg.MaxSize == 0 {
return NewNoOpCache(), nil
}

cache, err := NewStandardCache(
ctx,
shardCount,
maxSize,
readPool,
miscPool,
estimatedOverheadPerEntry,
cacheName,
metricsScrapeInterval,
)
cache, err := NewStandardCache(ctx, cfg, readPool, miscPool)
if err != nil {
return nil, fmt.Errorf("failed to create cache: %w", err)
}
Expand Down
43 changes: 43 additions & 0 deletions sei-db/db_engine/dbcache/cache_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dbcache

import (
"fmt"
"time"

"github.com/sei-protocol/sei-chain/sei-db/common/unit"
)

// CacheConfig defines configuration for a sharded LRU read-through cache.
type CacheConfig struct {
// The number of shards in the cache. Must be a power of two and greater than 0.
ShardCount uint64
// The maximum size of the cache, in bytes. 0 disables the cache.
MaxSize uint64
// The estimated overhead per entry, in bytes. Used to calculate effective cache
// capacity. Derive experimentally; may differ between builds and architectures.
EstimatedOverheadPerEntry uint64
// Name used as the "cache" attribute on OTel metrics. Empty string disables metrics.
MetricsName string
// How often to scrape cache size for metrics. Ignored if MetricsName is empty.
MetricsScrapeInterval time.Duration
}

// DefaultCacheConfig returns a CacheConfig with sensible defaults.
func DefaultCacheConfig() CacheConfig {
return CacheConfig{
ShardCount: 8,
MaxSize: 512 * unit.MB,
EstimatedOverheadPerEntry: DefaultEstimatedOverheadPerEntry,
}
}

// Validate checks that the configuration is sane and returns an error if it is not.
func (c *CacheConfig) Validate() error {
if c.MaxSize > 0 && (c.ShardCount == 0 || (c.ShardCount&(c.ShardCount-1)) != 0) {
return fmt.Errorf("shard count must be a non-zero power of two")
}
if c.MetricsName != "" && c.MetricsScrapeInterval <= 0 {
return fmt.Errorf("metrics scrape interval must be positive when metrics name is set")
}
return nil
}
37 changes: 12 additions & 25 deletions sei-db/db_engine/dbcache/cache_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"sync"
"time"

"github.com/sei-protocol/sei-chain/sei-db/common/threading"
"github.com/sei-protocol/sei-chain/sei-db/db_engine/types"
Expand All @@ -29,45 +28,33 @@ type cache struct {
miscPool threading.Pool
}

// Creates a new Cache. If cacheName is non-empty, OTel metrics are enabled and the
// background size scrape runs every metricsScrapeInterval.
// Creates a new Cache. If cfg.MetricsName is non-empty, OTel metrics are enabled and the
// background size scrape runs every cfg.MetricsScrapeInterval.
func NewStandardCache(
ctx context.Context,
// The number of shards in the cache. Must be a power of two and greater than 0.
shardCount uint64,
// The maximum size of the cache, in bytes.
maxSize uint64,
// A work pool for reading from the DB.
cfg *CacheConfig,
readPool threading.Pool,
// A work pool for miscellaneous operations that are neither computationally intensive nor IO bound.
miscPool threading.Pool,
// The estimated overhead per entry, in bytes. This is used to calculate the maximum size of the cache.
// This value should be derived experimentally, and may differ between different builds and architectures.
estimatedOverheadPerEntry uint64,
// Name used as the "cache" attribute on metrics. Empty string disables metrics.
cacheName string,
// How often to scrape cache size for metrics. Ignored if cacheName is empty.
metricsScrapeInterval time.Duration,
) (Cache, error) {
if shardCount == 0 || (shardCount&(shardCount-1)) != 0 {
if cfg.ShardCount == 0 || (cfg.ShardCount&(cfg.ShardCount-1)) != 0 {
return nil, ErrNumShardsNotPowerOfTwo
}
if maxSize == 0 {
if cfg.MaxSize == 0 {
return nil, fmt.Errorf("maxSize must be greater than 0")
}

shardManager, err := newShardManager(shardCount)
shardManager, err := newShardManager(cfg.ShardCount)
if err != nil {
return nil, fmt.Errorf("failed to create shard manager: %w", err)
}
sizePerShard := maxSize / shardCount
sizePerShard := cfg.MaxSize / cfg.ShardCount
if sizePerShard == 0 {
return nil, fmt.Errorf("maxSize must be greater than shardCount")
}

shards := make([]*shard, shardCount)
for i := uint64(0); i < shardCount; i++ {
shards[i], err = NewShard(ctx, readPool, sizePerShard, estimatedOverheadPerEntry)
shards := make([]*shard, cfg.ShardCount)
for i := uint64(0); i < cfg.ShardCount; i++ {
shards[i], err = NewShard(ctx, readPool, sizePerShard, cfg.EstimatedOverheadPerEntry)
if err != nil {
return nil, fmt.Errorf("failed to create shard: %w", err)
}
Expand All @@ -81,8 +68,8 @@ func NewStandardCache(
miscPool: miscPool,
}

if cacheName != "" {
metrics := newCacheMetrics(ctx, cacheName, metricsScrapeInterval, c.getCacheSizeInfo)
if cfg.MetricsName != "" {
metrics := newCacheMetrics(ctx, cfg.MetricsName, cfg.MetricsScrapeInterval, c.getCacheSizeInfo)
for _, s := range c.shards {
s.metrics = metrics
}
Expand Down
54 changes: 37 additions & 17 deletions sei-db/db_engine/dbcache/cache_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func newTestCache(t *testing.T, store map[string][]byte, shardCount, maxSize uin
return v, true, nil
}
pool := threading.NewAdHocPool()
c, err := NewStandardCache(context.Background(), shardCount, maxSize, pool, pool, 16, "", 0)
c, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: shardCount, MaxSize: maxSize, EstimatedOverheadPerEntry: 16,
}, pool, pool)
require.NoError(t, err)
return c, read
}
Expand All @@ -42,50 +44,64 @@ func newTestCache(t *testing.T, store map[string][]byte, shardCount, maxSize uin

func TestNewStandardCacheValid(t *testing.T) {
pool := threading.NewAdHocPool()
c, err := NewStandardCache(context.Background(), 4, 1024, pool, pool, 16, "", 0)
c, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: 4, MaxSize: 1024, EstimatedOverheadPerEntry: 16,
}, pool, pool)
require.NoError(t, err)
require.NotNil(t, c)
}

func TestNewStandardCacheSingleShard(t *testing.T) {
pool := threading.NewAdHocPool()
c, err := NewStandardCache(context.Background(), 1, 1024, pool, pool, 16, "", 0)
c, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: 1, MaxSize: 1024, EstimatedOverheadPerEntry: 16,
}, pool, pool)
require.NoError(t, err)
require.NotNil(t, c)
}

func TestNewStandardCacheShardCountZero(t *testing.T) {
pool := threading.NewAdHocPool()
_, err := NewStandardCache(context.Background(), 0, 1024, pool, pool, 16, "", 0)
_, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: 0, MaxSize: 1024, EstimatedOverheadPerEntry: 16,
}, pool, pool)
require.Error(t, err)
}

func TestNewStandardCacheShardCountNotPowerOfTwo(t *testing.T) {
pool := threading.NewAdHocPool()
for _, n := range []uint64{3, 5, 6, 7, 9, 10} {
_, err := NewStandardCache(context.Background(), n, 1024, pool, pool, 16, "", 0)
_, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: n, MaxSize: 1024, EstimatedOverheadPerEntry: 16,
}, pool, pool)
require.Error(t, err, "shardCount=%d", n)
}
}

func TestNewStandardCacheMaxSizeZero(t *testing.T) {
pool := threading.NewAdHocPool()
_, err := NewStandardCache(context.Background(), 4, 0, pool, pool, 16, "", 0)
_, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: 4, MaxSize: 0, EstimatedOverheadPerEntry: 16,
}, pool, pool)
require.Error(t, err)
}

func TestNewStandardCacheMaxSizeLessThanShardCount(t *testing.T) {
pool := threading.NewAdHocPool()
// shardCount=4, maxSize=3 → sizePerShard=0
_, err := NewStandardCache(context.Background(), 4, 3, pool, pool, 16, "", 0)
_, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: 4, MaxSize: 3, EstimatedOverheadPerEntry: 16,
}, pool, pool)
require.Error(t, err)
}

func TestNewStandardCacheWithMetrics(t *testing.T) {
pool := threading.NewAdHocPool()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := NewStandardCache(ctx, 2, 1024, pool, pool, 0, "test-cache", time.Hour)
c, err := NewStandardCache(ctx, &CacheConfig{
ShardCount: 2, MaxSize: 1024, MetricsName: "test-cache", MetricsScrapeInterval: time.Hour,
}, pool, pool)
require.NoError(t, err)
require.NotNil(t, c)
}
Expand Down Expand Up @@ -144,7 +160,7 @@ func TestCacheGetDBError(t *testing.T) {
dbErr := errors.New("db fail")
readFunc := func(key []byte) ([]byte, bool, error) { return nil, false, dbErr }
pool := threading.NewAdHocPool()
c, _ := NewStandardCache(context.Background(), 1, 4096, pool, pool, 0, "", 0)
c, _ := NewStandardCache(context.Background(), &CacheConfig{ShardCount: 1, MaxSize: 4096}, pool, pool)

_, _, err := c.Get(readFunc, []byte("k"), true)
require.Error(t, err)
Expand All @@ -158,7 +174,7 @@ func TestCacheGetSameKeyConsistentShard(t *testing.T) {
return []byte("val"), true, nil
}
pool := threading.NewAdHocPool()
c, _ := NewStandardCache(context.Background(), 4, 4096, pool, pool, 0, "", 0)
c, _ := NewStandardCache(context.Background(), &CacheConfig{ShardCount: 4, MaxSize: 4096}, pool, pool)

val1, _, _ := c.Get(readFunc, []byte("key"), true)
val2, _, _ := c.Get(readFunc, []byte("key"), true)
Expand Down Expand Up @@ -327,7 +343,7 @@ func TestCacheBatchSetEmpty(t *testing.T) {

func TestCacheBatchSetPoolFailure(t *testing.T) {
readPool := threading.NewAdHocPool()
c, _ := NewStandardCache(context.Background(), 1, 4096, readPool, &failPool{}, 0, "", 0)
c, _ := NewStandardCache(context.Background(), &CacheConfig{ShardCount: 1, MaxSize: 4096}, readPool, &failPool{})

err := c.BatchSet([]CacheUpdate{
{Key: []byte("k"), Value: []byte("v")},
Expand Down Expand Up @@ -405,7 +421,7 @@ func TestCacheBatchGetDBError(t *testing.T) {
dbErr := errors.New("broken")
readFunc := func(key []byte) ([]byte, bool, error) { return nil, false, dbErr }
pool := threading.NewAdHocPool()
c, _ := NewStandardCache(context.Background(), 1, 4096, pool, pool, 0, "", 0)
c, _ := NewStandardCache(context.Background(), &CacheConfig{ShardCount: 1, MaxSize: 4096}, pool, pool)

keys := map[string]types.BatchGetResult{"fail": {}}
require.NoError(t, c.BatchGet(readFunc, keys), "BatchGet itself should not fail")
Expand All @@ -420,7 +436,7 @@ func TestCacheBatchGetEmpty(t *testing.T) {

func TestCacheBatchGetPoolFailure(t *testing.T) {
readPool := threading.NewAdHocPool()
c, _ := NewStandardCache(context.Background(), 1, 4096, readPool, &failPool{}, 0, "", 0)
c, _ := NewStandardCache(context.Background(), &CacheConfig{ShardCount: 1, MaxSize: 4096}, readPool, &failPool{})

keys := map[string]types.BatchGetResult{"k": {}}
err := c.BatchGet(noopRead, keys)
Expand All @@ -429,7 +445,7 @@ func TestCacheBatchGetPoolFailure(t *testing.T) {

func TestCacheBatchGetShardReadPoolFailure(t *testing.T) {
miscPool := threading.NewAdHocPool()
c, _ := NewStandardCache(context.Background(), 1, 4096, &failPool{}, miscPool, 0, "", 0)
c, _ := NewStandardCache(context.Background(), &CacheConfig{ShardCount: 1, MaxSize: 4096}, &failPool{}, miscPool)

keys := map[string]types.BatchGetResult{"a": {}, "b": {}}
require.NoError(t, c.BatchGet(noopRead, keys))
Expand Down Expand Up @@ -505,7 +521,9 @@ func TestCacheGetCacheSizeInfoAggregatesShards(t *testing.T) {
func TestCacheSizeInfoIncludesOverhead(t *testing.T) {
const overhead = 200
pool := threading.NewAdHocPool()
c, err := NewStandardCache(context.Background(), 1, 100_000, pool, pool, overhead, "", 0)
c, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: 1, MaxSize: 100_000, EstimatedOverheadPerEntry: overhead,
}, pool, pool)
require.NoError(t, err)
impl := c.(*cache)

Expand All @@ -523,7 +541,9 @@ func TestCacheOverheadCausesEarlierEviction(t *testing.T) {
pool := threading.NewAdHocPool()
// Single shard, maxSize=500. Each 10-byte value entry costs 1+10+200=211 bytes.
// Two entries = 422 < 500. Three entries = 633 > 500, so one must be evicted.
c, err := NewStandardCache(context.Background(), 1, 500, pool, pool, overhead, "", 0)
c, err := NewStandardCache(context.Background(), &CacheConfig{
ShardCount: 1, MaxSize: 500, EstimatedOverheadPerEntry: overhead,
}, pool, pool)
require.NoError(t, err)
impl := c.(*cache)

Expand Down Expand Up @@ -731,7 +751,7 @@ func TestCacheBatchGetAfterBatchSetWithDeletes(t *testing.T) {
func TestNewStandardCachePowerOfTwoShardCounts(t *testing.T) {
pool := threading.NewAdHocPool()
for _, n := range []uint64{1, 2, 4, 8, 16, 32, 64} {
c, err := NewStandardCache(context.Background(), n, n*100, pool, pool, 0, "", 0)
c, err := NewStandardCache(context.Background(), &CacheConfig{ShardCount: n, MaxSize: n * 100}, pool, pool)
require.NoError(t, err, "shardCount=%d", n)
require.NotNil(t, c, "shardCount=%d", n)
}
Expand Down
Loading
Loading