@Test public void testV6AndBelowCannotUseZStdCompression() { ByteBuffer buffer = ByteBuffer.allocate(256); MemoryRecordsBuilder builder = MemoryRecords.builder(buffer, RecordBatch.MAGIC_VALUE_V2, CompressionType.ZSTD, TimestampType.CREATE_TIME, 0L); builder.append(10L, null, "a".getBytes()); Map<TopicPartition, MemoryRecords> produceData = new HashMap<>(); produceData.put(new TopicPartition("test", 0), builder.build()); // Can't create ProduceRequest instance with version within [3, 7) for (short version = 3; version < 7; version++) { ProduceRequest.Builder requestBuilder = new ProduceRequest.Builder(version, version, (short) 1, 5000, produceData, null); assertThrowsInvalidRecordExceptionForAllVersions(requestBuilder); } // Works fine with current version (>= 7) ProduceRequest.Builder.forCurrentMagic((short) 1, 5000, produceData); }
public static Builder forMagic(byte magic, short acks, int timeout, Map<TopicPartition, MemoryRecords> partitionRecords, String transactionalId) { // 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, acks, timeout, partitionRecords, transactionalId); }