-
Notifications
You must be signed in to change notification settings - Fork 70
Topic Writer Backpressure #797
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
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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |||||||||||||||||||||||||||||||
| InternalMessage, | ||||||||||||||||||||||||||||||||
| TopicWriterStopped, | ||||||||||||||||||||||||||||||||
| TopicWriterError, | ||||||||||||||||||||||||||||||||
| TopicWriterBufferFullError, | ||||||||||||||||||||||||||||||||
| internal_message_size_bytes, | ||||||||||||||||||||||||||||||||
| messages_to_proto_requests, | ||||||||||||||||||||||||||||||||
| PublicWriteResult, | ||||||||||||||||||||||||||||||||
| PublicWriteResultTypes, | ||||||||||||||||||||||||||||||||
|
|
@@ -277,6 +279,8 @@ class WriterAsyncIOReconnector: | |||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||
| _stop_reason: asyncio.Future | ||||||||||||||||||||||||||||||||
| _init_info: Optional[PublicWriterInitInfo] | ||||||||||||||||||||||||||||||||
| _buffer_bytes: int | ||||||||||||||||||||||||||||||||
| _buffer_updated: asyncio.Event | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||
| self, driver: SupportedDriverType, settings: WriterSettings, tx: Optional["BaseQueryTxContext"] = None | ||||||||||||||||||||||||||||||||
|
|
@@ -317,6 +321,8 @@ def __init__( | |||||||||||||||||||||||||||||||
| self._messages = deque() | ||||||||||||||||||||||||||||||||
| self._messages_future = deque() | ||||||||||||||||||||||||||||||||
| self._new_messages = asyncio.Queue() | ||||||||||||||||||||||||||||||||
| self._buffer_bytes = 0 | ||||||||||||||||||||||||||||||||
| self._buffer_updated = asyncio.Event() | ||||||||||||||||||||||||||||||||
| self._stop_reason = self._loop.create_future() | ||||||||||||||||||||||||||||||||
| connection_task = asyncio.create_task(self._connection_loop()) | ||||||||||||||||||||||||||||||||
| connection_task.set_name("connection_loop") | ||||||||||||||||||||||||||||||||
|
|
@@ -371,7 +377,6 @@ async def wait_stop(self) -> BaseException: | |||||||||||||||||||||||||||||||
| return stop_reason | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| async def write_with_ack_future(self, messages: List[PublicMessage]) -> List[asyncio.Future]: | ||||||||||||||||||||||||||||||||
| # todo check internal buffer limit | ||||||||||||||||||||||||||||||||
| self._check_stop() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if self._settings.auto_seqno: | ||||||||||||||||||||||||||||||||
|
|
@@ -380,6 +385,29 @@ async def write_with_ack_future(self, messages: List[PublicMessage]) -> List[asy | |||||||||||||||||||||||||||||||
| internal_messages = self._prepare_internal_messages(messages) | ||||||||||||||||||||||||||||||||
| messages_future = [self._loop.create_future() for _ in internal_messages] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| max_buf = self._settings.max_buffer_size_bytes | ||||||||||||||||||||||||||||||||
| if max_buf is not None: | ||||||||||||||||||||||||||||||||
| new_bytes = sum(internal_message_size_bytes(m) for m in internal_messages) | ||||||||||||||||||||||||||||||||
| timeout_sec = self._settings.buffer_wait_timeout_sec | ||||||||||||||||||||||||||||||||
| deadline = self._loop.time() + timeout_sec | ||||||||||||||||||||||||||||||||
| while True: | ||||||||||||||||||||||||||||||||
| self._buffer_updated.clear() | ||||||||||||||||||||||||||||||||
| if self._buffer_bytes + new_bytes <= max_buf: | ||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||
| if self._loop.time() >= deadline: | ||||||||||||||||||||||||||||||||
| raise TopicWriterBufferFullError( | ||||||||||||||||||||||||||||||||
| "Topic writer buffer full: no free space within %.1f s (buffer=%d, need=%d, max=%d)" | ||||||||||||||||||||||||||||||||
| % (timeout_sec, self._buffer_bytes, new_bytes, max_buf) | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||
| await asyncio.wait_for( | ||||||||||||||||||||||||||||||||
| self._buffer_updated.wait(), | ||||||||||||||||||||||||||||||||
| timeout=min(0.5, max(0.01, deadline - self._loop.time())), | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| except asyncio.TimeoutError: | ||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||
|
Comment on lines
+392
to
+408
|
||||||||||||||||||||||||||||||||
| self._buffer_bytes += new_bytes | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
Comment on lines
+388
to
+410
|
||||||||||||||||||||||||||||||||
| self._messages_future.extend(messages_future) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if self._codec is not None and self._codec == PublicCodec.RAW: | ||||||||||||||||||||||||||||||||
|
|
@@ -629,6 +657,9 @@ async def _read_loop(self, writer: "WriterAsyncIOStream"): | |||||||||||||||||||||||||||||||
| def _handle_receive_ack(self, ack): | ||||||||||||||||||||||||||||||||
| current_message = self._messages.popleft() | ||||||||||||||||||||||||||||||||
| message_future = self._messages_future.popleft() | ||||||||||||||||||||||||||||||||
| if self._settings.max_buffer_size_bytes is not None: | ||||||||||||||||||||||||||||||||
| self._buffer_bytes = max(0, self._buffer_bytes - internal_message_size_bytes(current_message)) | ||||||||||||||||||||||||||||||||
| self._buffer_updated.set() | ||||||||||||||||||||||||||||||||
| if current_message.seq_no != ack.seq_no: | ||||||||||||||||||||||||||||||||
| raise TopicWriterError( | ||||||||||||||||||||||||||||||||
| "internal error - receive unexpected ack. Expected seqno: %s, received seqno: %s" | ||||||||||||||||||||||||||||||||
|
|
@@ -695,6 +726,7 @@ def _stop(self, reason: BaseException): | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for f in self._messages_future: | ||||||||||||||||||||||||||||||||
| f.set_exception(reason) | ||||||||||||||||||||||||||||||||
| f.exception() # mark as retrieved so asyncio does not log "Future exception was never retrieved" | ||||||||||||||||||||||||||||||||
|
Comment on lines
728
to
+729
|
||||||||||||||||||||||||||||||||
| f.set_exception(reason) | |
| f.exception() # mark as retrieved so asyncio does not log "Future exception was never retrieved" | |
| if not f.done(): | |
| try: | |
| f.set_exception(reason) | |
| except asyncio.InvalidStateError: | |
| # Future might have been completed or cancelled concurrently; ignore. | |
| pass | |
| try: | |
| # Mark exception as retrieved so asyncio does not log | |
| # "Future exception was never retrieved". | |
| f.exception() | |
| except asyncio.CancelledError: | |
| # It is valid for callers to cancel ack futures; ignore. | |
| pass |
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.
internal_message_size_bytes()currently useslen(msg.data)andlen(k)for metadata keys. This can drift from the actual bytes held in the internal queues becausemsg.datais later mutated during compression (_encode_data_inplace), andlen(k)counts characters not UTF-8 bytes. Consider accounting against a stable byte length (e.g.,msg.uncompressed_sizepluslen(k.encode('utf-8')) + len(v)), or store the accounted size on the message at enqueue time and subtract the same value on ack.