Skip to content
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
1 change: 1 addition & 0 deletions kafka_consumer/changelog.d/25485.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Look up a consumer group's coordinator again after a `NOT_COORDINATOR` or `COORDINATOR_NOT_AVAILABLE` error, so that with `close_admin_client: false` the group's offsets resume on the next check run after its coordinator moves to another broker.
17 changes: 16 additions & 1 deletion kafka_consumer/datadog_checks/kafka_consumer/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
# Licensed under a 3-clause BSD style license (see LICENSE)
from concurrent.futures import as_completed

from confluent_kafka import Consumer, ConsumerGroupTopicPartitions, IsolationLevel, KafkaException, TopicPartition
from confluent_kafka import (
Consumer,
ConsumerGroupTopicPartitions,
IsolationLevel,
KafkaError,
KafkaException,
TopicPartition,
)
from confluent_kafka.admin import AdminClient, OffsetSpec

# AWS MSK IAM authentication support
Expand Down Expand Up @@ -278,11 +285,14 @@ def list_consumer_group_offsets(self, groups):
)[consumer_group]
)
offsets = []
coordinator_error = False
for completed in as_completed(futures):
try:
response_offset_info = completed.result()
except KafkaException as e:
self.log.debug("Failed to read consumer offsets for future %s: %s", completed, e)
if e.args[0].code() in (KafkaError.NOT_COORDINATOR, KafkaError.COORDINATOR_NOT_AVAILABLE):
coordinator_error = True
continue
tpo = []
for tp in response_offset_info.topic_partitions:
Expand All @@ -296,6 +306,11 @@ def list_consumer_group_offsets(self, groups):
continue
tpo.append((tp.topic, tp.partition, tp.offset))
offsets.append((response_offset_info.group_id, tpo))
if coordinator_error:
# The AdminClient caches each group's coordinator and keeps using it after a coordinator error,
# so a group that moved to another broker would fail on every run. A new client looks it up again.
self.log.debug("Closing the AdminClient so the next request looks up group coordinators again")
self.close_admin_client()
return offsets

def describe_consumer_group(self, consumer_group):
Expand Down
33 changes: 33 additions & 0 deletions kafka_consumer/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import logging
import marshal
from collections import defaultdict
from concurrent.futures import Future
from contextlib import nullcontext as does_not_raise

import mock
import pytest
from confluent_kafka import ConsumerGroupTopicPartitions, KafkaError, KafkaException, TopicPartition

from datadog_checks.base import ConfigurationError
from datadog_checks.kafka_consumer import KafkaCheck
Expand Down Expand Up @@ -1362,3 +1364,34 @@ def test_consumer_groups_regex_prefilter_disabled_when_monitoring_unlisted_group
('staging-orders', None),
]
)


@pytest.mark.parametrize(
'error_code',
[
pytest.param(KafkaError.NOT_COORDINATOR, id='not coordinator'),
pytest.param(KafkaError.COORDINATOR_NOT_AVAILABLE, id='coordinator not available'),
],
)
def test_list_consumer_group_offsets_given_coordinator_error_returns_offsets_on_next_call(error_code):
"""After a group coordinator error, the next call looks the coordinator up again and returns the offsets."""
stale_offsets = Future()
stale_offsets.set_exception(KafkaException(KafkaError(error_code)))
fresh_offsets = Future()
fresh_offsets.set_result(ConsumerGroupTopicPartitions('consumer_group1', [TopicPartition('topic1', 0, 5)]))
# AdminClient is the network boundary. A broker can't be made to hand a group to another coordinator on
# demand, and librdkafka keeps sending to the coordinator it cached, so only a new AdminClient finds the new one.
stale_admin_client = mock.MagicMock()
stale_admin_client.list_consumer_group_offsets.return_value = {'consumer_group1': stale_offsets}
fresh_admin_client = mock.MagicMock()
fresh_admin_client.list_consumer_group_offsets.return_value = {'consumer_group1': fresh_offsets}
client = KafkaClient(mock.MagicMock(), logging.getLogger(__name__))
groups = [('consumer_group1', [('topic1', 0)])]

with mock.patch(
'datadog_checks.kafka_consumer.client.AdminClient', side_effect=[stale_admin_client, fresh_admin_client]
):
client.list_consumer_group_offsets(groups)
offsets = client.list_consumer_group_offsets(groups)

assert offsets == [('consumer_group1', [('topic1', 0, 5)])]
Loading