filer_grpc_server_sub_meta.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package weed_server
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/stats"
  5. "strings"
  6. "time"
  7. "google.golang.org/protobuf/proto"
  8. "github.com/seaweedfs/seaweedfs/weed/filer"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. "github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
  13. )
  14. const (
  15. // MaxUnsyncedEvents send empty notification with timestamp when certain amount of events have been filtered
  16. MaxUnsyncedEvents = 1e3
  17. )
  18. func (fs *FilerServer) SubscribeMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer) error {
  19. peerAddress := findClientAddress(stream.Context(), 0)
  20. alreadyKnown, clientName := fs.addClient(req.ClientName, peerAddress, req.ClientId, req.ClientEpoch)
  21. if alreadyKnown {
  22. return fmt.Errorf("duplicated subscription detected for client %s id %d", clientName, req.ClientId)
  23. }
  24. defer fs.deleteClient(clientName, req.ClientId, req.ClientEpoch)
  25. lastReadTime := time.Unix(0, req.SinceNs)
  26. glog.V(0).Infof(" %v starts to subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  27. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName)
  28. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  29. var processedTsNs int64
  30. var readPersistedLogErr error
  31. var readInMemoryLogErr error
  32. var isDone bool
  33. for {
  34. glog.V(4).Infof("read on disk %v aggregated subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  35. processedTsNs, isDone, readPersistedLogErr = fs.filer.ReadPersistedLogBuffer(lastReadTime, req.UntilNs, eachLogEntryFn)
  36. if readPersistedLogErr != nil {
  37. return fmt.Errorf("reading from persisted logs: %v", readPersistedLogErr)
  38. }
  39. if isDone {
  40. return nil
  41. }
  42. if processedTsNs != 0 {
  43. lastReadTime = time.Unix(0, processedTsNs)
  44. }
  45. glog.V(4).Infof("read in memory %v aggregated subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  46. lastReadTime, isDone, readInMemoryLogErr = fs.filer.MetaAggregator.MetaLogBuffer.LoopProcessLogData("aggMeta:"+clientName, lastReadTime, req.UntilNs, func() bool {
  47. fs.filer.MetaAggregator.ListenersLock.Lock()
  48. fs.filer.MetaAggregator.ListenersCond.Wait()
  49. fs.filer.MetaAggregator.ListenersLock.Unlock()
  50. return true
  51. }, eachLogEntryFn)
  52. if readInMemoryLogErr != nil {
  53. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  54. continue
  55. }
  56. glog.Errorf("processed to %v: %v", lastReadTime, readInMemoryLogErr)
  57. if readInMemoryLogErr != log_buffer.ResumeError {
  58. break
  59. }
  60. }
  61. if isDone {
  62. return nil
  63. }
  64. time.Sleep(1127 * time.Millisecond)
  65. }
  66. return readInMemoryLogErr
  67. }
  68. func (fs *FilerServer) SubscribeLocalMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeLocalMetadataServer) error {
  69. peerAddress := findClientAddress(stream.Context(), 0)
  70. // use negative client id to differentiate from addClient()/deleteClient() used in SubscribeMetadata()
  71. req.ClientId = -req.ClientId
  72. alreadyKnown, clientName := fs.addClient(req.ClientName, peerAddress, req.ClientId, req.ClientEpoch)
  73. if alreadyKnown {
  74. return fmt.Errorf("duplicated local subscription detected for client %s clientId:%d", clientName, req.ClientId)
  75. }
  76. defer func() {
  77. glog.V(0).Infof(" - %v local subscribe %s clientId:%d", clientName, req.PathPrefix, req.ClientId)
  78. fs.deleteClient(clientName, req.ClientId, req.ClientEpoch)
  79. }()
  80. lastReadTime := time.Unix(0, req.SinceNs)
  81. glog.V(0).Infof(" + %v local subscribe %s from %+v clientId:%d", clientName, req.PathPrefix, lastReadTime, req.ClientId)
  82. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName)
  83. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  84. var processedTsNs int64
  85. var readPersistedLogErr error
  86. var readInMemoryLogErr error
  87. var isDone bool
  88. for {
  89. // println("reading from persisted logs ...")
  90. glog.V(0).Infof("read on disk %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  91. processedTsNs, isDone, readPersistedLogErr = fs.filer.ReadPersistedLogBuffer(lastReadTime, req.UntilNs, eachLogEntryFn)
  92. if readPersistedLogErr != nil {
  93. glog.V(0).Infof("read on disk %v local subscribe %s from %+v: %v", clientName, req.PathPrefix, lastReadTime, readPersistedLogErr)
  94. return fmt.Errorf("reading from persisted logs: %v", readPersistedLogErr)
  95. }
  96. if isDone {
  97. return nil
  98. }
  99. if processedTsNs != 0 {
  100. lastReadTime = time.Unix(0, processedTsNs)
  101. } else {
  102. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  103. time.Sleep(1127 * time.Millisecond)
  104. continue
  105. }
  106. }
  107. glog.V(0).Infof("read in memory %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  108. lastReadTime, isDone, readInMemoryLogErr = fs.filer.LocalMetaLogBuffer.LoopProcessLogData("localMeta:"+clientName, lastReadTime, req.UntilNs, func() bool {
  109. fs.listenersLock.Lock()
  110. fs.listenersCond.Wait()
  111. fs.listenersLock.Unlock()
  112. return true
  113. }, eachLogEntryFn)
  114. if readInMemoryLogErr != nil {
  115. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  116. continue
  117. }
  118. glog.Errorf("processed to %v: %v", lastReadTime, readInMemoryLogErr)
  119. if readInMemoryLogErr != log_buffer.ResumeError {
  120. break
  121. }
  122. }
  123. if isDone {
  124. return nil
  125. }
  126. }
  127. return readInMemoryLogErr
  128. }
  129. func eachLogEntryFn(eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error) func(logEntry *filer_pb.LogEntry) error {
  130. return func(logEntry *filer_pb.LogEntry) error {
  131. event := &filer_pb.SubscribeMetadataResponse{}
  132. if err := proto.Unmarshal(logEntry.Data, event); err != nil {
  133. glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  134. return fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  135. }
  136. if err := eachEventNotificationFn(event.Directory, event.EventNotification, event.TsNs); err != nil {
  137. return err
  138. }
  139. return nil
  140. }
  141. }
  142. 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 {
  143. filtered := 0
  144. return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  145. defer func() {
  146. if filtered > MaxUnsyncedEvents {
  147. if err := stream.Send(&filer_pb.SubscribeMetadataResponse{
  148. EventNotification: &filer_pb.EventNotification{},
  149. TsNs: tsNs,
  150. }); err == nil {
  151. filtered = 0
  152. }
  153. }
  154. }()
  155. filtered++
  156. foundSelf := false
  157. for _, sig := range eventNotification.Signatures {
  158. if sig == req.Signature && req.Signature != 0 {
  159. return nil
  160. }
  161. if sig == fs.filer.Signature {
  162. foundSelf = true
  163. }
  164. }
  165. if !foundSelf {
  166. eventNotification.Signatures = append(eventNotification.Signatures, fs.filer.Signature)
  167. }
  168. // get complete path to the file or directory
  169. var entryName string
  170. if eventNotification.OldEntry != nil {
  171. entryName = eventNotification.OldEntry.Name
  172. } else if eventNotification.NewEntry != nil {
  173. entryName = eventNotification.NewEntry.Name
  174. }
  175. fullpath := util.Join(dirPath, entryName)
  176. // skip on filer internal meta logs
  177. if strings.HasPrefix(fullpath, filer.SystemLogDir) {
  178. return nil
  179. }
  180. if hasPrefixIn(fullpath, req.PathPrefixes) {
  181. // good
  182. } else if matchByDirectory(dirPath, req.Directories) {
  183. // good
  184. } else {
  185. if !strings.HasPrefix(fullpath, req.PathPrefix) {
  186. if eventNotification.NewParentPath != "" {
  187. newFullPath := util.Join(eventNotification.NewParentPath, entryName)
  188. if !strings.HasPrefix(newFullPath, req.PathPrefix) {
  189. return nil
  190. }
  191. } else {
  192. return nil
  193. }
  194. }
  195. }
  196. // collect timestamps for path
  197. stats.FilerServerLastSendTsOfSubscribeGauge.WithLabelValues(fs.option.Host.String(), req.ClientName, req.PathPrefix).Set(float64(tsNs))
  198. message := &filer_pb.SubscribeMetadataResponse{
  199. Directory: dirPath,
  200. EventNotification: eventNotification,
  201. TsNs: tsNs,
  202. }
  203. // println("sending", dirPath, entryName)
  204. if err := stream.Send(message); err != nil {
  205. glog.V(0).Infof("=> client %v: %+v", clientName, err)
  206. return err
  207. }
  208. filtered = 0
  209. return nil
  210. }
  211. }
  212. func hasPrefixIn(text string, prefixes []string) bool {
  213. for _, p := range prefixes {
  214. if strings.HasPrefix(text, p) {
  215. return true
  216. }
  217. }
  218. return false
  219. }
  220. func matchByDirectory(dirPath string, directories []string) bool {
  221. for _, dir := range directories {
  222. if dirPath == dir {
  223. return true
  224. }
  225. }
  226. return false
  227. }
  228. func (fs *FilerServer) addClient(clientType string, clientAddress string, clientId int32, clientEpoch int32) (alreadyKnown bool, clientName string) {
  229. clientName = clientType + "@" + clientAddress
  230. glog.V(0).Infof("+ listener %v", clientName)
  231. if clientId != 0 {
  232. fs.knownListenersLock.Lock()
  233. defer fs.knownListenersLock.Unlock()
  234. epoch, found := fs.knownListeners[clientId]
  235. if !found || epoch < clientEpoch {
  236. fs.knownListeners[clientId] = clientEpoch
  237. } else {
  238. alreadyKnown = true
  239. }
  240. }
  241. return
  242. }
  243. func (fs *FilerServer) deleteClient(clientName string, clientId int32, clientEpoch int32) {
  244. glog.V(0).Infof("- listener %v", clientName)
  245. if clientId != 0 {
  246. fs.knownListenersLock.Lock()
  247. defer fs.knownListenersLock.Unlock()
  248. epoch, found := fs.knownListeners[clientId]
  249. if found && epoch <= clientEpoch {
  250. delete(fs.knownListeners, clientId)
  251. }
  252. }
  253. }