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.

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