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.

82 lines
2.4 KiB

3 years ago
3 years ago
2 years ago
2 years ago
  1. package meta_cache
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/filer"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. )
  10. func SubscribeMetaEvents(mc *MetaCache, selfSignature int32, client filer_pb.FilerClient, dir string, lastTsNs int64) error {
  11. processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
  12. message := resp.EventNotification
  13. for _, sig := range message.Signatures {
  14. if sig == selfSignature && selfSignature != 0 {
  15. return nil
  16. }
  17. }
  18. dir := resp.Directory
  19. var oldPath util.FullPath
  20. var newEntry *filer.Entry
  21. if message.OldEntry != nil {
  22. oldPath = util.NewFullPath(dir, message.OldEntry.Name)
  23. glog.V(4).Infof("deleting %v", oldPath)
  24. }
  25. if message.NewEntry != nil {
  26. if message.NewParentPath != "" {
  27. dir = message.NewParentPath
  28. }
  29. key := util.NewFullPath(dir, message.NewEntry.Name)
  30. glog.V(4).Infof("creating %v", key)
  31. newEntry = filer.FromPbEntry(dir, message.NewEntry)
  32. }
  33. err := mc.AtomicUpdateEntryFromFiler(context.Background(), oldPath, newEntry)
  34. if err == nil {
  35. if message.OldEntry != nil && message.NewEntry != nil {
  36. oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
  37. mc.invalidateFunc(oldKey, message.OldEntry)
  38. if message.OldEntry.Name != message.NewEntry.Name {
  39. newKey := util.NewFullPath(dir, message.NewEntry.Name)
  40. mc.invalidateFunc(newKey, message.NewEntry)
  41. }
  42. } else if filer_pb.IsCreate(resp) {
  43. // no need to invalidate
  44. } else if filer_pb.IsDelete(resp) {
  45. oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
  46. mc.invalidateFunc(oldKey, message.OldEntry)
  47. }
  48. }
  49. return err
  50. }
  51. var clientEpoch int32
  52. metadataFollowOption := &pb.MetadataFollowOption{
  53. ClientName: "mount",
  54. ClientId: selfSignature,
  55. ClientEpoch: clientEpoch,
  56. SelfSignature: selfSignature,
  57. PathPrefix: dir,
  58. AdditionalPathPrefixes: nil,
  59. DirectoriesToWatch: nil,
  60. StartTsNs: lastTsNs,
  61. StopTsNs: 0,
  62. EventErrorType: pb.FatalOnError,
  63. }
  64. util.RetryForever("followMetaUpdates", func() error {
  65. clientEpoch++
  66. return pb.WithFilerClientFollowMetadata(client, metadataFollowOption, processEventFn)
  67. }, func(err error) bool {
  68. glog.Errorf("follow metadata updates: %v", err)
  69. return true
  70. })
  71. return nil
  72. }