[fix][broker] Fix rate limiter token bucket and clock consistency issues causing excessive throttling and connection timeouts - #23930
Merged
lhotari merged 63 commits intoFeb 8, 2025
Conversation
3 tasks
lhotari
requested review from
dao-jun,
eolivelli,
heesung-sohn,
merlimat,
nicoloboschi,
nodece,
poorbarcode and
shibd
February 5, 2025 13:48
…yed when there are races
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #23930 +/- ##
============================================
+ Coverage 73.57% 74.29% +0.72%
+ Complexity 32624 31923 -701
============================================
Files 1877 1853 -24
Lines 139502 143855 +4353
Branches 15299 16357 +1058
============================================
+ Hits 102638 106877 +4239
+ Misses 28908 28586 -322
- Partials 7956 8392 +436
Flags with carried forward coverage won't be shown. Click here to find out more.
|
lhotari
requested review from
Technoboy-,
rdhabalia,
thetumbled and
vineeth1995
February 5, 2025 19:09
rdhabalia
reviewed
Feb 5, 2025
Member
Author
|
I fixed the flaky throttling tests and moved them to broker-api group. There were multiple issues in the flaky tests. |
nodece
approved these changes
Feb 8, 2025
hanmz
pushed a commit
to hanmz/pulsar
that referenced
this pull request
Feb 12, 2025
…ues causing excessive throttling and connection timeouts (apache#23930)
mukesh-ctds
pushed a commit
to datastax/pulsar
that referenced
this pull request
Feb 20, 2025
…ues causing excessive throttling and connection timeouts (apache#23930) (cherry picked from commit f8e4c11) (cherry picked from commit c84d864)
4 tasks
15 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.
Fixes #23920
Motivation
Prior to this PR, connections in Pulsar could experience excessive throttling and timeouts due to issues in rate limiting implementation. This was caused by both calculation errors in token bucket logic and problems with clock source handling in virtualized environments. The PR addresses these issues to prevent unintended long throttling periods and connection timeouts.
Two key issues are addressed in this PR:
Calculation Issue in AsyncTokenBucket:
Clock Source Consistency:
System.nanoTime()isn't strictly monotonic and consistent across multiple CPUsSystem.nanoTime()was read on different threads.For details about timekeeping challenges in virtualized environments, see Linux kernel documentation on timekeeping.
Without this fix, when time leaps backwards (for example due to
System.nanoTimebeing read by a thread running on a different CPU with unsynchronized TSC), the rate limiter would incorrectly throttle connections for the duration of the time leap. In virtualized environments, the time difference between CPU cores can be significant. Even though such large variances are uncommon, they can cause extended connection timeouts when they occur. This PR significantly improves reliability by preventing such time-variance-induced throttling entirely.While both forward and backward time leaps can occur in these environments, only backward leaps are explicitly handled. Forward leaps cannot be reliably detected within the eventually consistent logic used in AsyncTokenBucket, and their impact is bounded - at most one rate period (default 1 second) of potential overuse. This design choice favors implementation simplicity and performance while still protecting against the more problematic backward leap case that could cause extended throttling.
Although this PR also covers clock source consistency issues, the most probable root cause for the reported issue #23920 is, however, the calculation logic that was assuming that the DefaultMonotonicSnapshotClock's update thread would always be executing consistently.
Modifications
AsyncTokenBucket improvements:
DefaultMonotonicSnapshotClock enhancements:
Rate limiter implementations:
Performance
AsyncTokenBucketBenchmark and DefaultMonotonicSnapshotClockBenchmark results with JMH demonstrate high throughput (test results with Apple M3 Max):
Results with Dell XPS 15 7590 i9-9980HK on Linux:
Documentation
docdoc-requireddoc-not-neededdoc-complete