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.

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