summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-26 23:36:28 +0200
committerPaul Buetow <paul@buetow.org>2026-03-26 23:36:28 +0200
commit637c4a09bfbe7045b0a639a616cfffc983da7e05 (patch)
treeca85258b0991935efc3bf127c9858dfe05c6ce7a /src/main
parent71182306390990e5ef9726e73924bc5a9f070282 (diff)
Fix Raft vote review findings for 04c78b3c-2267-495b-9aca-84b544a1882f
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/protocols/implementations/VSRaftProtocol.java24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/main/java/protocols/implementations/VSRaftProtocol.java b/src/main/java/protocols/implementations/VSRaftProtocol.java
index 597c1e0..230c6d9 100644
--- a/src/main/java/protocols/implementations/VSRaftProtocol.java
+++ b/src/main/java/protocols/implementations/VSRaftProtocol.java
@@ -40,6 +40,9 @@ public class VSRaftProtocol extends VSAbstractProtocol {
/** PIDs which still have to acknowledge the current operation. */
private ArrayList<Integer> ackPids;
+ /** Peer PIDs whose vote responses have been counted in this election. */
+ private ArrayList<Integer> voteResponsePids;
+
/** The local log index. */
private int logIndex;
@@ -152,6 +155,12 @@ public class VSRaftProtocol extends VSAbstractProtocol {
} else {
ackPids.clear();
}
+
+ if (voteResponsePids == null) {
+ voteResponsePids = new ArrayList<Integer>();
+ } else {
+ voteResponsePids.clear();
+ }
}
/**
@@ -161,6 +170,7 @@ public class VSRaftProtocol extends VSAbstractProtocol {
isLeader = true;
isCandidate = false;
votesReceived = 0;
+ voteResponsePids.clear();
leaderId = process.getProcessID();
lastHeartbeatTime = process.getTime();
isServer(true);
@@ -193,6 +203,7 @@ public class VSRaftProtocol extends VSAbstractProtocol {
leaderId = newLeaderId;
votedFor = -1;
votesReceived = 0;
+ voteResponsePids.clear();
resetElectionTimeout();
}
@@ -230,6 +241,7 @@ public class VSRaftProtocol extends VSAbstractProtocol {
currentTerm++;
votedFor = process.getProcessID();
votesReceived = 1;
+ voteResponsePids.clear();
isLeader = false;
isCandidate = true;
leaderId = -1;
@@ -284,9 +296,12 @@ public class VSRaftProtocol extends VSAbstractProtocol {
int candidateId = recvMessage.getInteger("candidateId");
boolean voteGranted = false;
- if (messageTerm >= currentTerm &&
- (votedFor == -1 || votedFor == candidateId)) {
+ if (messageTerm > currentTerm) {
becomeFollower(messageTerm, -1);
+ }
+
+ if (messageTerm == currentTerm &&
+ (votedFor == -1 || votedFor == candidateId)) {
votedFor = candidateId;
voteGranted = true;
}
@@ -307,6 +322,7 @@ public class VSRaftProtocol extends VSAbstractProtocol {
*/
private void handleVoteResponse(VSMessage recvMessage) {
int messageTerm = recvMessage.getInteger("term");
+ Integer responderPid = recvMessage.getIntegerObj("pid");
if (messageTerm > currentTerm) {
becomeFollower(messageTerm, -1);
@@ -315,10 +331,12 @@ public class VSRaftProtocol extends VSAbstractProtocol {
if (!isCandidate || !isForMe(recvMessage) ||
!recvMessage.getBoolean("voteGranted") ||
- messageTerm != currentTerm) {
+ messageTerm != currentTerm ||
+ voteResponsePids.contains(responderPid)) {
return;
}
+ voteResponsePids.add(responderPid);
votesReceived++;
if (votesReceived > getClusterSize() / 2) {