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.

202 lines
5.9 KiB

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