Skip to content

Commit

Permalink
Sort partitions before calling partitioner (dpkp#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
ms7s authored and dpkp committed Dec 19, 2016
1 parent e828395 commit 46f9b1f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions kafka/partitioner/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class DefaultPartitioner(object):
"""
@classmethod
def __call__(cls, key, all_partitions, available):
"""
Get the partition corresponding to key
:param key: partitioning key
:param all_partitions: list of all partitions sorted by partition ID
:param available: list of available partitions in no particular order
:return: one of the values from all_partitions or available
"""
if key is None:
if available:
return random.choice(available)
Expand Down
2 changes: 1 addition & 1 deletion kafka/producer/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def _partition(self, topic, partition, key, value,
assert partition in self._metadata.partitions_for_topic(topic), 'Unrecognized partition'
return partition

all_partitions = list(self._metadata.partitions_for_topic(topic))
all_partitions = sorted(self._metadata.partitions_for_topic(topic))
available = list(self._metadata.available_partitions_for_topic(topic))
return self.config['partitioner'](serialized_key,
all_partitions,
Expand Down

0 comments on commit 46f9b1f

Please sign in to comment.