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.

165 lines
4.5 KiB

  1. package meta_cache
  2. import (
  3. "context"
  4. "os"
  5. "github.com/seaweedfs/seaweedfs/weed/filer"
  6. "github.com/seaweedfs/seaweedfs/weed/filer/leveldb"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. )
  11. // need to have logic similar to FilerStoreWrapper
  12. // e.g. fill fileId field for chunks
  13. type MetaCache struct {
  14. root util.FullPath
  15. localStore filer.VirtualFilerStore
  16. // sync.RWMutex
  17. uidGidMapper *UidGidMapper
  18. markCachedFn func(fullpath util.FullPath)
  19. isCachedFn func(fullpath util.FullPath) bool
  20. invalidateFunc func(fullpath util.FullPath, entry *filer_pb.Entry)
  21. }
  22. func NewMetaCache(dbFolder string, uidGidMapper *UidGidMapper, root util.FullPath,
  23. markCachedFn func(path util.FullPath), isCachedFn func(path util.FullPath) bool, invalidateFunc func(util.FullPath, *filer_pb.Entry)) *MetaCache {
  24. return &MetaCache{
  25. root: root,
  26. localStore: openMetaStore(dbFolder),
  27. markCachedFn: markCachedFn,
  28. isCachedFn: isCachedFn,
  29. uidGidMapper: uidGidMapper,
  30. invalidateFunc: func(fullpath util.FullPath, entry *filer_pb.Entry) {
  31. invalidateFunc(fullpath, entry)
  32. },
  33. }
  34. }
  35. func openMetaStore(dbFolder string) filer.VirtualFilerStore {
  36. os.RemoveAll(dbFolder)
  37. os.MkdirAll(dbFolder, 0755)
  38. store := &leveldb.LevelDBStore{}
  39. config := &cacheConfig{
  40. dir: dbFolder,
  41. }
  42. if err := store.Initialize(config, ""); err != nil {
  43. glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
  44. }
  45. return filer.NewFilerStoreWrapper(store)
  46. }
  47. func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer.Entry) error {
  48. //mc.Lock()
  49. //defer mc.Unlock()
  50. return mc.doInsertEntry(ctx, entry)
  51. }
  52. func (mc *MetaCache) doInsertEntry(ctx context.Context, entry *filer.Entry) error {
  53. return mc.localStore.InsertEntry(ctx, entry)
  54. }
  55. func (mc *MetaCache) AtomicUpdateEntryFromFiler(ctx context.Context, oldPath util.FullPath, newEntry *filer.Entry) error {
  56. //mc.Lock()
  57. //defer mc.Unlock()
  58. oldDir, _ := oldPath.DirAndName()
  59. if mc.isCachedFn(util.FullPath(oldDir)) {
  60. if oldPath != "" {
  61. if newEntry != nil && oldPath == newEntry.FullPath {
  62. // skip the unnecessary deletion
  63. // leave the update to the following InsertEntry operation
  64. } else {
  65. ctx = context.WithValue(ctx, "OP", "MV")
  66. glog.V(3).Infof("DeleteEntry %s", oldPath)
  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.isCachedFn(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) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
  107. //mc.Lock()
  108. //defer mc.Unlock()
  109. return mc.localStore.DeleteFolderChildren(ctx, fp)
  110. }
  111. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
  112. //mc.RLock()
  113. //defer mc.RUnlock()
  114. if !mc.isCachedFn(dirPath) {
  115. // if this request comes after renaming, it should be fine
  116. glog.Warningf("unsynchronized dir: %v", dirPath)
  117. }
  118. _, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
  119. mc.mapIdFromFilerToLocal(entry)
  120. return eachEntryFunc(entry)
  121. })
  122. if err != nil {
  123. return err
  124. }
  125. return err
  126. }
  127. func (mc *MetaCache) Shutdown() {
  128. //mc.Lock()
  129. //defer mc.Unlock()
  130. mc.localStore.Shutdown()
  131. }
  132. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  133. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  134. }
  135. func (mc *MetaCache) Debug() {
  136. if debuggable, ok := mc.localStore.(filer.Debuggable); ok {
  137. println("start debugging")
  138. debuggable.Debug(os.Stderr)
  139. }
  140. }