filer_notify.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/util/log"
  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. log.Tracef("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. log.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. log.Debugf("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. return lastTsNs, nil
  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. }