-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Handle New Topic Creation #174
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 all 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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import collections | ||
import copy | ||
import functools | ||
import itertools | ||
import logging | ||
import collections | ||
|
||
import time | ||
import kafka.common | ||
|
||
from functools import partial | ||
from itertools import count | ||
from kafka.common import (TopicAndPartition, | ||
ConnectionError, FailedPayloadsError, | ||
PartitionUnavailableError, | ||
|
@@ -21,7 +21,7 @@ | |
class KafkaClient(object): | ||
|
||
CLIENT_ID = "kafka-python" | ||
ID_GEN = count() | ||
ID_GEN = itertools.count() | ||
|
||
# NOTE: The timeout given to the client should always be greater than the | ||
# one passed to SimpleConsumer.get_message(), otherwise you can get a | ||
|
@@ -213,6 +213,16 @@ def reset_all_metadata(self): | |
def has_metadata_for_topic(self, topic): | ||
return topic in self.topic_partitions | ||
|
||
def ensure_topic_exists(self, topic, timeout = 30): | ||
start_time = time.time() | ||
|
||
self.load_metadata_for_topics(topic) | ||
while not self.has_metadata_for_topic(topic): | ||
if time.time() > start_time + timeout: | ||
raise KafkaTimeoutError("Unable to create topic {}".format(topic)) | ||
self.load_metadata_for_topics(topic) | ||
time.sleep(.5) | ||
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. sleep time should probably also be a param that users can override 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. That's a great idea. I'll get back to this based on the style review in the other conversation as soon as the world isn't literally blowing up at work. |
||
|
||
def close(self): | ||
for conn in self.conns.values(): | ||
conn.close() | ||
|
@@ -289,7 +299,7 @@ def send_produce_request(self, payloads=[], acks=1, timeout=1000, | |
order of input payloads | ||
""" | ||
|
||
encoder = partial( | ||
encoder = functools.partial( | ||
KafkaProtocol.encode_produce_request, | ||
acks=acks, | ||
timeout=timeout) | ||
|
@@ -321,7 +331,7 @@ def send_fetch_request(self, payloads=[], fail_on_error=True, | |
to the same brokers. | ||
""" | ||
|
||
encoder = partial(KafkaProtocol.encode_fetch_request, | ||
encoder = functools.partial(KafkaProtocol.encode_fetch_request, | ||
max_wait_time=max_wait_time, | ||
min_bytes=min_bytes) | ||
|
||
|
@@ -359,7 +369,7 @@ def send_offset_request(self, payloads=[], fail_on_error=True, | |
|
||
def send_offset_commit_request(self, group, payloads=[], | ||
fail_on_error=True, callback=None): | ||
encoder = partial(KafkaProtocol.encode_offset_commit_request, | ||
encoder = functools.partial(KafkaProtocol.encode_offset_commit_request, | ||
group=group) | ||
decoder = KafkaProtocol.decode_offset_commit_response | ||
resps = self._send_broker_aware_request(payloads, encoder, decoder) | ||
|
@@ -378,7 +388,7 @@ def send_offset_commit_request(self, group, payloads=[], | |
def send_offset_fetch_request(self, group, payloads=[], | ||
fail_on_error=True, callback=None): | ||
|
||
encoder = partial(KafkaProtocol.encode_offset_fetch_request, | ||
encoder = functools.partial(KafkaProtocol.encode_offset_fetch_request, | ||
group=group) | ||
decoder = KafkaProtocol.decode_offset_fetch_response | ||
resps = self._send_broker_aware_request(payloads, encoder, decoder) | ||
|
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.
Where is KafkaTimeoutError defined?