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

support for open ended ranges #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 6 additions & 7 deletions kafka_replayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ def _binary_search(self, consumer, tp, start, end, target_time):
return self._binary_search(consumer, tp, start, insertion_point, target_time)
return start

def replay(self, start_time, end_time):
def replay(self, start_time, end_time=None):
"""Replay all specified partitions over the specified time range (inclusive).

Args:
start_time: The start timestamp in milliseconds
end_time: The end timestamp in milliseconds
end_time: The end timestamp in milliseconds or None for open ended range

Yields:
The next ConsumerRecord found within the given time range
Expand All @@ -116,11 +116,11 @@ def replay(self, start_time, end_time):
"""
if start_time < 0:
raise ValueError('start_time must be non-negative')
if end_time < 0:
if end_time and end_time < 0:
raise ValueError('end_time must be non-negative')
if start_time > self._get_time_millis():
raise ValueError('start_time must not be in the future')
if start_time > end_time:
if end_time and start_time > end_time:
raise ValueError('end_time must be at least start_time')
count = 0
last_timestamp = 0
Expand All @@ -140,7 +140,7 @@ def replay(self, start_time, end_time):
partitions = set()
else:
last_timestamp = record.timestamp
if last_timestamp > end_time:
if end_time and last_timestamp > end_time:
# Since partitions are ordered, if we see a too-new timestamp, mark the
# partition complete.
partitions.discard(record.partition)
Expand All @@ -149,7 +149,7 @@ def replay(self, start_time, end_time):
consumer.pause(tp)
self._logger.debug('Completed partition {0}'.format(tp))
elif (record.partition in partitions and last_timestamp >= start_time
and last_timestamp <= end_time):
and (not end_time or last_timestamp <= end_time)):
# Send the record to the client if it's within the specified time range
yield record
count += 1
Expand All @@ -162,4 +162,3 @@ def replay(self, start_time, end_time):
self._logger.info('Processed {0} offsets, last timestamp: {1}'.format(
count, last_timestamp))
consumer.close()