Skip to content

Commit 2347d97

Browse files
committed
prevent race condition in queueMemtable by capturing memtable reference once
1 parent 99513a5 commit 2347d97

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

flusher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ func newFlusher(db *DB) *Flusher {
2929

3030
// queueMemtable queues the current active memtable for flushing to disk.
3131
func (flusher *Flusher) queueMemtable() error {
32+
currentMemtable := flusher.db.memtable.Load().(*Memtable)
3233

33-
if atomic.LoadInt64(&flusher.lastQueuedId) == flusher.db.memtable.Load().(*Memtable).id {
34+
if atomic.LoadInt64(&flusher.lastQueuedId) == currentMemtable.id {
3435
return nil // Already queued
3536
}
3637

@@ -54,17 +55,16 @@ func (flusher *Flusher) queueMemtable() error {
5455

5556
// Add the new WAL to the LRU cache
5657
flusher.db.lru.Put(newMemtable.wal.path, walBm, func(key string, value interface{}) {
58+
5759
// Close the block manager when evicted from LRU
5860
if bm, ok := value.(*blockmanager.BlockManager); ok {
5961
_ = bm.Close()
6062
}
6163
})
6264

63-
lastMemt := flusher.db.memtable.Load().(*Memtable)
64-
6565
// Push the current memtable to the immutable queue
66-
flusher.immutable.Enqueue(lastMemt)
67-
atomic.StoreInt64(&flusher.lastQueuedId, lastMemt.id)
66+
flusher.immutable.Enqueue(currentMemtable)
67+
atomic.StoreInt64(&flusher.lastQueuedId, currentMemtable.id)
6868

6969
flusher.db.log(fmt.Sprintf("Flusher: new active memtable created with WAL %s", newMemtable.wal.path))
7070

0 commit comments

Comments
 (0)