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



##########
File path: 
metadata/src/main/java/org/apache/kafka/controller/PartitionChangeBuilder.java
##########
@@ -0,0 +1,229 @@
+/*
+ * 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;
+
+/**
+ * PartitionMutator 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) {

Review comment:
       I also added a unit test for `electLeaders`




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