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


##########
raft/src/main/java/org/apache/kafka/raft/ElectionState.java:
##########
@@ -64,47 +62,100 @@ public boolean isLeader(int nodeId) {
         return leaderIdOrSentinel() == nodeId;
     }
 
-    public boolean isVotedCandidate(int nodeId) {
-        if (nodeId < 0)
-            throw new IllegalArgumentException("Invalid negative nodeId: " + 
nodeId);
-        return votedIdOpt.orElse(-1) == nodeId;
+    /**
+     * Return if the replica has voted for the given candidate.
+     *
+     * A replica has voted for a candidate if all of the following are true:
+     * 1. the node's id and voted id match and
+     * 2. if the voted directory id is set, it matches the node's directory id
+     *
+     * @param nodeKey the id and directory id of the replica
+     * @return true when the arguments match, otherwise false
+     */
+    public boolean isVotedCandidate(ReplicaKey nodeKey) {
+        if (nodeKey.id() < 0) {
+            throw new IllegalArgumentException("Invalid node key " + nodeKey);
+        } else if (!votedKey.isPresent()) {
+            return false;
+        } else if (votedKey.get().id() != nodeKey.id()) {
+            return false;
+        } else if (!votedKey.get().directoryId().isPresent()) {
+            // when the persisted voted uuid is not present assume that we 
voted for this candidate;
+            // this happends when the kraft version is 0.

Review Comment:
   Fixed.



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