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.

182 lines
5.2 KiB

6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. notification.Queue.SendMessage(fullpath, eventNotification)
  51. }
  52. f.logMetaEvent(ctx, fullpath, eventNotification)
  53. }
  54. func (f *Filer) logMetaEvent(ctx context.Context, fullpath string, eventNotification *filer_pb.EventNotification) {
  55. dir, _ := util.FullPath(fullpath).DirAndName()
  56. event := &filer_pb.SubscribeMetadataResponse{
  57. Directory: dir,
  58. EventNotification: eventNotification,
  59. TsNs: time.Now().UnixNano(),
  60. }
  61. data, err := proto.Marshal(event)
  62. if err != nil {
  63. glog.Errorf("failed to marshal filer_pb.SubscribeMetadataResponse %+v: %v", event, err)
  64. return
  65. }
  66. f.LocalMetaLogBuffer.AddToBuffer([]byte(dir), data, event.TsNs)
  67. }
  68. func (f *Filer) logFlushFunc(startTime, stopTime time.Time, buf []byte) {
  69. if len(buf) == 0 {
  70. return
  71. }
  72. startTime, stopTime = startTime.UTC(), stopTime.UTC()
  73. targetFile := fmt.Sprintf("%s/%04d-%02d-%02d/%02d-%02d.segment", SystemLogDir,
  74. startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
  75. // startTime.Second(), startTime.Nanosecond(),
  76. )
  77. for {
  78. if err := f.appendToFile(targetFile, buf); err != nil {
  79. glog.V(1).Infof("log write failed %s: %v", targetFile, err)
  80. time.Sleep(737 * time.Millisecond)
  81. } else {
  82. break
  83. }
  84. }
  85. }
  86. func (f *Filer) ReadPersistedLogBuffer(startTime time.Time, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) (lastTsNs int64, err error) {
  87. startTime = startTime.UTC()
  88. startDate := fmt.Sprintf("%04d-%02d-%02d", startTime.Year(), startTime.Month(), startTime.Day())
  89. startHourMinute := fmt.Sprintf("%02d-%02d.segment", startTime.Hour(), startTime.Minute())
  90. sizeBuf := make([]byte, 4)
  91. startTsNs := startTime.UnixNano()
  92. dayEntries, listDayErr := f.ListDirectoryEntries(context.Background(), SystemLogDir, startDate, true, 366, "", "")
  93. if listDayErr != nil {
  94. return lastTsNs, fmt.Errorf("fail to list log by day: %v", listDayErr)
  95. }
  96. for _, dayEntry := range dayEntries {
  97. // println("checking day", dayEntry.FullPath)
  98. hourMinuteEntries, listHourMinuteErr := f.ListDirectoryEntries(context.Background(), util.NewFullPath(SystemLogDir, dayEntry.Name()), "", false, 24*60, "", "")
  99. if listHourMinuteErr != nil {
  100. return lastTsNs, fmt.Errorf("fail to list log %s by day: %v", dayEntry.Name(), listHourMinuteErr)
  101. }
  102. for _, hourMinuteEntry := range hourMinuteEntries {
  103. // println("checking hh-mm", hourMinuteEntry.FullPath)
  104. if dayEntry.Name() == startDate {
  105. if strings.Compare(hourMinuteEntry.Name(), startHourMinute) < 0 {
  106. continue
  107. }
  108. }
  109. // println("processing", hourMinuteEntry.FullPath)
  110. chunkedFileReader := NewChunkStreamReaderFromFiler(f.MasterClient, hourMinuteEntry.Chunks)
  111. if lastTsNs, err = ReadEachLogEntry(chunkedFileReader, sizeBuf, startTsNs, eachLogEntryFn); err != nil {
  112. chunkedFileReader.Close()
  113. if err == io.EOF {
  114. continue
  115. }
  116. return lastTsNs, fmt.Errorf("reading %s: %v", hourMinuteEntry.FullPath, err)
  117. }
  118. chunkedFileReader.Close()
  119. }
  120. }
  121. return lastTsNs, nil
  122. }
  123. func ReadEachLogEntry(r io.Reader, sizeBuf []byte, ns int64, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) (lastTsNs int64, err error) {
  124. for {
  125. n, err := r.Read(sizeBuf)
  126. if err != nil {
  127. return lastTsNs, err
  128. }
  129. if n != 4 {
  130. return lastTsNs, fmt.Errorf("size %d bytes, expected 4 bytes", n)
  131. }
  132. size := util.BytesToUint32(sizeBuf)
  133. // println("entry size", size)
  134. entryData := make([]byte, size)
  135. n, err = r.Read(entryData)
  136. if err != nil {
  137. return lastTsNs, err
  138. }
  139. if n != int(size) {
  140. return lastTsNs, fmt.Errorf("entry data %d bytes, expected %d bytes", n, size)
  141. }
  142. logEntry := &filer_pb.LogEntry{}
  143. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  144. return lastTsNs, err
  145. }
  146. if logEntry.TsNs <= ns {
  147. continue
  148. }
  149. // println("each log: ", logEntry.TsNs)
  150. if err := eachLogEntryFn(logEntry); err != nil {
  151. return lastTsNs, err
  152. } else {
  153. lastTsNs = logEntry.TsNs
  154. }
  155. }
  156. }