commons icon indicating copy to clipboard operation
commons copied to clipboard

com.twitter.common.stats.Windowed.getCurrentIndex() needs to return a long

Open vkueffel opened this issue 10 years ago • 1 comments

When using e.g. WindowedStatistics with a time window/number of slice combination that results in a sliceDuration of less than 662 ms(currently) index calculation in Windowed.getCurrentIndex() fails because System.currentTimeMillis()/662 > Integer.MAX_VALUE and casting it to int produces unpredictable results.

"Best case" is that getCurrentIndex() returns a negative value (as happened to me), causing an ArrayIndexOutOfBoundsException because the whole code in sync() is skipped and index will remain at the initial value of -1.

Simple fix, should just be like this:

protected long getCurrentIndex() {
  return clock.nowMillis() / sliceDuration;
}

vkueffel avatar Feb 11 '15 22:02 vkueffel

I fixed this and created a pull request. Although it looks like it failed.

benpoliquin avatar Feb 25 '15 03:02 benpoliquin