Skip to content

Commit

Permalink
don't call onMemChange before all work is done in memdb (#636)
Browse files Browse the repository at this point in the history
Signed-off-by: ekexium <[email protected]>
  • Loading branch information
ekexium authored Dec 8, 2022
1 parent ad59ca8 commit 6d5cb2d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion internal/unionstore/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ package unionstore

import (
"bytes"
"fmt"
"math"
"reflect"
"sync"
Expand Down Expand Up @@ -142,7 +143,7 @@ func (db *MemDB) Cleanup(h int) {
if h < len(db.stages) {
// This should never happens in production environment.
// Use panic to make debug easier.
panic("cannot cleanup staging buffer")
panic(fmt.Sprintf("cannot cleanup staging buffer, h=%v, len(db.stages)=%v", h, len(db.stages)))
}

cp := &db.stages[h-1]
Expand All @@ -154,6 +155,7 @@ func (db *MemDB) Cleanup(h int) {
}
}
db.stages = db.stages[:h-1]
db.vlog.onMemChange()
}

// Checkpoint returns a checkpoint of MemDB.
Expand All @@ -166,6 +168,7 @@ func (db *MemDB) Checkpoint() *MemDBCheckpoint {
func (db *MemDB) RevertToCheckpoint(cp *MemDBCheckpoint) {
db.vlog.revertToCheckpoint(db, cp)
db.vlog.truncate(cp)
db.vlog.onMemChange()
}

// Reset resets the MemBuffer to initial states.
Expand Down
4 changes: 3 additions & 1 deletion internal/unionstore/memdb_arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (a *memdbArena) enlarge(allocSize, blockSize int) {
// for some operations (e.g. revertToCheckpoint)
}

// onMemChange should only be called right before exiting memdb.
// This is because the hook can lead to a panic, and leave memdb in an inconsistent state.
func (a *memdbArena) onMemChange() {
hook := a.memChangeHook.Load()
if hook != nil {
Expand Down Expand Up @@ -220,7 +222,7 @@ func (a *memdbArena) truncate(snap *MemDBCheckpoint) {
for _, block := range a.blocks {
a.capacity += uint64(block.length)
}
a.onMemChange()
// We shall not call a.onMemChange() here, since it may cause a panic and leave memdb in an inconsistent state
}

type nodeAllocator struct {
Expand Down

0 comments on commit 6d5cb2d

Please sign in to comment.