-
Notifications
You must be signed in to change notification settings - Fork 62
Ability to batch messages in topic reader #491
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 3 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 |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| import gzip | ||
| import typing | ||
| from asyncio import Task | ||
| from collections import deque | ||
| from collections import OrderedDict | ||
| from typing import Optional, Set, Dict, Union, Callable | ||
|
|
||
| import ydb | ||
|
|
@@ -264,7 +264,7 @@ class ReaderStream: | |
|
|
||
| _state_changed: asyncio.Event | ||
| _closed: bool | ||
| _message_batches: typing.Deque[datatypes.PublicBatch] | ||
| _message_batches: typing.Dict[int, datatypes.PublicBatch] | ||
| _first_error: asyncio.Future[YdbError] | ||
|
|
||
| _update_token_interval: Union[int, float] | ||
|
|
@@ -296,7 +296,7 @@ def __init__( | |
| self._closed = False | ||
| self._first_error = asyncio.get_running_loop().create_future() | ||
| self._batches_to_decode = asyncio.Queue() | ||
| self._message_batches = deque() | ||
| self._message_batches = OrderedDict() | ||
|
|
||
| self._update_token_interval = settings.update_token_interval | ||
| self._get_token_function = get_token_function | ||
|
|
@@ -359,29 +359,38 @@ async def wait_messages(self): | |
| await self._state_changed.wait() | ||
| self._state_changed.clear() | ||
|
|
||
| def _get_first_batch(self) -> typing.Tuple[int, datatypes.PublicBatch]: | ||
| first_id, batch = self._message_batches.popitem(last=False) | ||
|
||
| return first_id, batch | ||
|
|
||
| def receive_batch_nowait(self): | ||
| if self._get_first_error(): | ||
| raise self._get_first_error() | ||
|
|
||
| if not self._message_batches: | ||
| return None | ||
|
|
||
| batch = self._message_batches.popleft() | ||
| _, batch = self._get_first_batch() | ||
| self._buffer_release_bytes(batch._bytes_size) | ||
|
|
||
| return batch | ||
|
|
||
| def receive_message_nowait(self): | ||
| if self._get_first_error(): | ||
| raise self._get_first_error() | ||
|
|
||
| try: | ||
| batch = self._message_batches[0] | ||
| message = batch.pop_message() | ||
| except IndexError: | ||
| if not self._message_batches: | ||
| return None | ||
|
|
||
| if batch.empty(): | ||
| self.receive_batch_nowait() | ||
| part_sess_id, batch = self._get_first_batch() | ||
|
|
||
| message = batch.messages.pop(0) | ||
|
|
||
| if len(batch.messages) == 0: | ||
| self._buffer_release_bytes(batch._bytes_size) | ||
| else: | ||
| # TODO: we should somehow release bytes from single message as well | ||
| self._message_batches[part_sess_id] = batch | ||
|
|
||
| return message | ||
|
|
||
|
|
@@ -605,9 +614,18 @@ async def _decode_batches_loop(self): | |
| while True: | ||
| batch = await self._batches_to_decode.get() | ||
| await self._decode_batch_inplace(batch) | ||
| self._message_batches.append(batch) | ||
| self._add_batch_to_queue(batch) | ||
| self._state_changed.set() | ||
|
|
||
| def _add_batch_to_queue(self, batch: datatypes.PublicBatch): | ||
| part_sess_id = batch._partition_session.id | ||
| if part_sess_id in self._message_batches: | ||
| self._message_batches[part_sess_id].messages.extend(batch.messages) | ||
|
||
| self._message_batches[part_sess_id]._bytes_size += batch._bytes_size | ||
| return | ||
|
|
||
| self._message_batches[part_sess_id] = batch | ||
|
|
||
| async def _decode_batch_inplace(self, batch): | ||
| if batch._codec == Codec.CODEC_RAW: | ||
| return | ||
|
|
||
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.