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.

157 lines
4.4 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
  1. package filer2
  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(oldEntry, newEntry *Entry, deleteChunks bool) {
  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. newParentPath := ""
  28. if newEntry != nil {
  29. newParentPath, _ = newEntry.FullPath.DirAndName()
  30. }
  31. eventNotification := &filer_pb.EventNotification{
  32. OldEntry: oldEntry.ToProtoEntry(),
  33. NewEntry: newEntry.ToProtoEntry(),
  34. DeleteChunks: deleteChunks,
  35. NewParentPath: newParentPath,
  36. }
  37. if notification.Queue != nil {
  38. glog.V(3).Infof("notifying entry update %v", fullpath)
  39. notification.Queue.SendMessage(fullpath, eventNotification)
  40. }
  41. f.logMetaEvent(fullpath, eventNotification)
  42. }
  43. func (f *Filer) logMetaEvent(fullpath string, eventNotification *filer_pb.EventNotification) {
  44. dir, _ := util.FullPath(fullpath).DirAndName()
  45. event := &filer_pb.SubscribeMetadataResponse{
  46. Directory: dir,
  47. EventNotification: eventNotification,
  48. TsNs: time.Now().UnixNano(),
  49. }
  50. data, err := proto.Marshal(event)
  51. if err != nil {
  52. glog.Errorf("failed to marshal filer_pb.SubscribeMetadataResponse %+v: %v", event, err)
  53. return
  54. }
  55. f.MetaLogBuffer.AddToBuffer([]byte(dir), data)
  56. }
  57. func (f *Filer) logFlushFunc(startTime, stopTime time.Time, buf []byte) {
  58. targetFile := fmt.Sprintf("%s/%04d-%02d-%02d/%02d-%02d.segment", SystemLogDir,
  59. startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
  60. // startTime.Second(), startTime.Nanosecond(),
  61. )
  62. if err := f.appendToFile(targetFile, buf); err != nil {
  63. glog.V(0).Infof("log write failed %s: %v", targetFile, err)
  64. }
  65. }
  66. func (f *Filer) ReadPersistedLogBuffer(startTime time.Time, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) error {
  67. startDate := fmt.Sprintf("%04d-%02d-%02d", startTime.Year(), startTime.Month(), startTime.Day())
  68. startHourMinute := fmt.Sprintf("%02d-%02d.segment", startTime.Hour(), startTime.Minute())
  69. sizeBuf := make([]byte, 4)
  70. startTsNs := startTime.UnixNano()
  71. dayEntries, listDayErr := f.ListDirectoryEntries(context.Background(), SystemLogDir, startDate, true, 366)
  72. if listDayErr != nil {
  73. return fmt.Errorf("fail to list log by day: %v", listDayErr)
  74. }
  75. for _, dayEntry := range dayEntries {
  76. // println("checking day", dayEntry.FullPath)
  77. hourMinuteEntries, listHourMinuteErr := f.ListDirectoryEntries(context.Background(), util.NewFullPath(SystemLogDir, dayEntry.Name()), "", false, 24*60)
  78. if listHourMinuteErr != nil {
  79. return fmt.Errorf("fail to list log %s by day: %v", dayEntry.Name(), listHourMinuteErr)
  80. }
  81. for _, hourMinuteEntry := range hourMinuteEntries {
  82. // println("checking hh-mm", hourMinuteEntry.FullPath)
  83. if dayEntry.Name() == startDate {
  84. if strings.Compare(hourMinuteEntry.Name(), startHourMinute) < 0 {
  85. continue
  86. }
  87. }
  88. // println("processing", hourMinuteEntry.FullPath)
  89. chunkedFileReader := NewChunkStreamReaderFromFiler(f.MasterClient, hourMinuteEntry.Chunks)
  90. if err := ReadEachLogEntry(chunkedFileReader, sizeBuf, startTsNs, eachLogEntryFn); err != nil {
  91. chunkedFileReader.Close()
  92. if err == io.EOF {
  93. break
  94. }
  95. return fmt.Errorf("reading %s: %v", hourMinuteEntry.FullPath, err)
  96. }
  97. chunkedFileReader.Close()
  98. }
  99. }
  100. return nil
  101. }
  102. func ReadEachLogEntry(r io.Reader, sizeBuf []byte, ns int64, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) error {
  103. for {
  104. n, err := r.Read(sizeBuf)
  105. if err != nil {
  106. return err
  107. }
  108. if n != 4 {
  109. return fmt.Errorf("size %d bytes, expected 4 bytes", n)
  110. }
  111. size := util.BytesToUint32(sizeBuf)
  112. // println("entry size", size)
  113. entryData := make([]byte, size)
  114. n, err = r.Read(entryData)
  115. if err != nil {
  116. return err
  117. }
  118. if n != int(size) {
  119. return fmt.Errorf("entry data %d bytes, expected %d bytes", n, size)
  120. }
  121. logEntry := &filer_pb.LogEntry{}
  122. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  123. return err
  124. }
  125. if logEntry.TsNs <= ns {
  126. return nil
  127. }
  128. // println("each log: ", logEntry.TsNs)
  129. if err := eachLogEntryFn(logEntry); err != nil {
  130. return err
  131. }
  132. }
  133. }