You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "jsancio (via GitHub)" <gi...@apache.org> on 2026/08/03 15:07:05 UTC
Re: [PR] KAFKA-16517 Do not decode metadata records in the internal kraft partition listener [kafka]
jsancio commented on code in PR #22805:
URL: https://github.com/apache/kafka/pull/22805#discussion_r3705148816
##########
raft/src/test/java/org/apache/kafka/raft/internals/RecordsDecodingStrategyTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.raft.internals;
+
+import org.apache.kafka.common.message.KRaftVersionRecord;
+import org.apache.kafka.common.record.internal.CompressionType;
+import org.apache.kafka.common.record.internal.ControlRecordType;
+import org.apache.kafka.common.record.internal.MemoryRecords;
+import org.apache.kafka.common.utils.internals.BufferSupplier;
+import org.apache.kafka.common.utils.internals.LogContext;
+import org.apache.kafka.raft.Batch;
+import org.apache.kafka.raft.ControlRecord;
+import org.apache.kafka.raft.internals.RecordsIteratorTest.TestBatch;
+
+import org.junit.jupiter.api.Test;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+final class RecordsDecodingStrategyTest {
+ private static final StringSerde STRING_SERDE = new StringSerde();
+
+ // The records under test: a control batch at offset 0 followed by a data batch at offsets 1,2,3.
+ private static final long CONTROL_BASE_OFFSET = 0L;
+ private static final long DATA_BASE_OFFSET = 1L;
+ private static final List<String> DATA_RECORDS = List.of("a", "b", "c");
+ private static final long DATA_LAST_OFFSET = DATA_BASE_OFFSET + DATA_RECORDS.size() - 1;
+ private static final List<ControlRecord> CONTROL_RECORDS = List.of(ControlRecord.of(new KRaftVersionRecord()));
+ private static final MemoryRecords RECORDS = controlBatchThenDataBatch();
+
+ private static RecordsIterator<String> iterator(RecordsDecodingStrategy<String> strategy) {
+ return new RecordsIterator<>(
+ RECORDS,
+ strategy,
+ BufferSupplier.NO_CACHING,
+ Integer.MAX_VALUE,
+ true,
+ new LogContext()
+ );
+ }
Review Comment:
This is testing RecordsDecodingStartegy indirectly through the RecordsIterator. Can you test `RecordsDecofingStrategy::readBatch` by calling the method directly?
##########
raft/src/main/java/org/apache/kafka/raft/internals/RecordsDecodingStrategy.java:
##########
@@ -0,0 +1,258 @@
+/*
+ * 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.raft.internals;
+
+import org.apache.kafka.common.protocol.ByteBufferAccessor;
+import org.apache.kafka.common.record.internal.DefaultRecordBatch;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.common.utils.internals.BufferSupplier;
+import org.apache.kafka.common.utils.internals.ByteUtils;
+import org.apache.kafka.raft.Batch;
+import org.apache.kafka.raft.ControlRecord;
+import org.apache.kafka.server.common.serialization.RecordSerde;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.BiFunction;
+
+/**
+ * Decides which records {@link RecordsIterator} decodes when turning a batch into a {@link Batch}.
+ * A batch's records are decoded only when this strategy is interested in them; otherwise the batch
+ * is returned as a {@link Batch#skipped} batch carrying only the offset information.
+ *
+ * <p>Use one of the factory methods to select the behavior:
+ * <ul>
+ * <li>{@link #dataAndControl} decodes both the control and data records.</li>
+ * <li>{@link #controlOnly} decodes only the control records and skips the data records. Used by
+ * the internal kraft partition listener, which needs no serde.</li>
+ * <li>{@link #dataOnly} decodes only the data records and skips the control records.</li>
+ * <li>{@link #none} skips both.</li>
+ * </ul>
+ */
+public final class RecordsDecodingStrategy<T> {
+ private final boolean decodeControlRecords;
+ // When present, data records are decoded with this serde; when empty, they are skipped.
+ private final Optional<RecordSerde<T>> serde;
+
+ private RecordsDecodingStrategy(boolean decodeControlRecords, Optional<RecordSerde<T>> serde) {
+ this.decodeControlRecords = decodeControlRecords;
+ this.serde = serde;
+ }
+
+ /**
+ * Decodes both the control and data records of a batch.
+ */
+ public static <T> RecordsDecodingStrategy<T> dataAndControl(RecordSerde<T> serde) {
+ return new RecordsDecodingStrategy<>(true, Optional.of(serde));
+ }
+
+ /**
+ * Decodes only the data records of a batch and skips the control records.
+ */
+ public static <T> RecordsDecodingStrategy<T> dataOnly(RecordSerde<T> serde) {
+ return new RecordsDecodingStrategy<>(false, Optional.of(serde));
+ }
+
+ /**
+ * Decodes only the control records of a batch and skips the data records.
+ */
+ public static <T> RecordsDecodingStrategy<T> controlOnly() {
+ return new RecordsDecodingStrategy<>(true, Optional.empty());
+ }
+
+ /**
+ * Skips both the control and data records of a batch.
+ */
+ public static <T> RecordsDecodingStrategy<T> none() {
+ return new RecordsDecodingStrategy<>(false, Optional.empty());
+ }
+
+ Batch<T> readBatch(DefaultRecordBatch batch, BufferSupplier bufferSupplier, int numRecords) {
Review Comment:
This should be public and should be documented with Java Doc. This is this type's only public interface.
The signature should be `Batch<T> readBatch(DefaultRecordBatch, BufferSupplier)`. Number of records is a property of the default record batch.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org