From 07091c44cdd4b5cfe8ebbbb7354c9bd3668e5e5d Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 16 Apr 2019 19:48:07 -0700 Subject: [PATCH] lock btree writes fix https://github.com/chrislusf/seaweedfs/issues/930 --- weed/filer2/memdb/memdb_store.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/weed/filer2/memdb/memdb_store.go b/weed/filer2/memdb/memdb_store.go index 855a13faa..9c10a5472 100644 --- a/weed/filer2/memdb/memdb_store.go +++ b/weed/filer2/memdb/memdb_store.go @@ -7,6 +7,7 @@ import ( "github.com/chrislusf/seaweedfs/weed/util" "github.com/google/btree" "strings" + "sync" ) func init() { @@ -14,7 +15,8 @@ func init() { } type MemDbStore struct { - tree *btree.BTree + tree *btree.BTree + treeLock sync.Mutex } type entryItem struct { @@ -46,7 +48,9 @@ func (store *MemDbStore) RollbackTransaction(ctx context.Context) error { func (store *MemDbStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) { // println("inserting", entry.FullPath) + store.treeLock.Lock() store.tree.ReplaceOrInsert(entryItem{entry}) + store.treeLock.Unlock() return nil } @@ -54,7 +58,9 @@ func (store *MemDbStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) ( if _, err = store.FindEntry(ctx, entry.FullPath); err != nil { return fmt.Errorf("no such file %s : %v", entry.FullPath, err) } + store.treeLock.Lock() store.tree.ReplaceOrInsert(entryItem{entry}) + store.treeLock.Unlock() return nil } @@ -68,7 +74,9 @@ func (store *MemDbStore) FindEntry(ctx context.Context, fullpath filer2.FullPath } func (store *MemDbStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) { + store.treeLock.Lock() store.tree.Delete(entryItem{&filer2.Entry{FullPath: fullpath}}) + store.treeLock.Unlock() return nil }