meta_cache.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package meta_cache
  2. import (
  3. "context"
  4. "os"
  5. "sync"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/filer/leveldb"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. )
  12. // need to have logic similar to FilerStoreWrapper
  13. // e.g. fill fileId field for chunks
  14. type MetaCache struct {
  15. root util.FullPath
  16. localStore filer.VirtualFilerStore
  17. sync.RWMutex
  18. uidGidMapper *UidGidMapper
  19. markCachedFn func(fullpath util.FullPath)
  20. isCachedFn func(fullpath util.FullPath) bool
  21. invalidateFunc func(fullpath util.FullPath, entry *filer_pb.Entry)
  22. }
  23. func NewMetaCache(dbFolder string, uidGidMapper *UidGidMapper, root util.FullPath,
  24. markCachedFn func(path util.FullPath), isCachedFn func(path util.FullPath) bool, invalidateFunc func(util.FullPath, *filer_pb.Entry)) *MetaCache {
  25. return &MetaCache{
  26. root: root,
  27. localStore: openMetaStore(dbFolder),
  28. markCachedFn: markCachedFn,
  29. isCachedFn: isCachedFn,
  30. uidGidMapper: uidGidMapper,
  31. invalidateFunc: func(fullpath util.FullPath, entry *filer_pb.Entry) {
  32. invalidateFunc(fullpath, entry)
  33. },
  34. }
  35. }
  36. func openMetaStore(dbFolder string) filer.VirtualFilerStore {
  37. os.RemoveAll(dbFolder)
  38. os.MkdirAll(dbFolder, 0755)
  39. store := &leveldb.LevelDBStore{}
  40. config := &cacheConfig{
  41. dir: dbFolder,
  42. }
  43. if err := store.Initialize(config, ""); err != nil {
  44. glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
  45. }
  46. return filer.NewFilerStoreWrapper(store)
  47. }
  48. func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer.Entry) error {
  49. mc.Lock()
  50. defer mc.Unlock()
  51. return mc.doInsertEntry(ctx, entry)
  52. }
  53. func (mc *MetaCache) doInsertEntry(ctx context.Context, entry *filer.Entry) error {
  54. return mc.localStore.InsertEntry(ctx, entry)
  55. }
  56. func (mc *MetaCache) AtomicUpdateEntryFromFiler(ctx context.Context, oldPath util.FullPath, newEntry *filer.Entry) error {
  57. mc.Lock()
  58. defer mc.Unlock()
  59. entry, err := mc.localStore.FindEntry(ctx, oldPath)
  60. if err != nil && err != filer_pb.ErrNotFound {
  61. glog.Errorf("Metacache: find entry error: %v", err)
  62. return err
  63. }
  64. if entry != nil {
  65. if oldPath != "" {
  66. if newEntry != nil && oldPath == newEntry.FullPath {
  67. // skip the unnecessary deletion
  68. // leave the update to the following InsertEntry operation
  69. } else {
  70. ctx = context.WithValue(ctx, "OP", "MV")
  71. glog.V(3).Infof("DeleteEntry %s", oldPath)
  72. if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
  73. return err
  74. }
  75. }
  76. }
  77. } else {
  78. // println("unknown old directory:", oldDir)
  79. }
  80. if newEntry != nil {
  81. newDir, _ := newEntry.DirAndName()
  82. if mc.isCachedFn(util.FullPath(newDir)) {
  83. glog.V(3).Infof("InsertEntry %s/%s", newDir, newEntry.Name())
  84. if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
  85. return err
  86. }
  87. }
  88. }
  89. return nil
  90. }
  91. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
  92. mc.Lock()
  93. defer mc.Unlock()
  94. return mc.localStore.UpdateEntry(ctx, entry)
  95. }
  96. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
  97. mc.RLock()
  98. defer mc.RUnlock()
  99. entry, err = mc.localStore.FindEntry(ctx, fp)
  100. if err != nil {
  101. return nil, err
  102. }
  103. mc.mapIdFromFilerToLocal(entry)
  104. return
  105. }
  106. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  107. mc.Lock()
  108. defer mc.Unlock()
  109. return mc.localStore.DeleteEntry(ctx, fp)
  110. }
  111. func (mc *MetaCache) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
  112. mc.Lock()
  113. defer mc.Unlock()
  114. return mc.localStore.DeleteFolderChildren(ctx, fp)
  115. }
  116. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
  117. mc.RLock()
  118. defer mc.RUnlock()
  119. if !mc.isCachedFn(dirPath) {
  120. // if this request comes after renaming, it should be fine
  121. glog.Warningf("unsynchronized dir: %v", dirPath)
  122. }
  123. _, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
  124. mc.mapIdFromFilerToLocal(entry)
  125. return eachEntryFunc(entry)
  126. })
  127. if err != nil {
  128. return err
  129. }
  130. return err
  131. }
  132. func (mc *MetaCache) Shutdown() {
  133. mc.Lock()
  134. defer mc.Unlock()
  135. mc.localStore.Shutdown()
  136. }
  137. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  138. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  139. }
  140. func (mc *MetaCache) Debug() {
  141. if debuggable, ok := mc.localStore.(filer.Debuggable); ok {
  142. println("start debugging")
  143. debuggable.Debug(os.Stderr)
  144. }
  145. }