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.

81 lines
2.4 KiB

3 years ago
3 years ago
2 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. metadataFollowOption := &pb.MetadataFollowOption{
  52. ClientName: "mount",
  53. ClientId: selfSignature,
  54. ClientEpoch: 1,
  55. SelfSignature: selfSignature,
  56. PathPrefix: dir,
  57. AdditionalPathPrefixes: nil,
  58. DirectoriesToWatch: nil,
  59. StartTsNs: lastTsNs,
  60. StopTsNs: 0,
  61. EventErrorType: pb.FatalOnError,
  62. }
  63. util.RetryUntil("followMetaUpdates", func() error {
  64. metadataFollowOption.ClientEpoch++
  65. return pb.WithFilerClientFollowMetadata(client, metadataFollowOption, processEventFn)
  66. }, func(err error) bool {
  67. glog.Errorf("follow metadata updates: %v", err)
  68. return true
  69. })
  70. return nil
  71. }