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.

154 lines
4.3 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/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/chrislusf/seaweedfs/weed/util/bounded_tree"
  10. "os"
  11. )
  12. // need to have logic similar to FilerStoreWrapper
  13. // e.g. fill fileId field for chunks
  14. type MetaCache struct {
  15. localStore filer.VirtualFilerStore
  16. // sync.RWMutex
  17. visitedBoundary *bounded_tree.BoundedTree
  18. uidGidMapper *UidGidMapper
  19. invalidateFunc func(fullpath util.FullPath, entry *filer_pb.Entry)
  20. }
  21. func NewMetaCache(dbFolder string, baseDir util.FullPath, uidGidMapper *UidGidMapper, invalidateFunc func(util.FullPath, *filer_pb.Entry)) *MetaCache {
  22. return &MetaCache{
  23. localStore: openMetaStore(dbFolder),
  24. visitedBoundary: bounded_tree.NewBoundedTree(baseDir),
  25. uidGidMapper: uidGidMapper,
  26. invalidateFunc: func(fullpath util.FullPath, entry *filer_pb.Entry) {
  27. invalidateFunc(fullpath, entry)
  28. },
  29. }
  30. }
  31. func openMetaStore(dbFolder string) filer.VirtualFilerStore {
  32. os.RemoveAll(dbFolder)
  33. os.MkdirAll(dbFolder, 0755)
  34. store := &leveldb.LevelDBStore{}
  35. config := &cacheConfig{
  36. dir: dbFolder,
  37. }
  38. if err := store.Initialize(config, ""); err != nil {
  39. glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
  40. }
  41. return filer.NewFilerStoreWrapper(store)
  42. }
  43. func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer.Entry) error {
  44. //mc.Lock()
  45. //defer mc.Unlock()
  46. return mc.doInsertEntry(ctx, entry)
  47. }
  48. func (mc *MetaCache) doInsertEntry(ctx context.Context, entry *filer.Entry) error {
  49. return mc.localStore.InsertEntry(ctx, entry)
  50. }
  51. func (mc *MetaCache) AtomicUpdateEntryFromFiler(ctx context.Context, oldPath util.FullPath, newEntry *filer.Entry) error {
  52. //mc.Lock()
  53. //defer mc.Unlock()
  54. oldDir, _ := oldPath.DirAndName()
  55. if mc.visitedBoundary.HasVisited(util.FullPath(oldDir)) {
  56. if oldPath != "" {
  57. if newEntry != nil && oldPath == newEntry.FullPath {
  58. // skip the unnecessary deletion
  59. // leave the update to the following InsertEntry operation
  60. } else {
  61. glog.V(3).Infof("DeleteEntry %s", oldPath)
  62. if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
  63. return err
  64. }
  65. }
  66. }
  67. } else {
  68. // println("unknown old directory:", oldDir)
  69. }
  70. if newEntry != nil {
  71. newDir, _ := newEntry.DirAndName()
  72. if mc.visitedBoundary.HasVisited(util.FullPath(newDir)) {
  73. glog.V(3).Infof("InsertEntry %s/%s", newDir, newEntry.Name())
  74. if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
  75. return err
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
  82. //mc.Lock()
  83. //defer mc.Unlock()
  84. return mc.localStore.UpdateEntry(ctx, entry)
  85. }
  86. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
  87. //mc.RLock()
  88. //defer mc.RUnlock()
  89. entry, err = mc.localStore.FindEntry(ctx, fp)
  90. if err != nil {
  91. return nil, err
  92. }
  93. mc.mapIdFromFilerToLocal(entry)
  94. return
  95. }
  96. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  97. //mc.Lock()
  98. //defer mc.Unlock()
  99. return mc.localStore.DeleteEntry(ctx, fp)
  100. }
  101. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
  102. //mc.RLock()
  103. //defer mc.RUnlock()
  104. if !mc.visitedBoundary.HasVisited(dirPath) {
  105. // if this request comes after renaming, it should be fine
  106. glog.Warningf("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. }
  125. func (mc *MetaCache) Debug() {
  126. if debuggable, ok := mc.localStore.(filer.Debuggable); ok {
  127. println("start debugging")
  128. debuggable.Debug(os.Stderr)
  129. }
  130. }