junrao commented on a change in pull request #10753:
URL: https://github.com/apache/kafka/pull/10753#discussion_r669998610



##########
File path: 
metadata/src/main/java/org/apache/kafka/controller/PartitionReassignmentRevert.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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 org.apache.kafka.common.errors.InvalidReplicaAssignmentException;
+import org.apache.kafka.metadata.PartitionRegistration;
+import org.apache.kafka.metadata.Replicas;
+
+import java.util.ArrayList;
+import java.util.Set;
+import java.util.List;
+import java.util.Objects;
+
+
+class PartitionReassignmentRevert {
+    private final List<Integer> replicas;
+    private final List<Integer> isr;
+    private final boolean unclean;
+
+    PartitionReassignmentRevert(PartitionRegistration registration) {
+        // Figure out the replica list and ISR that we will have after 
reverting the
+        // reassignment. In general, we want to take out any replica that the 
reassignment
+        // was adding, but keep the ones the reassignment was removing. (But 
see the
+        // special case below.)
+        Set<Integer> adding = Replicas.toSet(registration.addingReplicas);
+        this.replicas = new ArrayList<>(registration.replicas.length);
+        this.isr = new ArrayList<>(registration.isr.length);
+        for (int i = 0; i < registration.isr.length; i++) {
+            int replica = registration.isr[i];
+            if (!adding.contains(replica)) {
+                this.isr.add(replica);
+            }
+        }
+        for (int replica : registration.replicas) {
+            if (!adding.contains(replica)) {
+                this.replicas.add(replica);
+            }
+        }
+        if (isr.isEmpty()) {
+            // In the special case that all the replicas that are in the ISR 
are also
+            // contained in addingReplicas, we choose the first remaining 
replica and add
+            // it to the ISR. This is considered an unclean leader election. 
Therefore,
+            // calling code must check that unclean leader election is enabled 
before
+            // accepting the new ISR.
+            if (this.replicas.isEmpty()) {
+                // This should not be reachable, since it would require a 
partition
+                // starting with an empty replica set prior to the 
reassignment we are
+                // trying to revert.
+                throw new InvalidReplicaAssignmentException("Invalid replica " 
+
+                    "assignment: addingReplicas contains all replicas.");
+            }
+            isr.add(replicas.get(0));

Review comment:
       Hmm, do we need to change isr here? It seems that BestLeader handles the 
unclean leader election with empty isr already.

##########
File path: 
metadata/src/main/java/org/apache/kafka/controller/PartitionChangeBuilder.java
##########
@@ -0,0 +1,243 @@
+/*
+ * 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 org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.metadata.PartitionChangeRecord;
+import org.apache.kafka.metadata.PartitionRegistration;
+import org.apache.kafka.metadata.Replicas;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import static 
org.apache.kafka.common.metadata.MetadataRecordType.PARTITION_CHANGE_RECORD;
+import static org.apache.kafka.metadata.LeaderConstants.NO_LEADER;
+import static org.apache.kafka.metadata.LeaderConstants.NO_LEADER_CHANGE;
+
+/**
+ * PartitionChangeBuilder handles changing partition registrations.
+ */
+public class PartitionChangeBuilder {
+    public static boolean changeRecordIsNoOp(PartitionChangeRecord record) {
+        if (record.isr() != null) return false;
+        if (record.leader() != NO_LEADER_CHANGE) return false;
+        if (record.replicas() != null) return false;
+        if (record.removingReplicas() != null) return false;
+        if (record.addingReplicas() != null) return false;
+        return true;
+    }
+
+    private final PartitionRegistration partition;
+    private final Uuid topicId;
+    private final int partitionId;
+    private final Function<Integer, Boolean> isAcceptableLeader;
+    private final Supplier<Boolean> uncleanElectionOk;
+    private List<Integer> targetIsr;
+    private List<Integer> targetReplicas;
+    private List<Integer> targetRemoving;
+    private List<Integer> targetAdding;
+    private boolean alwaysElectPreferredIfPossible;
+
+    public PartitionChangeBuilder(PartitionRegistration partition,
+                                  Uuid topicId,
+                                  int partitionId,
+                                  Function<Integer, Boolean> 
isAcceptableLeader,
+                                  Supplier<Boolean> uncleanElectionOk) {
+        this.partition = partition;
+        this.topicId = topicId;
+        this.partitionId = partitionId;
+        this.isAcceptableLeader = isAcceptableLeader;
+        this.uncleanElectionOk = uncleanElectionOk;
+        this.targetIsr = Replicas.toList(partition.isr);
+        this.targetReplicas = Replicas.toList(partition.replicas);
+        this.targetRemoving = Replicas.toList(partition.removingReplicas);
+        this.targetAdding = Replicas.toList(partition.addingReplicas);
+        this.alwaysElectPreferredIfPossible = false;
+    }
+
+    public PartitionChangeBuilder setTargetIsr(List<Integer> targetIsr) {
+        this.targetIsr = targetIsr;
+        return this;
+    }
+
+    public PartitionChangeBuilder setTargetReplicas(List<Integer> 
targetReplicas) {
+        this.targetReplicas = targetReplicas;
+        return this;
+    }
+
+    public PartitionChangeBuilder setAlwaysElectPreferredIfPossible(boolean 
alwaysElectPreferredIfPossible) {
+        this.alwaysElectPreferredIfPossible = alwaysElectPreferredIfPossible;
+        return this;
+    }
+
+    public PartitionChangeBuilder setTargetRemoving(List<Integer> 
targetRemoving) {
+        this.targetRemoving = targetRemoving;
+        return this;
+    }
+
+    public PartitionChangeBuilder setTargetAdding(List<Integer> targetAdding) {
+        this.targetAdding = targetAdding;
+        return this;
+    }
+
+    boolean shouldTryElection() {
+        // If the new isr doesn't have the current leader, we need to try to 
elect a new
+        // one. Note: this also handles the case where the current leader is 
NO_LEADER,
+        // since that value cannot appear in targetIsr.
+        if (!targetIsr.contains(partition.leader)) return true;
+
+        // Check if we want to try to get away from a non-preferred leader.
+        if (alwaysElectPreferredIfPossible && !partition.hasPreferredLeader()) 
return true;
+
+        return false;
+    }
+
+    class BestLeader {
+        final int node;
+        final boolean unclean;
+
+        BestLeader() {
+            for (int replica : targetIsr) {

Review comment:
       Hmm, replicas in targetIsr is not necessarily ordered in the same way as 
the order in the replica assignment. To favor preferred leader, it seems that 
we need to iterate the replicas in the order in targetReplicas?




-- 
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


Reply via email to