pub struct DefaultPartitioner<H = BuildHasherDefault<DefaultHasher>> { /* private fields */ }Expand description
As its name implies DefaultPartitioner is the default
partitioner for Producer.
For every message it proceeds as follows:
-
If the messages contains a non-negative partition value it leaves the message untouched. This will cause
Producerto try to send the message to exactly that partition to. -
Otherwise, if the message has an “unspecified”
partition- this is, it has a negative partition value - and a specified key,DefaultPartitionerwill compute a hash from the key using the underlying hasher and takehash % num_all_partitionsto derive the partition to send the message to. This will consistently cause messages with the same key to be sent to the same partition. -
Otherwise - a message with an “unspecified”
partitionand no key -DefaultPartitionerwill “randomly” pick one from the “available” partitions trying to distribute the messages across the multiple partitions. In particular, it tries to distribute such messages across the “available” partitions in a round robin fashion. “Available” it this context means partitions with a known leader.
This behavior may not suffice every workload. If your application
is dependent on a particular distribution scheme different from
the one outlined above, you want to provide your own partioner to
the Producer at its initialization time.
See Builder::with_partitioner.
Implementations§
Source§impl DefaultPartitioner
impl DefaultPartitioner
Sourcepub fn with_hasher<B: BuildHasher>(hash_builder: B) -> DefaultPartitioner<B>
pub fn with_hasher<B: BuildHasher>(hash_builder: B) -> DefaultPartitioner<B>
Creates a new partitioner which will use the given hash builder to hash message keys.