Add RAFT stuff

This commit is contained in:
Piv
2020-02-21 21:08:50 +10:30
parent 18a5a33b5f
commit e13551f798
17 changed files with 1156 additions and 0 deletions

24
MyRaft/leader.py Normal file
View File

@@ -0,0 +1,24 @@
import MyRaft.state as state
import MyRaft.node as node
class Leader(state.State):
"""The leader class represents the leader state in the raft algorithm"""
def __init__(self, context: node.RaftNode):
state.State.__init__(self, context)
print("We're a leader!")
# For indexes for each server to send.
self.nextIndex = []
self.matchIndex = []
# Change our timeout.
self._context.set_timeout(self._context._heartbeat_timeout, 0)
# Send empty AppendEntries.
self._context.send_empty_AppendEntries()
def heartbeat_elapsed(self):
print("Sending an append entries message")
self._context.send_empty_AppendEntries()
# Don't forget to reset timer, otherwise they'll try run for leader.
self._context.set_timeout(self._context._heartbeat_timeout, 0)