use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.
the class NetworkClientTest method testUnsupportedApiVersionsRequestWithVersionProvidedByTheBroker.
@Test
public void testUnsupportedApiVersionsRequestWithVersionProvidedByTheBroker() {
// initiate the connection
client.ready(node, time.milliseconds());
// handle the connection, initiate first ApiVersionsRequest
client.poll(0, time.milliseconds());
// ApiVersionsRequest is in flight but not sent yet
assertTrue(client.hasInFlightRequests(node.idString()));
// completes initiated sends
client.poll(0, time.milliseconds());
assertEquals(1, selector.completedSends().size());
ByteBuffer buffer = selector.completedSendBuffers().get(0).buffer();
RequestHeader header = parseHeader(buffer);
assertEquals(ApiKeys.API_VERSIONS, header.apiKey());
assertEquals(3, header.apiVersion());
// prepare response
ApiVersionCollection apiKeys = new ApiVersionCollection();
apiKeys.add(new ApiVersion().setApiKey(ApiKeys.API_VERSIONS.id).setMinVersion((short) 0).setMaxVersion((short) 2));
delayedApiVersionsResponse(0, (short) 0, new ApiVersionsResponse(new ApiVersionsResponseData().setErrorCode(Errors.UNSUPPORTED_VERSION.code()).setApiKeys(apiKeys)));
// handle ApiVersionResponse, initiate second ApiVersionRequest
client.poll(0, time.milliseconds());
// ApiVersionsRequest is in flight but not sent yet
assertTrue(client.hasInFlightRequests(node.idString()));
// ApiVersionsResponse has been received
assertEquals(1, selector.completedReceives().size());
// clean up the buffers
selector.completedSends().clear();
selector.completedSendBuffers().clear();
selector.completedReceives().clear();
// completes initiated sends
client.poll(0, time.milliseconds());
// ApiVersionsRequest has been sent
assertEquals(1, selector.completedSends().size());
buffer = selector.completedSendBuffers().get(0).buffer();
header = parseHeader(buffer);
assertEquals(ApiKeys.API_VERSIONS, header.apiKey());
assertEquals(2, header.apiVersion());
// prepare response
delayedApiVersionsResponse(1, (short) 0, defaultApiVersionsResponse());
// handle completed receives
client.poll(0, time.milliseconds());
// the ApiVersionsRequest is gone
assertFalse(client.hasInFlightRequests(node.idString()));
assertEquals(1, selector.completedReceives().size());
// the client is ready
assertTrue(client.isReady(node, time.milliseconds()));
}
use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.
the class NodeApiVersionsTest method testUsableVersionLatestVersions.
@ParameterizedTest
@EnumSource(ApiMessageType.ListenerType.class)
public void testUsableVersionLatestVersions(ApiMessageType.ListenerType scope) {
ApiVersionsResponse defaultResponse = ApiVersionsResponse.defaultApiVersionsResponse(scope);
List<ApiVersion> versionList = new LinkedList<>(defaultResponse.data().apiKeys());
// Add an API key that we don't know about.
versionList.add(new ApiVersion().setApiKey((short) 100).setMinVersion((short) 0).setMaxVersion((short) 1));
NodeApiVersions versions = new NodeApiVersions(versionList);
for (ApiKeys apiKey : ApiKeys.apisForListener(scope)) {
assertEquals(apiKey.latestVersion(), versions.latestUsableVersion(apiKey));
}
}
use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.
the class KafkaAdminClient method describeFeatures.
@Override
public DescribeFeaturesResult describeFeatures(final DescribeFeaturesOptions options) {
final KafkaFutureImpl<FeatureMetadata> future = new KafkaFutureImpl<>();
final long now = time.milliseconds();
final Call call = new Call("describeFeatures", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {
private FeatureMetadata createFeatureMetadata(final ApiVersionsResponse response) {
final Map<String, FinalizedVersionRange> finalizedFeatures = new HashMap<>();
for (final FinalizedFeatureKey key : response.data().finalizedFeatures().valuesSet()) {
finalizedFeatures.put(key.name(), new FinalizedVersionRange(key.minVersionLevel(), key.maxVersionLevel()));
}
Optional<Long> finalizedFeaturesEpoch;
if (response.data().finalizedFeaturesEpoch() >= 0L) {
finalizedFeaturesEpoch = Optional.of(response.data().finalizedFeaturesEpoch());
} else {
finalizedFeaturesEpoch = Optional.empty();
}
final Map<String, SupportedVersionRange> supportedFeatures = new HashMap<>();
for (final SupportedFeatureKey key : response.data().supportedFeatures().valuesSet()) {
supportedFeatures.put(key.name(), new SupportedVersionRange(key.minVersion(), key.maxVersion()));
}
return new FeatureMetadata(finalizedFeatures, finalizedFeaturesEpoch, supportedFeatures);
}
@Override
ApiVersionsRequest.Builder createRequest(int timeoutMs) {
return new ApiVersionsRequest.Builder();
}
@Override
void handleResponse(AbstractResponse response) {
final ApiVersionsResponse apiVersionsResponse = (ApiVersionsResponse) response;
if (apiVersionsResponse.data().errorCode() == Errors.NONE.code()) {
future.complete(createFeatureMetadata(apiVersionsResponse));
} else {
future.completeExceptionally(Errors.forCode(apiVersionsResponse.data().errorCode()).exception());
}
}
@Override
void handleFailure(Throwable throwable) {
completeAllExceptionally(Collections.singletonList(future), throwable);
}
};
runnable.call(call, now);
return new DescribeFeaturesResult(future);
}
use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.
the class NetworkClient method handleCompletedReceives.
/**
* Handle any completed receives and update the response list with the responses received.
*
* @param responses The list of responses to update
* @param now The current time
*/
private void handleCompletedReceives(List<ClientResponse> responses, long now) {
for (NetworkReceive receive : this.selector.completedReceives()) {
String source = receive.source();
InFlightRequest req = inFlightRequests.completeNext(source);
AbstractResponse response = parseResponse(receive.payload(), req.header);
if (throttleTimeSensor != null)
throttleTimeSensor.record(response.throttleTimeMs(), now);
if (log.isDebugEnabled()) {
log.debug("Received {} response from node {} for request with header {}: {}", req.header.apiKey(), req.destination, req.header, response);
}
// If the received response includes a throttle delay, throttle the connection.
maybeThrottle(response, req.header.apiVersion(), req.destination, now);
if (req.isInternalRequest && response instanceof MetadataResponse)
metadataUpdater.handleSuccessfulResponse(req.header, now, (MetadataResponse) response);
else if (req.isInternalRequest && response instanceof ApiVersionsResponse)
handleApiVersionsResponse(responses, req, now, (ApiVersionsResponse) response);
else
responses.add(req.completed(response, now));
}
}
use of org.apache.kafka.common.requests.ApiVersionsResponse in project kafka by apache.
the class SaslAuthenticatorTest method testInvalidApiVersionsRequest.
/**
* Tests that invalid ApiVersionRequest is handled by the server correctly and
* returns an INVALID_REQUEST error.
*/
@Test
public void testInvalidApiVersionsRequest() throws Exception {
short handshakeVersion = ApiKeys.SASL_HANDSHAKE.latestVersion();
SecurityProtocol securityProtocol = SecurityProtocol.SASL_PLAINTEXT;
configureMechanisms("PLAIN", Arrays.asList("PLAIN"));
server = createEchoServer(securityProtocol);
// Send ApiVersionsRequest with invalid version and validate error response.
String node = "1";
short version = ApiKeys.API_VERSIONS.latestVersion();
createClientConnection(SecurityProtocol.PLAINTEXT, node);
RequestHeader header = new RequestHeader(ApiKeys.API_VERSIONS, version, "someclient", 1);
ApiVersionsRequest request = new ApiVersionsRequest(new ApiVersionsRequestData().setClientSoftwareName(" ").setClientSoftwareVersion(" "), version);
selector.send(new NetworkSend(node, request.toSend(header)));
ByteBuffer responseBuffer = waitForResponse();
ResponseHeader.parse(responseBuffer, ApiKeys.API_VERSIONS.responseHeaderVersion(version));
ApiVersionsResponse response = ApiVersionsResponse.parse(responseBuffer, version);
assertEquals(Errors.INVALID_REQUEST.code(), response.data().errorCode());
// Send ApiVersionsRequest with a supported version. This should succeed.
sendVersionRequestReceiveResponse(node);
// Test that client can authenticate successfully
sendHandshakeRequestReceiveResponse(node, handshakeVersion);
authenticateUsingSaslPlainAndCheckConnection(node, handshakeVersion > 0);
}
Aggregations