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.

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