You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.9 KiB

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