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.

149 lines
3.4 KiB

6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 years ago
  1. package filer2
  2. import (
  3. "fmt"
  4. "strings"
  5. "sync"
  6. "time"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/notification"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func (f *Filer) NotifyUpdateEvent(oldEntry, newEntry *Entry, deleteChunks bool) {
  14. var fullpath string
  15. if oldEntry != nil {
  16. fullpath = string(oldEntry.FullPath)
  17. } else if newEntry != nil {
  18. fullpath = string(newEntry.FullPath)
  19. } else {
  20. return
  21. }
  22. // println("fullpath:", fullpath)
  23. if strings.HasPrefix(fullpath, "/.meta") {
  24. return
  25. }
  26. newParentPath := ""
  27. if newEntry != nil {
  28. newParentPath, _ = newEntry.FullPath.DirAndName()
  29. }
  30. eventNotification := &filer_pb.EventNotification{
  31. OldEntry: oldEntry.ToProtoEntry(),
  32. NewEntry: newEntry.ToProtoEntry(),
  33. DeleteChunks: deleteChunks,
  34. NewParentPath: newParentPath,
  35. }
  36. if notification.Queue != nil {
  37. glog.V(3).Infof("notifying entry update %v", fullpath)
  38. notification.Queue.SendMessage(fullpath, eventNotification)
  39. }
  40. f.logMetaEvent(time.Now(), fullpath, eventNotification)
  41. }
  42. func (f *Filer) logMetaEvent(ts time.Time, fullpath string, eventNotification *filer_pb.EventNotification) {
  43. dir, _ := util.FullPath(fullpath).DirAndName()
  44. event := &filer_pb.FullEventNotification{
  45. Directory: dir,
  46. EventNotification: eventNotification,
  47. }
  48. data, err := proto.Marshal(event)
  49. if err != nil {
  50. glog.Errorf("failed to marshal filer_pb.FullEventNotification %+v: %v", event, err)
  51. return
  52. }
  53. f.metaLogBuffer.AddToBuffer(ts, []byte(dir), data)
  54. }
  55. func (f *Filer) logFlushFunc(startTime, stopTime time.Time, buf []byte) {
  56. targetFile := fmt.Sprintf("/.meta/log/%04d/%02d/%02d/%02d/%02d/%02d-%02d.log",
  57. startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
  58. startTime.Second(), stopTime.Second())
  59. if err := f.appendToFile(targetFile, buf); err != nil {
  60. glog.V(0).Infof("log write failed %s: %v", targetFile, err)
  61. }
  62. }
  63. type LogBuffer struct {
  64. buf []byte
  65. pos int
  66. startTime time.Time
  67. stopTime time.Time
  68. sizeBuf []byte
  69. flushInterval time.Duration
  70. flushFn func(startTime, stopTime time.Time, buf []byte)
  71. sync.Mutex
  72. }
  73. func NewLogBuffer(flushInterval time.Duration, flushFn func(startTime, stopTime time.Time, buf []byte)) *LogBuffer {
  74. lb := &LogBuffer{
  75. buf: make([]byte, 4*0124*1024),
  76. sizeBuf: make([]byte, 4),
  77. flushInterval: 2 * time.Second, // flushInterval,
  78. flushFn: flushFn,
  79. }
  80. go lb.loopFlush()
  81. return lb
  82. }
  83. func (m *LogBuffer) loopFlush() {
  84. for {
  85. m.Lock()
  86. m.flush()
  87. m.Unlock()
  88. time.Sleep(m.flushInterval)
  89. }
  90. }
  91. func (m *LogBuffer) flush() {
  92. if m.flushFn != nil && m.pos > 0 {
  93. m.flushFn(m.startTime, m.stopTime, m.buf[:m.pos])
  94. m.pos = 0
  95. }
  96. }
  97. func (m *LogBuffer) AddToBuffer(ts time.Time, key, data []byte) {
  98. logEntry := &filer_pb.LogEntry{
  99. TsNs: ts.UnixNano(),
  100. PartitionKeyHash: util.HashToInt32(key),
  101. Data: data,
  102. }
  103. logEntryData, _ := proto.Marshal(logEntry)
  104. size := len(logEntryData)
  105. m.Lock()
  106. defer m.Unlock()
  107. if m.pos == 0 {
  108. m.startTime = ts
  109. }
  110. if m.startTime.Add(m.flushInterval).Before(ts) || len(m.buf)-m.pos < size+4 {
  111. m.flush()
  112. m.startTime = ts
  113. }
  114. m.stopTime = ts
  115. util.Uint32toBytes(m.sizeBuf, uint32(size))
  116. copy(m.buf[m.pos:m.pos+4], m.sizeBuf)
  117. copy(m.buf[m.pos+4:m.pos+4+size], logEntryData)
  118. m.pos += size + 4
  119. }