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.

77 lines
1.9 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. var oldPath util.FullPath
  21. var newEntry *filer.Entry
  22. if message.OldEntry != nil {
  23. oldPath = util.NewFullPath(resp.Directory, message.OldEntry.Name)
  24. glog.V(4).Infof("deleting %v", oldPath)
  25. }
  26. if message.NewEntry != nil {
  27. dir := resp.Directory
  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. return mc.AtomicUpdateEntry(context.Background(), oldPath, newEntry)
  36. }
  37. for {
  38. err := client.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  39. stream, err := client.SubscribeMetadata(context.Background(), &filer_pb.SubscribeMetadataRequest{
  40. ClientName: "mount",
  41. PathPrefix: dir,
  42. SinceNs: lastTsNs,
  43. Signature: selfSignature,
  44. })
  45. if err != nil {
  46. return fmt.Errorf("subscribe: %v", err)
  47. }
  48. for {
  49. resp, listenErr := stream.Recv()
  50. if listenErr == io.EOF {
  51. return nil
  52. }
  53. if listenErr != nil {
  54. return listenErr
  55. }
  56. if err := processEventFn(resp); err != nil {
  57. glog.Fatalf("process %v: %v", resp, err)
  58. }
  59. lastTsNs = resp.TsNs
  60. }
  61. })
  62. if err != nil {
  63. glog.Errorf("subscribing filer meta change: %v", err)
  64. time.Sleep(time.Second)
  65. }
  66. }
  67. }