Search in sources :

Example 1 with ProduceRequestData

use of org.apache.kafka.common.message.ProduceRequestData 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 2 with ProduceRequestData

use of org.apache.kafka.common.message.ProduceRequestData 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 3 with ProduceRequestData

use of org.apache.kafka.common.message.ProduceRequestData in project kafka by apache.

the class ProduceRequestTest method testMixedTransactionalData.

@Test
public void testMixedTransactionalData() {
    final long producerId = 15L;
    final short producerEpoch = 5;
    final int sequence = 10;
    final MemoryRecords nonTxnRecords = MemoryRecords.withRecords(CompressionType.NONE, new SimpleRecord("foo".getBytes()));
    final MemoryRecords txnRecords = MemoryRecords.withTransactionalRecords(CompressionType.NONE, producerId, producerEpoch, sequence, new SimpleRecord("bar".getBytes()));
    ProduceRequest.Builder builder = ProduceRequest.forMagic(RecordBatch.CURRENT_MAGIC_VALUE, new ProduceRequestData().setTopicData(new ProduceRequestData.TopicProduceDataCollection(Arrays.asList(new ProduceRequestData.TopicProduceData().setName("foo").setPartitionData(Collections.singletonList(new ProduceRequestData.PartitionProduceData().setIndex(0).setRecords(txnRecords))), new ProduceRequestData.TopicProduceData().setName("foo").setPartitionData(Collections.singletonList(new ProduceRequestData.PartitionProduceData().setIndex(1).setRecords(nonTxnRecords)))).iterator())).setAcks((short) -1).setTimeoutMs(5000));
    final ProduceRequest request = builder.build();
    assertTrue(RequestUtils.hasTransactionalRecords(request));
    assertTrue(RequestTestUtils.hasIdempotentRecords(request));
}
Also used : ProduceRequestData(org.apache.kafka.common.message.ProduceRequestData) SimpleRecord(org.apache.kafka.common.record.SimpleRecord) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) Test(org.junit.jupiter.api.Test)

Example 4 with ProduceRequestData

use of org.apache.kafka.common.message.ProduceRequestData in project kafka by apache.

the class ProduceRequestTest method shouldBeFlaggedAsTransactionalWhenTransactionalRecords.

@Test
public void shouldBeFlaggedAsTransactionalWhenTransactionalRecords() {
    final MemoryRecords memoryRecords = MemoryRecords.withTransactionalRecords(0, CompressionType.NONE, 1L, (short) 1, 1, 1, simpleRecord);
    final ProduceRequest request = ProduceRequest.forCurrentMagic(new ProduceRequestData().setTopicData(new ProduceRequestData.TopicProduceDataCollection(Collections.singletonList(new ProduceRequestData.TopicProduceData().setName("topic").setPartitionData(Collections.singletonList(new ProduceRequestData.PartitionProduceData().setIndex(1).setRecords(memoryRecords)))).iterator())).setAcks((short) -1).setTimeoutMs(10)).build();
    assertTrue(RequestUtils.hasTransactionalRecords(request));
}
Also used : ProduceRequestData(org.apache.kafka.common.message.ProduceRequestData) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) Test(org.junit.jupiter.api.Test)

Example 5 with ProduceRequestData

use of org.apache.kafka.common.message.ProduceRequestData in project kafka by apache.

the class ProduceRequestTest method testBuildWithOldMessageFormat.

@Test
public void testBuildWithOldMessageFormat() {
    ByteBuffer buffer = ByteBuffer.allocate(256);
    MemoryRecordsBuilder builder = MemoryRecords.builder(buffer, RecordBatch.MAGIC_VALUE_V1, CompressionType.NONE, TimestampType.CREATE_TIME, 0L);
    builder.append(10L, null, "a".getBytes());
    ProduceRequest.Builder requestBuilder = ProduceRequest.forMagic(RecordBatch.MAGIC_VALUE_V1, new ProduceRequestData().setTopicData(new ProduceRequestData.TopicProduceDataCollection(Collections.singletonList(new ProduceRequestData.TopicProduceData().setName("test").setPartitionData(Collections.singletonList(new ProduceRequestData.PartitionProduceData().setIndex(9).setRecords(builder.build())))).iterator())).setAcks((short) 1).setTimeoutMs(5000));
    assertEquals(2, requestBuilder.oldestAllowedVersion());
    assertEquals(2, requestBuilder.latestAllowedVersion());
}
Also used : ProduceRequestData(org.apache.kafka.common.message.ProduceRequestData) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ProduceRequestData (org.apache.kafka.common.message.ProduceRequestData)16 Test (org.junit.jupiter.api.Test)13 ByteBuffer (java.nio.ByteBuffer)9 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)7 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)6 SimpleRecord (org.apache.kafka.common.record.SimpleRecord)4 ProduceRequest (org.apache.kafka.common.requests.ProduceRequest)4 ProduceResponse (org.apache.kafka.common.requests.ProduceResponse)4 ApiVersions (org.apache.kafka.clients.ApiVersions)2 ClientRequest (org.apache.kafka.clients.ClientRequest)2 Cluster (org.apache.kafka.common.Cluster)2 MetricName (org.apache.kafka.common.MetricName)2 Node (org.apache.kafka.common.Node)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 ProduceResponseData (org.apache.kafka.common.message.ProduceResponseData)2 Sensor (org.apache.kafka.common.metrics.Sensor)2 NetworkReceive (org.apache.kafka.common.network.NetworkReceive)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1