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.

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