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.

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