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.

146 lines
3.3 KiB

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