use of org.apache.kafka.common.message.ProduceResponseData in project kafka by apache.
the class ProduceRequest method getErrorResponse.
@Override
public ProduceResponse getErrorResponse(int throttleTimeMs, Throwable e) {
/* In case the producer doesn't actually want any response */
if (acks == 0)
return null;
ApiError apiError = ApiError.fromThrowable(e);
ProduceResponseData data = new ProduceResponseData().setThrottleTimeMs(throttleTimeMs);
partitionSizes().forEach((tp, ignored) -> {
ProduceResponseData.TopicProduceResponse tpr = data.responses().find(tp.topic());
if (tpr == null) {
tpr = new ProduceResponseData.TopicProduceResponse().setName(tp.topic());
data.responses().add(tpr);
}
tpr.partitionResponses().add(new ProduceResponseData.PartitionProduceResponse().setIndex(tp.partition()).setRecordErrors(Collections.emptyList()).setBaseOffset(INVALID_OFFSET).setLogAppendTimeMs(RecordBatch.NO_TIMESTAMP).setLogStartOffset(INVALID_OFFSET).setErrorMessage(apiError.message()).setErrorCode(apiError.error().code()));
});
return new ProduceResponse(data);
}
use of org.apache.kafka.common.message.ProduceResponseData in project kafka by apache.
the class NetworkClientTest method checkSimpleRequestResponse.
private void checkSimpleRequestResponse(NetworkClient networkClient) {
// has to be before creating any request, as it may send ApiVersionsRequest and its response is mocked with correlation id 0
awaitReady(networkClient, node);
short requestVersion = PRODUCE.latestVersion();
ProduceRequest.Builder builder = new ProduceRequest.Builder(requestVersion, requestVersion, new ProduceRequestData().setAcks((short) 1).setTimeoutMs(1000));
TestCallbackHandler handler = new TestCallbackHandler();
ClientRequest request = networkClient.newClientRequest(node.idString(), builder, time.milliseconds(), true, defaultRequestTimeoutMs, handler);
networkClient.send(request, time.milliseconds());
networkClient.poll(1, time.milliseconds());
assertEquals(1, networkClient.inFlightRequestCount());
ProduceResponse produceResponse = new ProduceResponse(new ProduceResponseData());
ByteBuffer buffer = RequestTestUtils.serializeResponseWithHeader(produceResponse, requestVersion, request.correlationId());
selector.completeReceive(new NetworkReceive(node.idString(), buffer));
List<ClientResponse> responses = networkClient.poll(1, time.milliseconds());
assertEquals(1, responses.size());
assertTrue(handler.executed, "The handler should have executed.");
assertTrue(handler.response.hasResponse(), "Should have a response body.");
assertEquals(request.correlationId(), handler.response.requestHeader().correlationId(), "Should be correlated to the original request");
}
use of org.apache.kafka.common.message.ProduceResponseData in project kafka by apache.
the class NetworkClientTest method testConnectionThrottling.
@Test
public void testConnectionThrottling() {
// Instrument the test to return a response with a 100ms throttle delay.
awaitReady(client, node);
short requestVersion = PRODUCE.latestVersion();
ProduceRequest.Builder builder = new ProduceRequest.Builder(requestVersion, requestVersion, new ProduceRequestData().setAcks((short) 1).setTimeoutMs(1000));
TestCallbackHandler handler = new TestCallbackHandler();
ClientRequest request = client.newClientRequest(node.idString(), builder, time.milliseconds(), true, defaultRequestTimeoutMs, handler);
client.send(request, time.milliseconds());
client.poll(1, time.milliseconds());
int throttleTime = 100;
ProduceResponse produceResponse = new ProduceResponse(new ProduceResponseData().setThrottleTimeMs(throttleTime));
ByteBuffer buffer = RequestTestUtils.serializeResponseWithHeader(produceResponse, requestVersion, request.correlationId());
selector.completeReceive(new NetworkReceive(node.idString(), buffer));
client.poll(1, time.milliseconds());
// The connection is not ready due to throttling.
assertFalse(client.ready(node, time.milliseconds()));
assertEquals(100, client.throttleDelayMs(node, time.milliseconds()));
// After 50ms, the connection is not ready yet.
time.sleep(50);
assertFalse(client.ready(node, time.milliseconds()));
assertEquals(50, client.throttleDelayMs(node, time.milliseconds()));
// After another 50ms, the throttling is done and the connection becomes ready again.
time.sleep(50);
assertTrue(client.ready(node, time.milliseconds()));
assertEquals(0, client.throttleDelayMs(node, time.milliseconds()));
}
use of org.apache.kafka.common.message.ProduceResponseData in project kafka by apache.
the class SenderTest method produceResponse.
private ProduceResponse produceResponse(Map<TopicPartition, OffsetAndError> responses) {
ProduceResponseData data = new ProduceResponseData();
for (Map.Entry<TopicPartition, OffsetAndError> entry : responses.entrySet()) {
TopicPartition topicPartition = entry.getKey();
ProduceResponseData.TopicProduceResponse topicData = data.responses().find(topicPartition.topic());
if (topicData == null) {
topicData = new ProduceResponseData.TopicProduceResponse().setName(topicPartition.topic());
data.responses().add(topicData);
}
OffsetAndError offsetAndError = entry.getValue();
ProduceResponseData.PartitionProduceResponse partitionData = new ProduceResponseData.PartitionProduceResponse().setIndex(topicPartition.partition()).setBaseOffset(offsetAndError.offset).setErrorCode(offsetAndError.error.code()).setRecordErrors(offsetAndError.recordErrors);
topicData.partitionResponses().add(partitionData);
}
return new ProduceResponse(data);
}
use of org.apache.kafka.common.message.ProduceResponseData in project kafka by apache.
the class ProduceResponse method toData.
private static ProduceResponseData toData(Map<TopicPartition, PartitionResponse> responses, int throttleTimeMs) {
ProduceResponseData data = new ProduceResponseData().setThrottleTimeMs(throttleTimeMs);
responses.forEach((tp, response) -> {
ProduceResponseData.TopicProduceResponse tpr = data.responses().find(tp.topic());
if (tpr == null) {
tpr = new ProduceResponseData.TopicProduceResponse().setName(tp.topic());
data.responses().add(tpr);
}
tpr.partitionResponses().add(new ProduceResponseData.PartitionProduceResponse().setIndex(tp.partition()).setBaseOffset(response.baseOffset).setLogStartOffset(response.logStartOffset).setLogAppendTimeMs(response.logAppendTime).setErrorMessage(response.errorMessage).setErrorCode(response.error.code()).setRecordErrors(response.recordErrors.stream().map(e -> new ProduceResponseData.BatchIndexAndErrorMessage().setBatchIndex(e.batchIndex).setBatchIndexErrorMessage(e.message)).collect(Collectors.toList())));
});
return data;
}
Aggregations