From f468a8506cddf6a60921887a0f596e5a0be8baa3 Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Thu, 28 Apr 2022 12:10:36 +0800 Subject: [PATCH 1/2] make lock earlier Signed-off-by: xiongjiwei --- internal/unionstore/memdb.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/unionstore/memdb.go b/internal/unionstore/memdb.go index 621e41bfe..3941a9a6a 100644 --- a/internal/unionstore/memdb.go +++ b/internal/unionstore/memdb.go @@ -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) { @@ -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 } @@ -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() @@ -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") @@ -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 } From b7b9844b73b2ba85476726fe58942ced1c14781c Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Thu, 28 Apr 2022 15:22:17 +0800 Subject: [PATCH 2/2] fmt Signed-off-by: xiongjiwei --- internal/unionstore/memdb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/unionstore/memdb.go b/internal/unionstore/memdb.go index 3941a9a6a..194b8864b 100644 --- a/internal/unionstore/memdb.go +++ b/internal/unionstore/memdb.go @@ -298,7 +298,7 @@ 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")