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.

175 lines
4.9 KiB

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