Skip to content
Open
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
37 changes: 25 additions & 12 deletions faust/transport/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ async def perform_seek(self) -> None:
_committed_offsets = await self.seek_to_committed()
read_offset.update(
{
tp: offset if offset is not None and offset >= 0 else None
tp: self._read_offset_for_position(offset)
for tp, offset in _committed_offsets.items()
}
)
Expand All @@ -590,7 +590,18 @@ async def seek(self, partition: TP, offset: int) -> None:
# reset livelock detection
await self._seek(partition, offset)
# set new read offset so we will reread messages
self._read_offset[ensure_TP(partition)] = offset if offset else None
self._read_offset[ensure_TP(partition)] = self._read_offset_for_position(offset)

@staticmethod
def _read_offset_for_position(offset: Optional[int]) -> Optional[int]:
# ``_read_offset`` is the offset of the last message read, while a
# seek position (and a committed offset) is the offset of the *next*
# message to read. Store ``position - 1`` so that gap detection in
# ``_drain_messages`` also notices a missing message right at the
# position (e.g. a transaction marker at the committed offset).
if offset is None or offset <= 0:
return None
return offset - 1

@abc.abstractmethod
async def _seek(self, partition: TP, offset: int) -> None: ...
Expand Down Expand Up @@ -1128,17 +1139,17 @@ def _new_offset(self, tp: TP) -> Optional[int]:
gap_for_tp.chop(0, new_max_offset)
acked.sort()

# We iterate over it until we handle gap in the head of acked queue
# then return the previous committed offset.
# The committed offset is the offset of the next message to
# process, so everything from it up to the first acked offset
# must be acked (or known to be missing) before we can move on.
# For example if acked[tp] is:
# 34 35 36 37
# ^-- gap
# self._committed_offset[tp] is 31
# the return value will be None (the same as 31)
# ^-- gap: 32 and 33 are still being processed
# self._committed_offset[tp] is 32
# the return value will be None (nothing new to commit).
committed_offset = self._committed_offset[tp]
if committed_offset:
if min(acked) - committed_offset > 1:
return None
if committed_offset is not None and acked[0] > committed_offset:
return None

