meta_cache.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. entry, err := mc.FindEntry(ctx, oldPath)
  59. if err != nil && err != filer_pb.ErrNotFound {
  60. glog.Errorf("Metacache: find entry error: %v", err)
  61. return err
  62. }
  63. if entry != nil {
  64. if oldPath != "" {
  65. if newEntry != nil && oldPath == newEntry.FullPath {
  66. // skip the unnecessary deletion
  67. // leave the update to the following InsertEntry operation
  68. } else {
  69. ctx = context.WithValue(ctx, "OP", "MV")
  70. glog.V(3).Infof("DeleteEntry %s", oldPath)
  71. if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
  72. return err
  73. }
  74. }
  75. }
  76. } else {
  77. // println("unknown old directory:", oldDir)
  78. }
  79. if newEntry != nil {
  80. newDir, _ := newEntry.DirAndName()
  81. if mc.isCachedFn(util.FullPath(newDir)) {
  82. glog.V(3).Infof("InsertEntry %s/%s", newDir, newEntry.Name())
  83. if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
  84. return err
  85. }
  86. }
  87. }
  88. return nil
  89. }
  90. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
  91. //mc.Lock()
  92. //defer mc.Unlock()
  93. return mc.localStore.UpdateEntry(ctx, entry)
  94. }
  95. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
  96. //mc.RLock()
  97. //defer mc.RUnlock()
  98. entry, err = mc.localStore.FindEntry(ctx, fp)
  99. if err != nil {
  100. return nil, err
  101. }
  102. mc.mapIdFromFilerToLocal(entry)
  103. return
  104. }
  105. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  106. //mc.Lock()
  107. //defer mc.Unlock()
  108. return mc.localStore.DeleteEntry(ctx, fp)
  109. }
  110. func (mc *MetaCache) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
  111. //mc.Lock()
  112. //defer mc.Unlock()
  113. return mc.localStore.DeleteFolderChildren(ctx, fp)
  114. }
  115. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
  116. //mc.RLock()
  117. //defer mc.RUnlock()
  118. if !mc.isCachedFn(dirPath) {
  119. // if this request comes after renaming, it should be fine
  120. glog.Warningf("unsynchronized dir: %v", dirPath)
  121. }
  122. _, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
  123. mc.mapIdFromFilerToLocal(entry)
  124. return eachEntryFunc(entry)
  125. })
  126. if err != nil {
  127. return err
  128. }
  129. return err
  130. }
  131. func (mc *MetaCache) Shutdown() {
  132. //mc.Lock()
  133. //defer mc.Unlock()
  134. mc.localStore.Shutdown()
  135. }
  136. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  137. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  138. }
  139. func (mc *MetaCache) Debug() {
  140. if debuggable, ok := mc.localStore.(filer.Debuggable); ok {
  141. println("start debugging")
  142. debuggable.Debug(os.Stderr)
  143. }
  144. }