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.

257 lines
6.0 KiB

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
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 log_buffer
  2. import (
  3. "bytes"
  4. "sync"
  5. "time"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. const BufferSize = 4 * 1024 * 1024
  12. const PreviousBufferCount = 3
  13. type dataToFlush struct {
  14. startTime time.Time
  15. stopTime time.Time
  16. data *bytes.Buffer
  17. }
  18. type LogBuffer struct {
  19. prevBuffers *SealedBuffers
  20. buf []byte
  21. idx []int
  22. pos int
  23. startTime time.Time
  24. stopTime time.Time
  25. sizeBuf []byte
  26. flushInterval time.Duration
  27. flushFn func(startTime, stopTime time.Time, buf []byte)
  28. notifyFn func()
  29. isStopping bool
  30. flushChan chan *dataToFlush
  31. sync.RWMutex
  32. }
  33. func NewLogBuffer(flushInterval time.Duration, flushFn func(startTime, stopTime time.Time, buf []byte), notifyFn func()) *LogBuffer {
  34. lb := &LogBuffer{
  35. prevBuffers: newSealedBuffers(PreviousBufferCount),
  36. buf: make([]byte, BufferSize),
  37. sizeBuf: make([]byte, 4),
  38. flushInterval: flushInterval,
  39. flushFn: flushFn,
  40. notifyFn: notifyFn,
  41. flushChan: make(chan *dataToFlush, 256),
  42. }
  43. go lb.loopFlush()
  44. go lb.loopInterval()
  45. return lb
  46. }
  47. func (m *LogBuffer) AddToBuffer(partitionKey, data []byte) {
  48. m.Lock()
  49. defer func() {
  50. m.Unlock()
  51. if m.notifyFn != nil {
  52. m.notifyFn()
  53. }
  54. }()
  55. // need to put the timestamp inside the lock
  56. ts := time.Now()
  57. logEntry := &filer_pb.LogEntry{
  58. TsNs: ts.UnixNano(),
  59. PartitionKeyHash: util.HashToInt32(partitionKey),
  60. Data: data,
  61. }
  62. logEntryData, _ := proto.Marshal(logEntry)
  63. size := len(logEntryData)
  64. if m.pos == 0 {
  65. m.startTime = ts
  66. }
  67. if m.startTime.Add(m.flushInterval).Before(ts) || len(m.buf)-m.pos < size+4 {
  68. m.flushChan <- m.copyToFlush()
  69. m.startTime = ts
  70. if len(m.buf) < size+4 {
  71. m.buf = make([]byte, 2*size+4)
  72. }
  73. }
  74. m.stopTime = ts
  75. m.idx = append(m.idx, m.pos)
  76. util.Uint32toBytes(m.sizeBuf, uint32(size))
  77. copy(m.buf[m.pos:m.pos+4], m.sizeBuf)
  78. copy(m.buf[m.pos+4:m.pos+4+size], logEntryData)
  79. m.pos += size + 4
  80. // fmt.Printf("entry size %d total %d count %d\n", size, m.pos, len(m.idx))
  81. }
  82. func (m *LogBuffer) Shutdown() {
  83. if m.isStopping {
  84. return
  85. }
  86. m.isStopping = true
  87. m.Lock()
  88. toFlush := m.copyToFlush()
  89. m.Unlock()
  90. m.flushChan <- toFlush
  91. close(m.flushChan)
  92. }
  93. func (m *LogBuffer) loopFlush() {
  94. for d := range m.flushChan {
  95. if d != nil {
  96. m.flushFn(d.startTime, d.stopTime, d.data.Bytes())
  97. d.releaseMemory()
  98. }
  99. }
  100. }
  101. func (m *LogBuffer) loopInterval() {
  102. for !m.isStopping {
  103. time.Sleep(m.flushInterval)
  104. m.Lock()
  105. // println("loop interval")
  106. toFlush := m.copyToFlush()
  107. m.Unlock()
  108. m.flushChan <- toFlush
  109. }
  110. }
  111. func (m *LogBuffer) copyToFlush() *dataToFlush {
  112. if m.flushFn != nil && m.pos > 0 {
  113. // fmt.Printf("flush buffer %d pos %d empty space %d\n", len(m.buf), m.pos, len(m.buf)-m.pos)
  114. d := &dataToFlush{
  115. startTime: m.startTime,
  116. stopTime: m.stopTime,
  117. data: copiedBytes(m.buf[:m.pos]),
  118. }
  119. // fmt.Printf("flusing [0,%d) with %d entries\n", m.pos, len(m.idx))
  120. m.buf = m.prevBuffers.SealBuffer(m.startTime, m.stopTime, m.buf, m.pos)
  121. m.pos = 0
  122. m.idx = m.idx[:0]
  123. return d
  124. }
  125. return nil
  126. }
  127. func (d *dataToFlush) releaseMemory() {
  128. d.data.Reset()
  129. bufferPool.Put(d.data)
  130. }
  131. func (m *LogBuffer) ReadFromBuffer(lastReadTime time.Time) (bufferCopy *bytes.Buffer) {
  132. m.RLock()
  133. defer m.RUnlock()
  134. // fmt.Printf("read from buffer: %v last stop time: %v\n", lastReadTime.UnixNano(), m.stopTime.UnixNano())
  135. if lastReadTime.Equal(m.stopTime) {
  136. return nil
  137. }
  138. if lastReadTime.After(m.stopTime) {
  139. // glog.Fatalf("unexpected last read time %v, older than latest %v", lastReadTime, m.stopTime)
  140. return nil
  141. }
  142. if lastReadTime.Before(m.startTime) {
  143. // println("checking ", lastReadTime.UnixNano())
  144. for i, buf := range m.prevBuffers.buffers {
  145. if buf.startTime.After(lastReadTime) {
  146. if i == 0 {
  147. // println("return the earliest in memory", buf.startTime.UnixNano())
  148. return copiedBytes(buf.buf[:buf.size])
  149. }
  150. return copiedBytes(buf.buf[:buf.size])
  151. }
  152. if !buf.startTime.After(lastReadTime) && buf.stopTime.After(lastReadTime) {
  153. pos := buf.locateByTs(lastReadTime)
  154. // fmt.Printf("locate buffer[%d] pos %d\n", i, pos)
  155. return copiedBytes(buf.buf[pos:buf.size])
  156. }
  157. }
  158. return copiedBytes(m.buf[:m.pos])
  159. }
  160. lastTs := lastReadTime.UnixNano()
  161. l, h := 0, len(m.idx)-1
  162. /*
  163. for i, pos := range m.idx {
  164. logEntry, ts := readTs(m.buf, pos)
  165. event := &filer_pb.SubscribeMetadataResponse{}
  166. proto.Unmarshal(logEntry.Data, event)
  167. entry := event.EventNotification.OldEntry
  168. if entry == nil {
  169. entry = event.EventNotification.NewEntry
  170. }
  171. fmt.Printf("entry %d ts: %v offset:%d dir:%s name:%s\n", i, time.Unix(0, ts), pos, event.Directory, entry.Name)
  172. }
  173. fmt.Printf("l=%d, h=%d\n", l, h)
  174. */
  175. for l <= h {
  176. mid := (l + h) / 2
  177. pos := m.idx[mid]
  178. _, t := readTs(m.buf, pos)
  179. if t <= lastTs {
  180. l = mid + 1
  181. } else if lastTs < t {
  182. var prevT int64
  183. if mid > 0 {
  184. _, prevT = readTs(m.buf, m.idx[mid-1])
  185. }
  186. if prevT <= lastTs {
  187. // fmt.Printf("found l=%d, m-1=%d(ts=%d), m=%d(ts=%d), h=%d [%d, %d) \n", l, mid-1, prevT, mid, t, h, pos, m.pos)
  188. return copiedBytes(m.buf[pos:m.pos])
  189. }
  190. h = mid
  191. }
  192. // fmt.Printf("l=%d, h=%d\n", l, h)
  193. }
  194. // FIXME: this could be that the buffer has been flushed already
  195. return nil
  196. }
  197. func (m *LogBuffer) ReleaseMeory(b *bytes.Buffer) {
  198. b.Reset()
  199. bufferPool.Put(b)
  200. }
  201. var bufferPool = sync.Pool{
  202. New: func() interface{} {
  203. return new(bytes.Buffer)
  204. },
  205. }
  206. func copiedBytes(buf []byte) (copied *bytes.Buffer) {
  207. copied = bufferPool.Get().(*bytes.Buffer)
  208. copied.Write(buf)
  209. return
  210. }
  211. func readTs(buf []byte, pos int) (size int, ts int64) {
  212. size = int(util.BytesToUint32(buf[pos : pos+4]))
  213. entryData := buf[pos+4 : pos+4+size]
  214. logEntry := &filer_pb.LogEntry{}
  215. err := proto.Unmarshal(entryData, logEntry)
  216. if err != nil {
  217. glog.Fatalf("unexpected unmarshal filer_pb.LogEntry: %v", err)
  218. }
  219. return size, logEntry.TsNs
  220. }