FrankYang0529 commented on code in PR #19630:
URL: https://github.com/apache/kafka/pull/19630#discussion_r2073054727


##########
server/src/main/java/org/apache/kafka/server/PartitionFetchState.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.server;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.utils.Time;
+
+import java.util.Optional;
+
+/**
+ * Class to keep partition offset and its state (truncatingLog, delayed)
+ * This represents a partition as being either:
+ * (1) Truncating its log, for example, having recently become a follower
+ * (2) Delayed, for example, due to an error, where we subsequently back off a 
bit
+ * (3) ReadyForFetch, the active state where the thread is actively fetching 
data.
+ */
+public class PartitionFetchState {
+    private final Optional<Uuid> topicId;
+    private final long fetchOffset;
+    private final Optional<Long> lag;
+    private final int currentLeaderEpoch;
+    private final Optional<Long> delay;
+    private final ReplicaState state;
+    private final Optional<Integer> lastFetchedEpoch;
+    private final Optional<Long> dueMs;
+
+
+    public static PartitionFetchState create(Optional<Uuid> topicId,
+                                             long offset,
+                                             Optional<Long> lag,
+                                             int currentLeaderEpoch,
+                                             ReplicaState state,
+                                             Optional<Integer> 
lastFetchedEpoch) {
+        return new PartitionFetchState(topicId, offset, lag, 
currentLeaderEpoch,
+                Optional.empty(), state, lastFetchedEpoch);
+    }
+
+    public PartitionFetchState(Optional<Uuid> topicId,
+                               long fetchOffset,
+                               Optional<Long> lag,
+                               int currentLeaderEpoch,
+                               Optional<Long> delay,
+                               ReplicaState state,
+                               Optional<Integer> lastFetchedEpoch) {
+        this.topicId = topicId;
+        this.fetchOffset = fetchOffset;
+        this.lag = lag;
+        this.currentLeaderEpoch = currentLeaderEpoch;
+        this.delay = delay;
+        this.state = state;
+        this.lastFetchedEpoch = lastFetchedEpoch;
+
+        // Initialize dueMs (equivalent to private val dueMs)
+        this.dueMs = delay.map(aLong -> aLong + Time.SYSTEM.milliseconds());
+    }
+
+    public boolean isReadyForFetch() {
+        return state == ReplicaState.FETCHING && !isDelayed();
+    }
+
+    public boolean isReplicaInSync() {
+        return lag.isPresent() && lag.get() <= 0;
+    }
+
+    public boolean isTruncating() {
+        return state == ReplicaState.TRUNCATING && !isDelayed();
+    }
+
+    public boolean isDelayed() {
+        return dueMs.isPresent() && dueMs.get() > Time.SYSTEM.milliseconds();
+    }
+
+    public Optional<Uuid> getTopicId() {
+        return topicId;
+    }
+
+    public long getFetchOffset() {
+        return fetchOffset;
+    }
+
+    public Optional<Long> getLag() {
+        return lag;
+    }
+
+    public int getCurrentLeaderEpoch() {
+        return currentLeaderEpoch;
+    }
+
+    public Optional<Long> getDelay() {
+        return delay;
+    }
+
+    public ReplicaState getState() {
+        return state;
+    }
+
+    public Optional<Integer> getLastFetchedEpoch() {
+        return lastFetchedEpoch;
+    }

Review Comment:
   Could you remove `get` prefix in these functions, so we can avoid lot of 
naming change? Thanks.



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