meta_cache_subscribe.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. util.RetryForever("followMetaUpdates", func() error {
  53. clientEpoch++
  54. return pb.WithFilerClientFollowMetadata(client, "mount", selfSignature, clientEpoch, dir, nil, &lastTsNs, 0, selfSignature, processEventFn, pb.FatalOnError)
  55. }, func(err error) bool {
  56. glog.Errorf("follow metadata updates: %v", err)
  57. return true
  58. })
  59. return nil
  60. }