meta_cache_init.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package meta_cache
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/filer"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. )
  10. func EnsureVisited(mc *MetaCache, client filer_pb.FilerClient, dirPath util.FullPath) error {
  11. currentPath := dirPath
  12. for {
  13. // the directory children are already cached
  14. // so no need for this and upper directories
  15. if mc.isCachedFn(currentPath) {
  16. return nil
  17. }
  18. if err := doEnsureVisited(mc, client, currentPath); err != nil {
  19. return err
  20. }
  21. // continue to parent directory
  22. if currentPath != mc.root {
  23. parent, _ := currentPath.DirAndName()
  24. currentPath = util.FullPath(parent)
  25. } else {
  26. break
  27. }
  28. }
  29. return nil
  30. }
  31. func doEnsureVisited(mc *MetaCache, client filer_pb.FilerClient, path util.FullPath) error {
  32. glog.V(4).Infof("ReadDirAllEntries %s ...", path)
  33. err := util.Retry("ReadDirAllEntries", func() error {
  34. return filer_pb.ReadDirAllEntries(client, path, "", func(pbEntry *filer_pb.Entry, isLast bool) error {
  35. entry := filer.FromPbEntry(string(path), pbEntry)
  36. if IsHiddenSystemEntry(string(path), entry.Name()) {
  37. return nil
  38. }
  39. if err := mc.doInsertEntry(context.Background(), entry); err != nil {
  40. glog.V(0).Infof("read %s: %v", entry.FullPath, err)
  41. return err
  42. }
  43. return nil
  44. })
  45. })
  46. if err != nil {
  47. err = fmt.Errorf("list %s: %v", path, err)
  48. } else {
  49. mc.markCachedFn(path)
  50. }
  51. return err
  52. }
  53. func IsHiddenSystemEntry(dir, name string) bool {
  54. return dir == "/" && (name == "topics" || name == "etc")
  55. }