3

I'm new to Kafka, and am trying to build a service to service messaging platofrm on it. Here's my setup:

Kafka 0.9.0.1
Zookeeper 3.4.8
kafka-python 1.3.3

My application creates a KafkaProducer from which I send a stream of messages to a single topic with 6 partitions. I also create 7 KafkaConsumers (under a single group_id, 6 of which get assigned to the 6 partitions and one is left in an idle state (which is expected). While the producer is streaming, I increase the partition count to 7, with an expectation that the stream would not be distributed across 7 partitions and would wake up the idle consumer. However, it seems like the producer doesn't pick up the newly added partition until I re-initialize it by rebooting the application. I scale the partition count by running this:

kafka-topics --alter --zookeeper localhost:2181 --topic test --partitions 7

Is there a way for the producer to pick up the change in partition count without re-initializing it?

Here're the associated code snippets:

Producer

class Producer(threading.Thread):
daemon = True

def __init__(self, name, manager):
    super(Producer, self).__init__()
    self.producer = KafkaProducer(bootstrap_servers='localhost:9092')

def run(self):
    while not self.killed:
        if not self.q.empty():
            self._busy()
            self.producer.send('test', value=self.q.get())
        else:
            self._free()

Consumers

class Consumer(threading.Thread):
    daemon = True

    def __init__(self, name, manager):
        super(Consumer, self).__init__()
        self.consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
                                 group_id='test_group',
                                 client_id="Consumer " + self.name)
        self.consumer.subscribe(['test'])

    def run(self):
        while not self.killed:
            messages = self.consumer.poll()

            for topic, records in messages.iteritems():
                print self.consumer.config['client_id'] + ": " + str(records)
Saksham Gupta
  • 93
  • 1
  • 8
  • 1
    can you share how did you do partitions assignment for `producer`. Also as and when your partitions increase in `producer` there is a corresponding change required in `consumer` too. – Krishna Oza Jun 28 '18 at 08:31

1 Answers1

0

I ran into a possibly similar problem and was able to find the solution. I wrote it here: How does librdkafka producer learn about new topic partitions in Kafka

If your test was too short, that's probably the reason why the producer didn't learn about the new partitions. Parameter topic.metadata.refresh.interval.ms is 300000 (in ms) by default, so the broker will refresh metadata in producers every 5 minutes. If your test took more than 5 minutes after adding partitions, then this is not the cause.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
rodolk
  • 5,606
  • 3
  • 28
  • 34