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

Avoid consuming duplicate compressed messages from mid-batch #1367

Merged
merged 2 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions kafka/consumer/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,21 @@ def _parse_fetched_data(self, completed_fetch):

return parsed_records

class PartitionRecords(six.Iterator):
class PartitionRecords(object):
def __init__(self, fetch_offset, tp, messages):
self.fetch_offset = fetch_offset
self.topic_partition = tp
self.messages = messages
self.message_idx = 0
# When fetching an offset that is in the middle of a
# compressed batch, we will get all messages in the batch.
# But we want to start 'take' at the fetch_offset
for i, msg in enumerate(messages):
if msg.offset == fetch_offset:
self.message_idx = i
break
else:
self.message_idx = 0
self.messages = None

# For truthiness evaluation we need to define __len__ or __nonzero__
def __len__(self):
Expand Down
40 changes: 40 additions & 0 deletions test/test_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,43 @@ def test__parse_fetched_data__out_of_range(fetcher, topic, mocker):
partition_record = fetcher._parse_fetched_data(completed_fetch)
assert partition_record is None
assert fetcher._subscriptions.assignment[tp].awaiting_reset is True


def test_partition_records_offset():
"""Test that compressed messagesets are handle correctly
when fetch offset is in the middle of the message list
"""
batch_start = 120
batch_end = 130
fetch_offset = 123
tp = TopicPartition('foo', 0)
messages = [ConsumerRecord(tp.topic, tp.partition, i,
None, None, 'key', 'value', 'checksum', 0, 0)
for i in range(batch_start, batch_end)]
records = Fetcher.PartitionRecords(fetch_offset, None, messages)
assert len(records) > 0
msgs = records.take(1)
assert msgs[0].offset == 123
assert records.fetch_offset == 124
msgs = records.take(2)
assert len(msgs) == 2
assert len(records) > 0
records.discard()
assert len(records) == 0


def test_partition_records_empty():
records = Fetcher.PartitionRecords(0, None, [])
assert len(records) == 0


def test_partition_records_no_fetch_offset():
batch_start = 0
batch_end = 100
fetch_offset = 123
tp = TopicPartition('foo', 0)
messages = [ConsumerRecord(tp.topic, tp.partition, i,
None, None, 'key', 'value', 'checksum', 0, 0)
for i in range(batch_start, batch_end)]
records = Fetcher.PartitionRecords(fetch_offset, None, messages)
assert len(records) == 0