Skip to content

Commit a7256eb

Browse files
ekerstensEric Kerstens
andauthored
Revert cancel getmany and add extra seek (#256)
* Revert cancel getmany and add extra seek * Revert test * Modifications for tests Co-authored-by: Eric Kerstens <ekerstens@expediagroup.com>
1 parent 84d58fd commit a7256eb

File tree

4 files changed

+27
-46
lines changed

4 files changed

+27
-46
lines changed

faust/app/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,8 +1681,6 @@ async def _on_partitions_revoked(self, revoked: Set[TP]) -> None:
16811681
T(self.flow_control.suspend)()
16821682
on_timeout.info("consumer.pause_partitions")
16831683
T(consumer.pause_partitions)(assignment)
1684-
on_timeout.info("consumer.wait_for_stopped_flow")
1685-
await T(consumer.wait_for_stopped_flow)()
16861684

16871685
# Every agent instance has an incoming buffer of messages
16881686
# (a asyncio.Queue) -- we clear those to make sure

faust/transport/consumer.py

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ class Consumer(Service, ConsumerT):
436436
flow_active: bool = True
437437
can_resume_flow: Event
438438
suspend_flow: Event
439-
not_waiting_next_records: Event
440439

441440
def __init__(
442441
self,
@@ -478,8 +477,6 @@ def __init__(
478477
self.randomly_assigned_topics = set()
479478
self.can_resume_flow = Event()
480479
self.suspend_flow = Event()
481-
self.not_waiting_next_records = Event()
482-
self.not_waiting_next_records.set()
483480
self._reset_state()
484481
super().__init__(loop=loop or self.transport.loop, **kwargs)
485482
self.transactions = self.transport.create_transaction_manager(
@@ -503,7 +500,6 @@ def _reset_state(self) -> None:
503500
self._buffered_partitions = set()
504501
self.can_resume_flow.clear()
505502
self.suspend_flow.clear()
506-
self.not_waiting_next_records.set()
507503
self.flow_active = True
508504
self._time_start = monotonic()
509505

@@ -585,11 +581,6 @@ def stop_flow(self) -> None:
585581
self.can_resume_flow.clear()
586582
self.suspend_flow.set()
587583

588-
async def wait_for_stopped_flow(self) -> None:
589-
"""Wait until the consumer is not waiting on any newly fetched records."""
590-
if not self.not_waiting_next_records.is_set():
591-
await self.not_waiting_next_records.wait()
592-
593584
def resume_flow(self) -> None:
594585
"""Allow consumer to process messages."""
595586
self.flow_active = True
@@ -742,48 +733,40 @@ async def getmany(self, timeout: float) -> AsyncIterator[Tuple[TP, Message]]:
742733
self.app.monitor.track_tp_end_offset(tp, highwater_mark)
743734
# convert timestamp to seconds from int milliseconds.
744735
yield tp, to_message(tp, record)
736+
else:
737+
self.log.dev(
738+
"getmany called while flow not active. Seek back to committed offsets."
739+
)
740+
await self.perform_seek()
745741

746742
async def _wait_next_records(
747743
self, timeout: float
748744
) -> Tuple[Optional[RecordMap], Optional[Set[TP]]]:
749745
if not self.flow_active:
750746
await self.wait(self.can_resume_flow)
747+
751748
# Implementation for the Fetcher service.
752-
try:
753-
self.not_waiting_next_records.clear()
749+
is_client_only = self.app.client_only
754750

755-
is_client_only = self.app.client_only
751+
active_partitions: Optional[Set[TP]]
752+
if is_client_only:
753+
active_partitions = None
754+
else:
755+
active_partitions = self._get_active_partitions()
756756

757-
active_partitions: Optional[Set[TP]]
758-
if is_client_only:
759-
active_partitions = None
760-
else:
761-
active_partitions = self._get_active_partitions()
762-
763-
records: RecordMap = {}
764-
if is_client_only or active_partitions:
765-
# Fetch records only if active partitions to avoid the risk of
766-
# fetching all partitions in the beginning when none of the
767-
# partitions is paused/resumed.
768-
suspend_flow = self.suspend_flow.wait()
769-
getmany = self._getmany(
770-
active_partitions=active_partitions,
771-
timeout=timeout,
772-
)
773-
wait_results = await self.wait_first(getmany, suspend_flow)
774-
for coro, result in zip(wait_results.done, wait_results.results):
775-
# Ignore records fetched while flow was suspended
776-
if coro is suspend_flow:
777-
records = {}
778-
break
779-
if coro is getmany:
780-
records = result
781-
else:
782-
# We should still release to the event loop
783-
await self.sleep(1)
784-
return records, active_partitions
785-
finally:
786-
self.not_waiting_next_records.set()
757+
records: RecordMap = {}
758+
if is_client_only or active_partitions:
759+
# Fetch records only if active partitions to avoid the risk of
760+
# fetching all partitions in the beginning when none of the
761+
# partitions is paused/resumed.
762+
records = await self._getmany(
763+
active_partitions=active_partitions,
764+
timeout=timeout,
765+
)
766+
else:
767+
# We should still release to the event loop
768+
await self.sleep(1)
769+
return records, active_partitions
787770

788771
@abc.abstractmethod
789772
def _to_message(self, tp: TP, record: Any) -> ConsumerMessage:

tests/unit/app/test_base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ async def test_on_partitions_revoked(self, *, app):
299299
transactions=Mock(
300300
on_partitions_revoked=AsyncMock(),
301301
),
302-
wait_for_stopped_flow=AsyncMock(),
303302
)
304303
app.tables = Mock()
305304
app.flow_control = Mock()

tests/unit/transport/test_consumer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def _to_message(self, *args, **kwargs):
386386
...
387387

388388
async def seek_to_committed(self, *args, **kwargs):
389-
...
389+
return {}
390390

391391
async def seek_wait(self, *args, **kwargs):
392392
...
@@ -544,6 +544,7 @@ def to_message(tp, record):
544544
assert not consumer.should_stop
545545
consumer.flow_active = False
546546
consumer.can_resume_flow.set()
547+
# Test is hanging here
547548
assert [a async for a in consumer.getmany(1.0)] == []
548549
assert not consumer.should_stop
549550
consumer.flow_active = True

0 commit comments

Comments
 (0)