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.

251 lines
7.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 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. "github.com/chrislusf/seaweedfs/weed/util/log_buffer"
  12. )
  13. const (
  14. // MaxUnsyncedEvents send empty notification with timestamp when certain amount of events have been filtered
  15. MaxUnsyncedEvents = 1e3
  16. )
  17. func (fs *FilerServer) SubscribeMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer) error {
  18. peerAddress := findClientAddress(stream.Context(), 0)
  19. clientName := fs.addClient(req.ClientName, peerAddress)
  20. defer fs.deleteClient(clientName)
  21. lastReadTime := time.Unix(0, req.SinceNs)
  22. glog.V(0).Infof(" %v starts to subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  23. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName)
  24. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  25. var processedTsNs int64
  26. var readPersistedLogErr error
  27. var readInMemoryLogErr error
  28. for {
  29. glog.V(4).Infof("read on disk %v aggregated subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  30. processedTsNs, readPersistedLogErr = fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  31. if readPersistedLogErr != nil {
  32. return fmt.Errorf("reading from persisted logs: %v", readPersistedLogErr)
  33. }
  34. if processedTsNs != 0 {
  35. lastReadTime = time.Unix(0, processedTsNs)
  36. } else {
  37. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  38. time.Sleep(1127 * time.Millisecond)
  39. continue
  40. }
  41. }
  42. glog.V(4).Infof("read in memory %v aggregated subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  43. lastReadTime, readInMemoryLogErr = fs.filer.MetaAggregator.MetaLogBuffer.LoopProcessLogData("aggMeta:"+clientName, lastReadTime, func() bool {
  44. fs.filer.MetaAggregator.ListenersLock.Lock()
  45. fs.filer.MetaAggregator.ListenersCond.Wait()
  46. fs.filer.MetaAggregator.ListenersLock.Unlock()
  47. return true
  48. }, eachLogEntryFn)
  49. if readInMemoryLogErr != nil {
  50. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  51. continue
  52. }
  53. glog.Errorf("processed to %v: %v", lastReadTime, readInMemoryLogErr)
  54. if readInMemoryLogErr != log_buffer.ResumeError {
  55. break
  56. }
  57. }
  58. time.Sleep(1127 * time.Millisecond)
  59. }
  60. return readInMemoryLogErr
  61. }
  62. func (fs *FilerServer) SubscribeLocalMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeLocalMetadataServer) error {
  63. peerAddress := findClientAddress(stream.Context(), 0)
  64. clientName := fs.addClient(req.ClientName, peerAddress)
  65. defer fs.deleteClient(clientName)
  66. lastReadTime := time.Unix(0, req.SinceNs)
  67. glog.V(0).Infof(" %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  68. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName)
  69. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  70. var processedTsNs int64
  71. var readPersistedLogErr error
  72. var readInMemoryLogErr error
  73. for {
  74. // println("reading from persisted logs ...")
  75. glog.V(0).Infof("read on disk %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  76. processedTsNs, readPersistedLogErr = fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  77. if readPersistedLogErr != nil {
  78. return fmt.Errorf("reading from persisted logs: %v", readPersistedLogErr)
  79. }
  80. if processedTsNs != 0 {
  81. lastReadTime = time.Unix(0, processedTsNs)
  82. } else {
  83. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  84. time.Sleep(1127 * time.Millisecond)
  85. continue
  86. }
  87. }
  88. glog.V(0).Infof("read in memory %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  89. lastReadTime, readInMemoryLogErr = fs.filer.LocalMetaLogBuffer.LoopProcessLogData("localMeta:"+clientName, lastReadTime, func() bool {
  90. fs.listenersLock.Lock()
  91. fs.listenersCond.Wait()
  92. fs.listenersLock.Unlock()
  93. return true
  94. }, eachLogEntryFn)
  95. if readInMemoryLogErr != nil {
  96. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  97. continue
  98. }
  99. glog.Errorf("processed to %v: %v", lastReadTime, readInMemoryLogErr)
  100. time.Sleep(1127 * time.Millisecond)
  101. if readInMemoryLogErr != log_buffer.ResumeError {
  102. break
  103. }
  104. }
  105. }
  106. return readInMemoryLogErr
  107. }
  108. func eachLogEntryFn(eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error) func(logEntry *filer_pb.LogEntry) error {
  109. return func(logEntry *filer_pb.LogEntry) error {
  110. event := &filer_pb.SubscribeMetadataResponse{}
  111. if err := proto.Unmarshal(logEntry.Data, event); err != nil {
  112. glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  113. return fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  114. }
  115. if err := eachEventNotificationFn(event.Directory, event.EventNotification, event.TsNs); err != nil {
  116. return err
  117. }
  118. return nil
  119. }
  120. }
  121. func (fs *FilerServer) eachEventNotificationFn(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer, clientName string) func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  122. filtered := 0
  123. return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  124. defer func() {
  125. if filtered > MaxUnsyncedEvents {
  126. if err := stream.Send(&filer_pb.SubscribeMetadataResponse{
  127. EventNotification: &filer_pb.EventNotification{},
  128. TsNs: tsNs,
  129. }); err == nil {
  130. filtered = 0
  131. }
  132. }
  133. }()
  134. filtered++
  135. foundSelf := false
  136. for _, sig := range eventNotification.Signatures {
  137. if sig == req.Signature && req.Signature != 0 {
  138. return nil
  139. }
  140. if sig == fs.filer.Signature {
  141. foundSelf = true
  142. }
  143. }
  144. if !foundSelf {
  145. eventNotification.Signatures = append(eventNotification.Signatures, fs.filer.Signature)
  146. }
  147. // get complete path to the file or directory
  148. var entryName string
  149. if eventNotification.OldEntry != nil {
  150. entryName = eventNotification.OldEntry.Name
  151. } else if eventNotification.NewEntry != nil {
  152. entryName = eventNotification.NewEntry.Name
  153. }
  154. fullpath := util.Join(dirPath, entryName)
  155. // skip on filer internal meta logs
  156. if strings.HasPrefix(fullpath, filer.SystemLogDir) {
  157. return nil
  158. }
  159. if hasPrefixIn(fullpath, req.PathPrefixes) {
  160. // good
  161. } else {
  162. if !strings.HasPrefix(fullpath, req.PathPrefix) {
  163. if eventNotification.NewParentPath != "" {
  164. newFullPath := util.Join(eventNotification.NewParentPath, entryName)
  165. if !strings.HasPrefix(newFullPath, req.PathPrefix) {
  166. return nil
  167. }
  168. } else {
  169. return nil
  170. }
  171. }
  172. }
  173. message := &filer_pb.SubscribeMetadataResponse{
  174. Directory: dirPath,
  175. EventNotification: eventNotification,
  176. TsNs: tsNs,
  177. }
  178. // println("sending", dirPath, entryName)
  179. if err := stream.Send(message); err != nil {
  180. glog.V(0).Infof("=> client %v: %+v", clientName, err)
  181. return err
  182. }
  183. filtered = 0
  184. return nil
  185. }
  186. }
  187. func hasPrefixIn(text string, prefixes []string) bool {
  188. for _, p := range prefixes {
  189. if strings.HasPrefix(text, p) {
  190. return true
  191. }
  192. }
  193. return false
  194. }
  195. func (fs *FilerServer) addClient(clientType string, clientAddress string) (clientName string) {
  196. clientName = clientType + "@" + clientAddress
  197. glog.V(0).Infof("+ listener %v", clientName)
  198. return
  199. }
  200. func (fs *FilerServer) deleteClient(clientName string) {
  201. glog.V(0).Infof("- listener %v", clientName)
  202. }