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.

160 lines
4.4 KiB

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