use of org.apache.kafka.common.requests.RequestHeader in project apache-kafka-on-k8s by banzaicloud.
the class SaslClientAuthenticator method nextRequestHeader.
private RequestHeader nextRequestHeader(ApiKeys apiKey, short version) {
String clientId = (String) configs.get(CommonClientConfigs.CLIENT_ID_CONFIG);
currentRequestHeader = new RequestHeader(apiKey, version, clientId, correlationId++);
return currentRequestHeader;
}
use of org.apache.kafka.common.requests.RequestHeader in project apache-kafka-on-k8s by banzaicloud.
the class SaslAuthenticatorTest method testApiVersionsRequestWithUnsupportedVersion.
/**
* Tests that unsupported version of ApiVersionsRequest before SASL handshake request
* returns error response and does not result in authentication failure. This test
* is similar to {@link #testUnauthenticatedApiVersionsRequest(SecurityProtocol, short)}
* where a non-SASL client is used to send requests that are processed by
* {@link SaslServerAuthenticator} of the server prior to client authentication.
*/
@Test
public void testApiVersionsRequestWithUnsupportedVersion() throws Exception {
short handshakeVersion = ApiKeys.SASL_HANDSHAKE.latestVersion();
SecurityProtocol securityProtocol = SecurityProtocol.SASL_PLAINTEXT;
configureMechanisms("PLAIN", Arrays.asList("PLAIN"));
server = createEchoServer(securityProtocol);
// Send ApiVersionsRequest with unsupported version and validate error response.
String node = "1";
createClientConnection(SecurityProtocol.PLAINTEXT, node);
RequestHeader header = new RequestHeader(ApiKeys.API_VERSIONS, Short.MAX_VALUE, "someclient", 1);
ApiVersionsRequest request = new ApiVersionsRequest.Builder().build();
selector.send(request.toSend(node, header));
ByteBuffer responseBuffer = waitForResponse();
ResponseHeader.parse(responseBuffer);
ApiVersionsResponse response = ApiVersionsResponse.parse(responseBuffer, (short) 0);
assertEquals(Errors.UNSUPPORTED_VERSION, response.error());
// Send ApiVersionsRequest with a supported version. This should succeed.
sendVersionRequestReceiveResponse(node);
// Test that client can authenticate successfully
sendHandshakeRequestReceiveResponse(node, handshakeVersion);
authenticateUsingSaslPlainAndCheckConnection(node, handshakeVersion > 0);
}
use of org.apache.kafka.common.requests.RequestHeader in project apache-kafka-on-k8s by banzaicloud.
the class InFlightRequestsTest method addRequest.
private int addRequest(String destination) {
int correlationId = this.correlationId;
this.correlationId += 1;
RequestHeader requestHeader = new RequestHeader(ApiKeys.METADATA, (short) 0, "clientId", correlationId);
NetworkClient.InFlightRequest ifr = new NetworkClient.InFlightRequest(requestHeader, 0, destination, null, false, false, null, null, 0);
inFlightRequests.add(ifr);
return correlationId;
}
use of org.apache.kafka.common.requests.RequestHeader in project apache-kafka-on-k8s by banzaicloud.
the class Sender method handleProduceResponse.
/**
* Handle a produce response
*/
private void handleProduceResponse(ClientResponse response, Map<TopicPartition, ProducerBatch> batches, long now) {
RequestHeader requestHeader = response.requestHeader();
int correlationId = requestHeader.correlationId();
if (response.wasDisconnected()) {
log.trace("Cancelled request with header {} due to node {} being disconnected", requestHeader, response.destination());
for (ProducerBatch batch : batches.values()) completeBatch(batch, new ProduceResponse.PartitionResponse(Errors.NETWORK_EXCEPTION), correlationId, now);
} else if (response.versionMismatch() != null) {
log.warn("Cancelled request {} due to a version mismatch with node {}", response, response.destination(), response.versionMismatch());
for (ProducerBatch batch : batches.values()) completeBatch(batch, new ProduceResponse.PartitionResponse(Errors.UNSUPPORTED_VERSION), correlationId, now);
} else {
log.trace("Received produce response from node {} with correlation id {}", response.destination(), correlationId);
// if we have a response, parse it
if (response.hasResponse()) {
ProduceResponse produceResponse = (ProduceResponse) response.responseBody();
for (Map.Entry<TopicPartition, ProduceResponse.PartitionResponse> entry : produceResponse.responses().entrySet()) {
TopicPartition tp = entry.getKey();
ProduceResponse.PartitionResponse partResp = entry.getValue();
ProducerBatch batch = batches.get(tp);
completeBatch(batch, partResp, correlationId, now);
}
this.sensors.recordLatency(response.destination(), response.requestLatencyMs());
} else {
// this is the acks = 0 case, just complete all requests
for (ProducerBatch batch : batches.values()) {
completeBatch(batch, new ProduceResponse.PartitionResponse(Errors.NONE), correlationId, now);
}
}
}
}
use of org.apache.kafka.common.requests.RequestHeader 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()));
}
Aggregations