smjn commented on code in PR #17149: URL: https://github.com/apache/kafka/pull/17149#discussion_r1779349855
########## share-coordinator/src/main/java/org/apache/kafka/coordinator/share/PersisterStateBatchCombiner.java: ########## @@ -0,0 +1,394 @@ +/* + * 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.coordinator.share; + +import org.apache.kafka.server.share.PersisterStateBatch; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.TreeSet; + +public class PersisterStateBatchCombiner { + private List<PersisterStateBatch> combinedBatchList; // link between pruning and merging + private final long startOffset; + private TreeSet<PersisterStateBatch> sortedBatches; + private List<PersisterStateBatch> finalBatchList; // final list is built here + + public PersisterStateBatchCombiner( + List<PersisterStateBatch> batchesSoFar, + List<PersisterStateBatch> newBatches, + long startOffset + ) { + initializeCombinedList(batchesSoFar, newBatches); + finalBatchList = new ArrayList<>(combinedBatchList.size() * 2); // heuristic size + this.startOffset = startOffset; + } + + private void initializeCombinedList(List<PersisterStateBatch> batchesSoFar, List<PersisterStateBatch> newBatches) { + boolean soFarEmpty = batchesSoFar == null || batchesSoFar.isEmpty(); + boolean newBatchesEmpty = newBatches == null || newBatches.isEmpty(); + + if (soFarEmpty && newBatchesEmpty) { + combinedBatchList = new ArrayList<>(); + } else if (soFarEmpty) { + combinedBatchList = newBatches; + } else if (newBatchesEmpty) { + combinedBatchList = batchesSoFar; + } else { + combinedBatchList = new ArrayList<>(batchesSoFar.size() + newBatches.size()); + combinedBatchList.addAll(batchesSoFar); + combinedBatchList.addAll(newBatches); + } + } + + /** + * Algorithm: Merge current state batches and new batches into a single non-overlapping batch list. + * Input: batchesSoFar, newBatches, startOffset + * Output: combined list with non-overlapping batches (finalBatchList) + * <p> + * - Add both currentBatches and newBatches into a single list combinedBatchList + * - Remove/prune any batches from the combinedBatchList: + * - if batch.lastOffset < startOffset then remove batch from combinedBatchList + * - else if batch.firstOffset > startOffset then we will keep the batch + * - else if batch.firstOffset <= startOffset <= batch.lastOffset then keep [startOffset, batch.lastOffset] part only and discard rest. + * - create a treeset sortedBatches using pruned combinedBatchList + * - do repeat until all batches in sortedSet are non-overlapping: + * - find first 2 overlapping batches in sortedBatches set, say, prev and candidate. + * - remove any non-overlapping batches from sortedBatches encountered during the find operation and add them to a finalBatchList + * - based on various conditions of offset overlap and batch state differences combine the batches or + * create new batches, if required, and add to the sortedSet. + * - done + * - return the finalBatchList + * + * @return list of {@link PersisterStateBatch} representing non-overlapping combined batches + */ + public List<PersisterStateBatch> combineStateBatches() { + pruneBatches(); + mergeBatches(); + return finalBatchList; + } + + private void mergeBatches() { + if (combinedBatchList.size() < 2) { + finalBatchList = combinedBatchList; + return; + } + + sortedBatches = new TreeSet<>(combinedBatchList); + + BatchOverlapState overlapState = getOverlappingState(); + + while (overlapState != BatchOverlapState.EMPTY) { + PersisterStateBatch prev = overlapState.prev(); + PersisterStateBatch candidate = overlapState.candidate(); + + // remove both previous and candidate for easier + // assessment about adding batches to sortedBatches + sortedBatches.remove(prev); + sortedBatches.remove(candidate); + + if (compareBatchState(candidate, prev) == 0) { // same state and overlap or contiguous + // overlap and same state (prev.firstOffset <= candidate.firstOffset) due to sort + // covers: + // case: 1 2 3 4 5 6 7 (contiguous) + // prev: ------ ------- ------- ------- ------- -------- ------- + // candidate: ------ ---- ---------- --- ---- ------- ------- + handleSameStateOverlap(prev, candidate); + } else { // diff state and non-contiguous overlap Review Comment: No it's not. getOverlappingState returns continuous pair ONLY IF the pair same delivery count and state. The condition in the if block will capture continuous case for same state. If it reaches else it has to be overlapping and different state. The getOverlappingState does not return continuous and different state pairs. In fact, it updates the treeset by removing any which are found. -- 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]
