question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

first call to KafkaConsumer.poll() takes a long time

See original GitHub issue

Based on https://github.com/dpkp/kafka-python/issues/673 I am polling quickly on the consumer to set the high water mark once it is created. But that first poll(), which has the sole purpose of setting the high water mark can take up to 20 seconds to complete, regardless of what the timeout is set to:

from __future__ import absolute_import, division, print_function
from kafka import KafkaConsumer
import time

timeout = 100

consumer = KafkaConsumer('test', bootstrap_servers='localhost:9093')

def poll():
    start = time.time()
    print("polling for %d ms" % timeout)
    partitions = consumer.poll(timeout)
    end = time.time()
    print("poll found %d partitions in %d ms" % (len(partitions), (end-start)*1000))

poll()
poll()

===============
bash$ python consumer.py 
polling for 100 ms
poll found 0 partitions in 26619 ms
polling for 100 ms
poll found 0 partitions in 100 ms

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
StuAtGitcommented, Dec 13, 2017

I found an interesting edge case that can trigger unusually long polls:

  • two consumers, different topic, same consumer id

  • processing loop that issues a poll() to one consumer, and then another

  • something goes wrong and the program doesn’t unsubscribe properly (or you’re creating a new group).

The second poll() always would take max.poll.interval.ms to subscribe, as if it was waiting for a member of it’s group to expire. Note that this would happen, even if I waited hours between runs, and max.poll.interval.ms was set to a few minutes, so it doesn’t appear to be waiting for the dead consumer to exceed max.poll.interval.ms.

If I swapped the order in which I consumed, it would still block on the second poll() (even though it was on the other topic, that previously subscribed quickly).

And finally:

  • putting the two consumers in different groups fixed the problem instantly. Even though one consumer was in the old group (I just appended a suffix to the previous group id for one of the consumers).

Note that in two functioning topics, where I had subscribed & unsubscribed previously, things were fine. The main problem was once it started failing, it never recovered.

The second poll() would always take max.poll.interval.ms, and cause the first to expire.

Putting them in separate consumer groups seemed to fixed everything, even though I thought the same group id could be used across separate topics, and it would essentially be two different groups.

7reactions
dpkpcommented, May 20, 2016

Try setting group_id=None in your consumer, or call consumer.close() before ending script, or use assign() not subscribe (). Otherwise you are rejoining an existing group that has known but unresponsive members. The group coordinator will wait until those members checkin/leave/timeout. Since the consumers no longer exist (it’s your prior script runs) they have to timeout.

And consumer.poll() blocks during group rebalance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What happens when you call poll on Kafka Consumer?
Depending, which poll you call - the one taking long or Duration as parameter it will wait for synchronization with Kafka Cluster indefinitely ......
Read more >
Kafka Poll Takes Very Long Time - Stack Overflow
However, poll() takes a very long time i.e. from around 10 up to 45 seconds. Another issue seen is the occurrence of these...
Read more >
Kafka Consumer Important Settings: Poll & Internal Threads ...
Consumers poll brokers periodically using the .poll() method. If two .poll() calls are separated by more than max.poll.interval.ms time, then the consumer ...
Read more >
Consuming messages - IBM Cloud Docs
The consumer calls poll() , receives a batch of messages, processes them promptly, ... consumer can be short, even if message processing takes...
Read more >
KafkaConsumer (kafka 0.10.2.1 API)
It automatically advances every time the consumer receives messages in a call to poll(long) . The committed position is the last offset that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found