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)