# Note: acked is always kept sorted.
# find first list of consecutive numbers
Expand All @@ -1158,7 +1169,9 @@ async def _add_gap(self, tp: TP, offset_from: int, offset_to: int) -> None:
committed = self._committed_offset[tp]
gap_for_tp = self._gap[tp]
if committed is not None:
offset_from = max(offset_from, committed + 1)
# committed is the next offset to process, so it may itself
# be missing.
offset_from = max(offset_from, committed)
# intervaltree intervals exclude the end
if offset_from <= offset_to:
gap_for_tp.addi(offset_from, offset_to + 1)
Expand Down Expand Up @@ -1209,7 +1222,7 @@ async def _drain_messages(self, fetcher: ServiceT) -> None: # pragma: no cover
if r_offset is None or offset >= r_offset:
gap = offset - (r_offset or 0)
# We have a gap in income messages
if gap > 1 and r_offset:
if gap > 1 and r_offset is not None:
acks_enabled = acks_enabled_for(message.topic)
if acks_enabled:
await self._add_gap(tp, r_offset + 1, offset)
Expand Down
5 changes: 3 additions & 2 deletions faust/transport/drivers/aiokafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,9 @@ async def _seek_wait(
for tp, offset in partitions.items():
self.log.dev("SEEK %r -> %r", tp, offset)
consumer.seek(tp, offset)
if offset > 0:
self.consumer._read_offset[tp] = offset
read_offset = self.consumer._read_offset_for_position(offset)
if read_offset is not None:
self.consumer._read_offset[tp] = read_offset
elif tp in self.consumer._read_offset.keys():
del self.consumer._read_offset[tp]
await asyncio.wait_for(
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/transport/drivers/test_aiokafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ async def test__seek_wait(self, *, cthread, _consumer):

await cthread._seek_wait(_consumer, partitions)

assert cthread.consumer._read_offset[TP2] == 3
assert cthread.consumer._read_offset[TP2] == 2
assert TP1 not in cthread.consumer._read_offset

_consumer.position.assert_has_calls(
Expand Down
53 changes: 49 additions & 4 deletions tests/unit/transport/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,9 @@ async def test_perform_seek(self, *, consumer):

await consumer.perform_seek()

assert consumer._read_offset[TP1] == 4001
assert consumer._read_offset[TP2] == 0
# read offset is the last offset read, one before the position
assert consumer._read_offset[TP1] == 4000
assert consumer._read_offset[TP2] is None

assert consumer._committed_offset[TP1] == 4001
assert consumer._committed_offset[TP2] is None
Expand All @@ -653,7 +654,7 @@ async def test_seek(self, *, consumer):

await consumer.seek(TP1, 401)

assert consumer._read_offset[TP1] == 401
assert consumer._read_offset[TP1] == 400
consumer._seek.assert_called_once_with(TP1, 401)

def test_stop_flow(self, *, consumer):
Expand Down Expand Up @@ -1093,7 +1094,10 @@ def test_new_offset(self, tp, acked, expected_offset, expected_acked, *, consume
11,
),
(TP1, [3, 4], IntervalTree(), None),
(TP1, [3, 4], IntervalTree([Interval(2, 3)]), 5),
# offset 1 (the committed offset) is still being processed.
(TP1, [3, 4], IntervalTree([Interval(2, 3)]), None),
(TP1, [3, 4], IntervalTree([Interval(1, 3)]), 5),
(TP1, [2, 3, 4], IntervalTree(), None),
],
)
def test_new_offset_with_gaps(self, tp, acked, gaps, expected_offset, *, consumer):
Expand All @@ -1102,6 +1106,37 @@ def test_new_offset_with_gaps(self, tp, acked, gaps, expected_offset, *, consume
consumer._gap[tp] = gaps
assert consumer._new_offset(tp) == expected_offset

def test_new_offset__does_not_skip_unacked_committed_offset(self, *, consumer):
# Regression test for #606: with concurrency > 1 the message at
# the committed offset may still be processing while later ones
# are acked. Committing past it would lose the message on crash.
tp = TP1
consumer._committed_offset[tp] = 2585
consumer._acked[tp] = [2586, 2587, 2588]
consumer._acked_index[tp] = {2586, 2587, 2588}

assert consumer._new_offset(tp) is None
assert consumer._acked[tp] == [2586, 2587, 2588]

# once the slow message is acked everything can be committed.
consumer._acked[tp].append(2585)
consumer._acked_index[tp].add(2585)
assert consumer._new_offset(tp) == 2589

def test_new_offset__committed_offset_zero(self, *, consumer):
tp = TP1
consumer._committed_offset[tp] = 0
consumer._acked[tp] = [1, 2]
assert consumer._new_offset(tp) is None

def test_new_offset__committed_offset_missing(self, *, consumer):
# e.g. a transaction marker sits at the committed offset.
tp = TP1
consumer._committed_offset[tp] = 10
consumer._acked[tp] = [11, 12]
consumer._gap[tp] = IntervalTree([Interval(10, 11)])
assert consumer._new_offset(tp) == 13

@pytest.mark.asyncio
async def test_on_task_error(self, *, consumer):
consumer.commit = AsyncMock(name="commit")
Expand All @@ -1116,6 +1151,16 @@ async def test__add_gap(self, *, consumer):

assert consumer._gap[tp] == IntervalTree([Interval(300, 344)])

@pytest.mark.asyncio
async def test__add_gap__includes_committed_offset(self, *, consumer):
# The committed offset is the next offset to process, so it can
# itself be part of a gap.
tp = TP1
consumer._committed_offset[tp] = 300
await consumer._add_gap(TP1, 299, 343)

assert consumer._gap[tp] == IntervalTree([Interval(300, 344)])

@pytest.mark.asyncio
async def test__add_gap__previous_to_committed(self, *, consumer):
tp = TP1
Expand Down