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

fix(produce): Apply backpressure instead of crashing #281

Merged
merged 2 commits into from
Aug 31, 2023
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
6 changes: 5 additions & 1 deletion arroyo/processing/strategies/produce.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def submit(
future: Optional[Future[BrokerValue[TStrategyPayload]]] = None

if not isinstance(message.payload, FilteredPayload):
future = self.__producer.produce(self.__topic, message.payload)
try:
future = self.__producer.produce(self.__topic, message.payload)
except BufferError as exc:
logger.exception(exc)
raise MessageRejected from exc

self.__queue.append((message, future))

Expand Down
11 changes: 10 additions & 1 deletion tests/processing/strategies/test_produce.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from unittest import mock

import pytest

from arroyo.backends.kafka import KafkaPayload
from arroyo.backends.local.backend import LocalBroker
from arroyo.backends.local.storages.memory import MemoryMessageStorage
from arroyo.processing.strategies.abstract import MessageRejected
from arroyo.processing.strategies.produce import Produce
from arroyo.types import Message, Partition, Topic, Value
from arroyo.utils.clock import TestingClock
Expand All @@ -19,7 +22,7 @@ def test_produce() -> None:
producer = broker.get_producer()
next_step = mock.Mock()

strategy = Produce(producer, result_topic, next_step)
strategy = Produce(producer, result_topic, next_step, 2)

value = b'{"something": "something"}'
data = KafkaPayload(None, value, [])
Expand All @@ -41,4 +44,10 @@ def test_produce() -> None:
strategy.poll()
assert next_step.submit.call_count == 2
assert next_step.poll.call_count == 2

# Backpressure if buffer size = 2 exceeded
with pytest.raises(MessageRejected):
for _ in range(3):
strategy.submit(message)

strategy.join()
Loading