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.

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