Search in sources :

Example 1 with ProduceResponseData

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);
}
Also used : ProduceResponseData(org.apache.kafka.common.message.ProduceResponseData)

Example 2 with ProduceResponseData

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");
}
Also used : ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) ProduceResponse(org.apache.kafka.common.requests.ProduceResponse) ProduceRequestData(org.apache.kafka.common.message.ProduceRequestData) NetworkReceive(org.apache.kafka.common.network.NetworkReceive) ProduceResponseData(org.apache.kafka.common.message.ProduceResponseData) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ProduceResponseData

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()));
}
Also used : ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) ProduceResponse(org.apache.kafka.common.requests.ProduceResponse) ProduceRequestData(org.apache.kafka.common.message.ProduceRequestData) NetworkReceive(org.apache.kafka.common.network.NetworkReceive) ProduceResponseData(org.apache.kafka.common.message.ProduceResponseData) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with ProduceResponseData

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);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ProduceResponse(org.apache.kafka.common.requests.ProduceResponse) ProduceResponseData(org.apache.kafka.common.message.ProduceResponseData) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap)

Example 5 with ProduceResponseData

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;
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) HashMap(java.util.HashMap) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) Objects(java.util.Objects) List(java.util.List) RecordBatch(org.apache.kafka.common.record.RecordBatch) Map(java.util.Map) Errors(org.apache.kafka.common.protocol.Errors) Collections(java.util.Collections) ProduceResponseData(org.apache.kafka.common.message.ProduceResponseData) ProduceResponseData(org.apache.kafka.common.message.ProduceResponseData)

Aggregations

ProduceResponseData (org.apache.kafka.common.message.ProduceResponseData)6 ProduceResponse (org.apache.kafka.common.requests.ProduceResponse)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 ProduceRequestData (org.apache.kafka.common.message.ProduceRequestData)2 NetworkReceive (org.apache.kafka.common.network.NetworkReceive)2 ProduceRequest (org.apache.kafka.common.requests.ProduceRequest)2 Collections (java.util.Collections)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)1 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)1 Errors (org.apache.kafka.common.protocol.Errors)1 RecordBatch (org.apache.kafka.common.record.RecordBatch)1 Test (org.junit.jupiter.api.Test)1