fvaleri commented on code in PR #24261:
URL: https://github.com/apache/kafka/pull/24261#discussion_r4615399071


##########
server-common/src/main/java/org/apache/kafka/server/mirror/MirrorPartitionMetadata.java:
##########
@@ -16,34 +16,91 @@
  */
 package org.apache.kafka.server.mirror;
 
+import org.apache.kafka.server.common.OffsetAndEpoch;
+
 import java.util.Objects;
 
 /**
  * An immutable snapshot of the mirror metadata of a partition.
  *
- * @param state             The mirror partition state.
- * @param leaderEpoch       The highest leader epoch seen for the partition, 
or -1 if not available.
- * @param stateEpoch        The current state epoch for the partition.
- * @param lastMirrorEpoch   The last mirror epoch, or -1 if not available.
- * @param lastMirrorOffset  The last mirror offset, or -1 if not available.
- * @param errorMessage      The error message when in {@link 
MirrorPartitionState#FAILED} state, or null otherwise.
- * @param retryAttempt      The retry attempt count in {@link 
MirrorPartitionState#FAILED} state, 0 if not failed,
- *                          or -1 if non-retryable.
- * @param previousState     The mirror partition state before entering {@link 
MirrorPartitionState#FAILED},
- *                          or {@link MirrorPartitionState#UNKNOWN} if not 
recorded.
+ * @param state          The mirror partition state.
+ * @param stateEpoch     The current state epoch for the partition, which 
tells when the cached metadata can be safely
+ *                       updated.
+ * @param lastMirror     The last mirror epoch and offset, or {@link 
#EMPTY_LAST_MIRROR} if not available.
+ * @param errorMessage   The error message when in {@link 
MirrorPartitionState#FAILED} state, or null otherwise.
+ * @param retryAttempt   The retry attempt count in {@link 
MirrorPartitionState#FAILED} state, 0 if not failed,
+ *                       or -1 if non-retryable.
+ * @param previousState  The mirror partition state before entering {@link 
MirrorPartitionState#FAILED},
+ *                       or {@link MirrorPartitionState#UNKNOWN} if not 
recorded.
  */
 public record MirrorPartitionMetadata(
     MirrorPartitionState state,
-    int leaderEpoch,
     int stateEpoch,
-    int lastMirrorEpoch,
-    long lastMirrorOffset,
+    OffsetAndEpoch lastMirror,
     String errorMessage,
     short retryAttempt,
     MirrorPartitionState previousState
 ) {
+    /**
+     * The last mirror epoch and offset when they are not available.
+     */
+    public static final OffsetAndEpoch EMPTY_LAST_MIRROR = new 
OffsetAndEpoch(-1L, -1);
+
+    /**
+     * The metadata of a partition with no recorded state, with the default 
value of every field.
+     */
+    public static final MirrorPartitionMetadata EMPTY = builder().build();
+
     public MirrorPartitionMetadata {
         Objects.requireNonNull(state);
+        Objects.requireNonNull(lastMirror);
         Objects.requireNonNull(previousState);
     }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static class Builder {
+        private MirrorPartitionState state = MirrorPartitionState.UNKNOWN;
+        private int stateEpoch = 0;
+        private OffsetAndEpoch lastMirror = EMPTY_LAST_MIRROR;
+        private String errorMessage = null;
+        private short retryAttempt = 0;
+        private MirrorPartitionState previousState = 
MirrorPartitionState.UNKNOWN;
+
+        public Builder setState(MirrorPartitionState state) {

Review Comment:
   Wouldn't withState read more natural? Same for all the other setters.



##########
server-common/src/main/java/org/apache/kafka/server/mirror/MirrorPartitionMetadata.java:
##########
@@ -16,34 +16,91 @@
  */
 package org.apache.kafka.server.mirror;
 
+import org.apache.kafka.server.common.OffsetAndEpoch;
+
 import java.util.Objects;
 
 /**
  * An immutable snapshot of the mirror metadata of a partition.
  *
- * @param state             The mirror partition state.
- * @param leaderEpoch       The highest leader epoch seen for the partition, 
or -1 if not available.
- * @param stateEpoch        The current state epoch for the partition.
- * @param lastMirrorEpoch   The last mirror epoch, or -1 if not available.
- * @param lastMirrorOffset  The last mirror offset, or -1 if not available.
- * @param errorMessage      The error message when in {@link 
MirrorPartitionState#FAILED} state, or null otherwise.
- * @param retryAttempt      The retry attempt count in {@link 
MirrorPartitionState#FAILED} state, 0 if not failed,
- *                          or -1 if non-retryable.
- * @param previousState     The mirror partition state before entering {@link 
MirrorPartitionState#FAILED},
- *                          or {@link MirrorPartitionState#UNKNOWN} if not 
recorded.
+ * @param state          The mirror partition state.
+ * @param stateEpoch     The current state epoch for the partition, which 
tells when the cached metadata can be safely
+ *                       updated.
+ * @param lastMirror     The last mirror epoch and offset, or {@link 
#EMPTY_LAST_MIRROR} if not available.
+ * @param errorMessage   The error message when in {@link 
MirrorPartitionState#FAILED} state, or null otherwise.
+ * @param retryAttempt   The retry attempt count in {@link 
MirrorPartitionState#FAILED} state, 0 if not failed,
+ *                       or -1 if non-retryable.
+ * @param previousState  The mirror partition state before entering {@link 
MirrorPartitionState#FAILED},
+ *                       or {@link MirrorPartitionState#UNKNOWN} if not 
recorded.
  */
 public record MirrorPartitionMetadata(
     MirrorPartitionState state,
-    int leaderEpoch,
     int stateEpoch,
-    int lastMirrorEpoch,
-    long lastMirrorOffset,
+    OffsetAndEpoch lastMirror,
     String errorMessage,
     short retryAttempt,
     MirrorPartitionState previousState
 ) {
+    /**
+     * The last mirror epoch and offset when they are not available.
+     */
+    public static final OffsetAndEpoch EMPTY_LAST_MIRROR = new 
OffsetAndEpoch(-1L, -1);
+
+    /**
+     * The metadata of a partition with no recorded state, with the default 
value of every field.
+     */
+    public static final MirrorPartitionMetadata EMPTY = builder().build();
+
     public MirrorPartitionMetadata {
         Objects.requireNonNull(state);
+        Objects.requireNonNull(lastMirror);
         Objects.requireNonNull(previousState);
     }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static class Builder {
+        private MirrorPartitionState state = MirrorPartitionState.UNKNOWN;
+        private int stateEpoch = 0;
+        private OffsetAndEpoch lastMirror = EMPTY_LAST_MIRROR;
+        private String errorMessage = null;
+        private short retryAttempt = 0;
+        private MirrorPartitionState previousState = 
MirrorPartitionState.UNKNOWN;

Review Comment:
   Let's create a no-args constructor to set default values from EMPTY defined 
as constant:
   
   ```java
   public static final MirrorPartitionMetadata EMPTY = new 
MirrorPartitionMetadata(MirrorPartitionState.UNKNOWN, 0, EpochOffset.EMPTY, 
null, (short) 0, MirrorPartitionState.UNKNOWN);
   ```
   
   It is also useful to add a copy constructor taking an "existing" 
MirrorPartitionMetadata as input parameter. This will be used by the 
coordinator shard (leader for __mirror_state partition) to constantly evolve 
metadata through state transition. Without it, every mutation site has to 
manually decompose the record into individual fields and feed them to the 
builder, which is tedious and error-prone.



##########
server-common/src/test/java/org/apache/kafka/server/mirror/MirrorPartitionMetadataTest.java:
##########
@@ -25,46 +27,65 @@
 
 class MirrorPartitionMetadataTest {

Review Comment:
   Tests don't cover a couple of edge cases:
   
   -  partial builder usage (setting only some fields and verifying the rest 
default correctly)
   - the retryAttempt = -1 sentinel value that we can define as `public static 
final short NON_RETRYABLE_ERROR = -1` at the record level.



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

Reply via email to