Skip to content

Commit 31a29ec

Browse files
authored
KAFKA-2832: Add a consumer config option to exclude internal topics (#765)
Use exclude_internal_topics config in KafkaConsumer to avoid subscribe patterns matching internal topics Raise error during rebalance if subscribed topics are not authorized
1 parent 506d023 commit 31a29ec

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

kafka/consumer/group.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ class KafkaConsumer(six.Iterator):
176176
selector (selectors.BaseSelector): Provide a specific selector
177177
implementation to use for I/O multiplexing.
178178
Default: selectors.DefaultSelector
179+
exclude_internal_topics (bool): Whether records from internal topics
180+
(such as offsets) should be exposed to the consumer. If set to True
181+
the only way to receive records from an internal topic is
182+
subscribing to it. Requires 0.10+ Default: True
179183
180184
Note:
181185
Configuration parameters are described in more detail at
@@ -222,6 +226,7 @@ class KafkaConsumer(six.Iterator):
222226
'metrics_num_samples': 2,
223227
'metrics_sample_window_ms': 30000,
224228
'selector': selectors.DefaultSelector,
229+
'exclude_internal_topics': True,
225230
}
226231

227232
def __init__(self, *topics, **configs):

kafka/coordinator/consumer.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ConsumerCoordinator(BaseCoordinator):
3636
'heartbeat_interval_ms': 3000,
3737
'retry_backoff_ms': 100,
3838
'api_version': (0, 9),
39+
'exclude_internal_topics': True,
3940
}
4041

4142
def __init__(self, client, subscription, metrics, metric_group_prefix,
@@ -70,6 +71,10 @@ def __init__(self, client, subscription, metrics, metric_group_prefix,
7071
using Kafka's group managementment facilities. Default: 30000
7172
retry_backoff_ms (int): Milliseconds to backoff when retrying on
7273
errors. Default: 100.
74+
exclude_internal_topics (bool): Whether records from internal topics
75+
(such as offsets) should be exposed to the consumer. If set to
76+
True the only way to receive records from an internal topic is
77+
subscribing to it. Requires 0.10+. Default: True
7378
"""
7479
super(ConsumerCoordinator, self).__init__(client, **configs)
7580
self.config = copy.copy(self.DEFAULT_CONFIG)
@@ -131,13 +136,12 @@ def group_protocols(self):
131136

132137
def _handle_metadata_update(self, cluster):
133138
# if we encounter any unauthorized topics, raise an exception
134-
# TODO
135-
#if self._cluster.unauthorized_topics:
136-
# raise TopicAuthorizationError(self._cluster.unauthorized_topics)
139+
if cluster.unauthorized_topics:
140+
raise Errors.TopicAuthorizationFailedError(cluster.unauthorized_topics)
137141

138142
if self._subscription.subscribed_pattern:
139143
topics = []
140-
for topic in cluster.topics():
144+
for topic in cluster.topics(self.config['exclude_internal_topics']):
141145
if self._subscription.subscribed_pattern.match(topic):
142146
topics.append(topic)
143147

0 commit comments

Comments
 (0)