diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-26 23:25:13 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-26 23:25:13 +0200 |
| commit | 3820f2fe179995aa6aa12e1fd2ab9b07a7938620 (patch) | |
| tree | 04f84003462ae7c15ef4295e2caf1c102fa74a51 /src/main/java | |
| parent | 0881fac983503a5320f5b093c738ebc44cc06a70 (diff) | |
Fix Raft jitter deadline election guard (0bac83d3-1322-4940-a9ee-58eb1e0d6245)
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/protocols/implementations/VSRaftProtocol.java | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/main/java/protocols/implementations/VSRaftProtocol.java b/src/main/java/protocols/implementations/VSRaftProtocol.java index eaf63a7..6257538 100644 --- a/src/main/java/protocols/implementations/VSRaftProtocol.java +++ b/src/main/java/protocols/implementations/VSRaftProtocol.java @@ -33,6 +33,9 @@ public class VSRaftProtocol extends VSAbstractProtocol { /** The local time when the last heartbeat was observed. */ private long lastHeartbeatTime; + /** The randomized local deadline for the next election timeout. */ + private long electionDeadline; + /** PIDs which still have to acknowledge the current operation. */ private ArrayList<Integer> ackPids; @@ -105,9 +108,9 @@ public class VSRaftProtocol extends VSAbstractProtocol { * @see protocols.VSAbstractProtocol#onClientSchedule() */ public void onClientSchedule() { - long elapsedSinceHeartbeat = process.getTime() - lastHeartbeatTime; + long currentTime = process.getTime(); - if (!isLeader && elapsedSinceHeartbeat >= getLong("electionTimeout")) { + if (!isLeader && currentTime >= electionDeadline) { startElection(); } } @@ -137,6 +140,7 @@ public class VSRaftProtocol extends VSAbstractProtocol { isLeader = false; isCandidate = false; lastHeartbeatTime = 0; + electionDeadline = 0; logIndex = 0; commitIndex = 0; @@ -183,10 +187,11 @@ public class VSRaftProtocol extends VSAbstractProtocol { long jitterPercentage = Math.abs(process.getRandomPercentage()); long jitter = (getLong("electionJitter") * jitterPercentage) / 100L; boolean previousContextIsServer = currentContextIsServer(); + electionDeadline = process.getTime() + getLong("electionTimeout") + jitter; currentContextIsServer(false); removeSchedules(); - scheduleAt(process.getTime() + getLong("electionTimeout") + jitter); + scheduleAt(electionDeadline); currentContextIsServer(previousContextIsServer); } |
