2022-02-14 09:09:31 +00:00
|
|
|
package meta_cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-08 18:50:57 +00:00
|
|
|
"os"
|
2023-07-12 05:23:32 +00:00
|
|
|
"sync"
|
2022-12-08 18:50:57 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer/leveldb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2022-02-14 09:09:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// need to have logic similar to FilerStoreWrapper
|
|
|
|
// e.g. fill fileId field for chunks
|
|
|
|
|
|
|
|
type MetaCache struct {
|
2022-02-28 20:16:53 +00:00
|
|
|
root util.FullPath
|
2022-02-14 09:09:31 +00:00
|
|
|
localStore filer.VirtualFilerStore
|
2023-07-12 05:23:32 +00:00
|
|
|
sync.RWMutex
|
2022-02-14 09:09:31 +00:00
|
|
|
uidGidMapper *UidGidMapper
|
|
|
|
markCachedFn func(fullpath util.FullPath)
|
|
|
|
isCachedFn func(fullpath util.FullPath) bool
|
|
|
|
invalidateFunc func(fullpath util.FullPath, entry *filer_pb.Entry)
|
|
|
|
}
|
|
|
|
|
2022-02-28 20:16:53 +00:00
|
|
|
func NewMetaCache(dbFolder string, uidGidMapper *UidGidMapper, root util.FullPath,
|
|
|
|
markCachedFn func(path util.FullPath), isCachedFn func(path util.FullPath) bool, invalidateFunc func(util.FullPath, *filer_pb.Entry)) *MetaCache {
|
2022-02-14 09:09:31 +00:00
|
|
|
return &MetaCache{
|
2022-02-28 20:16:53 +00:00
|
|
|
root: root,
|
2022-02-14 09:09:31 +00:00
|
|
|
localStore: openMetaStore(dbFolder),
|
|
|
|
markCachedFn: markCachedFn,
|
|
|
|
isCachedFn: isCachedFn,
|
|
|
|
uidGidMapper: uidGidMapper,
|
|
|
|
invalidateFunc: func(fullpath util.FullPath, entry *filer_pb.Entry) {
|
|
|
|
invalidateFunc(fullpath, entry)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func openMetaStore(dbFolder string) filer.VirtualFilerStore {
|
|
|
|
|
|
|
|
os.RemoveAll(dbFolder)
|
|
|
|
os.MkdirAll(dbFolder, 0755)
|
|
|
|
|
|
|
|
store := &leveldb.LevelDBStore{}
|
|
|
|
config := &cacheConfig{
|
|
|
|
dir: dbFolder,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.Initialize(config, ""); err != nil {
|
|
|
|
glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filer.NewFilerStoreWrapper(store)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer.Entry) error {
|
2023-07-12 05:23:32 +00:00
|
|
|
mc.Lock()
|
|
|
|
defer mc.Unlock()
|
2022-02-14 09:09:31 +00:00
|
|
|
return mc.doInsertEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) doInsertEntry(ctx context.Context, entry *filer.Entry) error {
|
|
|
|
return mc.localStore.InsertEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
2022-08-01 05:51:41 +00:00
|
|
|
func (mc *MetaCache) AtomicUpdateEntryFromFiler(ctx context.Context, oldPath util.FullPath, newEntry *filer.Entry) error {
|
2023-07-12 05:23:32 +00:00
|
|
|
mc.Lock()
|
|
|
|
defer mc.Unlock()
|
2022-02-14 09:09:31 +00:00
|
|
|
|
2023-07-12 05:23:32 +00:00
|
|
|
entry, err := mc.localStore.FindEntry(ctx, oldPath)
|
2023-01-29 15:55:27 +00:00
|
|
|
if err != nil && err != filer_pb.ErrNotFound {
|
|
|
|
glog.Errorf("Metacache: find entry error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if entry != nil {
|
2022-02-14 09:09:31 +00:00
|
|
|
if oldPath != "" {
|
|
|
|
if newEntry != nil && oldPath == newEntry.FullPath {
|
|
|
|
// skip the unnecessary deletion
|
|
|
|
// leave the update to the following InsertEntry operation
|
|
|
|
} else {
|
2022-12-08 18:50:57 +00:00
|
|
|
ctx = context.WithValue(ctx, "OP", "MV")
|
2022-02-14 09:09:31 +00:00
|
|
|
glog.V(3).Infof("DeleteEntry %s", oldPath)
|
2022-08-01 05:51:41 +00:00
|
|
|
if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
|
|
|
|
return err
|
2022-02-14 09:09:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// println("unknown old directory:", oldDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if newEntry != nil {
|
|
|
|
newDir, _ := newEntry.DirAndName()
|
|
|
|
if mc.isCachedFn(util.FullPath(newDir)) {
|
|
|
|
glog.V(3).Infof("InsertEntry %s/%s", newDir, newEntry.Name())
|
|
|
|
if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
|
2023-07-12 05:23:32 +00:00
|
|
|
mc.Lock()
|
|
|
|
defer mc.Unlock()
|
2022-02-14 09:09:31 +00:00
|
|
|
return mc.localStore.UpdateEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
|
2023-07-12 05:23:32 +00:00
|
|
|
mc.RLock()
|
|
|
|
defer mc.RUnlock()
|
2022-02-14 09:09:31 +00:00
|
|
|
entry, err = mc.localStore.FindEntry(ctx, fp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mc.mapIdFromFilerToLocal(entry)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
|
2023-07-12 05:23:32 +00:00
|
|
|
mc.Lock()
|
|
|
|
defer mc.Unlock()
|
2022-02-14 09:09:31 +00:00
|
|
|
return mc.localStore.DeleteEntry(ctx, fp)
|
|
|
|
}
|
|
|
|
func (mc *MetaCache) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
|
2023-07-12 05:23:32 +00:00
|
|
|
mc.Lock()
|
|
|
|
defer mc.Unlock()
|
2022-02-14 09:09:31 +00:00
|
|
|
return mc.localStore.DeleteFolderChildren(ctx, fp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
|
2023-07-12 05:23:32 +00:00
|
|
|
mc.RLock()
|
|
|
|
defer mc.RUnlock()
|
2022-02-14 09:09:31 +00:00
|
|
|
|
|
|
|
if !mc.isCachedFn(dirPath) {
|
|
|
|
// if this request comes after renaming, it should be fine
|
|
|
|
glog.Warningf("unsynchronized dir: %v", dirPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
|
|
|
|
mc.mapIdFromFilerToLocal(entry)
|
|
|
|
return eachEntryFunc(entry)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) Shutdown() {
|
2023-07-12 05:23:32 +00:00
|
|
|
mc.Lock()
|
|
|
|
defer mc.Unlock()
|
2022-02-14 09:09:31 +00:00
|
|
|
mc.localStore.Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
|
|
|
|
entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MetaCache) Debug() {
|
|
|
|
if debuggable, ok := mc.localStore.(filer.Debuggable); ok {
|
|
|
|
println("start debugging")
|
|
|
|
debuggable.Debug(os.Stderr)
|
|
|
|
}
|
|
|
|
}
|