FrankYang0529 commented on code in PR #19810:
URL: https://github.com/apache/kafka/pull/19810#discussion_r2106229955


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerWithLegacyMessageFormatIntegrationTest.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.compress.Compression;
+import org.apache.kafka.common.record.AbstractRecords;
+import org.apache.kafka.common.record.CompressionType;
+import org.apache.kafka.common.record.MemoryRecords;
+import org.apache.kafka.common.record.MemoryRecordsBuilder;
+import org.apache.kafka.common.record.RecordBatch;
+import org.apache.kafka.common.record.RecordVersion;
+import org.apache.kafka.common.record.SimpleRecord;
+import org.apache.kafka.common.record.TimestampType;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.storage.internals.log.UnifiedLog;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+
+@ClusterTestDefaults(
+    brokers = 3
+)
+public class ConsumerWithLegacyMessageFormatIntegrationTest {
+
+    private final ClusterInstance cluster;
+
+    private final String topic1 = "part-test-topic-1";
+    private final String topic2 = "part-test-topic-2";
+    private final String topic3 = "part-test-topic-3";
+
+    private final TopicPartition t1p0 = new TopicPartition(topic1, 0);
+    private final TopicPartition t1p1 = new TopicPartition(topic1, 1);
+    private final TopicPartition t2p0 = new TopicPartition(topic2, 0);
+    private final TopicPartition t2p1 = new TopicPartition(topic2, 1);
+    private final TopicPartition t3p0 = new TopicPartition(topic3, 0);
+    private final TopicPartition t3p1 = new TopicPartition(topic3, 1);
+
+    public ConsumerWithLegacyMessageFormatIntegrationTest(ClusterInstance 
cluster) {
+        this.cluster = cluster;
+    }
+
+    private void appendLegacyRecords(int numRecords, TopicPartition tp, int 
brokerId, byte magicValue) {
+        List<SimpleRecord> records = new ArrayList<>();
+        for (int i = 0; i < numRecords; i++) {
+            records.add(new SimpleRecord(i, ("key " + i).getBytes(), ("value " 
+ i).getBytes()));
+        }
+
+        ByteBuffer buffer = 
ByteBuffer.allocate(AbstractRecords.estimateSizeInBytes(magicValue,
+                CompressionType.NONE, records));
+        MemoryRecordsBuilder builder = MemoryRecords.builder(
+                buffer,
+                magicValue,
+                Compression.of(CompressionType.NONE).build(),
+                TimestampType.CREATE_TIME,
+                0L,
+                RecordBatch.NO_TIMESTAMP,
+                RecordBatch.NO_PRODUCER_ID,
+                RecordBatch.NO_PRODUCER_EPOCH,
+                0,
+                false,
+                RecordBatch.NO_PARTITION_LEADER_EPOCH
+        );
+
+        records.forEach(builder::append);
+
+        cluster.brokers().values().stream()
+                .filter(b -> b.config().brokerId() == brokerId)
+                .forEach(b -> {
+                    UnifiedLog unifiedLog = 
b.replicaManager().logManager().getLog(tp, false).get();
+                    
unifiedLog.appendAsLeaderWithRecordVersion(builder.build(), 0, 
RecordVersion.lookup(magicValue));
+                    try {
+                        // Default isolation.level is read_uncommitted. It 
makes Partition#fetchOffsetForTimestamp to return UnifiedLog#highWatermark,
+                        // so increasing high watermark to make it return the 
correct offset.
+                        
unifiedLog.maybeIncrementHighWatermark(unifiedLog.logEndOffsetMetadata());
+                    } catch (IOException e) {
+                        throw new RuntimeException(e);
+                    }

Review Comment:
   ```suggestion
                       // Default isolation.level is read_uncommitted. It makes 
Partition#fetchOffsetForTimestamp to return UnifiedLog#highWatermark,
                       // so increasing high watermark to make it return the 
correct offset.
                       assertDoesNotThrow(() -> 
unifiedLog.maybeIncrementHighWatermark(unifiedLog.logEndOffsetMetadata()));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to