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.

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