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.

71 lines
1.7 KiB

  1. package meta_cache
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  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, client filer_pb.FilerClient, dir string) error {
  13. processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
  14. message := resp.EventNotification
  15. ctx := context.Background()
  16. var err error
  17. if message.OldEntry != nil {
  18. key := util.NewFullPath(resp.Directory, message.OldEntry.Name)
  19. glog.V(4).Infof("deleting %v", key)
  20. err = mc.DeleteEntry(ctx, key)
  21. }
  22. if message.NewEntry != nil {
  23. dir := resp.Directory
  24. if message.NewParentPath != "" {
  25. dir = message.NewParentPath
  26. }
  27. key := util.NewFullPath(dir, message.NewEntry.Name)
  28. glog.V(4).Infof("creating %v", key)
  29. err = mc.InsertEntry(ctx, filer2.FromPbEntry(dir, message.NewEntry))
  30. }
  31. return err
  32. }
  33. var lastTsNs int64
  34. for {
  35. err := client.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  36. stream, err := client.SubscribeMetadata(context.Background(), &filer_pb.SubscribeMetadataRequest{
  37. ClientName: "mount",
  38. PathPrefix: dir,
  39. SinceNs: lastTsNs,
  40. })
  41. if err != nil {
  42. return fmt.Errorf("subscribe: %v", err)
  43. }
  44. for {
  45. resp, listenErr := stream.Recv()
  46. if listenErr == io.EOF {
  47. return nil
  48. }
  49. if listenErr != nil {
  50. return listenErr
  51. }
  52. if err := processEventFn(resp); err != nil {
  53. return fmt.Errorf("process %v: %v", resp, err)
  54. }
  55. lastTsNs = resp.TsNs
  56. }
  57. })
  58. if err != nil {
  59. glog.V(0).Infof("subscribing filer meta change: %v", err)
  60. time.Sleep(time.Second)
  61. }
  62. }
  63. }