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.

157 lines
4.9 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
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/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, 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. 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, clientSignature int32) func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  77. return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  78. for _, sig := range eventNotification.Signatures {
  79. if sig == clientSignature && clientSignature != 0 {
  80. return nil
  81. }
  82. }
  83. // get complete path to the file or directory
  84. var entryName string
  85. if eventNotification.OldEntry != nil {
  86. entryName = eventNotification.OldEntry.Name
  87. } else if eventNotification.NewEntry != nil {
  88. entryName = eventNotification.NewEntry.Name
  89. }
  90. fullpath := util.Join(dirPath, entryName)
  91. // skip on filer internal meta logs
  92. if strings.HasPrefix(fullpath, filer2.SystemLogDir) {
  93. return nil
  94. }
  95. if !strings.HasPrefix(fullpath, req.PathPrefix) {
  96. return nil
  97. }
  98. message := &filer_pb.SubscribeMetadataResponse{
  99. Directory: dirPath,
  100. EventNotification: eventNotification,
  101. TsNs: tsNs,
  102. }
  103. // println("sending", dirPath, entryName)
  104. if err := stream.Send(message); err != nil {
  105. glog.V(0).Infof("=> client %v: %+v", clientName, err)
  106. return err
  107. }
  108. return nil
  109. }
  110. }
  111. func (fs *FilerServer) addClient(clientType string, clientAddress string) (clientName string) {
  112. clientName = clientType + "@" + clientAddress
  113. glog.V(0).Infof("+ listener %v", clientName)
  114. return
  115. }
  116. func (fs *FilerServer) deleteClient(clientName string) {
  117. glog.V(0).Infof("- listener %v", clientName)
  118. }