Skip to content

Conversation

kaustubh-garimella-genius
Copy link

@kaustubh-garimella-genius kaustubh-garimella-genius commented Oct 6, 2025

Should address: #322

https://pulsar.apache.org/docs/next/client-libraries-producers/#choose-partitions-when-using-a-key

Copying the behavior in the Java Client, where the producer takes in a configuration for a RoutingPolicy (RoundRobin, Single or Custom) for publishing to a partitioned topic.

Non breaking behavior since if you don't specify a routing policy, it will automtically be Round Robin. Similarly to the Java Client, if you explicitly have a partition_key set in your message, it will deterministically hash the key and route to a partition that way

@kaustubh-garimella-genius
Copy link
Author

@BewareMyPower when you get a chance, please take another look. Updated with some additional tests and slightly changed the behavior to work with a stable vec of producers.

@kaustubh-garimella-genius
Copy link
Author

Fixed the round robin unit test

pub enum RoutingPolicy {
#[default]
RoundRobin,
Single(usize),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This strategy is different from other clients. The index should be generated once rather than provided by the application side. Because the application cannot know the actual number of partitions in advance, the index could exceed the actual number of partitions

Comment on lines +1703 to +1715
let mut producers = Vec::new();
for i in 0..partition_count {
let producer = TopicProducer::new(
pulsar.clone(),
addr.clone(),
format!("{}-{}", topic.clone(), i),
Some(i.to_string()),
options.clone(),
)
.await
.unwrap();
producers.push(producer);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to leverage the REST API to create a partitioned topic. See example here:

format!("http://127.0.0.1:8080/admin/v2/persistent/public/default/{topic}/stats");

https://pulsar.apache.org/docs/next/admin-api-topics/#create-1

Then you won't need to create the PartitionedProducer directly for tests, whose visibility is only within the crate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants