commons
commons copied to clipboard
com.twitter.common.stats.Windowed.getCurrentIndex() needs to return a long
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;
}
I fixed this and created a pull request. Although it looks like it failed.