public static Builder forMagic(byte magic, ProduceRequestData data) {
        // Message format upgrades correspond with a bump in the produce request version. Older
        // message format versions are generally not supported by the produce request versions
        // following the bump.

        final short minVersion;
        final short maxVersion;
        if (magic < RecordBatch.MAGIC_VALUE_V2) {
            minVersion = 2;
            maxVersion = 2;
        } else {
            minVersion = 3;
            maxVersion = ApiKeys.PRODUCE.latestVersion();
        }
        return new Builder(minVersion, maxVersion, data);
    }
@Test
    public void testMixedTransactionalData() {
        final long producerId = 15L;
        final short producerEpoch = 5;
        final int sequence = 10;

        final MemoryRecords nonTxnRecords = MemoryRecords.withRecords(Compression.NONE,
                new SimpleRecord("foo".getBytes()));
        final MemoryRecords txnRecords = MemoryRecords.withTransactionalRecords(Compression.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));
    }