filer_grpc_server_sub_meta.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. } else {
  37. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  38. time.Sleep(1127 * time.Millisecond)
  39. continue
  40. }
  41. }
  42. glog.V(4).Infof("read in memory %v aggregated subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  43. lastReadTime, readInMemoryLogErr = fs.filer.MetaAggregator.MetaLogBuffer.LoopProcessLogData("aggMeta:"+clientName, lastReadTime, func() bool {
  44. fs.filer.MetaAggregator.ListenersLock.Lock()
  45. fs.filer.MetaAggregator.ListenersCond.Wait()
  46. fs.filer.MetaAggregator.ListenersLock.Unlock()
  47. return true
  48. }, eachLogEntryFn)
  49. if readInMemoryLogErr != nil {
  50. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  51. continue
  52. }
  53. glog.Errorf("processed to %v: %v", lastReadTime, readInMemoryLogErr)
  54. if readInMemoryLogErr != log_buffer.ResumeError {
  55. break
  56. }
  57. }
  58. time.Sleep(1127 * time.Millisecond)
  59. }
  60. return readInMemoryLogErr
  61. }
  62. func (fs *FilerServer) SubscribeLocalMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeLocalMetadataServer) error {
  63. peerAddress := findClientAddress(stream.Context(), 0)
  64. clientName := fs.addClient(req.ClientName, peerAddress)
  65. defer fs.deleteClient(clientName)
  66. lastReadTime := time.Unix(0, req.SinceNs)
  67. glog.V(0).Infof(" %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  68. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName)
  69. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  70. var processedTsNs int64
  71. var readPersistedLogErr error
  72. var readInMemoryLogErr error
  73. for {
  74. // println("reading from persisted logs ...")
  75. glog.V(0).Infof("read on disk %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  76. processedTsNs, readPersistedLogErr = fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  77. if readPersistedLogErr != nil {
  78. return fmt.Errorf("reading from persisted logs: %v", readPersistedLogErr)
  79. }
  80. if processedTsNs != 0 {
  81. lastReadTime = time.Unix(0, processedTsNs)
  82. } else {
  83. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  84. time.Sleep(1127 * time.Millisecond)
  85. continue
  86. }
  87. }
  88. glog.V(0).Infof("read in memory %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  89. lastReadTime, readInMemoryLogErr = fs.filer.LocalMetaLogBuffer.LoopProcessLogData("localMeta:"+clientName, lastReadTime, func() bool {
  90. fs.listenersLock.Lock()
  91. fs.listenersCond.Wait()
  92. fs.listenersLock.Unlock()
  93. return true
  94. }, eachLogEntryFn)
  95. if readInMemoryLogErr != nil {
  96. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  97. continue
  98. }
  99. glog.Errorf("processed to %v: %v", lastReadTime, readInMemoryLogErr)
  100. time.Sleep(1127 * time.Millisecond)
  101. if readInMemoryLogErr != log_buffer.ResumeError {
  102. break
  103. }
  104. }
  105. }
  106. return readInMemoryLogErr
  107. }
  108. func eachLogEntryFn(eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error) func(logEntry *filer_pb.LogEntry) error {
  109. return func(logEntry *filer_pb.LogEntry) error {
  110. event := &filer_pb.SubscribeMetadataResponse{}
  111. if err := proto.Unmarshal(logEntry.Data, event); err != nil {
  112. glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  113. return fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  114. }
  115. if err := eachEventNotificationFn(event.Directory, event.EventNotification, event.TsNs); err != nil {
  116. return err
  117. }
  118. return nil
  119. }
  120. }
  121. 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 {
  122. filtered := 0
  123. return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  124. defer func() {
  125. if filtered > MaxUnsyncedEvents {
  126. if err := stream.Send(&filer_pb.SubscribeMetadataResponse{
  127. EventNotification: &filer_pb.EventNotification{},
  128. TsNs: tsNs,
  129. }); err == nil {
  130. filtered = 0
  131. }
  132. }
  133. }()
  134. filtered++
  135. foundSelf := false
  136. for _, sig := range eventNotification.Signatures {
  137. if sig == req.Signature && req.Signature != 0 {
  138. return nil
  139. }
  140. if sig == fs.filer.Signature {
  141. foundSelf = true
  142. }
  143. }
  144. if !foundSelf {
  145. eventNotification.Signatures = append(eventNotification.Signatures, fs.filer.Signature)
  146. }
  147. // get complete path to the file or directory
  148. var entryName string
  149. if eventNotification.OldEntry != nil {
  150. entryName = eventNotification.OldEntry.Name
  151. } else if eventNotification.NewEntry != nil {
  152. entryName = eventNotification.NewEntry.Name
  153. }
  154. fullpath := util.Join(dirPath, entryName)
  155. // skip on filer internal meta logs
  156. if strings.HasPrefix(fullpath, filer.SystemLogDir) {
  157. return nil
  158. }
  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. message := &filer_pb.SubscribeMetadataResponse{
  170. Directory: dirPath,
  171. EventNotification: eventNotification,
  172. TsNs: tsNs,
  173. }
  174. // println("sending", dirPath, entryName)
  175. if err := stream.Send(message); err != nil {
  176. glog.V(0).Infof("=> client %v: %+v", clientName, err)
  177. return err
  178. }
  179. filtered = 0
  180. return nil
  181. }
  182. }
  183. func (fs *FilerServer) addClient(clientType string, clientAddress string) (clientName string) {
  184. clientName = clientType + "@" + clientAddress
  185. glog.V(0).Infof("+ listener %v", clientName)
  186. return
  187. }
  188. func (fs *FilerServer) deleteClient(clientName string) {
  189. glog.V(0).Infof("- listener %v", clientName)
  190. }