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.

144 lines
3.7 KiB

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