Browse Source

FUSE mount: atomic local cache updates

pull/1298/head
Chris Lu 5 years ago
parent
commit
e93588ec78
  1. 60
      weed/filesys/meta_cache/meta_cache.go
  2. 13
      weed/filesys/meta_cache/meta_cache_subscribe.go

60
weed/filesys/meta_cache/meta_cache.go

@ -1,20 +1,24 @@
package meta_cache package meta_cache
import ( import (
"context"
"os" "os"
"sync"
"github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/filer2/leveldb" "github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/util"
) )
type MetaCache struct { type MetaCache struct {
filer2.FilerStore
actualStore filer2.FilerStore
sync.RWMutex
} }
func NewMetaCache(dbFolder string) *MetaCache { func NewMetaCache(dbFolder string) *MetaCache {
return &MetaCache{ return &MetaCache{
FilerStore: openMetaStore(dbFolder),
actualStore: openMetaStore(dbFolder),
} }
} }
@ -35,3 +39,55 @@ func openMetaStore(dbFolder string) filer2.FilerStore {
return store return store
} }
func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer2.Entry) error {
mc.Lock()
defer mc.Unlock()
return mc.actualStore.InsertEntry(ctx, entry)
}
func (mc *MetaCache) AtomicUpdateEntry(ctx context.Context, oldPath util.FullPath, newEntry *filer2.Entry) error {
mc.Lock()
defer mc.Unlock()
if oldPath != "" {
if err := mc.actualStore.DeleteEntry(ctx, oldPath); err != nil {
return err
}
}
if newEntry != nil {
if err := mc.actualStore.InsertEntry(ctx, newEntry); err != nil {
return err
}
}
return nil
}
func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer2.Entry) error {
mc.Lock()
defer mc.Unlock()
return mc.actualStore.UpdateEntry(ctx, entry)
}
func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer2.Entry, err error) {
mc.RLock()
defer mc.RUnlock()
return mc.actualStore.FindEntry(ctx, fp)
}
func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
mc.Lock()
defer mc.Unlock()
return mc.actualStore.DeleteEntry(ctx, fp)
}
func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*filer2.Entry, error) {
mc.RLock()
defer mc.RUnlock()
return mc.actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit)
}
func (mc *MetaCache) Shutdown() {
mc.Lock()
defer mc.Unlock()
mc.actualStore.Shutdown()
}

13
weed/filesys/meta_cache/meta_cache_subscribe.go

@ -16,12 +16,11 @@ func SubscribeMetaEvents(mc *MetaCache, client filer_pb.FilerClient, dir string,
processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error { processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
message := resp.EventNotification message := resp.EventNotification
ctx := context.Background()
var err error
var oldPath util.FullPath
var newEntry *filer2.Entry
if message.OldEntry != nil { if message.OldEntry != nil {
key := util.NewFullPath(resp.Directory, message.OldEntry.Name)
glog.V(4).Infof("deleting %v", key)
err = mc.DeleteEntry(ctx, key)
oldPath = util.NewFullPath(resp.Directory, message.OldEntry.Name)
glog.V(4).Infof("deleting %v", oldPath)
} }
if message.NewEntry != nil { if message.NewEntry != nil {
@ -31,9 +30,9 @@ func SubscribeMetaEvents(mc *MetaCache, client filer_pb.FilerClient, dir string,
} }
key := util.NewFullPath(dir, message.NewEntry.Name) key := util.NewFullPath(dir, message.NewEntry.Name)
glog.V(4).Infof("creating %v", key) glog.V(4).Infof("creating %v", key)
err = mc.InsertEntry(ctx, filer2.FromPbEntry(dir, message.NewEntry))
newEntry = filer2.FromPbEntry(dir, message.NewEntry)
} }
return err
return mc.AtomicUpdateEntry(context.Background(), oldPath, newEntry)
} }
for { for {

Loading…
Cancel
Save