RussellLuo / timingwheel

Golang implementation of Hierarchical Timing Wheels.
MIT License
656 stars 121 forks source link

bug of calculate expiration #3

Closed mupengX closed 5 years ago

mupengX commented 5 years ago

the timingwheel add() 方法中:

else if t.expiration < tw.currentTime+tw.interval {
        // Put it into its own bucket
        virtualID := t.expiration / tw.tick
        b := tw.buckets[virtualID%tw.wheelSize]
        b.Add(t)

        // Set the bucket expiration time
        if b.SetExpiration(virtualID * tw.tick) {
            tw.queue.Offer(b)
        }

        return true
    }

求VirtualID应该是irtualID := (t.expiration - tw.currentTime +1) / tw.tick ,b.SetExpiration(virtualID tw.tick) 应该是b.SetExpiration(tw.currentTime + virtualID tw.tick)

func (tw *TimingWheel) advanceClock(expiration int64) {
    if expiration >= tw.currentTime+tw.tick {
        tw.currentTime = truncate(expiration, tw.tick)

        // Try to advance the clock of the overflow wheel if present
        overflowWheel := atomic.LoadPointer(&tw.overflowWheel)
        if overflowWheel != nil {
            (*TimingWheel)(overflowWheel).advanceClock(tw.currentTime)
        }
    }
}

advanceClock()中传入的是bucket. Expiration(),按照现在的逻辑bucket. Expiration()=virtualID * tw.tick,advanceClock()方法中的if语句应该是不会成立的。

RussellLuo commented 5 years ago

@mupengX 谢谢关注!timingwheel 参考了 Kafka 的实现,你指出的这些细节,Kafka 也是这样处理的。

你可以看看 我的博文介绍 ,以及 Kafka 的源码(这里这里)。

mupengX commented 5 years ago

是我开始理解错了,谢谢!