[fix][broker] Guard AsyncTokenBucket against long overflow#25262
Merged
lhotari merged 2 commits intoapache:masterfrom Feb 26, 2026
Merged
[fix][broker] Guard AsyncTokenBucket against long overflow#25262lhotari merged 2 commits intoapache:masterfrom
lhotari merged 2 commits intoapache:masterfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #25262 +/- ##
============================================
- Coverage 72.73% 72.57% -0.17%
- Complexity 34047 34408 +361
============================================
Files 1959 1959
Lines 155418 155438 +20
Branches 17731 17738 +7
============================================
- Hits 113050 112803 -247
- Misses 33378 33634 +256
- Partials 8990 9001 +11
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
I'll check out the PR and take a closer look.
lhotari
approved these changes
Feb 26, 2026
Member
lhotari
left a comment
There was a problem hiding this comment.
Thanks for catching this @codelipenghui!
I pushed a change to optimize safeMulDivFloor by using Math.multiplyHigh and Long.divideUnsigned. I also modified the test to cover more large rate values.
lhotari
added a commit
that referenced
this pull request
Feb 26, 2026
Co-authored-by: Lari Hotari <lhotari@apache.org> (cherry picked from commit d207d9c)
lhotari
added a commit
that referenced
this pull request
Feb 26, 2026
Co-authored-by: Lari Hotari <lhotari@apache.org> (cherry picked from commit d207d9c)
4 tasks
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
The async dispatch rate limiter (
AsyncTokenBucket) performs multiply-then-divide arithmetic usinglong. With large rates and longer rate periods (for example,980,000,000tokens per10s), the multiplication can overflow and produce incorrect negative/intermediate values. In dispatch throttling this can keep available tokens at invalid values and effectively stall consumption.Modifications
AsyncTokenBucket:longmultiplication.BigIntegerfor overflow cases.Long.MAX_VALUE.calculateNewTokensSinceLastUpdatenow uses safe multiply/divide.calculateThrottlingDuration(long requiredTokens)now handles subtraction overflow and uses safe multiply/divide.AsyncTokenBucketTest:shouldRefillTokensWithoutOverflowForLargeRateAnd10sPeriodshouldCalculateThrottlingDurationWithoutOverflowForLargeNeedTokensVerifying this change
This change added tests and can be verified as follows:
./mvnw -pl pulsar-broker -Dtest=AsyncTokenBucketTest -Dsurefire.failIfNoSpecifiedTests=false test./mvnw -pl pulsar-broker -Dtest=SubscriptionMessageDispatchThrottlingTest -Dsurefire.failIfNoSpecifiedTests=false -DargLine='-Djdk.attach.allowAttachSelf=true -XX:+EnableDynamicAgentLoading' testDoes this pull request potentially affect one of the following parts:
Documentation
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: codelipenghui#26