meta_cache.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package meta_cache
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/filer"
  5. "github.com/seaweedfs/seaweedfs/weed/filer/leveldb"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. "os"
  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. glog.V(3).Infof("DeleteEntry %s", oldPath)
  66. if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
  67. return err
  68. }
  69. }
  70. }
  71. } else {
  72. // println("unknown old directory:", oldDir)
  73. }
  74. if newEntry != nil {
  75. newDir, _ := newEntry.DirAndName()
  76. if mc.isCachedFn(util.FullPath(newDir)) {
  77. glog.V(3).Infof("InsertEntry %s/%s", newDir, newEntry.Name())
  78. if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
  79. return err
  80. }
  81. }
  82. }
  83. return nil
  84. }
  85. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
  86. //mc.Lock()
  87. //defer mc.Unlock()
  88. return mc.localStore.UpdateEntry(ctx, entry)
  89. }
  90. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
  91. //mc.RLock()
  92. //defer mc.RUnlock()
  93. entry, err = mc.localStore.FindEntry(ctx, fp)
  94. if err != nil {
  95. return nil, err
  96. }
  97. mc.mapIdFromFilerToLocal(entry)
  98. return
  99. }
  100. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  101. //mc.Lock()
  102. //defer mc.Unlock()
  103. return mc.localStore.DeleteEntry(ctx, fp)
  104. }
  105. func (mc *MetaCache) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
  106. //mc.Lock()
  107. //defer mc.Unlock()
  108. return mc.localStore.DeleteFolderChildren(ctx, fp)
  109. }
  110. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
  111. //mc.RLock()
  112. //defer mc.RUnlock()
  113. if !mc.isCachedFn(dirPath) {
  114. // if this request comes after renaming, it should be fine
  115. glog.Warningf("unsynchronized dir: %v", dirPath)
  116. }
  117. _, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
  118. mc.mapIdFromFilerToLocal(entry)
  119. return eachEntryFunc(entry)
  120. })
  121. if err != nil {
  122. return err
  123. }
  124. return err
  125. }
  126. func (mc *MetaCache) Shutdown() {
  127. //mc.Lock()
  128. //defer mc.Unlock()
  129. mc.localStore.Shutdown()
  130. }
  131. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  132. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  133. }
  134. func (mc *MetaCache) Debug() {
  135. if debuggable, ok := mc.localStore.(filer.Debuggable); ok {
  136. println("start debugging")
  137. debuggable.Debug(os.Stderr)
  138. }
  139. }