cmccabe commented on a change in pull request #10753: URL: https://github.com/apache/kafka/pull/10753#discussion_r667232604
########## File path: metadata/src/main/java/org/apache/kafka/controller/RemovingAndAddingReplicas.java ########## @@ -0,0 +1,214 @@ +/* + * 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.controller; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; +import java.util.TreeMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.Objects; + + +class RemovingAndAddingReplicas { + private final Set<Integer> removing; + private final Set<Integer> adding; + + RemovingAndAddingReplicas(int[] removing, int[] adding) { + this.removing = new HashSet<>(); + for (int replica : removing) { + this.removing.add(replica); + } + this.adding = new HashSet<>(); + for (int replica : adding) { + this.adding.add(replica); + } + } + + RemovingAndAddingReplicas(List<Integer> removing, List<Integer> adding) { + this.removing = new HashSet<>(removing); + this.adding = new HashSet<>(adding); + } + + /** + * Calculate what replicas need to be added and removed to reach a specific target + * replica list. + * + * @param currentReplicas The current replica list. + * @param targetReplicas The target replica list. + * + * @return An object containing the removing and adding replicas. + */ + static RemovingAndAddingReplicas forTarget(List<Integer> currentReplicas, + List<Integer> targetReplicas) { + List<Integer> removingReplicas = new ArrayList<>(); + List<Integer> addingReplicas = new ArrayList<>(); + List<Integer> sortedCurrentReplicas = new ArrayList<>(currentReplicas); + sortedCurrentReplicas.sort(Integer::compareTo); + List<Integer> sortedTargetReplicas = new ArrayList<>(targetReplicas); + sortedTargetReplicas.sort(Integer::compareTo); + int i = 0, j = 0; + while (true) { + if (i < sortedCurrentReplicas.size()) { + int currentReplica = sortedCurrentReplicas.get(i); + if (j < sortedTargetReplicas.size()) { + int targetReplica = sortedTargetReplicas.get(j); + if (currentReplica < targetReplica) { + removingReplicas.add(currentReplica); + i++; + } else if (currentReplica > targetReplica) { + addingReplicas.add(targetReplica); + j++; + } else { + i++; + j++; + } + } else { + removingReplicas.add(currentReplica); + i++; + } + } else if (j < sortedTargetReplicas.size()) { + int targetReplica = sortedTargetReplicas.get(j); + addingReplicas.add(targetReplica); + j++; + } else { + break; + } + } + return new RemovingAndAddingReplicas(removingReplicas, addingReplicas); + } + + /** + * Calculate the merged replica list following a reassignment. + * + * The merged list will contain all of the target replicas, in the order they appear + * in the target list. It will also contain existing replicas that are scheduled to + * be removed. + * + * If a removing replica was in position X in the original replica list, it will + * appear in the merged list following the appearance of X non-new replicas. Review comment: In the latest change, I have split this class into `PartitionReassignmentReplicas` and `PartitionReassignmentRevert`, which are both a lot simpler than `RemovingAndAddingReplicas` was. -- 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