meta_replay.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package filer
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/util"
  8. )
  9. func Replay(filerStore FilerStore, resp *filer_pb.SubscribeMetadataResponse) error {
  10. message := resp.EventNotification
  11. var oldPath util.FullPath
  12. var newEntry *Entry
  13. if message.OldEntry != nil {
  14. oldPath = util.NewFullPath(resp.Directory, message.OldEntry.Name)
  15. glog.V(4).Infof("deleting %v", oldPath)
  16. if err := filerStore.DeleteEntry(context.Background(), oldPath); err != nil {
  17. return err
  18. }
  19. }
  20. if message.NewEntry != nil {
  21. dir := resp.Directory
  22. if message.NewParentPath != "" {
  23. dir = message.NewParentPath
  24. }
  25. key := util.NewFullPath(dir, message.NewEntry.Name)
  26. glog.V(4).Infof("creating %v", key)
  27. newEntry = FromPbEntry(dir, message.NewEntry)
  28. if err := filerStore.InsertEntry(context.Background(), newEntry); err != nil {
  29. return err
  30. }
  31. }
  32. return nil
  33. }
  34. // ParallelProcessDirectoryStructure processes each entry in parallel, and also ensure parent directories are processed first.
  35. // This also assumes the parent directories are in the entryChan already.
  36. func ParallelProcessDirectoryStructure(entryChan chan *Entry, concurrency int, eachEntryFn func(entry *Entry)(error)) (firstErr error) {
  37. executors := util.NewLimitedConcurrentExecutor(concurrency)
  38. var wg sync.WaitGroup
  39. for entry := range entryChan {
  40. wg.Add(1)
  41. if entry.IsDirectory() {
  42. func() {
  43. defer wg.Done()
  44. if err := eachEntryFn(entry); err != nil {
  45. if firstErr == nil {
  46. firstErr = err
  47. }
  48. }
  49. }()
  50. } else {
  51. executors.Execute(func() {
  52. defer wg.Done()
  53. if err := eachEntryFn(entry); err != nil {
  54. if firstErr == nil {
  55. firstErr = err
  56. }
  57. }
  58. })
  59. }
  60. if firstErr != nil {
  61. break
  62. }
  63. }
  64. wg.Wait()
  65. return
  66. }