use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion in project kafka by apache.
the class KafkaAdminClientTest method testDeleteMultipleConsumerGroupsWithOlderBroker.
@Test
public void testDeleteMultipleConsumerGroupsWithOlderBroker() throws Exception {
final List<String> groupIds = asList("group1", "group2");
ApiVersion findCoordinatorV3 = new ApiVersion().setApiKey(ApiKeys.FIND_COORDINATOR.id).setMinVersion((short) 0).setMaxVersion((short) 3);
ApiVersion describeGroups = new ApiVersion().setApiKey(ApiKeys.DESCRIBE_GROUPS.id).setMinVersion((short) 0).setMaxVersion(ApiKeys.DELETE_GROUPS.latestVersion());
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create(Arrays.asList(findCoordinatorV3, describeGroups)));
// Dummy response for MockClient to handle the UnsupportedVersionException correctly to switch from batched to un-batched
env.kafkaClient().prepareResponse(null);
// Retriable FindCoordinatorResponse errors should be retried
for (int i = 0; i < groupIds.size(); i++) {
env.kafkaClient().prepareResponse(prepareOldFindCoordinatorResponse(Errors.COORDINATOR_NOT_AVAILABLE, Node.noNode()));
}
for (int i = 0; i < groupIds.size(); i++) {
env.kafkaClient().prepareResponse(prepareOldFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
}
final DeletableGroupResultCollection validResponse = new DeletableGroupResultCollection();
validResponse.add(new DeletableGroupResult().setGroupId("group1").setErrorCode(Errors.NONE.code()));
validResponse.add(new DeletableGroupResult().setGroupId("group2").setErrorCode(Errors.NONE.code()));
env.kafkaClient().prepareResponse(new DeleteGroupsResponse(new DeleteGroupsResponseData().setResults(validResponse)));
final DeleteConsumerGroupsResult result = env.adminClient().deleteConsumerGroups(groupIds);
final KafkaFuture<Void> results = result.deletedGroups().get("group1");
assertNull(results.get(5, TimeUnit.SECONDS));
}
}
use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion 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.message.ApiVersionsResponseData.ApiVersion in project kafka by apache.
the class NodeApiVersions method latestUsableVersion.
/**
* Get the latest version supported by the broker within an allowed range of versions
*/
public short latestUsableVersion(ApiKeys apiKey, short oldestAllowedVersion, short latestAllowedVersion) {
if (!supportedVersions.containsKey(apiKey))
throw new UnsupportedVersionException("The broker does not support " + apiKey);
ApiVersion supportedVersion = supportedVersions.get(apiKey);
Optional<ApiVersion> intersectVersion = ApiVersionsResponse.intersect(supportedVersion, new ApiVersion().setApiKey(apiKey.id).setMinVersion(oldestAllowedVersion).setMaxVersion(latestAllowedVersion));
if (intersectVersion.isPresent())
return intersectVersion.get().maxVersion();
else
throw new UnsupportedVersionException("The broker does not support " + apiKey + " with version in range [" + oldestAllowedVersion + "," + latestAllowedVersion + "]. The supported" + " range is [" + supportedVersion.minVersion() + "," + supportedVersion.maxVersion() + "].");
}
use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion in project kafka by apache.
the class SaslClientAuthenticator method setSaslAuthenticateAndHandshakeVersions.
// Visible to override for testing
protected void setSaslAuthenticateAndHandshakeVersions(ApiVersionsResponse apiVersionsResponse) {
ApiVersion authenticateVersion = apiVersionsResponse.apiVersion(ApiKeys.SASL_AUTHENTICATE.id);
if (authenticateVersion != null) {
this.saslAuthenticateVersion = (short) Math.min(authenticateVersion.maxVersion(), ApiKeys.SASL_AUTHENTICATE.latestVersion());
}
ApiVersion handshakeVersion = apiVersionsResponse.apiVersion(ApiKeys.SASL_HANDSHAKE.id);
if (handshakeVersion != null) {
this.saslHandshakeVersion = (short) Math.min(handshakeVersion.maxVersion(), ApiKeys.SASL_HANDSHAKE.latestVersion());
}
}
use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion in project kafka by apache.
the class NodeApiVersionsTest method testConstructionFromApiVersionsResponse.
@ParameterizedTest
@EnumSource(ApiMessageType.ListenerType.class)
public void testConstructionFromApiVersionsResponse(ApiMessageType.ListenerType scope) {
ApiVersionsResponse apiVersionsResponse = ApiVersionsResponse.defaultApiVersionsResponse(scope);
NodeApiVersions versions = new NodeApiVersions(apiVersionsResponse.data().apiKeys());
for (ApiVersion apiVersionKey : apiVersionsResponse.data().apiKeys()) {
ApiVersion apiVersion = versions.apiVersion(ApiKeys.forId(apiVersionKey.apiKey()));
assertEquals(apiVersionKey.apiKey(), apiVersion.apiKey());
assertEquals(apiVersionKey.minVersion(), apiVersion.minVersion());
assertEquals(apiVersionKey.maxVersion(), apiVersion.maxVersion());
}
}
Aggregations