get_messages not working properly
Hi guys,
We found an issue with version 0.9.3 of kafka-python (installed via pip). The following code would always get only one message:
c = KafkaClient(address)
cons = SimpleConsumer(c, group, topic)
messages = cons.get_messages(100) # Trying to get the last 100 messages
print "len", len(messages) # Always print "1"
However, the same code works OK with version 0.9.2.
I cannot reproduce. I am also using version 0.9.3 installed via pip. I uninstalled and reinstalled, but still can't reproduce.
I made tests and results that number of messages is also limited by the buffer_size. count parameter does not force to get that number of messages. It is just the maximal count, but actually buffer_size has precedence on messages retrieved count.
@bertinatto Can you give more info on what you are seeing here? I just tested and 0.9.3 is working fine for me. Are you sure you don't have a consumer committing as messages are coming in so that you are only seeing one at a time?
I can reproduce it with the following code (kafka-python 0.9.3):
>>> prod = KeyedProducer(c)
>>> for i in range(1000):
... prod.send("test-topic", "KEY%i" % i, ("Data%i" % i) * 1000)
...
[ProduceResponse(topic='test-topic', partition=0, error=0, offset=0)]
[ProduceResponse(topic='test-topic', partition=0, error=0, offset=1)]
...
[ProduceResponse(topic='test-topic', partition=0, error=0, offset=999)]
>>> up = 10000000
>>> cons = SimpleConsumer(c, 'test-group', 'test-topic',
fetch_size_bytes=up, buffer_size=up, max_buffer_size=up) # set large enough buffer & fetch size
>>> cons.pending()
1000
>>> msgs = cons.get_messages(100)
>>> len(msgs) # each time for the first request it gets only one message, why?
1
>>> msgs = cons.get_messages(100)
>>> len(msgs) # for next requests it works well
100
Also it seems that it's related with message length, it works fine with "Data%i" % i message values.
I suspect this is a timeout issue. Can you try passing a timeout kwarg to the get_messages call? The fetch phase probably takes longer than the default 0.1 sec timeout, which forces a return with only 1 message. See http://kafka-python.readthedocs.org/en/latest/apidoc/kafka.consumer.html#kafka.consumer.simple.SimpleConsumer.get_messages for docs on the params.
@dpkp It's probably related with the fetch phase, but only with the initial fetch phase for the first message. Anyway timeout kwarg doesn't work, the behavior is the same even with timeout=100. I emphasize that it's only for large messages, and only for the first get_messages call. I'll try to investigate it later if I have some time.
KafkaClient processes FetchRequests in serial, which means that it will query each broker individually before moving to the next. It is possible that the first partition/broker queried does not have sufficient messages to respond to the request and triggers the request timeout. The only way I can think of to address this situation is to rewrite the client module to handle parallel broker requests.
See #327
Not planning to do any further work on SimpleConsumer -- it is deprecated in favor of KafkaConsumer, which has the same interface as the mainline java client (no get_messages).