filer_grpc_server_sub_meta.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package weed_server
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/golang/protobuf/proto"
  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 (fs *FilerServer) SubscribeMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer) error {
  13. peerAddress := findClientAddress(stream.Context(), 0)
  14. clientName := fs.addClient(req.ClientName, peerAddress)
  15. defer fs.deleteClient(clientName)
  16. lastReadTime := time.Unix(0, req.SinceNs)
  17. glog.V(0).Infof(" %v starts to subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  18. eachEventNotificationFn := eachEventNotificationFn(req, stream, clientName)
  19. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  20. processedTsNs, err := fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  21. if err != nil {
  22. return fmt.Errorf("reading from persisted logs: %v", err)
  23. }
  24. if processedTsNs != 0 {
  25. lastReadTime = time.Unix(0, processedTsNs)
  26. }
  27. err = fs.filer.MetaAggregator.MetaLogBuffer.LoopProcessLogData(lastReadTime, func() bool {
  28. fs.filer.MetaAggregator.ListenersLock.Lock()
  29. fs.filer.MetaAggregator.ListenersCond.Wait()
  30. fs.filer.MetaAggregator.ListenersLock.Unlock()
  31. return true
  32. }, eachLogEntryFn)
  33. return err
  34. }
  35. func (fs *FilerServer) SubscribeLocalMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeLocalMetadataServer) error {
  36. peerAddress := findClientAddress(stream.Context(), 0)
  37. clientName := fs.addClient(req.ClientName, peerAddress)
  38. defer fs.deleteClient(clientName)
  39. lastReadTime := time.Unix(0, req.SinceNs)
  40. glog.V(0).Infof(" %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  41. eachEventNotificationFn := eachEventNotificationFn(req, stream, clientName)
  42. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  43. if _, ok := fs.filer.Store.ActualStore.(filer2.FilerLocalStore); ok {
  44. // println("reading from persisted logs ...")
  45. processedTsNs, err := fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  46. if err != nil {
  47. return fmt.Errorf("reading from persisted logs: %v", err)
  48. }
  49. if processedTsNs != 0 {
  50. lastReadTime = time.Unix(0, processedTsNs)
  51. }
  52. glog.V(0).Infof("after local log reads, %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  53. }
  54. // println("reading from in memory logs ...")
  55. err := fs.filer.LocalMetaLogBuffer.LoopProcessLogData(lastReadTime, func() bool {
  56. fs.listenersLock.Lock()
  57. fs.listenersCond.Wait()
  58. fs.listenersLock.Unlock()
  59. return true
  60. }, eachLogEntryFn)
  61. return err
  62. }
  63. func eachLogEntryFn(eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error) func(logEntry *filer_pb.LogEntry) error {
  64. return func(logEntry *filer_pb.LogEntry) error {
  65. event := &filer_pb.SubscribeMetadataResponse{}
  66. if err := proto.Unmarshal(logEntry.Data, event); err != nil {
  67. glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  68. return fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  69. }
  70. if err := eachEventNotificationFn(event.Directory, event.EventNotification, event.TsNs); err != nil {
  71. return err
  72. }
  73. return nil
  74. }
  75. }
  76. func eachEventNotificationFn(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer, clientName string) func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  77. return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  78. // get complete path to the file or directory
  79. var entryName string
  80. if eventNotification.OldEntry != nil {
  81. entryName = eventNotification.OldEntry.Name
  82. } else if eventNotification.NewEntry != nil {
  83. entryName = eventNotification.NewEntry.Name
  84. }
  85. fullpath := util.Join(dirPath, entryName)
  86. // skip on filer internal meta logs
  87. if strings.HasPrefix(fullpath, filer2.SystemLogDir) {
  88. return nil
  89. }
  90. if !strings.HasPrefix(fullpath, req.PathPrefix) {
  91. return nil
  92. }
  93. message := &filer_pb.SubscribeMetadataResponse{
  94. Directory: dirPath,
  95. EventNotification: eventNotification,
  96. TsNs: tsNs,
  97. }
  98. // println("sending", dirPath, entryName)
  99. if err := stream.Send(message); err != nil {
  100. glog.V(0).Infof("=> client %v: %+v", clientName, err)
  101. return err
  102. }
  103. return nil
  104. }
  105. }
  106. func (fs *FilerServer) addClient(clientType string, clientAddress string) (clientName string) {
  107. clientName = clientType + "@" + clientAddress
  108. glog.V(0).Infof("+ listener %v", clientName)
  109. return
  110. }
  111. func (fs *FilerServer) deleteClient(clientName string) {
  112. glog.V(0).Infof("- listener %v", clientName)
  113. }