apoorvmittal10 commented on code in PR #22601: URL: https://github.com/apache/kafka/pull/22601#discussion_r3491585883
########## server/src/main/java/org/apache/kafka/server/share/dlq/ShareGroupDLQRecordFetcher.java: ########## @@ -0,0 +1,227 @@ +/* + * 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.protocol.Errors; +import org.apache.kafka.common.record.internal.Record; +import org.apache.kafka.common.record.internal.RecordBatch; +import org.apache.kafka.common.record.internal.Records; +import org.apache.kafka.common.requests.FetchRequest; +import org.apache.kafka.common.utils.Time; +import org.apache.kafka.server.share.LogReader; +import org.apache.kafka.server.storage.log.FetchIsolation; +import org.apache.kafka.server.storage.log.FetchParams; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletableFuture; + +/** + * Reads the original source records for the offset range described by a {@link ShareGroupDLQRecordParameter} + * so they can be copied into a DLQ record. Reads are issued one batch at a time in a loop via + * {@link LogReader#readAsync}, which combines the local read with the follow-up remote read for any offset + * tiered off the local log. When a read is already complete the loop continues in place; when it is still + * pending (a remote read in flight) the loop returns and is resumed from the callback - so the calling + * thread is never blocked on remote storage IO and the synchronous path never recurses. + * + * <p>Best-effort: the returned future always completes normally with whatever records could be read. + * Offsets that cannot be read - locally or remotely - are simply absent from the map, leaving the caller + * to produce a DLQ record with headers only for them. + * + * <p>Instances are single-use: create one fetcher per {@link #fetch()} call. + */ +public class ShareGroupDLQRecordFetcher { + private static final Logger log = LoggerFactory.getLogger(ShareGroupDLQRecordFetcher.class); + + private final LogReader logReader; + private final Time time; + private final ShareGroupDLQRecordParameter param; + + private final TopicIdPartition tp; + private final long endOffset; + private final int recordCount; + private final long startTime; + private final Map<Long, Record> recordMap; + // We are fetching data for one TopicIdPartition only. Hence, there is no need to keep recreating + // the maxBytes map, and we can re-use a single copy. In similar vein, we needn't clear the offsets + // map either and just update the value corresponding to the TopicIdPartition key across iterations. + private final LinkedHashMap<TopicIdPartition, Long> offsets = new LinkedHashMap<>(); + private final LinkedHashMap<TopicIdPartition, Integer> maxBytesMap = new LinkedHashMap<>(); + private final CompletableFuture<Map<Long, Record>> result = new CompletableFuture<>(); + private final FetchParams fetchParams; + + public ShareGroupDLQRecordFetcher(LogReader logReader, Time time, ShareGroupDLQRecordParameter param, int maxFetchBytes) { + this.logReader = logReader; + this.time = time; + this.param = param; + this.tp = param.topicIdPartition(); + this.endOffset = param.lastOffset(); + this.recordCount = (int) (param.lastOffset() - param.firstOffset() + 1); + this.startTime = time.hiResClockMs(); + this.recordMap = new HashMap<>(recordCount); + this.maxBytesMap.put(tp, maxFetchBytes); + this.fetchParams = new FetchParams( + FetchRequest.CONSUMER_REPLICA_ID, // -1, reading as a consumer + -1, // replicaEpoch + 0L, // maxWaitMs - don't block + 1, // minBytes + maxFetchBytes, // maxBytes + FetchIsolation.HIGH_WATERMARK, // committed only + Optional.empty() // clientMetadata + ); + } + + /** + * Fetches the source records for the configured offset range. + * + * @return A future that always completes normally with the records that could be read, keyed by offset. + */ + public CompletableFuture<Map<Long, Record>> fetch() { + try { + runFrom(param.firstOffset()); + } catch (Exception e) { + // Never let an unexpected error escape; skip record copy entirely. + log.warn("Unexpected error fetching records for {}. Skipping record copy.", param, e); + result.complete(Map.of()); + } + return result; + } + + // Visibility for testing + CompletableFuture<Map<Long, Record>> result() { + return result; + } Review Comment: nit: Move it to the end please, methods only being used for tests. -- 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]
