[improve][broker] Optimize AsyncTokenBucket overflow solution further to reduce fallback to BigInteger#25269
Merged
codelipenghui merged 1 commit intoapache:masterfrom Feb 26, 2026
Conversation
… to reduce fallback to BigInteger
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #25269 +/- ##
=============================================
+ Coverage 37.33% 72.60% +35.27%
- Complexity 13272 34441 +21169
=============================================
Files 1902 1959 +57
Lines 151358 155543 +4185
Branches 17252 17741 +489
=============================================
+ Hits 56502 112938 +56436
+ Misses 87162 33592 -53570
- Partials 7694 9013 +1319
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR further hardens AsyncTokenBucket’s overflow-safe arithmetic by caching pre-reduced rate parameters (rate and rate period) to reduce how often computations must fall back to BigInteger, which is especially relevant for very large byte-rate limits.
Changes:
- Introduce an internal cached
RateParametershelper that reduces(rate, ratePeriodNanos)by their highest common power of ten. - Route token-refill and throttling-duration calculations through the reduced parameters.
- Expand
AsyncTokenBucketTestcoverage with additional very-large rate inputs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pulsar-broker/src/main/java/org/apache/pulsar/broker/qos/AsyncTokenBucket.java | Adds cached reduced-rate parameters and uses them in refill/throttling calculations to reduce BigInteger fallback. |
| pulsar-broker/src/test/java/org/apache/pulsar/broker/qos/AsyncTokenBucketTest.java | Adds larger rate values to exercise overflow-safe paths at multi-GB/s scale. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
codelipenghui
approved these changes
Feb 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
#25262 added a solution to prevent overflow in token & duration calculations. The solution falls back to the usage of BigInteger for very large values. This PR introduces yet another optimization that reduces the need to fallback to BigInteger.
This would be relevant when using multi gigabyte byte rate limits.
Modifications
Documentation
docdoc-requireddoc-not-neededdoc-complete