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.

208 lines
6.3 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
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/util/log_buffer"
  5. "strings"
  6. "time"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func (fs *FilerServer) SubscribeMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer) error {
  14. peerAddress := findClientAddress(stream.Context(), 0)
  15. clientName := fs.addClient(req.ClientName, peerAddress)
  16. defer fs.deleteClient(clientName)
  17. lastReadTime := time.Unix(0, req.SinceNs)
  18. glog.V(0).Infof(" %v starts to subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  19. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName, req.Signature)
  20. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  21. var processedTsNs int64
  22. var err error
  23. for {
  24. glog.V(4).Infof("read on disk %v aggregated subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  25. processedTsNs, err = fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  26. if err != nil {
  27. return fmt.Errorf("reading from persisted logs: %v", err)
  28. }
  29. if processedTsNs != 0 {
  30. lastReadTime = time.Unix(0, processedTsNs)
  31. }
  32. glog.V(4).Infof("read in memory %v aggregated subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  33. lastReadTime, err = fs.filer.MetaAggregator.MetaLogBuffer.LoopProcessLogData("aggMeta:"+clientName, lastReadTime, func() bool {
  34. fs.filer.MetaAggregator.ListenersLock.Lock()
  35. fs.filer.MetaAggregator.ListenersCond.Wait()
  36. fs.filer.MetaAggregator.ListenersLock.Unlock()
  37. return true
  38. }, eachLogEntryFn)
  39. if err != nil {
  40. if err == log_buffer.ResumeFromDiskError {
  41. time.Sleep(5127 * time.Millisecond)
  42. continue
  43. }
  44. glog.Errorf("processed to %v: %v", lastReadTime, err)
  45. if err != log_buffer.ResumeError {
  46. break
  47. }
  48. }
  49. time.Sleep(5127 * time.Millisecond)
  50. }
  51. return err
  52. }
  53. func (fs *FilerServer) SubscribeLocalMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeLocalMetadataServer) error {
  54. peerAddress := findClientAddress(stream.Context(), 0)
  55. clientName := fs.addClient(req.ClientName, peerAddress)
  56. defer fs.deleteClient(clientName)
  57. lastReadTime := time.Unix(0, req.SinceNs)
  58. glog.V(0).Infof(" %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  59. eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName, req.Signature)
  60. eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
  61. var processedTsNs int64
  62. var err error
  63. for {
  64. // println("reading from persisted logs ...")
  65. glog.V(0).Infof("read on disk %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  66. processedTsNs, err = fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
  67. if err != nil {
  68. return fmt.Errorf("reading from persisted logs: %v", err)
  69. }
  70. if processedTsNs != 0 {
  71. lastReadTime = time.Unix(0, processedTsNs)
  72. }
  73. glog.V(0).Infof("read in memory %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
  74. lastReadTime, err = fs.filer.LocalMetaLogBuffer.LoopProcessLogData("localMeta:"+clientName, lastReadTime, func() bool {
  75. fs.listenersLock.Lock()
  76. fs.listenersCond.Wait()
  77. fs.listenersLock.Unlock()
  78. return true
  79. }, eachLogEntryFn)
  80. if err != nil {
  81. if err == log_buffer.ResumeFromDiskError {
  82. continue
  83. }
  84. glog.Errorf("processed to %v: %v", lastReadTime, err)
  85. time.Sleep(3127 * time.Millisecond)
  86. if err != log_buffer.ResumeError {
  87. break
  88. }
  89. }
  90. }
  91. return err
  92. }
  93. func eachLogEntryFn(eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error) func(logEntry *filer_pb.LogEntry) error {
  94. return func(logEntry *filer_pb.LogEntry) error {
  95. event := &filer_pb.SubscribeMetadataResponse{}
  96. if err := proto.Unmarshal(logEntry.Data, event); err != nil {
  97. glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  98. return fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  99. }
  100. if err := eachEventNotificationFn(event.Directory, event.EventNotification, event.TsNs); err != nil {
  101. return err
  102. }
  103. return nil
  104. }
  105. }
  106. func (fs *FilerServer) eachEventNotificationFn(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer, clientName string, clientSignature int32) func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  107. return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
  108. foundSelf := false
  109. for _, sig := range eventNotification.Signatures {
  110. if sig == clientSignature && clientSignature != 0 {
  111. return nil
  112. }
  113. if sig == fs.filer.Signature {
  114. foundSelf = true
  115. }
  116. }
  117. if !foundSelf {
  118. eventNotification.Signatures = append(eventNotification.Signatures, fs.filer.Signature)
  119. }
  120. // get complete path to the file or directory
  121. var entryName string
  122. if eventNotification.OldEntry != nil {
  123. entryName = eventNotification.OldEntry.Name
  124. } else if eventNotification.NewEntry != nil {
  125. entryName = eventNotification.NewEntry.Name
  126. }
  127. fullpath := util.Join(dirPath, entryName)
  128. // skip on filer internal meta logs
  129. if strings.HasPrefix(fullpath, filer.SystemLogDir) {
  130. return nil
  131. }
  132. if !strings.HasPrefix(fullpath, req.PathPrefix) {
  133. if eventNotification.NewParentPath != "" {
  134. newFullPath := util.Join(eventNotification.NewParentPath, entryName)
  135. if !strings.HasPrefix(newFullPath, req.PathPrefix) {
  136. return nil
  137. }
  138. } else {
  139. return nil
  140. }
  141. }
  142. message := &filer_pb.SubscribeMetadataResponse{
  143. Directory: dirPath,
  144. EventNotification: eventNotification,
  145. TsNs: tsNs,
  146. }
  147. // println("sending", dirPath, entryName)
  148. if err := stream.Send(message); err != nil {
  149. glog.V(0).Infof("=> client %v: %+v", clientName, err)
  150. return err
  151. }
  152. return nil
  153. }
  154. }
  155. func (fs *FilerServer) addClient(clientType string, clientAddress string) (clientName string) {
  156. clientName = clientType + "@" + clientAddress
  157. glog.V(0).Infof("+ listener %v", clientName)
  158. return
  159. }
  160. func (fs *FilerServer) deleteClient(clientName string) {
  161. glog.V(0).Infof("- listener %v", clientName)
  162. }