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.

152 lines
4.1 KiB

4 years ago
5 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. "strings"
  7. "sync"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/filer/leveldb"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/chrislusf/seaweedfs/weed/util/bounded_tree"
  13. )
  14. // need to have logic similar to FilerStoreWrapper
  15. // e.g. fill fileId field for chunks
  16. type MetaCache struct {
  17. localStore filer.VirtualFilerStore
  18. sync.RWMutex
  19. visitedBoundary *bounded_tree.BoundedTree
  20. uidGidMapper *UidGidMapper
  21. invalidateFunc func(util.FullPath)
  22. }
  23. func NewMetaCache(dbFolder string, baseDir util.FullPath, uidGidMapper *UidGidMapper, invalidateFunc func(util.FullPath)) *MetaCache {
  24. return &MetaCache{
  25. localStore: openMetaStore(dbFolder),
  26. visitedBoundary: bounded_tree.NewBoundedTree(baseDir),
  27. uidGidMapper: uidGidMapper,
  28. invalidateFunc: func(fullpath util.FullPath) {
  29. if baseDir != "/" && strings.HasPrefix(string(fullpath), string(baseDir)) {
  30. fullpath = fullpath[len(baseDir):]
  31. }
  32. invalidateFunc(fullpath)
  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. oldDir, _ := oldPath.DirAndName()
  60. if mc.visitedBoundary.HasVisited(util.FullPath(oldDir)) {
  61. if oldPath != "" {
  62. if newEntry != nil && oldPath == newEntry.FullPath {
  63. // skip the unnecessary deletion
  64. // leave the update to the following InsertEntry operation
  65. } else {
  66. glog.V(3).Infof("DeleteEntry %s/%s", oldPath, oldPath.Name())
  67. if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
  68. return err
  69. }
  70. }
  71. }
  72. } else {
  73. // println("unknown old directory:", oldDir)
  74. }
  75. if newEntry != nil {
  76. newDir, _ := newEntry.DirAndName()
  77. if mc.visitedBoundary.HasVisited(util.FullPath(newDir)) {
  78. glog.V(3).Infof("InsertEntry %s/%s", newDir, newEntry.Name())
  79. if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
  80. return err
  81. }
  82. }
  83. }
  84. return nil
  85. }
  86. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
  87. mc.Lock()
  88. defer mc.Unlock()
  89. return mc.localStore.UpdateEntry(ctx, entry)
  90. }
  91. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
  92. mc.RLock()
  93. defer mc.RUnlock()
  94. entry, err = mc.localStore.FindEntry(ctx, fp)
  95. if err != nil {
  96. return nil, err
  97. }
  98. mc.mapIdFromFilerToLocal(entry)
  99. return
  100. }
  101. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  102. mc.Lock()
  103. defer mc.Unlock()
  104. return mc.localStore.DeleteEntry(ctx, fp)
  105. }
  106. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
  107. mc.RLock()
  108. defer mc.RUnlock()
  109. if !mc.visitedBoundary.HasVisited(dirPath) {
  110. return fmt.Errorf("unsynchronized dir: %v", dirPath)
  111. }
  112. _, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
  113. mc.mapIdFromFilerToLocal(entry)
  114. return eachEntryFunc(entry)
  115. })
  116. if err != nil {
  117. return err
  118. }
  119. return err
  120. }
  121. func (mc *MetaCache) Shutdown() {
  122. mc.Lock()
  123. defer mc.Unlock()
  124. mc.localStore.Shutdown()
  125. }
  126. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  127. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  128. }