use of org.apache.kafka.common.requests.ListGroupsResponse in project kafka by apache.
the class KafkaAdminClientTest method testListConsumerGroupsWithStates.
@Test
public void testListConsumerGroupsWithStates() throws Exception {
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
env.kafkaClient().prepareResponse(prepareMetadataResponse(env.cluster(), Errors.NONE));
env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.NONE.code()).setGroups(Arrays.asList(new ListGroupsResponseData.ListedGroup().setGroupId("group-1").setProtocolType(ConsumerProtocol.PROTOCOL_TYPE).setGroupState("Stable"), new ListGroupsResponseData.ListedGroup().setGroupId("group-2").setGroupState("Empty")))), env.cluster().nodeById(0));
final ListConsumerGroupsOptions options = new ListConsumerGroupsOptions();
final ListConsumerGroupsResult result = env.adminClient().listConsumerGroups(options);
Collection<ConsumerGroupListing> listings = result.valid().get();
assertEquals(2, listings.size());
List<ConsumerGroupListing> expected = new ArrayList<>();
expected.add(new ConsumerGroupListing("group-2", true, Optional.of(ConsumerGroupState.EMPTY)));
expected.add(new ConsumerGroupListing("group-1", false, Optional.of(ConsumerGroupState.STABLE)));
assertEquals(expected, listings);
assertEquals(0, result.errors().get().size());
}
}
use of org.apache.kafka.common.requests.ListGroupsResponse in project kafka by apache.
the class KafkaAdminClientTest method testListConsumerGroups.
@Test
public void testListConsumerGroups() throws Exception {
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(4, 0), AdminClientConfig.RETRIES_CONFIG, "2")) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
// Empty metadata response should be retried
env.kafkaClient().prepareResponse(RequestTestUtils.metadataResponse(Collections.emptyList(), env.cluster().clusterResource().clusterId(), -1, Collections.emptyList()));
env.kafkaClient().prepareResponse(RequestTestUtils.metadataResponse(env.cluster().nodes(), env.cluster().clusterResource().clusterId(), env.cluster().controller().id(), Collections.emptyList()));
env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.NONE.code()).setGroups(Arrays.asList(new ListGroupsResponseData.ListedGroup().setGroupId("group-1").setProtocolType(ConsumerProtocol.PROTOCOL_TYPE).setGroupState("Stable"), new ListGroupsResponseData.ListedGroup().setGroupId("group-connect-1").setProtocolType("connector").setGroupState("Stable")))), env.cluster().nodeById(0));
// handle retriable errors
env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code()).setGroups(Collections.emptyList())), env.cluster().nodeById(1));
env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.COORDINATOR_LOAD_IN_PROGRESS.code()).setGroups(Collections.emptyList())), env.cluster().nodeById(1));
env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.NONE.code()).setGroups(Arrays.asList(new ListGroupsResponseData.ListedGroup().setGroupId("group-2").setProtocolType(ConsumerProtocol.PROTOCOL_TYPE).setGroupState("Stable"), new ListGroupsResponseData.ListedGroup().setGroupId("group-connect-2").setProtocolType("connector").setGroupState("Stable")))), env.cluster().nodeById(1));
env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.NONE.code()).setGroups(Arrays.asList(new ListGroupsResponseData.ListedGroup().setGroupId("group-3").setProtocolType(ConsumerProtocol.PROTOCOL_TYPE).setGroupState("Stable"), new ListGroupsResponseData.ListedGroup().setGroupId("group-connect-3").setProtocolType("connector").setGroupState("Stable")))), env.cluster().nodeById(2));
// fatal error
env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code()).setGroups(Collections.emptyList())), env.cluster().nodeById(3));
final ListConsumerGroupsResult result = env.adminClient().listConsumerGroups();
TestUtils.assertFutureError(result.all(), UnknownServerException.class);
Collection<ConsumerGroupListing> listings = result.valid().get();
assertEquals(3, listings.size());
Set<String> groupIds = new HashSet<>();
for (ConsumerGroupListing listing : listings) {
groupIds.add(listing.groupId());
assertTrue(listing.state().isPresent());
}
assertEquals(Utils.mkSet("group-1", "group-2", "group-3"), groupIds);
assertEquals(1, result.errors().get().size());
}
}
use of org.apache.kafka.common.requests.ListGroupsResponse in project kafka by apache.
the class KafkaAdminClientTest method testListConsumerGroupsWithStatesOlderBrokerVersion.
@Test
public void testListConsumerGroupsWithStatesOlderBrokerVersion() throws Exception {
ApiVersion listGroupV3 = new ApiVersion().setApiKey(ApiKeys.LIST_GROUPS.id).setMinVersion((short) 0).setMaxVersion((short) 3);
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create(Collections.singletonList(listGroupV3)));
env.kafkaClient().prepareResponse(prepareMetadataResponse(env.cluster(), Errors.NONE));
// Check we can list groups with older broker if we don't specify states
env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.NONE.code()).setGroups(Collections.singletonList(new ListGroupsResponseData.ListedGroup().setGroupId("group-1").setProtocolType(ConsumerProtocol.PROTOCOL_TYPE)))), env.cluster().nodeById(0));
ListConsumerGroupsOptions options = new ListConsumerGroupsOptions();
ListConsumerGroupsResult result = env.adminClient().listConsumerGroups(options);
Collection<ConsumerGroupListing> listing = result.all().get();
assertEquals(1, listing.size());
List<ConsumerGroupListing> expected = Collections.singletonList(new ConsumerGroupListing("group-1", false, Optional.empty()));
assertEquals(expected, listing);
// But we cannot set a state filter with older broker
env.kafkaClient().prepareResponse(prepareMetadataResponse(env.cluster(), Errors.NONE));
env.kafkaClient().prepareUnsupportedVersionResponse(body -> body instanceof ListGroupsRequest);
options = new ListConsumerGroupsOptions().inStates(singleton(ConsumerGroupState.STABLE));
result = env.adminClient().listConsumerGroups(options);
TestUtils.assertFutureThrows(result.all(), UnsupportedVersionException.class);
}
}
use of org.apache.kafka.common.requests.ListGroupsResponse in project kafka by apache.
the class KafkaAdminClient method listConsumerGroups.
@Override
public ListConsumerGroupsResult listConsumerGroups(ListConsumerGroupsOptions options) {
final KafkaFutureImpl<Collection<Object>> all = new KafkaFutureImpl<>();
final long nowMetadata = time.milliseconds();
final long deadline = calcDeadlineMs(nowMetadata, options.timeoutMs());
runnable.call(new Call("findAllBrokers", deadline, new LeastLoadedNodeProvider()) {
@Override
MetadataRequest.Builder createRequest(int timeoutMs) {
return new MetadataRequest.Builder(new MetadataRequestData().setTopics(Collections.emptyList()).setAllowAutoTopicCreation(true));
}
@Override
void handleResponse(AbstractResponse abstractResponse) {
MetadataResponse metadataResponse = (MetadataResponse) abstractResponse;
Collection<Node> nodes = metadataResponse.brokers();
if (nodes.isEmpty())
throw new StaleMetadataException("Metadata fetch failed due to missing broker list");
HashSet<Node> allNodes = new HashSet<>(nodes);
final ListConsumerGroupsResults results = new ListConsumerGroupsResults(allNodes, all);
for (final Node node : allNodes) {
final long nowList = time.milliseconds();
runnable.call(new Call("listConsumerGroups", deadline, new ConstantNodeIdProvider(node.id())) {
@Override
ListGroupsRequest.Builder createRequest(int timeoutMs) {
List<String> states = options.states().stream().map(s -> s.toString()).collect(Collectors.toList());
return new ListGroupsRequest.Builder(new ListGroupsRequestData().setStatesFilter(states));
}
private void maybeAddConsumerGroup(ListGroupsResponseData.ListedGroup group) {
String protocolType = group.protocolType();
if (protocolType.equals(ConsumerProtocol.PROTOCOL_TYPE) || protocolType.isEmpty()) {
final String groupId = group.groupId();
final Optional<ConsumerGroupState> state = group.groupState().equals("") ? Optional.empty() : Optional.of(ConsumerGroupState.parse(group.groupState()));
final ConsumerGroupListing groupListing = new ConsumerGroupListing(groupId, protocolType.isEmpty(), state);
results.addListing(groupListing);
}
}
@Override
void handleResponse(AbstractResponse abstractResponse) {
final ListGroupsResponse response = (ListGroupsResponse) abstractResponse;
synchronized (results) {
Errors error = Errors.forCode(response.data().errorCode());
if (error == Errors.COORDINATOR_LOAD_IN_PROGRESS || error == Errors.COORDINATOR_NOT_AVAILABLE) {
throw error.exception();
} else if (error != Errors.NONE) {
results.addError(error.exception(), node);
} else {
for (ListGroupsResponseData.ListedGroup group : response.data().groups()) {
maybeAddConsumerGroup(group);
}
}
results.tryComplete(node);
}
}
@Override
void handleFailure(Throwable throwable) {
synchronized (results) {
results.addError(throwable, node);
results.tryComplete(node);
}
}
}, nowList);
}
}
@Override
void handleFailure(Throwable throwable) {
KafkaException exception = new KafkaException("Failed to find brokers to send ListGroups", throwable);
all.complete(Collections.singletonList(exception));
}
}, nowMetadata);
return new ListConsumerGroupsResult(all);
}
Aggregations