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.

188 lines
5.7 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. "github.com/chrislusf/seaweedfs/weed/util/log_buffer"
  5. "strings"
  6. "time"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func (fs *FilerServer) SubscribeMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer) error {
  14. peerAddress := findClientAddress(stream.Context(), 0)
  15. clientName := fs.addClient(req.ClientName, peerAddress)
  16. defer fs.deleteClient(clientName)
  17. lastReadTime := time.Unix(0, req.SinceNs)
  18. glog.V(0).Infof(" %v starts to subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  19. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName, req.Signature)
  20. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  21. processedTsNs, err := fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  22. if err != nil {
  23. return fmt.Errorf("reading from persisted logs: %v", err)
  24. }
  25. if processedTsNs != 0 {
  26. lastReadTime = time.Unix(0, processedTsNs)
  27. }
  28. for {
  29. lastReadTime, err = fs.filer.MetaAggregator.MetaLogBuffer.LoopProcessLogData(lastReadTime, func() bool {
  30. fs.filer.MetaAggregator.ListenersLock.Lock()
  31. fs.filer.MetaAggregator.ListenersCond.Wait()
  32. fs.filer.MetaAggregator.ListenersLock.Unlock()
  33. return true
  34. }, eachLogEntryFn)
  35. if err != nil {
  36. glog.Errorf("processed to %v: %v", lastReadTime, err)
  37. time.Sleep(3127 * time.Millisecond)
  38. if err != log_buffer.ResumeError {
  39. break
  40. }
  41. }
  42. }
  43. return err
  44. }
  45. func (fs *FilerServer) SubscribeLocalMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeLocalMetadataServer) error {
  46. peerAddress := findClientAddress(stream.Context(), 0)
  47. clientName := fs.addClient(req.ClientName, peerAddress)
  48. defer fs.deleteClient(clientName)
  49. lastReadTime := time.Unix(0, req.SinceNs)
  50. glog.V(0).Infof(" %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  51. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName, req.Signature)
  52. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  53. // println("reading from persisted logs ...")
  54. processedTsNs, err := fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  55. if err != nil {
  56. return fmt.Errorf("reading from persisted logs: %v", err)
  57. }
  58. if processedTsNs != 0 {
  59. lastReadTime = time.Unix(0, processedTsNs)
  60. }
  61. glog.V(0).Infof("after local log reads, %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  62. // println("reading from in memory logs ...")
  63. for {
  64. lastReadTime, err = fs.filer.LocalMetaLogBuffer.LoopProcessLogData(lastReadTime, func() bool {
  65. fs.listenersLock.Lock()
  66. fs.listenersCond.Wait()
  67. fs.listenersLock.Unlock()
  68. return true
  69. }, eachLogEntryFn)
  70. if err != nil {
  71. glog.Errorf("processed to %v: %v", lastReadTime, err)
  72. time.Sleep(3127 * time.Millisecond)
  73. if err != log_buffer.ResumeError {
  74. break
  75. }
  76. }
  77. }
  78. return err
  79. }
  80. func eachLogEntryFn(eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error) func(logEntry *filer_pb.LogEntry) error {
  81. return func(logEntry *filer_pb.LogEntry) error {
  82. event := &filer_pb.SubscribeMetadataResponse{}
  83. if err := proto.Unmarshal(logEntry.Data, event); err != nil {
  84. glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  85. return fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  86. }
  87. if err := eachEventNotificationFn(event.Directory, event.EventNotification, event.TsNs); err != nil {
  88. return err
  89. }
  90. return nil
  91. }
  92. }
  93. func (fs *FilerServer) eachEventNotificationFn(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer, clientName string, clientSignature int32) func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  94. return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  95. foundSelf := false
  96. for _, sig := range eventNotification.Signatures {
  97. if sig == clientSignature && clientSignature != 0 {
  98. return nil
  99. }
  100. if sig == fs.filer.Signature {
  101. foundSelf = true
  102. }
  103. }
  104. if !foundSelf {
  105. eventNotification.Signatures = append(eventNotification.Signatures, fs.filer.Signature)
  106. }
  107. // get complete path to the file or directory
  108. var entryName string
  109. if eventNotification.OldEntry != nil {
  110. entryName = eventNotification.OldEntry.Name
  111. } else if eventNotification.NewEntry != nil {
  112. entryName = eventNotification.NewEntry.Name
  113. }
  114. fullpath := util.Join(dirPath, entryName)
  115. // skip on filer internal meta logs
  116. if strings.HasPrefix(fullpath, filer.SystemLogDir) {
  117. return nil
  118. }
  119. if !strings.HasPrefix(fullpath, req.PathPrefix) {
  120. if eventNotification.NewParentPath != "" {
  121. newFullPath := util.Join(eventNotification.NewParentPath, entryName)
  122. if !strings.HasPrefix(newFullPath, req.PathPrefix) {
  123. return nil
  124. }
  125. } else {
  126. return nil
  127. }
  128. }
  129. message := &filer_pb.SubscribeMetadataResponse{
  130. Directory: dirPath,
  131. EventNotification: eventNotification,
  132. TsNs: tsNs,
  133. }
  134. // println("sending", dirPath, entryName)
  135. if err := stream.Send(message); err != nil {
  136. glog.V(0).Infof("=> client %v: %+v", clientName, err)
  137. return err
  138. }
  139. return nil
  140. }
  141. }
  142. func (fs *FilerServer) addClient(clientType string, clientAddress string) (clientName string) {
  143. clientName = clientType + "@" + clientAddress
  144. glog.V(0).Infof("+ listener %v", clientName)
  145. return
  146. }
  147. func (fs *FilerServer) deleteClient(clientName string) {
  148. glog.V(0).Infof("- listener %v", clientName)
  149. }