Search in sources :

Example 11 with ProduceRequestData

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

the class ProduceRequestTest method testV3AndAboveShouldContainOnlyOneRecordBatch.

@Test
public void testV3AndAboveShouldContainOnlyOneRecordBatch() {
    ByteBuffer buffer = ByteBuffer.allocate(256);
    MemoryRecordsBuilder builder = MemoryRecords.builder(buffer, CompressionType.NONE, TimestampType.CREATE_TIME, 0L);
    builder.append(10L, null, "a".getBytes());
    builder.close();
    builder = MemoryRecords.builder(buffer, CompressionType.NONE, TimestampType.CREATE_TIME, 1L);
    builder.append(11L, "1".getBytes(), "b".getBytes());
    builder.append(12L, null, "c".getBytes());
    builder.close();
    buffer.flip();
    ProduceRequest.Builder requestBuilder = ProduceRequest.forCurrentMagic(new ProduceRequestData().setTopicData(new ProduceRequestData.TopicProduceDataCollection(Collections.singletonList(new ProduceRequestData.TopicProduceData().setName("test").setPartitionData(Collections.singletonList(new ProduceRequestData.PartitionProduceData().setIndex(0).setRecords(MemoryRecords.readableRecords(buffer))))).iterator())).setAcks((short) 1).setTimeoutMs(5000));
    assertThrowsForAllVersions(requestBuilder, InvalidRecordException.class);
}
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)

Example 12 with ProduceRequestData

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

the class ProduceRequestTest method shouldBeFlaggedAsIdempotentWhenIdempotentRecords.

@Test
public void shouldBeFlaggedAsIdempotentWhenIdempotentRecords() {
    final MemoryRecords memoryRecords = MemoryRecords.withIdempotentRecords(1, 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(RequestTestUtils.hasIdempotentRecords(request));
}
Also used : ProduceRequestData(org.apache.kafka.common.message.ProduceRequestData) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) Test(org.junit.jupiter.api.Test)

Example 13 with ProduceRequestData

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

the class ProduceRequestTest method testMixedIdempotentData.

@Test
public void testMixedIdempotentData() {
    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.withIdempotentRecords(CompressionType.NONE, producerId, producerEpoch, sequence, new SimpleRecord("bar".getBytes()));
    ProduceRequest.Builder builder = ProduceRequest.forMagic(RecordVersion.current().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();
    assertFalse(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 14 with ProduceRequestData

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

the class ProduceRequestTest method testV3AndAboveCannotUseMagicV1.

@Test
public void testV3AndAboveCannotUseMagicV1() {
    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.forCurrentMagic(new ProduceRequestData().setTopicData(new ProduceRequestData.TopicProduceDataCollection(Collections.singletonList(new ProduceRequestData.TopicProduceData().setName("test").setPartitionData(Collections.singletonList(new ProduceRequestData.PartitionProduceData().setIndex(0).setRecords(builder.build())))).iterator())).setAcks((short) 1).setTimeoutMs(5000));
    assertThrowsForAllVersions(requestBuilder, InvalidRecordException.class);
}
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)

Example 15 with ProduceRequestData

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

the class ProduceRequestTest method testV3AndAboveCannotUseMagicV0.

@Test
public void testV3AndAboveCannotUseMagicV0() {
    ByteBuffer buffer = ByteBuffer.allocate(256);
    MemoryRecordsBuilder builder = MemoryRecords.builder(buffer, RecordBatch.MAGIC_VALUE_V0, CompressionType.NONE, TimestampType.NO_TIMESTAMP_TYPE, 0L);
    builder.append(10L, null, "a".getBytes());
    ProduceRequest.Builder requestBuilder = ProduceRequest.forCurrentMagic(new ProduceRequestData().setTopicData(new ProduceRequestData.TopicProduceDataCollection(Collections.singletonList(new ProduceRequestData.TopicProduceData().setName("test").setPartitionData(Collections.singletonList(new ProduceRequestData.PartitionProduceData().setIndex(0).setRecords(builder.build())))).iterator())).setAcks((short) 1).setTimeoutMs(5000));
    assertThrowsForAllVersions(requestBuilder, InvalidRecordException.class);
}
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