-
Notifications
You must be signed in to change notification settings - Fork 50
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 data limit when reading a batch with TopicReaderSync #431
base: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -86,6 +86,57 @@ def async_wait_message(self) -> concurrent.futures.Future: | |
|
||
return self._caller.unsafe_call_with_future(self._async_reader.wait_message()) | ||
|
||
def _make_batch_slice( | ||
self, | ||
batch: Union[PublicBatch, None], | ||
max_messages: typing.Union[int, None] = None, | ||
max_bytes: typing.Union[int, None] = None, | ||
) -> Union[PublicBatch, None]: | ||
all_amount = float("inf") | ||
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. Why do you need all_amount as float const? 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. The rationale is that by default we have no limits on a data flow. |
||
|
||
# A non-empty batch must stay non-empty regardless of the max messages value | ||
if max_messages is not None: | ||
max_messages = max(max_messages, 1) | ||
else: | ||
max_messages = all_amount | ||
|
||
if max_bytes is not None: | ||
max_bytes = max(max_bytes, 1) | ||
else: | ||
max_bytes = all_amount | ||
|
||
is_batch_set = batch is not None | ||
is_msg_limit_set = max_messages < all_amount | ||
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. why do you need all_amount instead check 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. Because |
||
is_bytes_limit_set = max_bytes < all_amount | ||
is_limit_set = is_msg_limit_set or is_bytes_limit_set | ||
is_slice_required = is_batch_set and is_limit_set | ||
|
||
if not is_slice_required: | ||
return batch | ||
|
||
sliced_messages = [] | ||
bytes_taken = 0 | ||
|
||
for batch_message in batch.messages: | ||
sliced_messages.append(batch_message) | ||
bytes_taken += len(batch_message.data) | ||
|
||
is_enough_messages = len(sliced_messages) >= max_messages | ||
is_enough_bytes = bytes_taken >= max_bytes | ||
is_stop_required = is_enough_messages or is_enough_bytes | ||
|
||
if is_stop_required: | ||
break | ||
|
||
sliced_batch = PublicBatch( | ||
messages=sliced_messages, | ||
_partition_session=batch._partition_session, | ||
_bytes_size=bytes_taken, | ||
_codec=batch._codec, | ||
) | ||
|
||
return sliced_batch | ||
|
||
def receive_batch( | ||
self, | ||
*, | ||
|
@@ -102,11 +153,13 @@ def receive_batch( | |
""" | ||
self._check_closed() | ||
|
||
return self._caller.safe_call_with_result( | ||
maybe_batch: Union[PublicBatch, None] = self._caller.safe_call_with_result( | ||
self._async_reader.receive_batch(), | ||
timeout, | ||
) | ||
|
||
return self._make_batch_slice(maybe_batch, max_messages, max_bytes) | ||
|
||
def commit(self, mess: typing.Union[datatypes.PublicMessage, datatypes.PublicBatch]): | ||
""" | ||
Put commit message to internal buffer. | ||
|
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.
IMPORTANT
After applying the function, a caller will lose messages that have been trimmed from the batch and will not see these messages in the read session. A server does not allow to skip messages during commit. This can cause problems:
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.
Ok, I see, though I'm not quite sure I fully understand the path to a solution.
What was the expected approach to take?