TaiJuWu commented on code in PR #24414:
URL: https://github.com/apache/kafka/pull/24414#discussion_r4729516819


##########
server-common/src/main/java/org/apache/kafka/server/mirror/MirrorPartitionMetadata.java:
##########
@@ -113,12 +130,116 @@ public Builder withRetryAttempt(short retryAttempt) {
         }
 
         public Builder withPreviousState(MirrorPartitionState previousState) {
-            this.previousState = previousState;
+            this.previousState = Objects.requireNonNull(previousState);
+            return this;
+        }
+
+        /**
+         * Clears all failure-related fields: sets the error message to empty, 
the retry attempt count to zero,
+         * and the previous state to {@link MirrorPartitionState#UNKNOWN}. Use 
this after a successful recovery
+         * from {@link MirrorPartitionState#FAILED} to reset the failure/retry 
sequence.
+         */
+        public Builder withFailedStateReset() {
+            this.errorMessage = Optional.empty();
+            this.retryAttempt = (short) 0;
+            this.previousState = MirrorPartitionState.UNKNOWN;
+            return this;
+        }
+
+        /**
+         * Transitions to the given state and computes error-related fields.
+         *
+         * <p>When entering {@link MirrorPartitionState#FAILED}, sets the 
error message, computes the next retry attempt
+         * count, and records the previous state. If the partition is already 
in FAILED, the previous state is
+         * preserved across consecutive failures.
+         *
+         * <p>When entering {@link MirrorPartitionState#STOPPED} or {@link 
MirrorPartitionState#PAUSED}, or when
+         * transitioning between non-FAILED states, clears all error fields.
+         *
+         * <p>Otherwise, clears the error message.
+         *
+         * @param targetState      the target state of the transition.
+         * @param newStateEpoch    the state epoch for the transition.
+         * @param newErrorMessage  the error message to record, or empty if 
none.
+         * @param nonRetryable     whether the error is non-retryable (only 
relevant when entering FAILED).
+         * @param maxRetryAttempts the upper bound for the retry attempt count 
(only relevant when entering FAILED).
+         */
+        public Builder withStateTransition(MirrorPartitionState targetState,
+                                           int newStateEpoch,
+                                           Optional<String> newErrorMessage,
+                                           boolean nonRetryable,
+                                           short maxRetryAttempts) {
+            if (targetState == MirrorPartitionState.FAILED) {
+                if (maxRetryAttempts < 0) {
+                    throw new IllegalArgumentException("maxRetryAttempts must 
be non-negative");
+                }
+                this.errorMessage = newErrorMessage;
+                if (nonRetryable || this.retryAttempt == NON_RETRYABLE_ERROR) {
+                    this.retryAttempt = NON_RETRYABLE_ERROR;
+                } else {
+                    this.retryAttempt = (short) Math.min(maxRetryAttempts, 
this.retryAttempt + 1);
+                }
+                if (this.state != MirrorPartitionState.FAILED) {
+                    this.previousState = this.state;
+                }
+            } else if ((this.state != MirrorPartitionState.FAILED && 
this.state != targetState)
+                    || targetState == MirrorPartitionState.STOPPED
+                    || targetState == MirrorPartitionState.PAUSED) {
+                withFailedStateReset();
+            } else {
+                this.errorMessage = Optional.empty();
+            }
+            this.state = targetState;
+            this.stateEpoch = newStateEpoch;
             return this;
         }
 
         public MirrorPartitionMetadata build() {
-            return new MirrorPartitionMetadata(state, stateEpoch, lastMirror, 
errorMessage, retryAttempt, previousState);
+            return new MirrorPartitionMetadata(state, stateEpoch,
+                    lastMirror, errorMessage, retryAttempt, previousState);
+        }
+    }
+
+    /**
+     * Checks whether a transition from {@code source} to {@code target} is 
valid according to the
+     * mirror partition state machine defined in KIP-1279. Self-transitions 
are always valid, the
+     * {@link MirrorPartitionState#FAILED} state is reachable from any state, 
and any transition
+     * out of {@link MirrorPartitionState#FAILED} is permitted to allow 
retries.
+     * {@link MirrorPartitionState#UNKNOWN} is not a valid target 
(self-transition aside) because it
+     * represents the absence of cached state, not a reachable lifecycle state.
+     *
+     * @param source the current state.
+     * @param target the desired target state.
+     * @return {@code true} if the transition is valid, {@code false} 
otherwise.
+     */
+    @SuppressWarnings("cyclomaticComplexity")
+    public static boolean isValidStateTransition(MirrorPartitionState source,
+                                                 MirrorPartitionState target) {
+        if (source == null || target == null) {
+            return false;
+        }
+        // Re-asserting the current state triggers side effects (e.g. 
re-creating fetchers after failover)
+        if (source == target) {
+            return true;
+        }
+        if (source == MirrorPartitionState.FAILED) {
+            return target != MirrorPartitionState.UNKNOWN;

Review Comment:
   Why do we need to allow `FAIL -> STOPPED` path?
   This allows partitions become writable without `STOPPING`.



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