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.

146 lines
3.9 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. "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: invalidateFunc,
  28. }
  29. }
  30. func openMetaStore(dbFolder string) filer.VirtualFilerStore {
  31. os.RemoveAll(dbFolder)
  32. os.MkdirAll(dbFolder, 0755)
  33. store := &leveldb.LevelDBStore{}
  34. config := &cacheConfig{
  35. dir: dbFolder,
  36. }
  37. if err := store.Initialize(config, ""); err != nil {
  38. glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
  39. }
  40. return filer.NewFilerStoreWrapper(store)
  41. }
  42. func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer.Entry) error {
  43. mc.Lock()
  44. defer mc.Unlock()
  45. return mc.doInsertEntry(ctx, entry)
  46. }
  47. func (mc *MetaCache) doInsertEntry(ctx context.Context, entry *filer.Entry) error {
  48. return mc.localStore.InsertEntry(ctx, entry)
  49. }
  50. func (mc *MetaCache) AtomicUpdateEntryFromFiler(ctx context.Context, oldPath util.FullPath, newEntry *filer.Entry) error {
  51. mc.Lock()
  52. defer mc.Unlock()
  53. oldDir, _ := oldPath.DirAndName()
  54. if mc.visitedBoundary.HasVisited(util.FullPath(oldDir)) {
  55. if oldPath != "" {
  56. if newEntry != nil && oldPath == newEntry.FullPath {
  57. // skip the unnecessary deletion
  58. // leave the update to the following InsertEntry operation
  59. } else {
  60. glog.V(3).Infof("DeleteEntry %s/%s", oldPath, oldPath.Name())
  61. if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
  62. return err
  63. }
  64. }
  65. }
  66. } else {
  67. // println("unknown old directory:", oldDir)
  68. }
  69. if newEntry != nil {
  70. newDir, _ := newEntry.DirAndName()
  71. if mc.visitedBoundary.HasVisited(util.FullPath(newDir)) {
  72. glog.V(3).Infof("InsertEntry %s/%s", newDir, newEntry.Name())
  73. if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
  74. return err
  75. }
  76. }
  77. }
  78. return nil
  79. }
  80. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
  81. mc.Lock()
  82. defer mc.Unlock()
  83. return mc.localStore.UpdateEntry(ctx, entry)
  84. }
  85. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
  86. mc.RLock()
  87. defer mc.RUnlock()
  88. entry, err = mc.localStore.FindEntry(ctx, fp)
  89. if err != nil {
  90. return nil, err
  91. }
  92. mc.mapIdFromFilerToLocal(entry)
  93. return
  94. }
  95. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  96. mc.Lock()
  97. defer mc.Unlock()
  98. return mc.localStore.DeleteEntry(ctx, fp)
  99. }
  100. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
  101. mc.RLock()
  102. defer mc.RUnlock()
  103. if !mc.visitedBoundary.HasVisited(dirPath) {
  104. return fmt.Errorf("unsynchronized dir: %v", dirPath)
  105. }
  106. _, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
  107. mc.mapIdFromFilerToLocal(entry)
  108. return eachEntryFunc(entry)
  109. })
  110. if err != nil {
  111. return err
  112. }
  113. return err
  114. }
  115. func (mc *MetaCache) Shutdown() {
  116. mc.Lock()
  117. defer mc.Unlock()
  118. mc.localStore.Shutdown()
  119. }
  120. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  121. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  122. }