filer_client_bfs.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package filer_pb
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "io"
  7. "sync"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. )
  11. func TraverseBfs(filerClient FilerClient, parentPath util.FullPath, fn func(parentPath util.FullPath, entry *Entry)) (err error) {
  12. K := 5
  13. var jobQueueWg sync.WaitGroup
  14. queue := util.NewQueue[util.FullPath]()
  15. jobQueueWg.Add(1)
  16. queue.Enqueue(parentPath)
  17. terminates := make([]chan bool, K)
  18. for i := 0; i < K; i++ {
  19. terminates[i] = make(chan bool)
  20. go func(j int) {
  21. for {
  22. select {
  23. case <-terminates[j]:
  24. return
  25. default:
  26. t := queue.Dequeue()
  27. if t == "" {
  28. time.Sleep(329 * time.Millisecond)
  29. continue
  30. }
  31. dir := t
  32. processErr := processOneDirectory(filerClient, dir, queue, &jobQueueWg, fn)
  33. if processErr != nil {
  34. err = processErr
  35. }
  36. jobQueueWg.Done()
  37. }
  38. }
  39. }(i)
  40. }
  41. jobQueueWg.Wait()
  42. for i := 0; i < K; i++ {
  43. close(terminates[i])
  44. }
  45. return
  46. }
  47. func processOneDirectory(filerClient FilerClient, parentPath util.FullPath, queue *util.Queue[util.FullPath], jobQueueWg *sync.WaitGroup, fn func(parentPath util.FullPath, entry *Entry)) (err error) {
  48. return ReadDirAllEntries(filerClient, parentPath, "", func(entry *Entry, isLast bool) error {
  49. fn(parentPath, entry)
  50. if entry.IsDirectory {
  51. subDir := fmt.Sprintf("%s/%s", parentPath, entry.Name)
  52. if parentPath == "/" {
  53. subDir = "/" + entry.Name
  54. }
  55. jobQueueWg.Add(1)
  56. queue.Enqueue(util.FullPath(subDir))
  57. }
  58. return nil
  59. })
  60. }
  61. func StreamBfs(client SeaweedFilerClient, dir util.FullPath, olderThanTsNs int64, fn func(parentPath util.FullPath, entry *Entry)error) (err error) {
  62. glog.V(0).Infof("TraverseBfsMetadata %v if before %v", dir, time.Unix(0, olderThanTsNs))
  63. ctx, cancel := context.WithCancel(context.Background())
  64. defer cancel()
  65. stream, err := client.TraverseBfsMetadata(ctx, &TraverseBfsMetadataRequest{
  66. Directory: string(dir),
  67. })
  68. if err != nil {
  69. return fmt.Errorf("traverse bfs metadata: %v", err)
  70. }
  71. for {
  72. resp, err := stream.Recv()
  73. if err != nil {
  74. if err == io.EOF {
  75. break
  76. }
  77. return fmt.Errorf("traverse bfs metadata: %v", err)
  78. }
  79. if err := fn(util.FullPath(resp.Directory), resp.Entry); err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. }