forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 1k
new token bucket impl #6893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
new token bucket impl #6893
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| #![allow(clippy::arithmetic_side_effects)] | ||
| use { | ||
| solana_net_utils::token_bucket::*, | ||
| std::{ | ||
| net::{IpAddr, Ipv4Addr}, | ||
| sync::atomic::{AtomicUsize, Ordering}, | ||
| time::{Duration, Instant}, | ||
| }, | ||
| }; | ||
|
|
||
| fn bench_token_bucket() { | ||
| println!("Running bench_token_bucket..."); | ||
| let run_duration = Duration::from_secs(5); | ||
| let fill_rate = 10000.0; | ||
| let request_size = 3; | ||
| let target_rate = fill_rate / request_size as f64; | ||
| let tb = TokenBucket::new(1, 600, fill_rate); | ||
|
|
||
| let accepted = AtomicUsize::new(0); | ||
| let rejected = AtomicUsize::new(0); | ||
|
|
||
| let start = Instant::now(); | ||
| let workers = 8; | ||
|
|
||
| std::thread::scope(|scope| { | ||
| for _ in 0..workers { | ||
| scope.spawn(|| loop { | ||
| if start.elapsed() > run_duration { | ||
| break; | ||
| } | ||
| match tb.consume_tokens(request_size) { | ||
| Ok(_) => accepted.fetch_add(1, Ordering::Relaxed), | ||
| Err(_) => rejected.fetch_add(1, Ordering::Relaxed), | ||
| }; | ||
| }); | ||
| } | ||
| // periodically check for races | ||
| let jh = scope.spawn(|| loop { | ||
| std::thread::sleep(Duration::from_millis(100)); | ||
| let elapsed = start.elapsed(); | ||
| if elapsed > run_duration { | ||
| break; | ||
| } | ||
| let acc = accepted.load(Ordering::Relaxed); | ||
| let rate = acc as f64 / elapsed.as_secs_f64(); | ||
| assert!( | ||
| tb.current_tokens() < request_size * 2, | ||
| "bucket should have no spare tokens" | ||
| ); | ||
| assert!( | ||
| // allow 1% error | ||
| (rate - target_rate).abs() < target_rate / 100.0, | ||
| "Accepted rate should be about {target_rate}, actual {rate}" | ||
| ); | ||
| }); | ||
| jh.join().expect("Rate checks should pass"); | ||
| }); | ||
|
|
||
| let acc = accepted.load(Ordering::Relaxed); | ||
| let rej = rejected.load(Ordering::Relaxed); | ||
| println!("Run complete over {:?} seconds", run_duration.as_secs()); | ||
| println!("Accepted {acc}, Rejected: {rej}"); | ||
| println!( | ||
| "processed {} requests, {} per second", | ||
| acc + rej, | ||
| (acc + rej) as f32 / run_duration.as_secs_f32() | ||
| ); | ||
| } | ||
|
|
||
| fn bench_token_bucket_eviction() { | ||
| println!("Running bench_token_bucket_eviction..."); | ||
| let run_duration = Duration::from_secs(5); | ||
| let target_size = 256; | ||
| let tb = TokenBucket::new(1, 60, 100.0); | ||
| let mut limiter = KeyedRateLimiter::new(target_size, tb, 8); | ||
| // make shrinking more aggressive than default | ||
| // since only one worker is shrinking the | ||
| // datastructure at any given moment so we do not flake this test | ||
| // too hard | ||
| limiter.set_shrink_interval(32); | ||
|
|
||
| let accepted = AtomicUsize::new(0); | ||
| let rejected = AtomicUsize::new(0); | ||
|
|
||
| let start = Instant::now(); | ||
| let ip_pool = 1024; | ||
| let workers = 8; | ||
|
|
||
| let max_size = AtomicUsize::new(0); | ||
| std::thread::scope(|scope| { | ||
| for _ in 0..workers { | ||
| scope.spawn(|| { | ||
| for i in 1.. { | ||
| if Instant::now() > start + run_duration { | ||
| break; | ||
| } | ||
| let ip = IpAddr::V4(Ipv4Addr::from_bits(i % ip_pool as u32)); | ||
| if limiter.consume_tokens(ip, 1).is_ok() { | ||
| accepted.fetch_add(1, Ordering::Relaxed); | ||
| } else { | ||
| rejected.fetch_add(1, Ordering::Relaxed); | ||
| } | ||
| let len_approx = limiter.len_approx(); | ||
| max_size.fetch_max(len_approx, Ordering::Relaxed); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| let acc = accepted.load(Ordering::Relaxed); | ||
| let rej = rejected.load(Ordering::Relaxed); | ||
| println!("Run complete over {:?} seconds", run_duration.as_secs()); | ||
| eprintln!("Max observed size was {}", max_size.load(Ordering::Relaxed)); | ||
| assert!( | ||
| max_size.load(Ordering::Relaxed) <= target_size * 2, | ||
| "Max target size should never be exceeded" | ||
| ); | ||
| println!( | ||
| "processed {} requests, {} per second", | ||
| acc + rej, | ||
| (acc + rej) as f32 / run_duration.as_secs_f32() | ||
| ); | ||
| println!("Rejected: {rej}"); | ||
| } | ||
|
|
||
| fn bench_keyed_rate_limiter() { | ||
| println!("Running bench_keyed_rate_limiter..."); | ||
| let run_duration = Duration::from_secs(5); | ||
| let tb = TokenBucket::new(1, 60, 100.0); | ||
| let limiter = KeyedRateLimiter::new(2048, tb, 8); | ||
|
|
||
| let accepted = AtomicUsize::new(0); | ||
| let rejected = AtomicUsize::new(0); | ||
|
|
||
| let start = Instant::now(); | ||
| let ip_pool = 2048; | ||
| let expected_total_accepts = (run_duration.as_secs() * 100 * ip_pool) as i64; | ||
| let workers = 32; | ||
|
|
||
| std::thread::scope(|scope| { | ||
| for _ in 0..workers { | ||
| scope.spawn(|| { | ||
| for i in 1.. { | ||
| if Instant::now() > start + run_duration { | ||
| break; | ||
| } | ||
| let ip = IpAddr::V4(Ipv4Addr::from_bits(i % ip_pool as u32)); | ||
| if limiter.consume_tokens(ip, 1).is_ok() { | ||
| accepted.fetch_add(1, Ordering::Relaxed); | ||
| } else { | ||
| rejected.fetch_add(1, Ordering::Relaxed); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| let acc = accepted.load(Ordering::Relaxed); | ||
| let rej = rejected.load(Ordering::Relaxed); | ||
| println!("Run complete over {:?} seconds", run_duration.as_secs()); | ||
| println!("Accepted: {acc} (target {expected_total_accepts})"); | ||
| println!("Rejected: {rej}"); | ||
| println!( | ||
| "processed {} requests, {} per second", | ||
| acc + rej, | ||
| (acc + rej) as f32 / run_duration.as_secs_f32() | ||
| ); | ||
| assert!(((acc as i64) - expected_total_accepts).abs() < expected_total_accepts / 10); | ||
| } | ||
|
|
||
| fn main() { | ||
| bench_token_bucket(); | ||
| println!("=========="); | ||
| bench_token_bucket_eviction(); | ||
| println!("=========="); | ||
| bench_keyed_rate_limiter(); | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why haven't you used some benchmarking framework? And what are the results of the current benchmarking?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benching frameworks are not well suited here since the bench is multithreaded, and requires peculiar setup to run. Running it in a loop 10000 times does not really show meaningful perf since you need thread contention.
Here are results:
TL;DR we can process about 7 M requests per second per bucket, the KeyedRateLimiter may slow things down if there is a lot of churn to 3M requests per second.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds much more than we ever need
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well we'd want real code to do things other than token buckets but I do not know how to make this substantially faster, I'm quite certain we are close to hitting HW limits here.