-
Notifications
You must be signed in to change notification settings - Fork 141
feat: Updating producer to configure RoutingPolicy - adding partition_key as the default for Round Robin if set #366
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
BewareMyPower
merged 13 commits into
streamnative:master
from
kaustubh-garimella-genius:kg/routing_policy
Oct 24, 2025
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1694947
first draft of routing policy
kaustubh-garimella-genius b1a91d6
refactor routing policy to its own section
kaustubh-garimella-genius e836f14
add unit test
kaustubh-garimella-genius 59f9f39
revert producer example
kaustubh-garimella-genius d825b8e
revert formatting changes for cargo toml
kaustubh-garimella-genius 35e9e69
fix: update producers to be a vector, track last used index instead o…
kaustubh-garimella-genius 2c01bd3
feat: add test cases for routing policies
kaustubh-garimella-genius 3a08763
fix: fix for running all tests
kaustubh-garimella-genius ae6b66e
fix: Fix round robin test
kaustubh-garimella-genius 37f5dec
fix: change single producer to randomly select partition server side
kaustubh-garimella-genius cd6d6ba
fix: fix for unit tests and single routing policy behavior
kaustubh-garimella-genius 063bf95
fix: remove println statements in tests
kaustubh-garimella-genius 25a8a18
Fix lint
BewareMyPower 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
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
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,79 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use murmur3::murmur3_32; | ||
|
|
||
| use crate::producer::Message; | ||
|
|
||
| #[derive(Clone, Default)] | ||
| pub enum RoutingPolicy { | ||
| #[default] | ||
| RoundRobin, | ||
| Single(usize), | ||
BewareMyPower marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Custom(Arc<dyn CustomRoutingPolicy>), | ||
| } | ||
|
|
||
| impl RoutingPolicy { | ||
| pub fn compute_partition_index_for_key(key: &str, partition_count: usize) -> usize { | ||
| // Use murmur3 hash for deterministic results across restarts | ||
| // Using a fixed seed (0) ensures consistent hashing | ||
| let hash = murmur3_32(&mut key.as_bytes(), 0).unwrap_or(0); | ||
| (hash % partition_count as u32) as usize | ||
| } | ||
| } | ||
|
|
||
| pub trait CustomRoutingPolicy: Send + Sync { | ||
| fn route(&self, message: &Message, num_producers: usize) -> usize; | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use uuid::Uuid; | ||
|
|
||
| use crate::routing_policy::RoutingPolicy; | ||
|
|
||
| #[test] | ||
| fn test_compute_partition_index_consistency() { | ||
| let partition_count = 4; | ||
| let key = Uuid::new_v4().to_string(); | ||
|
|
||
| let partition_index = RoutingPolicy::compute_partition_index_for_key(&key, partition_count); | ||
| assert!(partition_index < partition_count); | ||
|
|
||
| for _ in 0..10 { | ||
| let other_partition_index = | ||
| RoutingPolicy::compute_partition_index_for_key(&key, partition_count); | ||
| assert!(other_partition_index < partition_count); | ||
| assert_eq!( | ||
| partition_index, other_partition_index, | ||
| "partition index should be deterministic for the same key" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_compute_partition_index_distribution() { | ||
| let partition_count = 4; | ||
| let mut partition_counts = vec![0; partition_count]; | ||
|
|
||
| let total = 1000; | ||
| for _ in 0..total { | ||
| let partition_index = RoutingPolicy::compute_partition_index_for_key( | ||
| &Uuid::new_v4().to_string(), | ||
| partition_count, | ||
| ); | ||
| partition_counts[partition_index] += 1; | ||
| } | ||
|
|
||
| for count in partition_counts { | ||
| let ratio = count as f64 / total as f64; | ||
| let expected_ratio = 1.0 / partition_count as f64; | ||
|
|
||
| assert!( | ||
| ratio > (expected_ratio - 0.1) && ratio < (expected_ratio + 0.1), | ||
| "distribution ratio {} is not near expected ratio {}", | ||
| ratio, | ||
| expected_ratio | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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.