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.

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