Skip to content

Commit 2a821c2

Browse files
KAFKA-17897: Deprecate Admin.listConsumerGroups [2/N]
1 parent 810beef commit 2a821c2

File tree

7 files changed

+676
-167
lines changed

7 files changed

+676
-167
lines changed

clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java

+67-32
Original file line numberDiff line numberDiff line change
@@ -3524,44 +3524,62 @@ void handleResponse(AbstractResponse abstractResponse) {
35243524
for (final Node node : allNodes) {
35253525
final long nowList = time.milliseconds();
35263526
runnable.call(new Call("listGroups", deadline, new ConstantNodeIdProvider(node.id())) {
3527+
3528+
// If only regular consumer group types are required, we can try an earlier request version if
3529+
// UnsupportedVersionException is thrown
3530+
final boolean canTryEarlierRequestVersion = options.regularConsumerGroupTypes();
3531+
boolean tryUsingEarlierRequestVersion = false;
3532+
35273533
@Override
35283534
ListGroupsRequest.Builder createRequest(int timeoutMs) {
3529-
List<String> groupTypes = options.types()
3530-
.stream()
3531-
.map(GroupType::toString)
3532-
.collect(Collectors.toList());
3533-
List<String> groupStates = options.groupStates()
3534-
.stream()
3535-
.map(GroupState::toString)
3536-
.collect(Collectors.toList());
3537-
return new ListGroupsRequest.Builder(new ListGroupsRequestData()
3538-
.setTypesFilter(groupTypes)
3539-
.setStatesFilter(groupStates)
3540-
);
3535+
if (tryUsingEarlierRequestVersion) {
3536+
List<String> groupStates = options.groupStates()
3537+
.stream()
3538+
.map(GroupState::toString)
3539+
.collect(Collectors.toList());
3540+
return new ListGroupsRequest.Builder(new ListGroupsRequestData()
3541+
.setStatesFilter(groupStates)
3542+
);
3543+
} else {
3544+
List<String> groupTypes = options.types()
3545+
.stream()
3546+
.map(GroupType::toString)
3547+
.collect(Collectors.toList());
3548+
List<String> groupStates = options.groupStates()
3549+
.stream()
3550+
.map(GroupState::toString)
3551+
.collect(Collectors.toList());
3552+
return new ListGroupsRequest.Builder(new ListGroupsRequestData()
3553+
.setTypesFilter(groupTypes)
3554+
.setStatesFilter(groupStates)
3555+
);
3556+
}
35413557
}
35423558

35433559
private void maybeAddGroup(ListGroupsResponseData.ListedGroup group) {
3544-
final String groupId = group.groupId();
3545-
final Optional<GroupType> type;
3546-
if (group.groupType() == null || group.groupType().isEmpty()) {
3547-
type = Optional.empty();
3548-
} else {
3549-
type = Optional.of(GroupType.parse(group.groupType()));
3550-
}
3551-
final String protocolType = group.protocolType();
3552-
final Optional<GroupState> groupState;
3553-
if (group.groupState() == null || group.groupState().isEmpty()) {
3554-
groupState = Optional.empty();
3555-
} else {
3556-
groupState = Optional.of(GroupState.parse(group.groupState()));
3560+
String protocolType = group.protocolType();
3561+
if (options.protocolTypes().isEmpty() || options.protocolTypes().contains(protocolType)) {
3562+
final String groupId = group.groupId();
3563+
final Optional<GroupType> type;
3564+
if (group.groupType() == null || group.groupType().isEmpty()) {
3565+
type = Optional.empty();
3566+
} else {
3567+
type = Optional.of(GroupType.parse(group.groupType()));
3568+
}
3569+
final Optional<GroupState> groupState;
3570+
if (group.groupState() == null || group.groupState().isEmpty()) {
3571+
groupState = Optional.empty();
3572+
} else {
3573+
groupState = Optional.of(GroupState.parse(group.groupState()));
3574+
}
3575+
final GroupListing groupListing = new GroupListing(
3576+
groupId,
3577+
type,
3578+
protocolType,
3579+
groupState
3580+
);
3581+
results.addListing(groupListing);
35573582
}
3558-
final GroupListing groupListing = new GroupListing(
3559-
groupId,
3560-
type,
3561-
protocolType,
3562-
groupState
3563-
);
3564-
results.addListing(groupListing);
35653583
}
35663584

35673585
@Override
@@ -3582,6 +3600,23 @@ void handleResponse(AbstractResponse abstractResponse) {
35823600
}
35833601
}
35843602

3603+
@Override
3604+
boolean handleUnsupportedVersionException(final UnsupportedVersionException exception) {
3605+
// If we cannot try the earlier request version, give up
3606+
if (!canTryEarlierRequestVersion) {
3607+
return false;
3608+
}
3609+
3610+
// If have already tried the earlier request version, give up
3611+
if (tryUsingEarlierRequestVersion) {
3612+
return false;
3613+
}
3614+
3615+
// Have a try using the earlier request version
3616+
tryUsingEarlierRequestVersion = true;
3617+
return true;
3618+
}
3619+
35853620
@Override
35863621
void handleFailure(Throwable throwable) {
35873622
synchronized (results) {

clients/src/main/java/org/apache/kafka/clients/admin/ListGroupsOptions.java

+32-1
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,38 @@ public class ListGroupsOptions extends AbstractOptions<ListGroupsOptions> {
3636
private Set<GroupType> types = Set.of();
3737
private Set<String> protocolTypes = Set.of();
3838

39+
// Types filter is supported by brokers with version 4.0.0 or later. Older brokers only support
40+
// classic groups, so listing consumer groups on an older broker does not need to use a types filter.
41+
private boolean regularConsumerGroupTypes = false;
42+
3943
/**
4044
* Only consumer groups will be returned by listGroups().
4145
* This operation sets filters on group type and protocol type which select consumer groups.
4246
*/
4347
public static ListGroupsOptions forConsumerGroups() {
4448
return new ListGroupsOptions()
45-
.withTypes(Set.of(GroupType.CLASSIC, GroupType.CONSUMER))
49+
.withTypes(Set.of(GroupType.CLASSIC, GroupType.CONSUMER), true)
4650
.withProtocolTypes(Set.of("", ConsumerProtocol.PROTOCOL_TYPE));
4751
}
4852

53+
/**
54+
* Only share groups will be returned by listGroups().
55+
* This operation sets a filter on group type which select share groups.
56+
*/
57+
public static ListGroupsOptions forShareGroups() {
58+
return new ListGroupsOptions()
59+
.withTypes(Set.of(GroupType.SHARE));
60+
}
61+
62+
/**
63+
* Only streams groups will be returned by listGroups().
64+
* This operation sets a filter on group type which select streams groups.
65+
*/
66+
public static ListGroupsOptions forStreamsGroups() {
67+
return new ListGroupsOptions()
68+
.withTypes(Set.of(GroupType.STREAMS));
69+
}
70+
4971
/**
5072
* If groupStates is set, only groups in these states will be returned by listGroups().
5173
* Otherwise, all groups are returned.
@@ -66,7 +88,12 @@ public ListGroupsOptions withProtocolTypes(Set<String> protocolTypes) {
6688
* Otherwise, all groups are returned.
6789
*/
6890
public ListGroupsOptions withTypes(Set<GroupType> types) {
91+
return this.withTypes(types, false);
92+
}
93+
94+
ListGroupsOptions withTypes(Set<GroupType> types, boolean regularConsumerGroupTypes) {
6995
this.types = (types == null || types.isEmpty()) ? Set.of() : Set.copyOf(types);
96+
this.regularConsumerGroupTypes = regularConsumerGroupTypes;
7097
return this;
7198
}
7299

@@ -90,4 +117,8 @@ public Set<String> protocolTypes() {
90117
public Set<GroupType> types() {
91118
return types;
92119
}
120+
121+
boolean regularConsumerGroupTypes() {
122+
return regularConsumerGroupTypes;
123+
}
93124
}

0 commit comments

Comments
 (0)