@@ -328,43 +328,27 @@ def _find_coordinator_id_process_response(self, response):
328
328
.format (response .API_VERSION ))
329
329
return response .coordinator_id
330
330
331
- def _find_coordinator_id (self , group_id ):
332
- """Find the broker node_id of the coordinator of the given group.
333
-
334
- Sends a FindCoordinatorRequest message to the cluster. Will block until
335
- the FindCoordinatorResponse is received. Any errors are immediately
336
- raised.
337
-
338
- :param group_id: The consumer group ID. This is typically the group
339
- name as a string.
340
- :return: The node_id of the broker that is the coordinator.
341
- """
342
- future = self ._find_coordinator_id_send_request (group_id )
343
- self ._wait_for_futures ([future ])
344
- response = future .value
345
- return self ._find_coordinator_id_process_response (response )
346
-
347
- def _find_many_coordinator_ids (self , group_ids ):
348
- """Find the broker node_id of the coordinator for each of the given groups.
331
+ def _find_coordinator_ids (self , group_ids ):
332
+ """Find the broker node_ids of the coordinators of the given groups.
349
333
350
334
Sends a FindCoordinatorRequest message to the cluster for each group_id.
351
335
Will block until the FindCoordinatorResponse is received for all groups.
352
336
Any errors are immediately raised.
353
337
354
338
:param group_ids: A list of consumer group IDs. This is typically the group
355
339
name as a string.
356
- :return: A list of tuples ( group_id, node_id) where node_id is the id
357
- of the broker that is the coordinator for the corresponding group.
340
+ :return: A dict of { group_id: node_id} where node_id is the id of the
341
+ broker that is the coordinator for the corresponding group.
358
342
"""
359
- futures = {
343
+ groups_futures = {
360
344
group_id : self ._find_coordinator_id_send_request (group_id )
361
345
for group_id in group_ids
362
346
}
363
- self ._wait_for_futures (list ( futures .values () ))
364
- groups_coordinators = [
365
- ( group_id , self ._find_coordinator_id_process_response (f .value ) )
366
- for group_id , f in futures .items ()
367
- ]
347
+ self ._wait_for_futures (groups_futures .values ())
348
+ groups_coordinators = {
349
+ group_id : self ._find_coordinator_id_process_response (future .value )
350
+ for group_id , future in groups_futures .items ()
351
+ }
368
352
return groups_coordinators
369
353
370
354
def _send_request_to_node (self , node_id , request ):
@@ -1095,18 +1079,19 @@ def describe_consumer_groups(self, group_ids, group_coordinator_id=None, include
1095
1079
partition assignments.
1096
1080
"""
1097
1081
group_descriptions = []
1098
- futures = []
1099
- for group_id in group_ids :
1100
- if group_coordinator_id is not None :
1101
- this_groups_coordinator_id = group_coordinator_id
1102
- else :
1103
- this_groups_coordinator_id = self ._find_coordinator_id (group_id )
1104
- f = self ._describe_consumer_groups_send_request (
1082
+
1083
+ if group_coordinator_id is not None :
1084
+ groups_coordinators = {group_id : group_coordinator_id for group_id in group_ids }
1085
+ else :
1086
+ groups_coordinators = self .groups_coordinators (group_ids )
1087
+
1088
+ futures = [
1089
+ self ._describe_consumer_groups_send_request (
1105
1090
group_id ,
1106
- this_groups_coordinator_id ,
1091
+ coordinator_id ,
1107
1092
include_authorized_operations )
1108
- futures . append ( f )
1109
-
1093
+ for group_id , coordinator_id in groups_coordinators . items ( )
1094
+ ]
1110
1095
self ._wait_for_futures (futures )
1111
1096
1112
1097
for future in futures :
@@ -1278,7 +1263,7 @@ def list_consumer_group_offsets(self, group_id, group_coordinator_id=None,
1278
1263
explicitly specified.
1279
1264
"""
1280
1265
if group_coordinator_id is None :
1281
- group_coordinator_id = self ._find_coordinator_id ( group_id )
1266
+ group_coordinator_id = self ._find_coordinator_ids ([ group_id ])[ group_id ]
1282
1267
future = self ._list_consumer_group_offsets_send_request (
1283
1268
group_id , group_coordinator_id , partitions )
1284
1269
self ._wait_for_futures ([future ])
@@ -1306,12 +1291,12 @@ def delete_consumer_groups(self, group_ids, group_coordinator_id=None):
1306
1291
if group_coordinator_id is not None :
1307
1292
futures = [self ._delete_consumer_groups_send_request (group_ids , group_coordinator_id )]
1308
1293
else :
1309
- groups_coordinators = defaultdict (list )
1310
- for group_id , group_coordinator_id in self ._find_many_coordinator_ids (group_ids ):
1311
- groups_coordinators [ group_coordinator_id ].append (group_id )
1294
+ coordinators_groups = defaultdict (list )
1295
+ for group_id , coordinator_id in self ._find_coordinator_ids (group_ids ). items ( ):
1296
+ coordinators_groups [ coordinator_id ].append (group_id )
1312
1297
futures = [
1313
- self ._delete_consumer_groups_send_request (group_ids , group_coordinator_id )
1314
- for group_coordinator_id , group_ids in groups_coordinators .items ()
1298
+ self ._delete_consumer_groups_send_request (group_ids , coordinator_id )
1299
+ for coordinator_id , group_ids in coordinators_groups .items ()
1315
1300
]
1316
1301
1317
1302
self ._wait_for_futures (futures )
0 commit comments