smjn commented on code in PR #23360: URL: https://github.com/apache/kafka/pull/23360#discussion_r3966794189
########## server/src/main/java/org/apache/kafka/server/share/dlq/ShareGroupDLQRecordHelper.java: ########## @@ -0,0 +1,265 @@ +/* + * 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.server.share.dlq; + +import org.apache.kafka.common.TopicIdPartition; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.compress.Compression; +import org.apache.kafka.common.header.Header; +import org.apache.kafka.common.header.internals.RecordHeader; +import org.apache.kafka.common.record.internal.DefaultRecord; +import org.apache.kafka.common.record.internal.DefaultRecordBatch; +import org.apache.kafka.common.record.internal.MemoryRecords; +import org.apache.kafka.common.record.internal.Record; +import org.apache.kafka.common.record.internal.SimpleRecord; +import org.apache.kafka.common.utils.Time; +import org.apache.kafka.server.share.LogReader; + +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +/** + * Helper for DLQ record building and source-record fetching. + */ +public class ShareGroupDLQRecordHelper { Review Comment: maybe add private constructor in line with ShareGroupDLQValidator, also class should be final ########## server/src/main/java/org/apache/kafka/server/share/dlq/ShareGroupDLQRecordHelper.java: ########## @@ -0,0 +1,265 @@ +/* + * 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.server.share.dlq; + +import org.apache.kafka.common.TopicIdPartition; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.compress.Compression; +import org.apache.kafka.common.header.Header; +import org.apache.kafka.common.header.internals.RecordHeader; +import org.apache.kafka.common.record.internal.DefaultRecord; +import org.apache.kafka.common.record.internal.DefaultRecordBatch; +import org.apache.kafka.common.record.internal.MemoryRecords; +import org.apache.kafka.common.record.internal.Record; +import org.apache.kafka.common.record.internal.SimpleRecord; +import org.apache.kafka.common.utils.Time; +import org.apache.kafka.server.share.LogReader; + +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +/** + * Helper for DLQ record building and source-record fetching. + */ +public class ShareGroupDLQRecordHelper { + + /** + * In most cases we expect the records getting DLQ'ed will be single offsets and + * not complete batches. Hence, using a large upper limit while reading from the log + * would be fruitless in most cases. Therefore, the value of 1 MB has been chosen + * for the DLQ-related log reads. + */ + private static final int DLQ_MAX_FETCH_BYTES = 1024 * 1024; + + protected static final String HEADER_DLQ_ERRORS_TOPIC = "__dlq.errors.topic"; + protected static final String HEADER_DLQ_ERRORS_PARTITION = "__dlq.errors.partition"; + protected static final String HEADER_DLQ_ERRORS_OFFSET = "__dlq.errors.offset"; + protected static final String HEADER_DLQ_ERRORS_GROUP = "__dlq.errors.group"; + protected static final String HEADER_DLQ_ERRORS_DELIVERY_COUNT = "__dlq.errors.delivery.count"; + protected static final String HEADER_DLQ_ERRORS_MESSAGE = "__dlq.errors.message"; + + /** + * Result of building DLQ records for a range of offsets, respecting maxMessageBytes. + * + * @param records The built MemoryRecords containing DLQ records with headers + * @param lastOffsetIncluded The last source offset included in this batch + * @param recordCount The number of individual records in the batch + */ + public record BuildResult(MemoryRecords records, long lastOffsetIncluded, int recordCount) { + } + + /** + * Builds DLQ headers for a single offset. + * + * @param sourceTopic The resolved source topic name + * @param partition The source partition number + * @param offset The source offset + * @param groupId The share group ID + * @param deliveryCount Optional delivery count + * @param cause Optional cause/reason for DLQ + * @return Array of DLQ headers + */ + private static Header[] headers( + String sourceTopic, + int partition, + long offset, + String groupId, + Optional<Short> deliveryCount, + Optional<Throwable> cause + ) { + String causeMessage = cause.map(Throwable::getMessage).orElse(null); + int size = 4 + (deliveryCount.isPresent() ? 1 : 0) + (causeMessage != null ? 1 : 0); + + Header[] headers = new Header[size]; + int counter = 0; + headers[counter++] = new RecordHeader(HEADER_DLQ_ERRORS_TOPIC, sourceTopic.getBytes(StandardCharsets.UTF_8)); + headers[counter++] = new RecordHeader(HEADER_DLQ_ERRORS_PARTITION, Integer.toString(partition).getBytes(StandardCharsets.UTF_8)); + headers[counter++] = new RecordHeader(HEADER_DLQ_ERRORS_OFFSET, Long.toString(offset).getBytes(StandardCharsets.UTF_8)); + headers[counter++] = new RecordHeader(HEADER_DLQ_ERRORS_GROUP, groupId.getBytes(StandardCharsets.UTF_8)); + if (deliveryCount.isPresent()) { + headers[counter++] = new RecordHeader(HEADER_DLQ_ERRORS_DELIVERY_COUNT, + Short.toString(deliveryCount.get()).getBytes(StandardCharsets.UTF_8)); + } + if (causeMessage != null) { + headers[counter] = new RecordHeader(HEADER_DLQ_ERRORS_MESSAGE, causeMessage.getBytes(StandardCharsets.UTF_8)); + } + return headers; + } + + /** + * Resolves the source topic name from a TopicIdPartition, falling back to the topic ID string. + * + * @param topicIdPartition The source topic-partition + * @param topicNameResolver Resolver that maps topic ID to name + * @return The resolved topic name + */ + public static String resolveSourceTopicName(TopicIdPartition topicIdPartition, java.util.function.Function<Uuid, Optional<String>> topicNameResolver) { Review Comment: import Function and use instead of fully qualified call ########## server/src/main/java/org/apache/kafka/server/share/dlq/DefaultShareGroupDLQManager.java: ########## @@ -90,37 +90,6 @@ public void stop() { } private static void validate(ShareGroupDLQRecordParameter param) { Review Comment: this method no longer serves a purpose, let us directly make the call to validator -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
