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.

155 lines
4.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/golang/protobuf/proto"
  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 (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, req.Signature)
  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, req.Signature)
  42. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  43. // println("reading from persisted logs ...")
  44. processedTsNs, err := fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  45. if err != nil {
  46. return fmt.Errorf("reading from persisted logs: %v", err)
  47. }
  48. if processedTsNs != 0 {
  49. lastReadTime = time.Unix(0, processedTsNs)
  50. }
  51. glog.V(0).Infof("after local log reads, %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  52. // println("reading from in memory logs ...")
  53. err = fs.filer.LocalMetaLogBuffer.LoopProcessLogData(lastReadTime, func() bool {
  54. fs.listenersLock.Lock()
  55. fs.listenersCond.Wait()
  56. fs.listenersLock.Unlock()
  57. return true
  58. }, eachLogEntryFn)
  59. return err
  60. }
  61. func eachLogEntryFn(eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error) func(logEntry *filer_pb.LogEntry) error {
  62. return func(logEntry *filer_pb.LogEntry) error {
  63. event := &filer_pb.SubscribeMetadataResponse{}
  64. if err := proto.Unmarshal(logEntry.Data, event); err != nil {
  65. glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  66. return fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  67. }
  68. if err := eachEventNotificationFn(event.Directory, event.EventNotification, event.TsNs); err != nil {
  69. return err
  70. }
  71. return nil
  72. }
  73. }
  74. func eachEventNotificationFn(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer, clientName string, clientSignature int32) func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  75. return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  76. for _, sig := range eventNotification.Signatures {
  77. if sig == clientSignature && clientSignature != 0 {
  78. return nil
  79. }
  80. }
  81. // get complete path to the file or directory
  82. var entryName string
  83. if eventNotification.OldEntry != nil {
  84. entryName = eventNotification.OldEntry.Name
  85. } else if eventNotification.NewEntry != nil {
  86. entryName = eventNotification.NewEntry.Name
  87. }
  88. fullpath := util.Join(dirPath, entryName)
  89. // skip on filer internal meta logs
  90. if strings.HasPrefix(fullpath, filer.SystemLogDir) {
  91. return nil
  92. }
  93. if !strings.HasPrefix(fullpath, req.PathPrefix) {
  94. return nil
  95. }
  96. message := &filer_pb.SubscribeMetadataResponse{
  97. Directory: dirPath,
  98. EventNotification: eventNotification,
  99. TsNs: tsNs,
  100. }
  101. // println("sending", dirPath, entryName)
  102. if err := stream.Send(message); err != nil {
  103. glog.V(0).Infof("=> client %v: %+v", clientName, err)
  104. return err
  105. }
  106. return nil
  107. }
  108. }
  109. func (fs *FilerServer) addClient(clientType string, clientAddress string) (clientName string) {
  110. clientName = clientType + "@" + clientAddress
  111. glog.V(0).Infof("+ listener %v", clientName)
  112. return
  113. }
  114. func (fs *FilerServer) deleteClient(clientName string) {
  115. glog.V(0).Infof("- listener %v", clientName)
  116. }