lock btree writes

fix https://github.com/chrislusf/seaweedfs/issues/930
This commit is contained in:
Chris Lu 2019-04-16 19:48:07 -07:00
parent 4393b99332
commit 07091c44cd

View file

@ -7,6 +7,7 @@ import (
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
"github.com/google/btree" "github.com/google/btree"
"strings" "strings"
"sync"
) )
func init() { func init() {
@ -14,7 +15,8 @@ func init() {
} }
type MemDbStore struct { type MemDbStore struct {
tree *btree.BTree tree *btree.BTree
treeLock sync.Mutex
} }
type entryItem struct { 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) { func (store *MemDbStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
// println("inserting", entry.FullPath) // println("inserting", entry.FullPath)
store.treeLock.Lock()
store.tree.ReplaceOrInsert(entryItem{entry}) store.tree.ReplaceOrInsert(entryItem{entry})
store.treeLock.Unlock()
return nil 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 { if _, err = store.FindEntry(ctx, entry.FullPath); err != nil {
return fmt.Errorf("no such file %s : %v", entry.FullPath, err) return fmt.Errorf("no such file %s : %v", entry.FullPath, err)
} }
store.treeLock.Lock()
store.tree.ReplaceOrInsert(entryItem{entry}) store.tree.ReplaceOrInsert(entryItem{entry})
store.treeLock.Unlock()
return nil 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) { func (store *MemDbStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
store.treeLock.Lock()
store.tree.Delete(entryItem{&filer2.Entry{FullPath: fullpath}}) store.tree.Delete(entryItem{&filer2.Entry{FullPath: fullpath}})
store.treeLock.Unlock()
return nil return nil
} }