You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "jsancio (via GitHub)" <gi...@apache.org> on 2023/06/01 00:18:35 UTC

[GitHub] [kafka] jsancio commented on a diff in pull request #13788: KAFKA-14791: Create a builder for PartitionRegistration

jsancio commented on code in PR #13788:
URL: https://github.com/apache/kafka/pull/13788#discussion_r1212435786


##########
metadata/src/main/java/org/apache/kafka/metadata/PartitionRegistration.java:
##########
@@ -57,7 +143,7 @@ public PartitionRegistration(PartitionRecord record) {
             record.partitionEpoch());
     }
 
-    public PartitionRegistration(int[] replicas, int[] isr, int[] removingReplicas,
+    protected PartitionRegistration(int[] replicas, int[] isr, int[] removingReplicas,

Review Comment:
   Let's make this private. We should discourage inheritance of this class.



##########
metadata/src/main/java/org/apache/kafka/metadata/PartitionRegistration.java:
##########
@@ -33,6 +33,92 @@
 
 
 public class PartitionRegistration {
+
+    /**
+     * A builder class which creates a PartitionRegistration.
+     */
+    static public class Builder {
+        private int[] replicas;
+        private int[] isr;
+        private int[] removingReplicas;
+        private int[] addingReplicas;
+        private Integer leader;
+        private LeaderRecoveryState leaderRecoveryState;
+        private Integer leaderEpoch;
+        private Integer partitionEpoch;
+
+        public Builder setReplicas(int[] replicas) {
+            this.replicas = replicas;
+            return this;
+        }
+
+        public Builder setIsr(int[] isr) {
+            this.isr = isr;
+            return this;
+        }
+
+        public Builder setRemovingReplicas(int[] removingReplicas) {
+            this.removingReplicas = removingReplicas;
+            return this;
+        }
+
+        public Builder setAddingReplicas(int[] addingReplicas) {
+            this.addingReplicas = addingReplicas;
+            return this;
+        }
+
+        public Builder setLeader(Integer leader) {
+            this.leader = leader;
+            return this;
+        }
+
+        public Builder setLeaderRecoveryState(LeaderRecoveryState leaderRecoveryState) {
+            this.leaderRecoveryState = leaderRecoveryState;
+            return this;
+        }
+
+        public Builder setLeaderEpoch(Integer leaderEpoch) {
+            this.leaderEpoch = leaderEpoch;
+            return this;
+        }
+
+        public Builder setPartitionEpoch(Integer partitionEpoch) {
+            this.partitionEpoch = partitionEpoch;
+            return this;
+        }
+
+        public PartitionRegistration build() throws Exception {

Review Comment:
   You should be able to remove the `throws Exception` since `IllegalStateException` is a `RuntimeException`.



##########
metadata/src/test/java/org/apache/kafka/controller/PartitionChangeBuilderTest.java:
##########
@@ -77,39 +77,75 @@ public void testChangeRecordIsNoOp() {
         );
     }
 
-    private final static PartitionRegistration FOO = new PartitionRegistration(
-        new int[] {2, 1, 3}, new int[] {2, 1, 3}, Replicas.NONE, Replicas.NONE,
-        1, LeaderRecoveryState.RECOVERED, 100, 200);
+    private static final PartitionRegistration FOO;
+
+    static {
+        try {
+            FOO = new PartitionRegistration.Builder().
+                setReplicas(new int[] {2, 1, 3}).
+                setIsr(new int[] {2, 1, 3}).
+                setRemovingReplicas(Replicas.NONE).
+                setAddingReplicas(Replicas.NONE).

Review Comment:
   We can remove this if we make the default value for these two fields `Replicas.NONE`.



##########
metadata/src/test/java/org/apache/kafka/controller/PartitionChangeBuilderTest.java:
##########
@@ -77,39 +77,75 @@ public void testChangeRecordIsNoOp() {
         );
     }
 
-    private final static PartitionRegistration FOO = new PartitionRegistration(
-        new int[] {2, 1, 3}, new int[] {2, 1, 3}, Replicas.NONE, Replicas.NONE,
-        1, LeaderRecoveryState.RECOVERED, 100, 200);
+    private static final PartitionRegistration FOO;
+
+    static {
+        try {
+            FOO = new PartitionRegistration.Builder().
+                setReplicas(new int[] {2, 1, 3}).
+                setIsr(new int[] {2, 1, 3}).
+                setRemovingReplicas(Replicas.NONE).
+                setAddingReplicas(Replicas.NONE).
+                setLeader(1).
+                setLeaderRecoveryState(LeaderRecoveryState.RECOVERED).
+                setLeaderEpoch(100).
+                setPartitionEpoch(200).
+                build();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }

Review Comment:
   Isn't `IllegalStateExection` a runtime exception?



##########
metadata/src/main/java/org/apache/kafka/metadata/PartitionRegistration.java:
##########
@@ -33,6 +33,92 @@
 
 
 public class PartitionRegistration {
+
+    /**
+     * A builder class which creates a PartitionRegistration.
+     */
+    static public class Builder {
+        private int[] replicas;
+        private int[] isr;
+        private int[] removingReplicas;
+        private int[] addingReplicas;
+        private Integer leader;
+        private LeaderRecoveryState leaderRecoveryState;
+        private Integer leaderEpoch;
+        private Integer partitionEpoch;
+
+        public Builder setReplicas(int[] replicas) {
+            this.replicas = replicas;
+            return this;
+        }
+
+        public Builder setIsr(int[] isr) {
+            this.isr = isr;
+            return this;
+        }
+
+        public Builder setRemovingReplicas(int[] removingReplicas) {
+            this.removingReplicas = removingReplicas;
+            return this;
+        }
+
+        public Builder setAddingReplicas(int[] addingReplicas) {
+            this.addingReplicas = addingReplicas;
+            return this;
+        }
+
+        public Builder setLeader(Integer leader) {
+            this.leader = leader;
+            return this;
+        }
+
+        public Builder setLeaderRecoveryState(LeaderRecoveryState leaderRecoveryState) {
+            this.leaderRecoveryState = leaderRecoveryState;
+            return this;
+        }
+
+        public Builder setLeaderEpoch(Integer leaderEpoch) {
+            this.leaderEpoch = leaderEpoch;
+            return this;
+        }
+
+        public Builder setPartitionEpoch(Integer partitionEpoch) {
+            this.partitionEpoch = partitionEpoch;
+            return this;
+        }
+
+        public PartitionRegistration build() throws Exception {
+            if (replicas == null) {
+                throw new IllegalStateException("You must set replicas.");
+            } else if (isr == null) {
+                throw new IllegalStateException("You must set isr.");
+            } else if (removingReplicas == null) {
+                throw new IllegalStateException("You must set removing replicas.");
+            } else if (addingReplicas == null) {
+                throw new IllegalStateException("You must set adding replicas.");

Review Comment:
   Can we default this to be the empty array: `int[0]`?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org