Skip to content
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

Added max_bytes option and FetchRequest_v3 usage. #962

Merged
merged 4 commits into from Mar 6, 2017

Conversation

tvoinarovskyi
Copy link
Collaborator

@tvoinarovskyi tvoinarovskyi commented Jan 30, 2017

Also added a check for api_version=(0, 10, 1) and MetadataRequest_v2
KIP-74
NOTE: Depends on PR #974

@tvoinarovskyi
Copy link
Collaborator Author

Took me long enough =) Fixes #870

# they are requested, so to avoid starvation with
# `fetch_max_bytes` option we need this shuffle
partition_data = list(partition_data.items())
random.shuffle(partition_data)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A note to this shuffle. As python dicts and sets are not ordered, currently it will work properly without this shuffle (it will basically be shuffled by _create_fetch_requests, as it uses sets and dicts), but I still prefere it here, as:

  • Python3.6 has ordered dicts, which can make the distribution way less equal
  • While dicts are not retain insert order, they preserve key order by hashes, which can (probably) result in unequal consumption.

Please correct me if I'm wrong here. My tests run OK even without this shuffle, so I'm a bit concerned.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, Github swallowed my earlier comment.

+1 for explicitly shuffling to avoid py3.6 issues.

However, since the protocol explicitly responds in the order requested, maybe this should be exposed to the user? Default to shuffling the order to avoid starvation, but allow the user to override that to always request a particular order... I can see someone using this as a sort of poor-man's priority-queue. Not sure if this is a good idea, or if it'd require reworking a lot of of kafka-python internals... just a thought.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did have the same idea, but could not find any applications to this. And it's probably not this PR's responsibility, as it's rather a feature.

first message in the first non-empty partition of the fetch is
larger than this value, the message will still be returned to
ensure that the consumer can make progress. NOTE: consumer performs
multiple fetches in parallel so memory usage will be higher.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note is a great idea, but tweak the wording slightly? It's unclear 1) what the memory usage will be higher than , and 2) how much higher... are we talking 20%, 2x, 5x, etc?

@@ -47,6 +47,30 @@ class MetadataResponse_v1(Struct):
)


class MetadataResponse_v2(Struct):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be in a dedicated commit? It just feels logically separate, and I'm unclear why it's needed for the Fetch_Request/Response stuff. Personally prefer a commit that says "Here's this new struct, here's the JIRA ticket KAFKA-XXXX where it was added. Requires broker version x.xx.xx.x. Here's what it changes compared to the old one" just because often spelunking in code I'll lookup the commit history to understand changes like this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broken out in #974.

else:
# As of version == 3 partitions will be returned in order as
# they are requested, so to avoid starvation with
# `fetch_max_bytes` option we need this shuffle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add to the code comment something about Python 3.6 consistent ordering of dicts...

...this shuffle. Otherwise, in Python >= 3.6 dicts have consistent order, so can't guarantee hashing to dict/set will randomize order of partitions.

larger than this value, the message will still be returned to
ensure that the consumer can make progress. NOTE: consumer performs
fetches to multiple nodes in parallel so memory usage will depend
on the number of nodes containing partitions for the topic.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call them 'brokers' rather than 'nodes'?

@jeffwidman
Copy link
Collaborator

LGTM with the caveat that I didn't have time to try the code, just read through it.

Copy link
Owner

@dpkp dpkp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are failing on py2 for some reason -- can you investigate?

kafka/conn.py Outdated
@@ -778,7 +778,8 @@ def filter(self, record):
log.addFilter(log_filter)

test_cases = [
((0, 10), ApiVersionRequest[0]()),
((0, 10, 1), MetadataRequest[2]([])),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: for any broker after 0.10 we should just use the results of ApiVersions api request to set api version support.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I implement it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes. forgot about this little wart of kafka brokers. so it turns out that kafka < 0.10 will silently ignore incorrect metadata api version requests (or at least not close the socket as expected). The socket behavior is different between py2 and py3, causing KafkaConnection._recv() to block indefinitely on py2.

So we can't use MetadataRequest[2] to check version and will need to implement the smarter ApiVersion approach to avoid breaking auto version checks on older brokers.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test via KAFKA_VERSION=0.8.0 tox -e py27 -- test/test_consumer_group.py::test_paused -- this hangs indefinitely on my laptop

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will try it.

@@ -47,6 +47,30 @@ class MetadataResponse_v1(Struct):
)


class MetadataResponse_v2(Struct):
API_KEY = 3
API_VERSION = 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be API_VERSION = 2 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ou, yea =)

@@ -47,6 +47,30 @@ class MetadataResponse_v1(Struct):
)


class MetadataResponse_v2(Struct):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree -- would prefer to separate this into separate PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's separated, see #974

@tvoinarovskyi
Copy link
Collaborator Author

As for the failing tests, I do suspect that something is strange, as they fail quite strangely on py27 and py26 on KAFKA<0.10.1, but the fail message has no sense:

ERROR: InvocationError: '/home/travis/build/dpkp/kafka-python/.tox/py27/bin/py.test --pylint --pylint-rcfile=pylint.rc --pylint-error-types=EF --cov=kafka --cov-config=.covrc'

And the tests run OK on my machine, I have no clue.

@tvoinarovskyi
Copy link
Collaborator Author

Changed check_version to use ApiVersionResponse result. Works quite good for me.

@tvoinarovskyi
Copy link
Collaborator Author

@dpkp Is there anything else for this PR, that needs to be addressed?

@@ -830,6 +849,10 @@ def connect():
self._sock.setblocking(False)

if f.succeeded():
if version == (0, 10):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better would be if isinstance(request, ApiVersionRequest[0]):

@@ -752,6 +753,24 @@ def _next_correlation_id(self):
self._correlation_id = (self._correlation_id + 1) % 2**31
return self._correlation_id

def _check_version_above_0_10(self, response):
test_cases = [
# format (<broker verion>, <needed struct>)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might note that the order here matters, and/or make sure we reverse sort it before checking for best match below

@dpkp
Copy link
Owner

dpkp commented Mar 3, 2017

Looks great. I added two very minor points that we can fix later (or you can address now if you have time). I'll merge this before next release.

@dpkp dpkp merged commit 9c19ea7 into dpkp:master Mar 6, 2017
dpkp added a commit that referenced this pull request Mar 6, 2017
@tvoinarovskyi
Copy link
Collaborator Author

Thanks for the cleanup, looks good =) I missed it on the weekend

88manpreet pushed a commit to Yelp/kafka-python that referenced this pull request Apr 7, 2017
* Added `max_bytes` option and FetchRequest_v3 usage.
* Add checks for versions above 0.10 based on ApiVersionResponse
88manpreet pushed a commit to Yelp/kafka-python that referenced this pull request Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants