use of org.apache.kafka.common.network.Send in project kafka by apache.
the class MockSelector method poll.
@Override
public void poll(long timeout) throws IOException {
this.completedSends.addAll(this.initiatedSends);
this.initiatedSends.clear();
for (Send completedSend : completedSends) {
Iterator<DelayedReceive> delayedReceiveIterator = delayedReceives.iterator();
while (delayedReceiveIterator.hasNext()) {
DelayedReceive delayedReceive = delayedReceiveIterator.next();
if (delayedReceive.source().equals(completedSend.destination())) {
completedReceives.add(delayedReceive.receive());
delayedReceiveIterator.remove();
}
}
}
time.sleep(timeout);
}
use of org.apache.kafka.common.network.Send in project kafka by apache.
the class RequestResponseTest method verifyFetchResponseFullWrite.
@Test
public void verifyFetchResponseFullWrite() throws Exception {
FetchResponse fetchResponse = createFetchResponse();
RequestHeader header = new RequestHeader(ApiKeys.FETCH.id, ApiKeys.FETCH.latestVersion(), "client", 15);
Send send = fetchResponse.toSend("1", header);
ByteBufferChannel channel = new ByteBufferChannel(send.size());
send.writeTo(channel);
channel.close();
ByteBuffer buf = channel.buf;
// read the size
int size = buf.getInt();
assertTrue(size > 0);
// read the header
ResponseHeader responseHeader = ResponseHeader.parse(channel.buf);
assertEquals(header.correlationId(), responseHeader.correlationId());
// read the body
Struct responseBody = ApiKeys.FETCH.responseSchema(header.apiVersion()).read(buf);
assertEquals(fetchResponse.toStruct(header.apiVersion()), responseBody);
assertEquals(size, responseHeader.sizeOf() + responseBody.sizeOf());
}
use of org.apache.kafka.common.network.Send in project apache-kafka-on-k8s by banzaicloud.
the class RequestContextTest method testSerdeUnsupportedApiVersionRequest.
@Test
public void testSerdeUnsupportedApiVersionRequest() throws Exception {
int correlationId = 23423;
RequestHeader header = new RequestHeader(ApiKeys.API_VERSIONS, Short.MAX_VALUE, "", correlationId);
RequestContext context = new RequestContext(header, "0", InetAddress.getLocalHost(), KafkaPrincipal.ANONYMOUS, new ListenerName("ssl"), SecurityProtocol.SASL_SSL);
assertEquals(0, context.apiVersion());
// Write some garbage to the request buffer. This should be ignored since we will treat
// the unknown version type as v0 which has an empty request body.
ByteBuffer requestBuffer = ByteBuffer.allocate(8);
requestBuffer.putInt(3709234);
requestBuffer.putInt(29034);
requestBuffer.flip();
RequestAndSize requestAndSize = context.parseRequest(requestBuffer);
assertTrue(requestAndSize.request instanceof ApiVersionsRequest);
ApiVersionsRequest request = (ApiVersionsRequest) requestAndSize.request;
assertTrue(request.hasUnsupportedRequestVersion());
Send send = context.buildResponse(new ApiVersionsResponse(0, Errors.UNSUPPORTED_VERSION, Collections.<ApiVersionsResponse.ApiVersion>emptyList()));
ByteBufferChannel channel = new ByteBufferChannel(256);
send.writeTo(channel);
ByteBuffer responseBuffer = channel.buffer();
responseBuffer.flip();
// strip off the size
responseBuffer.getInt();
ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer);
assertEquals(correlationId, responseHeader.correlationId());
Struct struct = ApiKeys.API_VERSIONS.parseResponse((short) 0, responseBuffer);
ApiVersionsResponse response = (ApiVersionsResponse) AbstractResponse.parseResponse(ApiKeys.API_VERSIONS, struct);
assertEquals(Errors.UNSUPPORTED_VERSION, response.error());
assertTrue(response.apiVersions().isEmpty());
}
use of org.apache.kafka.common.network.Send in project apache-kafka-on-k8s by banzaicloud.
the class RequestResponseTest method verifyFetchResponseFullWrite.
private void verifyFetchResponseFullWrite(short apiVersion, FetchResponse fetchResponse) throws Exception {
int correlationId = 15;
Send send = fetchResponse.toSend("1", new ResponseHeader(correlationId), apiVersion);
ByteBufferChannel channel = new ByteBufferChannel(send.size());
send.writeTo(channel);
channel.close();
ByteBuffer buf = channel.buffer();
// read the size
int size = buf.getInt();
assertTrue(size > 0);
// read the header
ResponseHeader responseHeader = ResponseHeader.parse(channel.buffer());
assertEquals(correlationId, responseHeader.correlationId());
// read the body
Struct responseBody = ApiKeys.FETCH.responseSchema(apiVersion).read(buf);
assertEquals(fetchResponse.toStruct(apiVersion), responseBody);
assertEquals(size, responseHeader.sizeOf() + responseBody.sizeOf());
}
use of org.apache.kafka.common.network.Send in project apache-kafka-on-k8s by banzaicloud.
the class NetworkClient method handleCompletedSends.
/**
* Handle any completed request send. In particular if no response is expected consider the request complete.
*
* @param responses The list of responses to update
* @param now The current time
*/
private void handleCompletedSends(List<ClientResponse> responses, long now) {
// if no response is expected then when the send is completed, return it
for (Send send : this.selector.completedSends()) {
InFlightRequest request = this.inFlightRequests.lastSent(send.destination());
if (!request.expectResponse) {
this.inFlightRequests.completeLastSent(send.destination());
responses.add(request.completed(null, now));
}
}
}
Aggregations