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.

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