Skip to content
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

unionstore: make lock earlier #484

Merged
merged 4 commits into from
Apr 29, 2022
Merged
Changes from 1 commit
Commits
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
16 changes: 9 additions & 7 deletions internal/unionstore/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,15 @@ func (db *MemDB) Staging() int {

// Release publish all modifications in the latest staging buffer to upper level.
func (db *MemDB) Release(h int) {
db.Lock()
defer db.Unlock()

if h != len(db.stages) {
// This should never happens in production environment.
// Use panic to make debug easier.
panic("cannot release staging buffer")
}

db.Lock()
defer db.Unlock()
if h == 1 {
tail := db.vlog.checkpoint()
if !db.stages[0].isSamePosition(&tail) {
Expand All @@ -131,6 +132,9 @@ func (db *MemDB) Release(h int) {
// Cleanup cleanup the resources referenced by the StagingHandle.
// If the changes are not published by `Release`, they will be discarded.
func (db *MemDB) Cleanup(h int) {
db.Lock()
defer db.Unlock()

if h > len(db.stages) {
return
}
Expand All @@ -140,8 +144,6 @@ func (db *MemDB) Cleanup(h int) {
panic("cannot cleanup staging buffer")
}

db.Lock()
defer db.Unlock()
cp := &db.stages[h-1]
if !db.vlogInvalid {
curr := db.vlog.checkpoint()
Expand Down Expand Up @@ -294,6 +296,9 @@ func (db *MemDB) Dirty() bool {
}

func (db *MemDB) set(key []byte, value []byte, ops ...kv.FlagsOp) error {
db.Lock()
defer db.Unlock()

if db.vlogInvalid {
// panic for easier debugging.
panic("vlog is resetted")
Expand All @@ -308,9 +313,6 @@ func (db *MemDB) set(key []byte, value []byte, ops ...kv.FlagsOp) error {
}
}

db.Lock()
defer db.Unlock()

if len(db.stages) == 0 {
db.dirty = true
}
Expand Down