-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add producer batch send queue size limit #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,16 @@ | |
| import time | ||
|
|
||
| try: | ||
| from queue import Empty | ||
| from queue import Empty, Full | ||
| except ImportError: | ||
| from Queue import Empty | ||
| from Queue import Empty, Full | ||
| from collections import defaultdict | ||
| from multiprocessing import Queue, Process | ||
|
|
||
| import six | ||
|
|
||
| from kafka.common import ( | ||
| BatchQueueOverfilledError, | ||
| ProduceRequest, TopicAndPartition, UnsupportedCodecError | ||
| ) | ||
| from kafka.protocol import CODEC_NONE, ALL_CODECS, create_message_set | ||
|
|
@@ -21,6 +22,7 @@ | |
|
|
||
| BATCH_SEND_DEFAULT_INTERVAL = 20 | ||
| BATCH_SEND_MSG_COUNT = 20 | ||
| BATCH_SEND_QUEUE_MAXSIZE = 0 | ||
|
|
||
| STOP_ASYNC_PRODUCER = -1 | ||
|
|
||
|
|
@@ -113,12 +115,14 @@ def __init__(self, client, async=False, | |
| codec=None, | ||
| batch_send=False, | ||
| batch_send_every_n=BATCH_SEND_MSG_COUNT, | ||
| batch_send_every_t=BATCH_SEND_DEFAULT_INTERVAL): | ||
| batch_send_every_t=BATCH_SEND_DEFAULT_INTERVAL, | ||
| batch_send_queue_maxsize=BATCH_SEND_QUEUE_MAXSIZE): | ||
|
|
||
| if batch_send: | ||
| async = True | ||
| assert batch_send_every_n > 0 | ||
| assert batch_send_every_t > 0 | ||
| assert batch_send_queue_maxsize >= 0 | ||
| else: | ||
| batch_send_every_n = 1 | ||
| batch_send_every_t = 3600 | ||
|
|
@@ -139,7 +143,8 @@ def __init__(self, client, async=False, | |
| log.warning("async producer does not guarantee message delivery!") | ||
| log.warning("Current implementation does not retry Failed messages") | ||
| log.warning("Use at your own risk! (or help improve with a PR!)") | ||
| self.queue = Queue() # Messages are sent through this queue | ||
| # Messages are sent through this queue | ||
| self.queue = Queue(maxsize=batch_send_queue_maxsize) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is used both in batch mode as well as in simple async mode -- the variable name probably should not be specific to batch mode |
||
| self.proc = Process(target=_send_upstream, | ||
| args=(self.queue, | ||
| self.client.copy(), | ||
|
|
@@ -188,7 +193,13 @@ def _send_messages(self, topic, partition, *msg, **kwargs): | |
|
|
||
| if self.async: | ||
| for m in msg: | ||
| self.queue.put((TopicAndPartition(topic, partition), m, key)) | ||
| try: | ||
| item = (TopicAndPartition(topic, partition), m, key) | ||
| self.queue.put_nowait(item) | ||
| except Full: | ||
| raise BatchQueueOverfilledError( | ||
| 'Producer batch send queue overfilled. ' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/batch/async/ |
||
| 'Current queue size %d.' % self.queue.qsize()) | ||
| resp = [] | ||
| else: | ||
| messages = create_message_set(msg, self.codec, key) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,10 @@ | |
|
|
||
| import logging | ||
|
|
||
| from mock import MagicMock | ||
| from mock import MagicMock, patch | ||
| from . import unittest | ||
|
|
||
| from kafka.common import BatchQueueOverfilledError | ||
| from kafka.producer.base import Producer | ||
|
|
||
| class TestKafkaProducer(unittest.TestCase): | ||
|
|
@@ -25,3 +26,16 @@ def test_producer_message_types(self): | |
| # This should not raise an exception | ||
| producer.send_messages(topic, partition, m) | ||
|
|
||
| @patch('kafka.producer.base.Process') | ||
| def test_producer_batch_send_queue_overfilled(self, process_mock): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should also test |
||
| queue_size = 2 | ||
| producer = Producer(MagicMock(), batch_send=True, | ||
| batch_send_queue_maxsize=queue_size) | ||
|
|
||
| topic = b'test-topic' | ||
| partition = 0 | ||
|
|
||
| message = b'test-message' | ||
| with self.assertRaises(BatchQueueOverfilledError): | ||
| message_list = [message] * (queue_size + 1) | ||
| producer.send_messages(topic, partition, *message_list) | ||
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.
How about
AsyncProducerQueueFull