Skip to content

Return 0 from RateCounter.rate() when no values have been recorded#3228

Merged
hengyunabc merged 1 commit into
alibaba:masterfrom
vasiliy-mikhailov:fix/ratecounter-zero-length
Jul 17, 2026
Merged

Return 0 from RateCounter.rate() when no values have been recorded#3228
hengyunabc merged 1 commit into
alibaba:masterfrom
vasiliy-mikhailov:fix/ratecounter-zero-length

Conversation

@vasiliy-mikhailov

@vasiliy-mikhailov vasiliy-mikhailov commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

RateCounter.rate() computes sum / (double) countLength. Before any value is
recorded countLength is 0, so the method returns NaN. Callers reading the
rate of a fresh counter then propagate NaN (and NaN compares false against
every threshold).

Return 0.0 when nothing has been recorded.

Verifying this change

The added RateCounterTest fails on the current master and passes with this change:

Before the fix (on master):

$ mvn test -Dtest=RateCounterTest -pl core
[INFO] Running com.taobao.arthas.core.util.metrics.RateCounterTest
[ERROR] Tests run: 4, Failures: 2, Errors: 0, Skipped: 0 <<< FAILURE!
org.opentest4j.AssertionFailedError: RateCounter.rate() should not return NaN when no values have been updated, but got: NaN ==> expected: <false> but was: <true>
	at com.taobao.arthas.core.util.metrics.RateCounterTest.testRateWhenNoUpdatesShouldNotBeNaN(RateCounterTest.java:18)
[INFO] BUILD FAILURE

After the fix:

$ mvn test -Dtest=RateCounterTest -pl core
[INFO] Running com.taobao.arthas.core.util.metrics.RateCounterTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

AI assistance disclosure

This contribution was produced with the help of an AI pipeline. The pipeline processed a large amount of source code to surface suspected bugs, reproduced a subset of them with failing unit tests and generated candidate fixes, and prepared pull requests from the ones that held up. Each PR was then reviewed and verified by a human before being opened: the fix and test were checked by hand and the test was confirmed to fail before the change and pass after.

@hengyunabc
hengyunabc merged commit 4379657 into alibaba:master Jul 17, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants