Skip to content

Commit

Permalink
fix(produce): Apply backpressure instead of crashing (#281)
Browse files Browse the repository at this point in the history
If we get local queue full, let's raise MessageRejected to slow down the consumer.
  • Loading branch information
lynnagara authored Aug 31, 2023
1 parent dd81bc9 commit 01f55cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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()

0 comments on commit 01f55cb

Please sign in to comment.