private void assertThrowsInvalidRecordException(ProduceRequest.Builder builder, short version) { try { builder.build(version).toStruct(); fail("Builder did not raise " + InvalidRecordException.class.getName() + " as expected"); } catch (RuntimeException e) { assertTrue("Unexpected exception type " + e.getClass().getName(), InvalidRecordException.class.isAssignableFrom(e.getClass())); } }
private ProduceRequest createProduceRequest(int version) { if (version < 2) throw new IllegalArgumentException("Produce request version 2 is not supported"); byte magic = version == 2 ? RecordBatch.MAGIC_VALUE_V1 : RecordBatch.MAGIC_VALUE_V2; MemoryRecords records = MemoryRecords.withRecords(magic, CompressionType.NONE, new SimpleRecord("woot".getBytes())); Map<TopicPartition, MemoryRecords> produceData = Collections.singletonMap(new TopicPartition("test", 0), records); return ProduceRequest.Builder.forMagic(magic, (short) 1, 5000, produceData, "transactionalId") .build((short) version); }
private ProduceRequest createNonIdempotentNonTransactionalRecords() { final MemoryRecords memoryRecords = MemoryRecords.withRecords(CompressionType.NONE, simpleRecord); return ProduceRequest.Builder.forCurrentMagic((short) -1, 10, Collections.singletonMap(new TopicPartition("topic", 1), memoryRecords)).build(); } }
@Test public void testMixedTransactionalData() { final long producerId = 15L; final short producerEpoch = 5; final int sequence = 10; final String transactionalId = "txnlId"; 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())); final Map<TopicPartition, MemoryRecords> recordsByPartition = new LinkedHashMap<>(); recordsByPartition.put(new TopicPartition("foo", 0), txnRecords); recordsByPartition.put(new TopicPartition("foo", 1), nonTxnRecords); final ProduceRequest.Builder builder = ProduceRequest.Builder.forMagic(RecordVersion.current().value, (short) -1, 5000, recordsByPartition, transactionalId); final ProduceRequest request = builder.build(); assertTrue(request.hasTransactionalRecords()); assertTrue(request.hasIdempotentRecords()); }
@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())); final Map<TopicPartition, MemoryRecords> recordsByPartition = new LinkedHashMap<>(); recordsByPartition.put(new TopicPartition("foo", 0), txnRecords); recordsByPartition.put(new TopicPartition("foo", 1), nonTxnRecords); final ProduceRequest.Builder builder = ProduceRequest.Builder.forMagic(RecordVersion.current().value, (short) -1, 5000, recordsByPartition, null); final ProduceRequest request = builder.build(); assertFalse(request.hasTransactionalRecords()); assertTrue(request.hasIdempotentRecords()); }
public ProduceRequest buildUnsafe(short version) { return build(version, false); }
@Override public ProduceRequest build(short version) { return build(version, true); }
@Test public void shouldBeFlaggedAsIdempotentWhenIdempotentRecords() throws Exception { final MemoryRecords memoryRecords = MemoryRecords.withIdempotentRecords(1, CompressionType.NONE, 1L, (short) 1, 1, 1, simpleRecord); final ProduceRequest request = ProduceRequest.Builder.forCurrentMagic((short) -1, 10, Collections.singletonMap(new TopicPartition("topic", 1), memoryRecords)).build(); assertTrue(request.hasIdempotentRecords()); }
@Test public void shouldBeFlaggedAsTransactionalWhenTransactionalRecords() throws Exception { final MemoryRecords memoryRecords = MemoryRecords.withTransactionalRecords(0, CompressionType.NONE, 1L, (short) 1, 1, 1, simpleRecord); final ProduceRequest request = ProduceRequest.Builder.forCurrentMagic((short) -1, 10, Collections.singletonMap(new TopicPartition("topic", 1), memoryRecords)).build(); assertTrue(request.hasTransactionalRecords()); }