summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-27 13:59:54 +0200
committerPaul Buetow <paul@buetow.org>2026-03-27 13:59:54 +0200
commitf797489f30e76e8afcd8b8eb1fd16c79e9cd1363 (patch)
tree71b5a2fc02ed67a887e1f2b2f5bf0354664614c7
parent38643ad8aca76dba2ac71987008d8c79370607cd (diff)
Clarify Raft leader election logs
-rw-r--r--src/main/java/protocols/implementations/VSRaftProtocol.java22
-rw-r--r--src/test/java/protocols/implementations/VSRaftProtocolTest.java6
2 files changed, 22 insertions, 6 deletions
diff --git a/src/main/java/protocols/implementations/VSRaftProtocol.java b/src/main/java/protocols/implementations/VSRaftProtocol.java
index bad893c..d359035 100644
--- a/src/main/java/protocols/implementations/VSRaftProtocol.java
+++ b/src/main/java/protocols/implementations/VSRaftProtocol.java
@@ -86,7 +86,7 @@ public class VSRaftProtocol extends VSAbstractProtocol {
* @see protocols.VSAbstractProtocol#onServerStart()
*/
public void onServerStart() {
- becomeLeader();
+ activateStartupLeader();
}
/* (non-Javadoc)
@@ -171,9 +171,25 @@ public class VSRaftProtocol extends VSAbstractProtocol {
}
/**
- * Transitions this process into the leader role and starts heartbeats.
+ * Activates the initial leader role at startup and starts heartbeats.
+ */
+ private void activateStartupLeader() {
+ becomeLeader("Startup leader activated");
+ }
+
+ /**
+ * Transitions this process into the leader role after winning an election.
*/
private void becomeLeader() {
+ becomeLeader("Leader elected by majority vote");
+ }
+
+ /**
+ * Transitions this process into the leader role and starts heartbeats.
+ *
+ * @param logPrefix the prefix to use for the leader transition log message
+ */
+ private void becomeLeader(String logPrefix) {
isLeader = true;
isCandidate = false;
votesReceived = 0;
@@ -182,7 +198,7 @@ public class VSRaftProtocol extends VSAbstractProtocol {
leaderId = process.getProcessID();
lastHeartbeatTime = process.getTime();
isServer(true);
- log("Leader elected: process " + leaderId + " (term " + currentTerm + ")");
+ log(logPrefix + ": process " + leaderId + " (term " + currentTerm + ")");
if (!getLongKeySet().contains("heartbeatInterval")) {
onServerInit();
diff --git a/src/test/java/protocols/implementations/VSRaftProtocolTest.java b/src/test/java/protocols/implementations/VSRaftProtocolTest.java
index 802c80f..e568431 100644
--- a/src/test/java/protocols/implementations/VSRaftProtocolTest.java
+++ b/src/test/java/protocols/implementations/VSRaftProtocolTest.java
@@ -96,7 +96,7 @@ class VSRaftProtocolTest {
assertEquals(2, getAckPids().size());
assertTrue(getAckPids().contains(2));
assertTrue(getAckPids().contains(3));
- verify(mockProcess).log("Leader elected: process 7 (term 0)");
+ verify(mockProcess).log("Startup leader activated: process 7 (term 0)");
assertEquals(1600L, taskCaptor.getValue().getTaskTime());
}
@@ -493,7 +493,7 @@ class VSRaftProtocolTest {
assertFalse(getBooleanField("isCandidate"));
assertEquals(7, getIntField("leaderId"));
assertTrue(protocol.isServer());
- verify(mockProcess).log("Leader elected: process 7 (term 3)");
+ verify(mockProcess).log("Leader elected by majority vote: process 7 (term 3)");
assertEquals(1800L, taskCaptor.getValue().getTaskTime());
}
@@ -691,7 +691,7 @@ class VSRaftProtocolTest {
throws Exception {
LeaderHarness leaderHarness = createLeaderHarness(11, 300L);
leaderHarness.protocol.onStart();
- verify(leaderHarness.process).log("Leader elected: process 11 (term 0)");
+ verify(leaderHarness.process).log("Startup leader activated: process 11 (term 0)");
clearInvocations(leaderHarness.process);
ArrayList<VSMessage> sentMessages = leaderHarness.protocol.getSentMessages();