filer_notify.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "time"
  8. "github.com/golang/protobuf/proto"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/notification"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. func (f *Filer) NotifyUpdateEvent(ctx context.Context, oldEntry, newEntry *Entry, deleteChunks, isFromOtherCluster bool, signatures []int32) {
  15. var fullpath string
  16. if oldEntry != nil {
  17. fullpath = string(oldEntry.FullPath)
  18. } else if newEntry != nil {
  19. fullpath = string(newEntry.FullPath)
  20. } else {
  21. return
  22. }
  23. // println("fullpath:", fullpath)
  24. if strings.HasPrefix(fullpath, SystemLogDir) {
  25. return
  26. }
  27. foundSelf := false
  28. for _, sig := range signatures {
  29. if sig == f.Signature {
  30. foundSelf = true
  31. }
  32. }
  33. if !foundSelf {
  34. signatures = append(signatures, f.Signature)
  35. }
  36. newParentPath := ""
  37. if newEntry != nil {
  38. newParentPath, _ = newEntry.FullPath.DirAndName()
  39. }
  40. eventNotification := &filer_pb.EventNotification{
  41. OldEntry: oldEntry.ToProtoEntry(),
  42. NewEntry: newEntry.ToProtoEntry(),
  43. DeleteChunks: deleteChunks,
  44. NewParentPath: newParentPath,
  45. IsFromOtherCluster: isFromOtherCluster,
  46. Signatures: signatures,
  47. }
  48. if notification.Queue != nil {
  49. glog.V(3).Infof("notifying entry update %v", fullpath)
  50. if err := notification.Queue.SendMessage(fullpath, eventNotification); err != nil {
  51. // throw message
  52. glog.Error(err)
  53. }
  54. }
  55. f.logMetaEvent(ctx, fullpath, eventNotification)
  56. }
  57. func (f *Filer) logMetaEvent(ctx context.Context, fullpath string, eventNotification *filer_pb.EventNotification) {
  58. dir, _ := util.FullPath(fullpath).DirAndName()
  59. event := &filer_pb.SubscribeMetadataResponse{
  60. Directory: dir,
  61. EventNotification: eventNotification,
  62. TsNs: time.Now().UnixNano(),
  63. }
  64. data, err := proto.Marshal(event)
  65. if err != nil {
  66. glog.Errorf("failed to marshal filer_pb.SubscribeMetadataResponse %+v: %v", event, err)
  67. return
  68. }
  69. f.LocalMetaLogBuffer.AddToBuffer([]byte(dir), data, event.TsNs)
  70. }
  71. func (f *Filer) logFlushFunc(startTime, stopTime time.Time, buf []byte) {
  72. if len(buf) == 0 {
  73. return
  74. }
  75. startTime, stopTime = startTime.UTC(), stopTime.UTC()
  76. targetFile := fmt.Sprintf("%s/%04d-%02d-%02d/%02d-%02d.segment", SystemLogDir,
  77. startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
  78. // startTime.Second(), startTime.Nanosecond(),
  79. )
  80. for {
  81. if err := f.appendToFile(targetFile, buf); err != nil {
  82. glog.V(1).Infof("log write failed %s: %v", targetFile, err)
  83. time.Sleep(737 * time.Millisecond)
  84. } else {
  85. break
  86. }
  87. }
  88. }
  89. func (f *Filer) ReadPersistedLogBuffer(startTime time.Time, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) (lastTsNs int64, err error) {
  90. startTime = startTime.UTC()
  91. startDate := fmt.Sprintf("%04d-%02d-%02d", startTime.Year(), startTime.Month(), startTime.Day())
  92. startHourMinute := fmt.Sprintf("%02d-%02d.segment", startTime.Hour(), startTime.Minute())
  93. sizeBuf := make([]byte, 4)
  94. startTsNs := startTime.UnixNano()
  95. dayEntries, _, listDayErr := f.ListDirectoryEntries(context.Background(), SystemLogDir, startDate, true, 366, "", "", "")
  96. if listDayErr != nil {
  97. return lastTsNs, fmt.Errorf("fail to list log by day: %v", listDayErr)
  98. }
  99. for _, dayEntry := range dayEntries {
  100. // println("checking day", dayEntry.FullPath)
  101. hourMinuteEntries, _, listHourMinuteErr := f.ListDirectoryEntries(context.Background(), util.NewFullPath(SystemLogDir, dayEntry.Name()), "", false, 24*60, "", "", "")
  102. if listHourMinuteErr != nil {
  103. return lastTsNs, fmt.Errorf("fail to list log %s by day: %v", dayEntry.Name(), listHourMinuteErr)
  104. }
  105. for _, hourMinuteEntry := range hourMinuteEntries {
  106. // println("checking hh-mm", hourMinuteEntry.FullPath)
  107. if dayEntry.Name() == startDate {
  108. if strings.Compare(hourMinuteEntry.Name(), startHourMinute) < 0 {
  109. continue
  110. }
  111. }
  112. // println("processing", hourMinuteEntry.FullPath)
  113. chunkedFileReader := NewChunkStreamReaderFromFiler(f.MasterClient, hourMinuteEntry.Chunks)
  114. if lastTsNs, err = ReadEachLogEntry(chunkedFileReader, sizeBuf, startTsNs, eachLogEntryFn); err != nil {
  115. chunkedFileReader.Close()
  116. if err == io.EOF {
  117. continue
  118. }
  119. return lastTsNs, fmt.Errorf("reading %s: %v", hourMinuteEntry.FullPath, err)
  120. }
  121. chunkedFileReader.Close()
  122. }
  123. }
  124. return lastTsNs, nil
  125. }
  126. func ReadEachLogEntry(r io.Reader, sizeBuf []byte, ns int64, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) (lastTsNs int64, err error) {
  127. for {
  128. n, err := r.Read(sizeBuf)
  129. if err != nil {
  130. return lastTsNs, err
  131. }
  132. if n != 4 {
  133. return lastTsNs, fmt.Errorf("size %d bytes, expected 4 bytes", n)
  134. }
  135. size := util.BytesToUint32(sizeBuf)
  136. // println("entry size", size)
  137. entryData := make([]byte, size)
  138. n, err = r.Read(entryData)
  139. if err != nil {
  140. return lastTsNs, err
  141. }
  142. if n != int(size) {
  143. return lastTsNs, fmt.Errorf("entry data %d bytes, expected %d bytes", n, size)
  144. }
  145. logEntry := &filer_pb.LogEntry{}
  146. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  147. return lastTsNs, err
  148. }
  149. if logEntry.TsNs <= ns {
  150. continue
  151. }
  152. // println("each log: ", logEntry.TsNs)
  153. if err := eachLogEntryFn(logEntry); err != nil {
  154. return lastTsNs, err
  155. } else {
  156. lastTsNs = logEntry.TsNs
  157. }
  158. }
  159. }