summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-26 22:49:30 +0200
committerPaul Buetow <paul@buetow.org>2026-03-26 22:49:30 +0200
commit0149274e22cac8d49953082337f4ffbe01537b4b (patch)
treee1084ecd3296812a7f5f35cd8d840a039e610476 /src/main/java
parent6b3af5524f36296c550465a1339341f24d385bfe (diff)
Add initial Raft protocol skeleton for bc61c489-f41b-473b-8c9b-91959ad958f7
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/events/VSRegisteredEvents.java1
-rw-r--r--src/main/java/protocols/implementations/VSRaftProtocol.java139
2 files changed, 140 insertions, 0 deletions
diff --git a/src/main/java/events/VSRegisteredEvents.java b/src/main/java/events/VSRegisteredEvents.java
index 92deeb0..8d31b0e 100644
--- a/src/main/java/events/VSRegisteredEvents.java
+++ b/src/main/java/events/VSRegisteredEvents.java
@@ -96,6 +96,7 @@ public final class VSRegisteredEvents {
registerEvent("protocols.implementations.VSInternalTimeSyncProtocol");
registerEvent("protocols.implementations.VSOnePhaseCommitProtocol");
registerEvent("protocols.implementations.VSPingPongProtocol");
+ registerEvent("protocols.implementations.VSRaftProtocol");
registerEvent("protocols.implementations.VSReliableMulticastProtocol");
registerEvent("protocols.implementations.VSTwoPhaseCommitProtocol");
registerEvent("protocols.implementations.VSTimestampDemoProtocol");
diff --git a/src/main/java/protocols/implementations/VSRaftProtocol.java b/src/main/java/protocols/implementations/VSRaftProtocol.java
new file mode 100644
index 0000000..8b919f6
--- /dev/null
+++ b/src/main/java/protocols/implementations/VSRaftProtocol.java
@@ -0,0 +1,139 @@
+package protocols.implementations;
+
+import java.util.ArrayList;
+import java.util.Vector;
+
+import core.VSMessage;
+import protocols.VSAbstractProtocol;
+
+/**
+ * The class VSRaftProtocol, a skeleton for a Raft-based protocol.
+ *
+ * @author Paul C. Buetow
+ */
+public class VSRaftProtocol extends VSAbstractProtocol {
+ /** The current Raft term. */
+ private int currentTerm;
+
+ /** The PID voted for in the current term. */
+ private int votedFor;
+
+ /** The number of votes received while acting as a candidate. */
+ private int votesReceived;
+
+ /** The current leader PID. */
+ private int leaderId;
+
+ /** True if this process currently acts as the leader. */
+ private boolean isLeader;
+
+ /** True if this process currently acts as a candidate. */
+ private boolean isCandidate;
+
+ /** The local time when the last heartbeat was observed. */
+ private long lastHeartbeatTime;
+
+ /** PIDs which still have to acknowledge the current operation. */
+ private ArrayList<Integer> ackPids;
+
+ /** The local log index. */
+ private int logIndex;
+
+ /** The last committed log index. */
+ private int commitIndex;
+
+ /**
+ * Instantiates a new Raft protocol skeleton.
+ */
+ public VSRaftProtocol() {
+ super(VSAbstractProtocol.HAS_ON_SERVER_START);
+ setClassname(getClass().toString());
+ resetState();
+ }
+
+ /* (non-Javadoc)
+ * @see events.VSAbstractProtocol#onServerInit()
+ */
+ public void onServerInit() {
+ Vector<Integer> vec = new Vector<Integer>();
+ vec.add(2);
+ vec.add(3);
+
+ initVector("pids", vec, "PIDs of participating follower processes");
+ initLong("heartbeatInterval", 1500, "Heartbeat interval", "ms");
+ initString("logEntry", "cmd1", "Log entry to replicate");
+ }
+
+ /* (non-Javadoc)
+ * @see events.VSAbstractProtocol#onClientInit()
+ */
+ public void onClientInit() {
+ initLong("electionTimeout", 4000, "Base election timeout", "ms");
+ initLong("electionJitter", 2000, "Election timeout jitter", "ms");
+ }
+
+ /* (non-Javadoc)
+ * @see protocols.VSAbstractProtocol#onServerStart()
+ */
+ public void onServerStart() {
+ }
+
+ /* (non-Javadoc)
+ * @see protocols.VSAbstractProtocol#onServerRecv(core.VSMessage)
+ */
+ public void onServerRecv(VSMessage recvMessage) {
+ }
+
+ /* (non-Javadoc)
+ * @see protocols.VSAbstractProtocol#onClientRecv(core.VSMessage)
+ */
+ public void onClientRecv(VSMessage recvMessage) {
+ }
+
+ /* (non-Javadoc)
+ * @see protocols.VSAbstractProtocol#onServerSchedule()
+ */
+ public void onServerSchedule() {
+ }
+
+ /* (non-Javadoc)
+ * @see protocols.VSAbstractProtocol#onClientSchedule()
+ */
+ public void onClientSchedule() {
+ }
+
+ /* (non-Javadoc)
+ * @see protocols.VSAbstractProtocol#onServerReset()
+ */
+ public void onServerReset() {
+ resetState();
+ }
+
+ /* (non-Javadoc)
+ * @see protocols.VSAbstractProtocol#onClientReset()
+ */
+ public void onClientReset() {
+ resetState();
+ }
+
+ /**
+ * Resets the shared Raft state to its initial values.
+ */
+ private void resetState() {
+ currentTerm = 0;
+ votedFor = -1;
+ votesReceived = 0;
+ leaderId = -1;
+ isLeader = false;
+ isCandidate = false;
+ lastHeartbeatTime = 0;
+ logIndex = 0;
+ commitIndex = 0;
+
+ if (ackPids == null) {
+ ackPids = new ArrayList<Integer>();
+ } else {
+ ackPids.clear();
+ }
+ }
+}