|
3 | 3 |
|
4 | 4 | import pytest
|
5 | 5 |
|
| 6 | +import itertools |
| 7 | +from collections import OrderedDict |
| 8 | + |
6 | 9 | from kafka.client_async import KafkaClient
|
7 |
| -from kafka.consumer.fetcher import Fetcher |
| 10 | +from kafka.consumer.fetcher import Fetcher, NoOffsetForPartitionError |
8 | 11 | from kafka.consumer.subscription_state import SubscriptionState
|
9 | 12 | from kafka.metrics import Metrics
|
10 | 13 | from kafka.protocol.fetch import FetchRequest
|
| 14 | +from kafka.protocol.offset import OffsetResponse |
11 | 15 | from kafka.structs import TopicPartition
|
| 16 | +from kafka.future import Future |
| 17 | +from kafka.errors import ( |
| 18 | + StaleMetadata, LeaderNotAvailableError, NotLeaderForPartitionError, |
| 19 | + UnknownTopicOrPartitionError |
| 20 | +) |
12 | 21 |
|
13 | 22 |
|
14 | 23 | @pytest.fixture
|
@@ -101,3 +110,175 @@ def test_update_fetch_positions(fetcher, mocker):
|
101 | 110 | fetcher.update_fetch_positions([partition])
|
102 | 111 | assert fetcher._reset_offset.call_count == 0
|
103 | 112 | fetcher._subscriptions.seek.assert_called_with(partition, 123)
|
| 113 | + |
| 114 | + |
| 115 | +def test__reset_offset(fetcher, mocker): |
| 116 | + tp = TopicPartition("topic", 0) |
| 117 | + fetcher._subscriptions.subscribe(topics="topic") |
| 118 | + fetcher._subscriptions.assign_from_subscribed([tp]) |
| 119 | + fetcher._subscriptions.need_offset_reset(tp) |
| 120 | + mocked = mocker.patch.object(fetcher, '_retrieve_offsets') |
| 121 | + |
| 122 | + mocked.return_value = {} |
| 123 | + with pytest.raises(NoOffsetForPartitionError): |
| 124 | + fetcher._reset_offset(tp) |
| 125 | + |
| 126 | + mocked.return_value = {tp: (1001, None)} |
| 127 | + fetcher._reset_offset(tp) |
| 128 | + assert not fetcher._subscriptions.assignment[tp].awaiting_reset |
| 129 | + assert fetcher._subscriptions.assignment[tp].position == 1001 |
| 130 | + |
| 131 | + |
| 132 | +def test__send_offset_requests(fetcher, mocker): |
| 133 | + tp = TopicPartition("topic_send_offset", 1) |
| 134 | + mocked_send = mocker.patch.object(fetcher, "_send_offset_request") |
| 135 | + send_futures = [] |
| 136 | + |
| 137 | + def send_side_effect(*args, **kw): |
| 138 | + f = Future() |
| 139 | + send_futures.append(f) |
| 140 | + return f |
| 141 | + mocked_send.side_effect = send_side_effect |
| 142 | + |
| 143 | + mocked_leader = mocker.patch.object( |
| 144 | + fetcher._client.cluster, "leader_for_partition") |
| 145 | + # First we report unavailable leader 2 times different ways and later |
| 146 | + # always as available |
| 147 | + mocked_leader.side_effect = itertools.chain( |
| 148 | + [None, -1], itertools.cycle([0])) |
| 149 | + |
| 150 | + # Leader == None |
| 151 | + fut = fetcher._send_offset_requests({tp: 0}) |
| 152 | + assert fut.failed() |
| 153 | + assert isinstance(fut.exception, StaleMetadata) |
| 154 | + assert not mocked_send.called |
| 155 | + |
| 156 | + # Leader == -1 |
| 157 | + fut = fetcher._send_offset_requests({tp: 0}) |
| 158 | + assert fut.failed() |
| 159 | + assert isinstance(fut.exception, LeaderNotAvailableError) |
| 160 | + assert not mocked_send.called |
| 161 | + |
| 162 | + # Leader == 0, send failed |
| 163 | + fut = fetcher._send_offset_requests({tp: 0}) |
| 164 | + assert not fut.is_done |
| 165 | + assert mocked_send.called |
| 166 | + # Check that we bound the futures correctly to chain failure |
| 167 | + send_futures.pop().failure(NotLeaderForPartitionError(tp)) |
| 168 | + assert fut.failed() |
| 169 | + assert isinstance(fut.exception, NotLeaderForPartitionError) |
| 170 | + |
| 171 | + # Leader == 0, send success |
| 172 | + fut = fetcher._send_offset_requests({tp: 0}) |
| 173 | + assert not fut.is_done |
| 174 | + assert mocked_send.called |
| 175 | + # Check that we bound the futures correctly to chain success |
| 176 | + send_futures.pop().success({tp: (10, 10000)}) |
| 177 | + assert fut.succeeded() |
| 178 | + assert fut.value == {tp: (10, 10000)} |
| 179 | + |
| 180 | + |
| 181 | +def test__send_offset_requests_multiple_nodes(fetcher, mocker): |
| 182 | + tp1 = TopicPartition("topic_send_offset", 1) |
| 183 | + tp2 = TopicPartition("topic_send_offset", 2) |
| 184 | + tp3 = TopicPartition("topic_send_offset", 3) |
| 185 | + tp4 = TopicPartition("topic_send_offset", 4) |
| 186 | + mocked_send = mocker.patch.object(fetcher, "_send_offset_request") |
| 187 | + send_futures = [] |
| 188 | + |
| 189 | + def send_side_effect(node_id, timestamps): |
| 190 | + f = Future() |
| 191 | + send_futures.append((node_id, timestamps, f)) |
| 192 | + return f |
| 193 | + mocked_send.side_effect = send_side_effect |
| 194 | + |
| 195 | + mocked_leader = mocker.patch.object( |
| 196 | + fetcher._client.cluster, "leader_for_partition") |
| 197 | + mocked_leader.side_effect = itertools.cycle([0, 1]) |
| 198 | + |
| 199 | + # -- All node succeeded case |
| 200 | + tss = OrderedDict([(tp1, 0), (tp2, 0), (tp3, 0), (tp4, 0)]) |
| 201 | + fut = fetcher._send_offset_requests(tss) |
| 202 | + assert not fut.is_done |
| 203 | + assert mocked_send.call_count == 2 |
| 204 | + |
| 205 | + req_by_node = {} |
| 206 | + second_future = None |
| 207 | + for node, timestamps, f in send_futures: |
| 208 | + req_by_node[node] = timestamps |
| 209 | + if node == 0: |
| 210 | + # Say tp3 does not have any messages so it's missing |
| 211 | + f.success({tp1: (11, 1001)}) |
| 212 | + else: |
| 213 | + second_future = f |
| 214 | + assert req_by_node == { |
| 215 | + 0: {tp1: 0, tp3: 0}, |
| 216 | + 1: {tp2: 0, tp4: 0} |
| 217 | + } |
| 218 | + |
| 219 | + # We only resolved 1 future so far, so result future is not yet ready |
| 220 | + assert not fut.is_done |
| 221 | + second_future.success({tp2: (12, 1002), tp4: (14, 1004)}) |
| 222 | + assert fut.succeeded() |
| 223 | + assert fut.value == {tp1: (11, 1001), tp2: (12, 1002), tp4: (14, 1004)} |
| 224 | + |
| 225 | + # -- First succeeded second not |
| 226 | + del send_futures[:] |
| 227 | + fut = fetcher._send_offset_requests(tss) |
| 228 | + assert len(send_futures) == 2 |
| 229 | + send_futures[0][2].success({tp1: (11, 1001)}) |
| 230 | + send_futures[1][2].failure(UnknownTopicOrPartitionError(tp1)) |
| 231 | + assert fut.failed() |
| 232 | + assert isinstance(fut.exception, UnknownTopicOrPartitionError) |
| 233 | + |
| 234 | + # -- First fails second succeeded |
| 235 | + del send_futures[:] |
| 236 | + fut = fetcher._send_offset_requests(tss) |
| 237 | + assert len(send_futures) == 2 |
| 238 | + send_futures[0][2].failure(UnknownTopicOrPartitionError(tp1)) |
| 239 | + send_futures[1][2].success({tp1: (11, 1001)}) |
| 240 | + assert fut.failed() |
| 241 | + assert isinstance(fut.exception, UnknownTopicOrPartitionError) |
| 242 | + |
| 243 | + |
| 244 | +def test__handle_offset_response(fetcher, mocker): |
| 245 | + # Broker returns UnsupportedForMessageFormatError, will omit partition |
| 246 | + fut = Future() |
| 247 | + res = OffsetResponse[1]([ |
| 248 | + ("topic", [(0, 43, -1, -1)]), |
| 249 | + ("topic", [(1, 0, 1000, 9999)]) |
| 250 | + ]) |
| 251 | + fetcher._handle_offset_response(fut, res) |
| 252 | + assert fut.succeeded() |
| 253 | + assert fut.value == {TopicPartition("topic", 1): (9999, 1000)} |
| 254 | + |
| 255 | + # Broker returns NotLeaderForPartitionError |
| 256 | + fut = Future() |
| 257 | + res = OffsetResponse[1]([ |
| 258 | + ("topic", [(0, 6, -1, -1)]), |
| 259 | + ]) |
| 260 | + fetcher._handle_offset_response(fut, res) |
| 261 | + assert fut.failed() |
| 262 | + assert isinstance(fut.exception, NotLeaderForPartitionError) |
| 263 | + |
| 264 | + # Broker returns UnknownTopicOrPartitionError |
| 265 | + fut = Future() |
| 266 | + res = OffsetResponse[1]([ |
| 267 | + ("topic", [(0, 3, -1, -1)]), |
| 268 | + ]) |
| 269 | + fetcher._handle_offset_response(fut, res) |
| 270 | + assert fut.failed() |
| 271 | + assert isinstance(fut.exception, UnknownTopicOrPartitionError) |
| 272 | + |
| 273 | + # Broker returns many errors and 1 result |
| 274 | + # Will fail on 1st error and return |
| 275 | + fut = Future() |
| 276 | + res = OffsetResponse[1]([ |
| 277 | + ("topic", [(0, 43, -1, -1)]), |
| 278 | + ("topic", [(1, 6, -1, -1)]), |
| 279 | + ("topic", [(2, 3, -1, -1)]), |
| 280 | + ("topic", [(3, 0, 1000, 9999)]) |
| 281 | + ]) |
| 282 | + fetcher._handle_offset_response(fut, res) |
| 283 | + assert fut.failed() |
| 284 | + assert isinstance(fut.exception, NotLeaderForPartitionError) |
0 commit comments