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
4.0 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. "github.com/chrislusf/seaweedfs/weed/filer"
  5. "github.com/chrislusf/seaweedfs/weed/filer/leveldb"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "github.com/chrislusf/seaweedfs/weed/util/bounded_tree"
  9. "os"
  10. )
  11. // need to have logic similar to FilerStoreWrapper
  12. // e.g. fill fileId field for chunks
  13. type MetaCache struct {
  14. localStore filer.VirtualFilerStore
  15. // sync.RWMutex
  16. visitedBoundary *bounded_tree.BoundedTree
  17. uidGidMapper *UidGidMapper
  18. invalidateFunc func(util.FullPath)
  19. }
  20. func NewMetaCache(dbFolder string, baseDir util.FullPath, uidGidMapper *UidGidMapper, invalidateFunc func(util.FullPath)) *MetaCache {
  21. return &MetaCache{
  22. localStore: openMetaStore(dbFolder),
  23. visitedBoundary: bounded_tree.NewBoundedTree(baseDir),
  24. uidGidMapper: uidGidMapper,
  25. invalidateFunc: func(fullpath util.FullPath) {
  26. invalidateFunc(fullpath)
  27. },
  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", oldPath)
  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. // if this request comes after renaming, it should be fine
  105. glog.Warningf("unsynchronized dir: %v", dirPath)
  106. }
  107. _, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
  108. mc.mapIdFromFilerToLocal(entry)
  109. return eachEntryFunc(entry)
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. return err
  115. }
  116. func (mc *MetaCache) Shutdown() {
  117. //mc.Lock()
  118. //defer mc.Unlock()
  119. mc.localStore.Shutdown()
  120. }
  121. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  122. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  123. }