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.

99 lines
2.6 KiB

  1. package meta_cache
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/filer"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. func SubscribeMetaEvents(mc *MetaCache, selfSignature int32, client filer_pb.FilerClient, dir string, lastTsNs int64) error {
  13. processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
  14. message := resp.EventNotification
  15. for _, sig := range message.Signatures {
  16. if sig == selfSignature && selfSignature != 0 {
  17. return nil
  18. }
  19. }
  20. dir := resp.Directory
  21. var oldPath util.FullPath
  22. var newEntry *filer.Entry
  23. if message.OldEntry != nil {
  24. oldPath = util.NewFullPath(dir, message.OldEntry.Name)
  25. glog.V(4).Infof("deleting %v", oldPath)
  26. }
  27. if message.NewEntry != nil {
  28. if message.NewParentPath != "" {
  29. dir = message.NewParentPath
  30. }
  31. key := util.NewFullPath(dir, message.NewEntry.Name)
  32. glog.V(4).Infof("creating %v", key)
  33. newEntry = filer.FromPbEntry(dir, message.NewEntry)
  34. }
  35. err := mc.AtomicUpdateEntryFromFiler(context.Background(), oldPath, newEntry)
  36. if err == nil {
  37. if message.OldEntry != nil && message.NewEntry != nil {
  38. if message.OldEntry.Name == message.NewEntry.Name {
  39. // no need to invalidate
  40. } else {
  41. oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
  42. mc.invalidateFunc(oldKey)
  43. newKey := util.NewFullPath(dir, message.NewEntry.Name)
  44. mc.invalidateFunc(newKey)
  45. }
  46. } else if message.OldEntry == nil && message.NewEntry != nil {
  47. // no need to invaalidate
  48. } else if message.OldEntry != nil && message.NewEntry == nil {
  49. oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
  50. mc.invalidateFunc(oldKey)
  51. }
  52. }
  53. return err
  54. }
  55. for {
  56. err := client.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  57. ctx, cancel := context.WithCancel(context.Background())
  58. defer cancel()
  59. stream, err := client.SubscribeMetadata(ctx, &filer_pb.SubscribeMetadataRequest{
  60. ClientName: "mount",
  61. PathPrefix: dir,
  62. SinceNs: lastTsNs,
  63. Signature: selfSignature,
  64. })
  65. if err != nil {
  66. return fmt.Errorf("subscribe: %v", err)
  67. }
  68. for {
  69. resp, listenErr := stream.Recv()
  70. if listenErr == io.EOF {
  71. return nil
  72. }
  73. if listenErr != nil {
  74. return listenErr
  75. }
  76. if err := processEventFn(resp); err != nil {
  77. glog.Fatalf("process %v: %v", resp, err)
  78. }
  79. lastTsNs = resp.TsNs
  80. }
  81. })
  82. if err != nil {
  83. glog.Errorf("subscribing filer meta change: %v", err)
  84. }
  85. time.Sleep(time.Second)
  86. }
  87. }