Skip to content

Commit

Permalink
Merge pull request #367 from dpkp/clean_metadata_refresh
Browse files Browse the repository at this point in the history
Clear local metadata cache before refresh in client.load_metadata_for_topics()
  • Loading branch information
dpkp committed Apr 13, 2015
2 parents 19643fb + 6326e18 commit cd81cf0
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions kafka/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,11 @@ def reinit(self):

def reset_topic_metadata(self, *topics):
for topic in topics:
try:
partitions = self.topic_partitions[topic]
except KeyError:
continue

for partition in partitions:
self.topics_to_brokers.pop(TopicAndPartition(topic, partition), None)

del self.topic_partitions[topic]
for topic_partition in list(self.topics_to_brokers.keys()):
if topic_partition.topic == topic:
del self.topics_to_brokers[topic_partition]
if topic in self.topic_partitions:
del self.topic_partitions[topic]

def reset_all_metadata(self):
self.topics_to_brokers.clear()
Expand Down Expand Up @@ -339,10 +335,17 @@ def load_metadata_for_topics(self, *topics):
(a single partition w/o a leader, for example)
"""
topics = [kafka_bytestring(t) for t in topics]

if topics:
for topic in topics:
self.reset_topic_metadata(topic)
else:
self.reset_all_metadata()

resp = self.send_metadata_request(topics)

log.debug("Broker metadata: %s", resp.brokers)
log.debug("Topic metadata: %s", resp.topics)
log.debug("Received new broker metadata: %s", resp.brokers)
log.debug("Received new topic metadata: %s", resp.topics)

self.brokers = dict([(broker.nodeId, broker)
for broker in resp.brokers])
Expand All @@ -351,8 +354,6 @@ def load_metadata_for_topics(self, *topics):
topic = topic_metadata.topic
partitions = topic_metadata.partitions

self.reset_topic_metadata(topic)

# Errors expected for new topics
try:
kafka.common.check_error(topic_metadata)
Expand Down

0 comments on commit cd81cf0

Please sign in to comment.