Skip to content

Commit ae2ceb1

Browse files
authored
Merge pull request #140 from wildcatdb/lru-new
I've written a simpler lru implementation for our use case. No break…
2 parents 7320968 + f35a5ff commit ae2ceb1

File tree

15 files changed

+572
-829
lines changed

15 files changed

+572
-829
lines changed

c/wildcat_c.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ typedef struct {
3434
int level_count;
3535
int level_multiplier;
3636
int block_manager_lru_size;
37-
double block_manager_lru_evict_ratio;
38-
double block_manager_lru_access_weight;
3937
int permission;
4038
int bloom_filter;
4139
int max_compaction_concurrency;
@@ -126,8 +124,6 @@ func fromCOptions(copts *C.wildcat_opts_t) *wildcat.Options {
126124
LevelCount: int(copts.level_count),
127125
LevelMultiplier: int(copts.level_multiplier),
128126
BlockManagerLRUSize: int(copts.block_manager_lru_size),
129-
BlockManagerLRUEvictRatio: float64(copts.block_manager_lru_evict_ratio),
130-
BlockManagerLRUAccesWeight: float64(copts.block_manager_lru_access_weight),
131127
Permission: os.FileMode(copts.permission),
132128
BloomFilter: copts.bloom_filter != 0,
133129
MaxCompactionConcurrency: int(copts.max_compaction_concurrency),

compactor.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,14 @@ func (compactor *Compactor) compactSSTables(sstables []*SSTable, sourceLevelIdx,
529529
if bm, ok := bm.(*blockmanager.BlockManager); ok {
530530
_ = bm.Close()
531531
}
532-
compactor.db.lru.Delete(oldKlogPath)
532+
compactor.db.lru.Remove(oldKlogPath)
533533
}
534534

535535
if bm, ok := compactor.db.lru.Get(oldVlogPath); ok {
536536
if bm, ok := bm.(*blockmanager.BlockManager); ok {
537537
_ = bm.Close()
538538
}
539-
compactor.db.lru.Delete(oldVlogPath)
539+
compactor.db.lru.Remove(oldVlogPath)
540540
}
541541

542542
_ = os.Remove(oldKlogPath)
@@ -576,13 +576,13 @@ func (compactor *Compactor) compactSSTables(sstables []*SSTable, sourceLevelIdx,
576576
return fmt.Errorf("failed to open final VLog block manager: %w", err)
577577
}
578578

579-
compactor.db.lru.Put(klogFinalPath, klogBm, func(key, value interface{}) {
579+
compactor.db.lru.Put(klogFinalPath, klogBm, func(key string, value interface{}) {
580580
if bm, ok := value.(*blockmanager.BlockManager); ok {
581581
_ = bm.Close()
582582
}
583583
})
584584

585-
compactor.db.lru.Put(vlogFinalPath, vlogBm, func(key, value interface{}) {
585+
compactor.db.lru.Put(vlogFinalPath, vlogBm, func(key string, value interface{}) {
586586
if bm, ok := value.(*blockmanager.BlockManager); ok {
587587
_ = bm.Close()
588588
}
@@ -1121,13 +1121,13 @@ func (compactor *Compactor) redistributeToLevel(tables []*SSTable, targetLevelId
11211121
targetLevel.sstables.Store(&newSSTables)
11221122
atomic.AddInt64(&targetLevel.currentSize, newSSTable.Size)
11231123

1124-
compactor.db.lru.Put(klogPath, klogBm, func(key, value interface{}) {
1124+
compactor.db.lru.Put(klogPath, klogBm, func(key string, value interface{}) {
11251125
if bm, ok := value.(*blockmanager.BlockManager); ok {
11261126
_ = bm.Close()
11271127
}
11281128
})
11291129

1130-
compactor.db.lru.Put(vlogPath, vlogBm, func(key, value interface{}) {
1130+
compactor.db.lru.Put(vlogPath, vlogBm, func(key string, value interface{}) {
11311131
if bm, ok := value.(*blockmanager.BlockManager); ok {
11321132
_ = bm.Close()
11331133
}
@@ -1181,14 +1181,14 @@ func (compactor *Compactor) removeSSTablesFromLevel(tablesToRemove []*SSTable, l
11811181
if bm, ok := bm.(*blockmanager.BlockManager); ok {
11821182
_ = bm.Close()
11831183
}
1184-
compactor.db.lru.Delete(klogPath)
1184+
compactor.db.lru.Remove(klogPath)
11851185
}
11861186

11871187
if bm, ok := compactor.db.lru.Get(vlogPath); ok {
11881188
if bm, ok := bm.(*blockmanager.BlockManager); ok {
11891189
_ = bm.Close()
11901190
}
1191-
compactor.db.lru.Delete(vlogPath)
1191+
compactor.db.lru.Remove(vlogPath)
11921192
}
11931193

11941194
_ = os.Remove(klogPath)
@@ -1244,7 +1244,7 @@ func getOrOpenBM(db *DB, path string) (*blockmanager.BlockManager, error) {
12441244
if err != nil {
12451245
return nil, fmt.Errorf("failed to open block manager: %w", err)
12461246
}
1247-
db.lru.Put(path, bm, func(key, value interface{}) {
1247+
db.lru.Put(path, bm, func(key string, value interface{}) {
12481248
if bm, ok := value.(*blockmanager.BlockManager); ok {
12491249
_ = bm.Close()
12501250
}

db.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ const (
6262
DefaultLevelMultiplier = 10 // Multiplier for the number of levels
6363
// 64MB -> 640MB -> 6.4GB -> 64GB -> 640GB -> 6.4TB
6464
DefaultBlockManagerLRUSize = 1024 // Size of the LRU cache for block managers
65-
DefaultBlockManagerLRUEvictRatio = 0.20 // Eviction ratio for the LRU cache
66-
DefaultBlockManagerLRUAccessWeight = 0.8 // Access weight for the LRU cache
6765
DefaultPermission = 0750 // Default permission for created files
6866
DefaultMaxCompactionConcurrency = 4 // Default max compaction concurrency
6967
DefaultCompactionCooldownPeriod = 5 * time.Second // Default cooldown period for compaction
@@ -97,8 +95,6 @@ type Options struct {
9795
LevelCount int // Number of levels in the LSM tree
9896
LevelMultiplier int // Multiplier for the number of levels
9997
BlockManagerLRUSize int // Size of the LRU cache for block managers
100-
BlockManagerLRUEvictRatio float64 // Eviction ratio for the LRU cache
101-
BlockManagerLRUAccesWeight float64 // Access weight for the LRU cache
10298
Permission os.FileMode // Permission for created files
10399
LogChannel chan string // Channel for logging
104100
STDOutLogging bool // Enable logging to standard output (default is false and if set, channel is ignored)
@@ -178,12 +174,12 @@ func Open(opts *Options) (*DB, error) {
178174
}
179175

180176
db := &DB{
181-
lru: lru.New(int64(opts.BlockManagerLRUSize), opts.BlockManagerLRUEvictRatio, opts.BlockManagerLRUAccesWeight), // New block manager LRU cache
182-
wg: &sync.WaitGroup{}, // Wait group for background operations
183-
opts: opts, // Set the options
184-
txnBuffer: buff, // Create a new buffer for transactions with the specified cap
185-
closeCh: make(chan struct{}), // Channel for closing the database
186-
txnTSGenerator: newIDGeneratorWithTimestamp(), // We use timestamp generator for monotonic generation (1579134612000000004, next ID will be 1579134612000000005)
177+
lru: lru.New(opts.BlockManagerLRUSize), // New block manager LRU cache
178+
wg: &sync.WaitGroup{}, // Wait group for background operations
179+
opts: opts, // Set the options
180+
txnBuffer: buff, // Create a new buffer for transactions with the specified cap
181+
closeCh: make(chan struct{}), // Channel for closing the database
182+
txnTSGenerator: newIDGeneratorWithTimestamp(), // We use timestamp generator for monotonic generation (1579134612000000004, next ID will be 1579134612000000005)
187183
}
188184

189185
// Initialize flusher and compactor
@@ -360,14 +356,6 @@ func (opts *Options) setDefaults() error {
360356
opts.BlockManagerLRUSize = DefaultBlockManagerLRUSize
361357
}
362358

363-
if opts.BlockManagerLRUEvictRatio <= 0 {
364-
opts.BlockManagerLRUEvictRatio = DefaultBlockManagerLRUEvictRatio
365-
}
366-
367-
if opts.BlockManagerLRUAccesWeight <= 0 {
368-
opts.BlockManagerLRUAccesWeight = DefaultBlockManagerLRUAccessWeight
369-
}
370-
371359
if opts.SSTableBTreeOrder <= 0 {
372360
opts.SSTableBTreeOrder = DefaultSSTableBTreeOrder
373361
} else {
@@ -433,7 +421,7 @@ func (db *DB) Close() error {
433421
db.wg.Wait()
434422

435423
// Close open block managers
436-
db.lru.ForEach(func(key, value interface{}, accessCount uint64) bool {
424+
db.lru.ForEach(func(key string, value interface{}) bool {
437425
if bm, ok := value.(*blockmanager.BlockManager); ok {
438426
_ = bm.Close()
439427
}
@@ -501,7 +489,7 @@ func (db *DB) reinstate() error {
501489
}
502490

503491
// Add the WAL to the LRU cache
504-
db.lru.Put(newWalPath, walBm, func(key, value interface{}) {
492+
db.lru.Put(newWalPath, walBm, func(key string, value interface{}) {
505493
// Close the block manager when evicted from LRU
506494
if bm, ok := value.(*blockmanager.BlockManager); ok {
507495
_ = bm.Close()
@@ -536,7 +524,7 @@ func (db *DB) reinstate() error {
536524
}
537525

538526
// Add WAL to LRU cache
539-
db.lru.Put(walPath, walBm, func(key, value interface{}) {
527+
db.lru.Put(walPath, walBm, func(key string, value interface{}) {
540528
// Close the block manager when evicted from LRU
541529
if bm, ok := value.(*blockmanager.BlockManager); ok {
542530
_ = bm.Close()
@@ -698,7 +686,7 @@ func (db *DB) reinstate() error {
698686
}
699687

700688
// Update or add to LRU cache
701-
db.lru.Put(activeWALPath, activeWalBm, func(key, value interface{}) {
689+
db.lru.Put(activeWALPath, activeWalBm, func(key string, value interface{}) {
702690
// Close the block manager when evicted from LRU
703691
if bm, ok := value.(*blockmanager.BlockManager); ok {
704692
_ = bm.Close()
@@ -1003,7 +991,6 @@ func (db *DB) Stats() string {
1003991
"Compaction Size Ratio", "Compaction Threshold", "Score Size Weight",
1004992
"Score Count Weight", "Flusher Interval", "Compactor Interval", "Bloom FPR",
1005993
"WAL Retry", "WAL Backoff", "SSTable B-Tree Order", "LRU Size",
1006-
"LRU Evict Ratio", "LRU Access Weight",
1007994
"File Version",
1008995
"Magic Number",
1009996
"Directory",
@@ -1014,8 +1001,7 @@ func (db *DB) Stats() string {
10141001
db.opts.CompactionSizeRatio, db.opts.CompactionSizeThreshold, db.opts.CompactionScoreSizeWeight,
10151002
db.opts.CompactionScoreCountWeight, db.opts.FlusherTickerInterval, db.opts.CompactorTickerInterval,
10161003
db.opts.BloomFilterFPR, db.opts.WalAppendRetry, db.opts.WalAppendBackoff,
1017-
db.opts.SSTableBTreeOrder, db.opts.BlockManagerLRUSize, db.opts.BlockManagerLRUEvictRatio,
1018-
db.opts.BlockManagerLRUAccesWeight,
1004+
db.opts.SSTableBTreeOrder, db.opts.BlockManagerLRUSize,
10191005
blockmanager.Version,
10201006
blockmanager.MagicNumber,
10211007
db.opts.Directory,

flusher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (flusher *Flusher) queueMemtable() error {
5353
}
5454

5555
// Add the new WAL to the LRU cache
56-
flusher.db.lru.Put(newMemtable.wal.path, walBm, func(key, value interface{}) {
56+
flusher.db.lru.Put(newMemtable.wal.path, walBm, func(key string, value interface{}) {
5757
// Close the block manager when evicted from LRU
5858
if bm, ok := value.(*blockmanager.BlockManager); ok {
5959
_ = bm.Close()
@@ -276,12 +276,12 @@ func (flusher *Flusher) flushMemtable(memt *Memtable) error {
276276
}
277277

278278
// Add both KLog and VLog to the LRU cache
279-
flusher.db.lru.Put(klogFinalPath, klogBm, func(key, value interface{}) {
279+
flusher.db.lru.Put(klogFinalPath, klogBm, func(key string, value interface{}) {
280280
if bm, ok := value.(*blockmanager.BlockManager); ok {
281281
_ = bm.Close()
282282
}
283283
})
284-
flusher.db.lru.Put(vlogFinalPath, vlogBm, func(key, value interface{}) {
284+
flusher.db.lru.Put(vlogFinalPath, vlogBm, func(key string, value interface{}) {
285285
if bm, ok := value.(*blockmanager.BlockManager); ok {
286286
_ = bm.Close()
287287
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ go 1.24.5
44

55
require go.mongodb.org/mongo-driver v1.17.3
66

7-
require golang.org/x/sys v0.33.0
7+
require (
8+
github.com/cespare/xxhash/v2 v2.3.0
9+
golang.org/x/sys v0.33.0
10+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
2+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
13
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
24
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=

level.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (l *Level) reopen() error {
9999
}
100100

101101
// Add the KLog to cache
102-
l.db.lru.Put(klogPath, klogBm, func(key, value interface{}) {
102+
l.db.lru.Put(klogPath, klogBm, func(key string, value interface{}) {
103103
if bm, ok := value.(*blockmanager.BlockManager); ok {
104104
_ = bm.Close()
105105
}

0 commit comments

Comments
 (0)