log_buffer.go 7.9 KB

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