Skip to content

Commit 20a7357

Browse files
committed
Add group coordinator lookup
We need a way to send a request to the group coordinator. I spent a day and a half trying to implement a `_send_request_to_group_coordinator()` that included: 1. caching the value of the group coordinator so that it wouldn't have to be repeatedly looked up on every call. This is particularly important because the `list_consumer_groups()`, `list_consumer_group_offsets()`, and `describe_consumer_groups()` will frequently be used by monitoring scripts. I know across the production clusters that I support, using a cached value will save ~1M calls per day. 2. clean and consistent error handling. This is difficult because the responses are inconsistent about error codes. Some have a top-level error code, some bury it within the description of the actual item. 3. Avoiding tight coupling between this method and the request/response classes... the custom parsing logic for errors etc, given that it's non-standard, should live in the callers, not here. So finally I gave up and just went with this simpler solution and made it so the callers can optionally bypass this if they somehow already know the group coordinator.
1 parent 4e77bdd commit 20a7357

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

kafka/admin/kafka.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from kafka.protocol.admin import (
1313
CreateTopicsRequest, DeleteTopicsRequest, DescribeConfigsRequest, AlterConfigsRequest, CreatePartitionsRequest,
1414
ListGroupsRequest, DescribeGroupsRequest)
15+
from kafka.protocol.commit import GroupCoordinatorRequest
1516
from kafka.protocol.metadata import MetadataRequest
1617
from kafka.version import __version__
1718

@@ -259,6 +260,44 @@ def _refresh_controller_id(self):
259260
"Kafka Admin interface cannot determine the controller using MetadataRequest_v{}."
260261
.format(version))
261262

263+
def _find_group_coordinator_id(self, group_id):
264+
"""Find the broker node_id of the coordinator of the given group.
265+
266+
Sends a FindCoordinatorRequest message to the cluster. Will block until
267+
the FindCoordinatorResponse is received. Any errors are immediately
268+
raised.
269+
270+
:param group_id: The consumer group ID. This is typically the group
271+
name as a string.
272+
:return: The node_id of the broker that is the coordinator.
273+
"""
274+
# Note: Java may change how this is implemented in KAFKA-6791.
275+
#
276+
# TODO add support for dynamically picking version of
277+
# GroupCoordinatorRequest which was renamed to FindCoordinatorRequest.
278+
# When I experimented with this, GroupCoordinatorResponse_v1 didn't
279+
# match GroupCoordinatorResponse_v0 and I couldn't figure out why.
280+
gc_request = GroupCoordinatorRequest[0](group_id)
281+
gc_response = self._send_request_to_node(self._client.least_loaded_node(), gc_request)
282+
# use the extra error checking in add_group_coordinator() rather than
283+
# immediately returning the group coordinator.
284+
success = self._client.cluster.add_group_coordinator(group_id, gc_response)
285+
if not success:
286+
error_type = Errors.for_code(gc_response.error_code)
287+
assert error_type is not Errors.NoError
288+
# Note: When error_type.retriable, Java will retry... see
289+
# KafkaAdminClient's handleFindCoordinatorError method
290+
raise error_type(
291+
"Could not identify group coordinator for group_id '{}' from response '{}'."
292+
.format(group_id, gc_response))
293+
group_coordinator = self._client.cluster.coordinator_for_group(group_id)
294+
# will be None if the coordinator was never populated, which should never happen here
295+
assert group_coordinator is not None
296+
# will be -1 if add_group_coordinator() failed... but by this point the
297+
# error should have been raised.
298+
assert group_coordinator != -1
299+
return group_coordinator
300+
262301
def _send_request_to_node(self, node, request):
263302
"""Send a kafka protocol message to a specific broker. Will block until the message result is received.
264303

0 commit comments

Comments
 (0)