Kafka源码分析-Server-日志存储(5)-LogSegm

2019-03-08  本文已影响0人  陈阳001

为了防止Log文件过大,将Log切分成多个日志文件,每个日志文件对应一个LogSegment。在LogSegment中封装一个FileMessageSet和一个OffsetIndex对象,提供日志文件和索引文件的读写功能以及其他的辅助功能。
看下核心字段:

 def append(offset: Long, messages: ByteBufferMessageSet) {
    //检测是否在满足添加索引项的条件
    if (messages.sizeInBytes > 0) {
      trace("Inserting %d bytes at offset %d at position %d".format(messages.sizeInBytes, offset, log.sizeInBytes()))
      // append an entry to the index (if needed)
      if(bytesSinceLastIndexEntry > indexIntervalBytes) {
        index.append(offset, log.sizeInBytes())
        //成功添加索引后,bytesSinceLastIndexEntry重置为0
        this.bytesSinceLastIndexEntry = 0
      }
      // append the messages 写日志文件
      log.append(messages)
      this.bytesSinceLastIndexEntry += messages.sizeInBytes
    }
  }
LogSegment索引append.png

读取消息的功能由LogSegment.read()方法实现,它有四个参数。

上一篇 下一篇

猜你喜欢

热点阅读