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.

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