3

I'm trying to create a simple Kafka producer based on confluent_kafka. My code is the following:

 #!/usr/bin/env python
 from confluent_kafka import Producer
 import json


 def delivery_report(err, msg):
     """Called once for each message produced to indicate delivery result.
     Triggered by poll() or flush().
     see https://github.com/confluentinc/confluent-kafka-python/blob/master/README.md"""
     if err is not None:
         print('Message delivery failed: {}'.format(err))
     else:
         print('Message delivered to {} [{}]'.format(
             msg.topic(), msg.partition()))


 class MySource:
     """Kafka producer"""
     def __init__(self, kafka_hosts, topic):
         """
         :kafka_host list(str): hostnames or 'host:port' of Kafka
         :topic str: topic to produce messages to
         """
         self.topic = topic
         # see https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md
         config = {
             'metadata.broker.list': ','.join(kafka_hosts),
             'group.id': 'mygroup',
             }
         self.producer = Producer(config)

     @staticmethod
     def main():
         topic = 'my-topic'
         message = json.dumps({
             'measurement': [1, 2, 3]})
         mys = MySource(['kafka'], topic)
         mys.producer.produce(
                 topic, message, on_delivery=delivery_report)
         mys.producer.flush()


 if __name__ == "__main__":
     MySource.main()

The first time I use a topic (here: "my-topic"), Kafka does react with "Auto creation of topic my-topic with 1 partitions and replication factor 1 is successful (kafka.server.KafkaApis)". However, the call-back function (on_delivery=delivery_report) is never called and it hangs at flush() (it terminates if I set a timeout for flush) neither the first time nor subsequent times. The Kafka logs does not show anything if I use an existing topic.

Yushin Washio
  • 675
  • 8
  • 12
  • 1
    First make sure to use the latest version of confluent-kafka-python and librdkafka by installing v0.11.4. – Edenhill May 29 '18 at 11:58
  • 1
    Second, you can enable `"debug": "topic,msg,broker"` (producer config) to get an idea what is happening under the hood. – Edenhill May 29 '18 at 11:59
  • @Edenhill Thank you for the advice. Yes, I'm using the binary package of confluent-kafka==0.11.4 from PyPI. The debug config is a good hint. It seems the issue has something to do with the leader since it repeadedly "re-queries" because "leader is internal" (`partition 0 Leader -1`), then partitions "0 unassigned messages in topic my-topic to 1 partitions" – Yushin Washio May 29 '18 at 14:30
  • 1
    Check that your new topic has a leader (with `kafkacat .. -L -t yourtopic` or `kafka-topics .. --describe --topic yourtopic`) – Edenhill May 29 '18 at 17:22
  • Thanks a lot, looking at the output, it seemed, the topics that I created before restarting kafka Docker container (and possibly zookeeper) had leader entries that do not exist any more. For some reason now it works (`on_delivery` is called) with using a new topic although I thought I had already tried that too... – Yushin Washio May 30 '18 at 09:17
  • For those who run kafka with docker-compose: https://stackoverflow.com/questions/53571823/why-could-kafka-warn-partitions-have-leader-brokers-without-a-matching-listener – Oleg Yablokov May 12 '21 at 21:17

0 Answers0