美文网首页
kafka 源码阅读-LogSegment(一)

kafka 源码阅读-LogSegment(一)

作者: 卫渐行 | 来源:发表于2020-12-05 21:39 被阅读0次

    主要是在极客时间中的学习笔记摘入

    kakfa源码阅读

    第一部分 日志

    • 日志组织架构
    kafka日志组成.jpg

    kafka日志对象有多个日志端对象组成,包括消息日志文件(.log)、位移索引文件(.index)、时间戳索引文件(.timeindex)以及已中止(Aborted)事务的索引文件(.txnindex)

    logsegment(kafka.log.LogSegment)

    构造函数中几个重要的参数

    @nonthreadsafe
    class LogSegment private[log] (val log: FileRecords,
                                   val lazyOffsetIndex: LazyIndex[OffsetIndex],
                                   val lazyTimeIndex: LazyIndex[TimeIndex],
                                   val txnIndex: TransactionIndex,
                                   val baseOffset: Long,
                                   val indexIntervalBytes: Int,
                                   val rollJitterMs: Long,
                                   val time: Time) 
    
    • filerecords: 实际保存kafka的消息对象

    • 位移索引文件

    • 时间索引未见

    • 已中止索引文件

    • indexIntervalBytes 其实就是 Broker 端参数 log.index.interval.bytes 值,它控制了日志段对象新增索引项的频率。默认情况下,日志段至少新写入 4KB 的消息数据才会新增一条索引项。而 rollJitterMs 是日志段对象新增倒计时的“扰动值”。因为目前 Broker 端日志段新增倒计时是全局设置,这就是说,在未来的某个时刻可能同时创建多个日志段对象,这将极大地增加物理磁盘 I/O 压力。有了 rollJitterMs 值的干扰,每个新增日志段在创建时会彼此岔开一小段时间,这样可以缓解物理磁盘的 I/O 负载瓶颈。

    • baseoffset : 每个日志端保存自己的起始位移大小,一旦对象呗创建,则是固定的,不能再被修改

    append 方法

    /**
     * Append the given messages starting with the given offset. Add
     * an entry to the index if needed.
     *
     * It is assumed this method is being called from within a lock.
     *
     * @param largestOffset The last offset in the message set 最大位移
     * @param largestTimestamp The largest timestamp in the message set. 最大时间戳 
     * @param shallowOffsetOfMaxTimestamp The offset of the message that has the largest timestamp in the messages to append.  最大时间戳对应的消息位移
     * @param records The log entries to append.  真正要写入的消息集合
     * @return the physical position in the file of the appended records
     * @throws LogSegmentOffsetOverflowException if the largest offset causes index offset overflow
     */
    @nonthreadsafe
    def append(largestOffset: Long,
               largestTimestamp: Long,
               shallowOffsetOfMaxTimestamp: Long,
               records: MemoryRecords): Unit = {
       //判断日志端是否为空,,如果日志端为空,kakfa需要记录要写入消息集合的最大时间戳,并将其作为后面新增日志端倒计时的依据
      if (records.sizeInBytes > 0) {
        trace(s"Inserting ${records.sizeInBytes} bytes at end offset $largestOffset at position ${log.sizeInBytes} " +
              s"with largest timestamp $largestTimestamp at shallow offset $shallowOffsetOfMaxTimestamp")
        val physicalPosition = log.sizeInBytes()
        if (physicalPosition == 0)
          rollingBasedTimestamp = Some(largestTimestamp)
    // 确保输入参数最大位移值是合法的, 确保lastest-baseoffset = [0,Int.MaxValue]之间 ,这是一个已知常见的问题
        ensureOffsetInRange(largestOffset)
    // append真正的写入,将内存中的消息对象写入操作系统的页缓存当中去
        // append the messages 
        val appendedBytes = log.append(records)
        trace(s"Appended $appendedBytes to ${log.file} at end offset $largestOffset")
        // Update the in memory max timestamp and corresponding offset. 更新最大日志最大时间戳,最大时间戳所属的位移值属性,每个日志段都要保存最大时间戳信息和所属消息的位移信息
        if (largestTimestamp > maxTimestampSoFar) {
          maxTimestampSoFar = largestTimestamp
          offsetOfMaxTimestampSoFar = shallowOffsetOfMaxTimestamp
        }
        // append an entry to the index (if needed) 更新索引项,以及写入的字节数;日志端没写入4KB,数据就要更新索引项,当写入字节数操作4KB的时候,append方法会调用索引对象的append方法新增索引项,同时清空已写入的字节数目
        if (bytesSinceLastIndexEntry > indexIntervalBytes) {
          offsetIndex.append(largestOffset, physicalPosition)
          timeIndex.maybeAppend(maxTimestampSoFar, offsetOfMaxTimestampSoFar)
          bytesSinceLastIndexEntry = 0
        }
        bytesSinceLastIndexEntry += records.sizeInBytes
      }
    }
    

    read 方法

    /**
       * Read a message set from this segment beginning with the first offset >= startOffset. The message set will include
       * no more than maxSize bytes and will end before maxOffset if a maxOffset is specified.
       *
       * @param startOffset A lower bound on the first offset to include in the message set we read 要读取的第一条信息位移
       * @param maxSize The maximum number of bytes to include in the message set we read 能读取的最大字节数  
       * @param maxPosition The maximum position in the log segment that should be exposed for read 能读到的最大文件位置
       * @param minOneMessage If this is true, the first message will be returned even if it exceeds `maxSize` (if one exists) 是否允许在消息体过大时,至少返回地第一条信息(为了保证不出现消费饿死的情况)
       *
       * @return The fetched data and the offset metadata of the first message whose offset is >= startOffset,
       *         or null if the startOffset is larger than the largest offset in this log
       */
      @threadsafe
      def read(startOffset: Long,
               maxSize: Int,
               maxPosition: Long = size,
               minOneMessage: Boolean = false): FetchDataInfo = {
        if (maxSize < 0)
          throw new IllegalArgumentException(s"Invalid max size $maxSize for log read from segment $log")
    // 定位要读取的起始文件位置, kafka要更加索引信息找到对应物理文件位置才开始读取消息
        val startOffsetAndSize = translateOffset(startOffset)
    
        // if the start position is already off the end of the log, return null
        if (startOffsetAndSize == null)
          return null
    
        val startPosition = startOffsetAndSize.position
        val offsetMetadata = LogOffsetMetadata(startOffset, this.baseOffset, startPosition)
    
        val adjustedMaxSize =
          if (minOneMessage) math.max(maxSize, startOffsetAndSize.size)
          else maxSize
    
        // return a log segment but with zero size in the case below
        if (adjustedMaxSize == 0)
          return FetchDataInfo(offsetMetadata, MemoryRecords.EMPTY)
    
        // calculate the length of the message set to read based on whether or not they gave us a maxOffset
        //举个例子,假设 maxSize=100,maxPosition=300,startPosition=250,那么 read 方法只能读取 50 字节,因为 maxPosition - startPosition = 50。我们把它和 maxSize 参数相比较,其中的最小值就是最终能够读取的总字节数。
        val fetchSize: Int = min((maxPosition - startPosition).toInt, adjustedMaxSize)
        // 从指定位置开始读取指定大小的消息集合
        FetchDataInfo(offsetMetadata, log.slice(startPosition, fetchSize),
          firstEntryIncomplete = adjustedMaxSize < startOffsetAndSize.size)
      }
    

    recover方法

    /**
     * Run recovery on the given segment. This will rebuild the index from the log file and lop off any invalid bytes
     * from the end of the log and index.
     *   Broker 在启动时会从磁盘上加载所有日志段信息到内存中,并创建相应的 LogSegment 对象实例。在这个过程中,它需要执行一系列的操作。
     * @param producerStateManager Producer state corresponding to the segment's base offset. This is needed to recover
     *                             the transaction index.
     * @param leaderEpochCache Optionally a cache for updating the leader epoch during recovery.
     * @return The number of bytes truncated from the log
     * @throws LogSegmentOffsetOverflowException if the log segment contains an offset that causes the index offset to overflow
     */
    @nonthreadsafe
    def recover(producerStateManager: ProducerStateManager, leaderEpochCache: Option[LeaderEpochFileCache] = None): Int = {
      offsetIndex.reset()
      timeIndex.reset()
      txnIndex.reset()
      var validBytes = 0
      var lastIndexEntry = 0
      maxTimestampSoFar = RecordBatch.NO_TIMESTAMP
      try {
        for (batch <- log.batches.asScala) {
          batch.ensureValid()
          ensureOffsetInRange(batch.lastOffset)
    
          // The max timestamp is exposed at the batch level, so no need to iterate the records
          if (batch.maxTimestamp > maxTimestampSoFar) {
            maxTimestampSoFar = batch.maxTimestamp
            offsetOfMaxTimestampSoFar = batch.lastOffset
          }
    
          // Build offset index
          if (validBytes - lastIndexEntry > indexIntervalBytes) {
            offsetIndex.append(batch.lastOffset, validBytes)
            timeIndex.maybeAppend(maxTimestampSoFar, offsetOfMaxTimestampSoFar)
            lastIndexEntry = validBytes
          }
          validBytes += batch.sizeInBytes()
    
          if (batch.magic >= RecordBatch.MAGIC_VALUE_V2) {
            leaderEpochCache.foreach { cache =>
              if (batch.partitionLeaderEpoch >= 0 && cache.latestEpoch.forall(batch.partitionLeaderEpoch > _))
                cache.assign(batch.partitionLeaderEpoch, batch.baseOffset)
            }
            updateProducerState(producerStateManager, batch)
          }
        }
      } catch {
        case e@ (_: CorruptRecordException | _: InvalidRecordException) =>
          warn("Found invalid messages in log segment %s at byte offset %d: %s. %s"
            .format(log.file.getAbsolutePath, validBytes, e.getMessage, e.getCause))
      }
      val truncated = log.sizeInBytes - validBytes
      if (truncated > 0)
        debug(s"Truncated $truncated invalid bytes at the end of segment ${log.file.getAbsoluteFile} during recovery")
    
      log.truncateTo(validBytes)
      offsetIndex.trimToValidSize()
      // A normally closed segment always appends the biggest timestamp ever seen into log segment, we do this as well.
      timeIndex.maybeAppend(maxTimestampSoFar, offsetOfMaxTimestampSoFar, skipFullCheck = true)
      timeIndex.trimToValidSize()
      truncated
    }
    //注意
    recover 开始时,代码依次调用索引对象的 reset 方法清空所有的索引文件,之后会开始遍历日志段中的所有消息集合或消息批次(RecordBatch)。对于读取到的每个消息集合,日志段必须要确保它们是合法的,这主要体现在两个方面:该集合中的消息必须要符合 Kafka 定义的二进制格式;该集合中最后一条消息的位移值不能越界,即它与日志段起始位移的差值必须是一个正整数值。
    

    相关文章

      网友评论

          本文标题:kafka 源码阅读-LogSegment(一)

          本文链接:https://www.haomeiwen.com/subject/yzhnwktx.html