Kafka时间轮每层初始时间为什么需要向下取整?

值得等待的““烟火”” 发表于: 2021-12-02   最后更新时间: 2021-12-02 22:37:48   692 游览

在学习kafka关于时间轮的实现的过程中,发现了两个困惑点:

1、为什么“currentTime”需要向下取整?如果不向下取整入,会有什么影响?代码显示如下:

@nonthreadsafe
private[timer] class TimingWheel(tickMs: Long, wheelSize: Int, startMs: Long, taskCounter: AtomicInteger, queue: DelayQueue[TimerTaskList]) {
  private[this] var currentTime = startMs - (startMs % tickMs) // rounding down to multiple of tickMs
}

2、创建多层时间轮时,为什么这里的“startMs”字段是当前层“currentTime”字段的值(这个值已经向下取整,而不是原始的值)?代码显示如下:

@nonthreadsafe
private[timer] class TimingWheel(tickMs: Long, wheelSize: Int, startMs: Long, taskCounter: AtomicInteger, queue: DelayQueue[TimerTaskList]) {
  private[this] var currentTime = startMs - (startMs % tickMs) // rounding down to multiple of tickMs
  private[this] def addOverflowWheel(): Unit = {
    synchronized {
      if (overflowWheel == null) {
        overflowWheel = new TimingWheel(
          tickMs = interval,
          wheelSize = wheelSize,
          startMs = currentTime,
          taskCounter = taskCounter,
          queue
        )
      }
    }
  }
}

希望有大佬能帮忙解惑!!!

添加评论
你的答案

查看kafka相关的其他问题或提一个您自己的问题