filer_grpc_server_sub_meta.go 10 KB

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