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.

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