This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
Allow more consumers than partitions for a balanced consumer #554
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a3a8677
removing restriction for the number of consumers vs. partitions; see …
vitalyli b4ca39b
removing restriction for the number of consumers vs. partitions; addi…
vitalyli 00b5bde
removing restriction for the number of consumers vs. partitions; end-…
vitalyli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
|
||
#!/usr/bin/env python | ||
import threading, logging, time, traceback | ||
|
||
from pykafka import KafkaClient | ||
import time | ||
|
||
#FIXME: update these to match your cluster | ||
kafka_hosts='kafka_host:9000' | ||
kafka_zookeeper='zkhost:2181/kafka' | ||
topic_name='test_topic' | ||
|
||
lock2 = threading.Lock() | ||
|
||
mdict = {} | ||
tdict={} | ||
|
||
#This test demonstrates how to setup one message to one consumer (a distributed logical queue) in a cluster of N consumers. | ||
#It also proves that it's necessary to address this issue to make it work: https://github.com/Parsely/pykafka/issues/527 | ||
|
||
class Producer(threading.Thread): | ||
daemon = True | ||
|
||
def run(self): | ||
th_name = threading.currentThread().getName() | ||
count = 0 | ||
client = KafkaClient(hosts=kafka_hosts) | ||
topic = client.topics[topic_name] | ||
producer = topic.get_producer() | ||
while True: | ||
msg = """{"test":%d}""" % (count) | ||
|
||
#keep track of all 10k messages produced | ||
lock2.acquire() | ||
mdict[msg]=1 | ||
lock2.release() | ||
|
||
producer.produce(msg.encode("utf-8")) | ||
count += 1 | ||
if (count >= 10000): | ||
break | ||
while True: | ||
|
||
time.sleep(5) | ||
if len(mdict) > 0: | ||
print(len(mdict)) | ||
else: | ||
#check if only one consumer processed a given message; total of 10k | ||
print(0) | ||
ccnt = 0 | ||
for v in tdict.values(): | ||
ccnt += v | ||
print(ccnt) | ||
|
||
class Consumer(threading.Thread): | ||
daemon = True | ||
|
||
def run(self): | ||
th_name = threading.currentThread().getName() | ||
|
||
client = KafkaClient(hosts=kafka_hosts) | ||
topic = client.topics[topic_name] | ||
|
||
consumer = topic.get_balanced_consumer(consumer_group='group1', auto_commit_enable=True, zookeeper_connect=kafka_zookeeper) | ||
|
||
while True: | ||
try: | ||
message = consumer.consume(block=True) | ||
|
||
txt = message.value.decode("utf-8") | ||
|
||
#keep track of not only that message was processed, but also by how many consumers (should be only one to one) | ||
lock2.acquire() | ||
if mdict.has_key(txt): | ||
mdict.pop(txt) | ||
|
||
cnt = tdict.get(txt) | ||
|
||
if cnt is None: | ||
tdict[txt]=1 | ||
else: | ||
tdict[txt]=(cnt+1) | ||
|
||
lock2.release() | ||
|
||
print ("Consumer %s; Offset %s; messsage %s" % (th_name, message.offset, txt)) | ||
|
||
except Exception as e: | ||
print(e) | ||
logging.error(traceback.format_exc()) | ||
consumer = topic.get_balanced_consumer(consumer_group='group1', zookeeper_connect=kafka_zookeeper) | ||
def main(): | ||
threads = [ | ||
Producer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer(), | ||
Consumer() | ||
] | ||
|
||
for t in threads: | ||
t.start() | ||
|
||
time.sleep(1000000) | ||
|
||
if __name__ == "__main__": | ||
logging.basicConfig( | ||
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s', | ||
level=logging.INFO | ||
) | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This endquote needs to be kept.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops; fixing