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.

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