Skip to content

Dont block pending FetchRequests when Metadata update requested #2576

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

Merged
merged 2 commits into from
Mar 28, 2025
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
31 changes: 23 additions & 8 deletions kafka/consumer/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def __init__(self, client, subscriptions, metrics, **configs):
self._sensors = FetchManagerMetrics(metrics, self.config['metric_group_prefix'])
self._isolation_level = READ_UNCOMMITTED
self._session_handlers = {}
self._nodes_with_pending_fetch_requests = set()

def send_fetches(self):
"""Send FetchRequests for all assigned partitions that do not already have
Expand All @@ -124,12 +125,12 @@ def send_fetches(self):
"""
futures = []
for node_id, (request, fetch_offsets) in six.iteritems(self._create_fetch_requests()):
if self._client.ready(node_id):
log.debug("Sending FetchRequest to node %s", node_id)
future = self._client.send(node_id, request, wakeup=False)
future.add_callback(self._handle_fetch_response, node_id, fetch_offsets, time.time())
future.add_errback(self._handle_fetch_error, node_id)
futures.append(future)
log.debug("Sending FetchRequest to node %s", node_id)
self._nodes_with_pending_fetch_requests.add(node_id)
future = self._client.send(node_id, request, wakeup=False)
future.add_callback(self._handle_fetch_response, node_id, fetch_offsets, time.time())
future.add_errback(self._handle_fetch_error, node_id)
futures.append(future)
self._fetch_futures.extend(futures)
self._clean_done_fetch_futures()
return futures
Expand Down Expand Up @@ -593,8 +594,20 @@ def _create_fetch_requests(self):
" Requesting metadata update", partition)
self._client.cluster.request_update()

elif self._client.in_flight_request_count(node_id) > 0:
log.log(0, "Skipping fetch for partition %s because there is an inflight request to node %s",
elif not self._client.connected(node_id) and self._client.connection_delay(node_id) > 0:
# If we try to send during the reconnect backoff window, then the request is just
# going to be failed anyway before being sent, so skip the send for now
log.log(0, "Skipping fetch for partition %s because node %s is awaiting reconnect backoff",
partition, node_id)

elif self._client.throttle_delay(node_id) > 0:
# If we try to send while throttled, then the request is just
# going to be failed anyway before being sent, so skip the send for now
log.log(0, "Skipping fetch for partition %s because node %s is throttled",
partition, node_id)

elif node_id in self._nodes_with_pending_fetch_requests:
log.log(0, "Skipping fetch for partition %s because there is a pending fetch request to node %s",
partition, node_id)
continue

Expand Down Expand Up @@ -707,12 +720,14 @@ def _handle_fetch_response(self, node_id, fetch_offsets, send_time, response):
self._completed_fetches.append(completed_fetch)

self._sensors.fetch_latency.record((time.time() - send_time) * 1000)
self._nodes_with_pending_fetch_requests.remove(node_id)

def _handle_fetch_error(self, node_id, exception):
level = logging.INFO if isinstance(exception, Errors.Cancelled) else logging.ERROR
log.log(level, 'Fetch to node %s failed: %s', node_id, exception)
if node_id in self._session_handlers:
self._session_handlers[node_id].handle_error(exception)
self._nodes_with_pending_fetch_requests.remove(node_id)

def _parse_fetched_data(self, completed_fetch):
tp = completed_fetch.topic_partition
Expand Down
2 changes: 2 additions & 0 deletions test/test_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ def test_fetched_records(fetcher, topic, mocker):
),
])
def test__handle_fetch_response(fetcher, fetch_offsets, fetch_response, num_partitions):
fetcher._nodes_with_pending_fetch_requests.add(0)
fetcher._handle_fetch_response(0, fetch_offsets, time.time(), fetch_response)
assert len(fetcher._completed_fetches) == num_partitions

Expand All @@ -438,6 +439,7 @@ def test__handle_fetch_response(fetcher, fetch_offsets, fetch_response, num_part
)
])
def test__handle_fetch_error(fetcher, caplog, exception, log_level):
fetcher._nodes_with_pending_fetch_requests.add(3)
fetcher._handle_fetch_error(3, exception)
assert len(caplog.records) == 1
assert caplog.records[0].levelname == logging.getLevelName(log_level)
Expand Down
Loading