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.

90 lines
2.2 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 && message.OldEntry != nil {
  37. oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
  38. mc.invalidateFunc(oldKey)
  39. if message.NewEntry != nil {
  40. key := util.NewFullPath(dir, message.NewEntry.Name)
  41. mc.invalidateFunc(key)
  42. }
  43. }
  44. return err
  45. }
  46. for {
  47. err := client.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  48. ctx, cancel := context.WithCancel(context.Background())
  49. defer cancel()
  50. stream, err := client.SubscribeMetadata(ctx, &filer_pb.SubscribeMetadataRequest{
  51. ClientName: "mount",
  52. PathPrefix: dir,
  53. SinceNs: lastTsNs,
  54. Signature: selfSignature,
  55. })
  56. if err != nil {
  57. return fmt.Errorf("subscribe: %v", err)
  58. }
  59. for {
  60. resp, listenErr := stream.Recv()
  61. if listenErr == io.EOF {
  62. return nil
  63. }
  64. if listenErr != nil {
  65. return listenErr
  66. }
  67. if err := processEventFn(resp); err != nil {
  68. glog.Fatalf("process %v: %v", resp, err)
  69. }
  70. lastTsNs = resp.TsNs
  71. }
  72. })
  73. if err != nil {
  74. glog.Errorf("subscribing filer meta change: %v", err)
  75. }
  76. time.Sleep(time.Second)
  77. }
  78. }