2023-08-09 10:06:34 +00:00
|
|
|
# nim-raft
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import types
|
|
|
|
import protocol
|
2023-08-14 20:49:21 +00:00
|
|
|
import consensus_module
|
2023-09-03 03:27:27 +00:00
|
|
|
import log_ops
|
2023-09-03 17:52:35 +00:00
|
|
|
import ../db/kvstore_mdbx
|
2023-09-04 09:47:27 +00:00
|
|
|
import chronicles
|
2023-09-07 02:04:27 +00:00
|
|
|
import std/random
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-09-04 09:47:27 +00:00
|
|
|
export
|
|
|
|
types,
|
|
|
|
protocol,
|
|
|
|
consensus_module,
|
|
|
|
log_ops,
|
|
|
|
chronicles
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-09-04 09:47:27 +00:00
|
|
|
# Forward declarations
|
2023-08-31 14:05:41 +00:00
|
|
|
proc RaftNodeSmInit[SmCommandType, SmStateType](stateMachine: var RaftNodeStateMachine[SmCommandType, SmStateType])
|
|
|
|
|
2023-09-04 09:47:27 +00:00
|
|
|
# Raft Node Public API
|
2023-09-11 16:55:30 +00:00
|
|
|
proc new*[SmCommandType, SmStateType](T: type RaftNode[SmCommandType, SmStateType];
|
2023-08-31 23:56:15 +00:00
|
|
|
id: RaftNodeId; peersIds: seq[RaftNodeId];
|
2023-08-31 20:52:52 +00:00
|
|
|
# persistentStorage: RaftNodePersistentStorage,
|
2023-09-11 16:55:30 +00:00
|
|
|
msgSendCallback: RaftMessageSendCallback;
|
|
|
|
electionTimeout: int=150;
|
2023-09-17 00:47:29 +00:00
|
|
|
heartBeatTimeout: int=150;
|
|
|
|
appendEntriesTimeout: int=50;
|
|
|
|
votingTimeout: int=50
|
2023-09-11 16:55:30 +00:00
|
|
|
): T =
|
2023-08-31 14:05:41 +00:00
|
|
|
var
|
2023-08-31 20:52:52 +00:00
|
|
|
peers: RaftNodePeers
|
|
|
|
|
|
|
|
for peerId in peersIds:
|
|
|
|
peers.add(RaftNodePeer(id: peerId, nextIndex: 0, matchIndex: 0, hasVoted: false, canVote: true))
|
|
|
|
|
2023-08-31 23:56:15 +00:00
|
|
|
result = T(
|
2023-08-31 20:52:52 +00:00
|
|
|
id: id, state: rnsFollower, currentTerm: 0, peers: peers, commitIndex: 0, lastApplied: 0,
|
2023-09-06 16:18:02 +00:00
|
|
|
msgSendCallback: msgSendCallback, votedFor: DefaultUUID, currentLeaderId: DefaultUUID,
|
2023-09-17 00:47:29 +00:00
|
|
|
electionTimeout: electionTimeout, heartBeatTimeout: heartBeatTimeout, appendEntriesTimeout: appendEntriesTimeout,
|
|
|
|
votingTimeout: votingTimeout
|
2023-08-31 14:05:41 +00:00
|
|
|
)
|
2023-08-31 23:56:15 +00:00
|
|
|
|
2023-09-02 21:16:26 +00:00
|
|
|
RaftNodeSmInit(result.stateMachine)
|
2023-09-06 19:27:22 +00:00
|
|
|
initRLock(result.raftStateMutex)
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-08-14 20:49:21 +00:00
|
|
|
proc RaftNodeLoad*[SmCommandType, SmStateType](
|
2023-08-09 10:06:34 +00:00
|
|
|
persistentStorage: RaftNodePersistentStorage, # Load Raft Node From Storage
|
2023-08-14 20:49:21 +00:00
|
|
|
msgSendCallback: RaftMessageSendCallback): Result[RaftNode[SmCommandType, SmStateType], string] =
|
2023-08-09 10:06:34 +00:00
|
|
|
discard
|
|
|
|
|
2023-09-02 22:59:35 +00:00
|
|
|
proc RaftNodeIdGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeId {.gcsafe.} = # Get Raft Node ID
|
2023-09-06 19:27:22 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
2023-09-06 16:18:02 +00:00
|
|
|
result = node.id
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-09-02 22:59:35 +00:00
|
|
|
proc RaftNodeStateGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeState = # Get Raft Node State
|
2023-09-06 19:27:22 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
2023-09-06 16:18:02 +00:00
|
|
|
result = node.state
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-09-02 22:59:35 +00:00
|
|
|
proc RaftNodeTermGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeTerm = # Get Raft Node Term
|
2023-09-06 19:27:22 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
2023-09-06 16:18:02 +00:00
|
|
|
result = node.currentTerm
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-08-14 20:49:21 +00:00
|
|
|
func RaftNodePeersGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodePeers = # Get Raft Node Peers
|
2023-09-06 19:27:22 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
2023-09-06 16:18:02 +00:00
|
|
|
result = node.peers
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-08-14 20:49:21 +00:00
|
|
|
func RaftNodeIsLeader*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): bool = # Check if Raft Node is Leader
|
2023-09-06 19:27:22 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
2023-09-06 16:18:02 +00:00
|
|
|
result = node.state == rnsLeader
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-08-14 20:49:21 +00:00
|
|
|
# Deliver Raft Message to the Raft Node and dispatch it
|
2023-09-17 00:47:29 +00:00
|
|
|
proc RaftNodeMessageDeliver*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], raftMessage: RaftMessageBase[SmCommandType, SmStateType]):
|
|
|
|
Future[RaftMessageResponseBase[SmCommandType, SmStateType]] {.async, gcsafe.} =
|
|
|
|
var
|
|
|
|
rm = RaftMessage[SmCommandType, SmStateType](raftMessage)
|
|
|
|
|
|
|
|
case rm.op # Dispatch different Raft Message types based on the operation code
|
|
|
|
of rmoRequestVote:
|
|
|
|
result = RaftNodeHandleRequestVote(node, rm)
|
2023-09-04 09:47:27 +00:00
|
|
|
of rmoAppendLogEntry:
|
2023-09-17 00:47:29 +00:00
|
|
|
if rm.logEntries.isSome:
|
|
|
|
result = RaftNodeHandleAppendEntries(node, rm)
|
2023-09-06 16:18:02 +00:00
|
|
|
else:
|
2023-09-17 00:47:29 +00:00
|
|
|
result = RaftNodeHandleHeartBeat(node, rm)
|
2023-09-04 09:47:27 +00:00
|
|
|
else: discard
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-09-07 02:41:05 +00:00
|
|
|
# Process Raft Node Client Requests
|
2023-09-17 00:47:29 +00:00
|
|
|
proc RaftNodeServeClientRequest*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], req: RaftNodeClientRequest[SmCommandType]):
|
|
|
|
Future[RaftNodeClientResponse[SmStateType]] {.async, gcsafe.} =
|
2023-09-03 00:53:48 +00:00
|
|
|
case req.op
|
2023-09-02 21:16:26 +00:00
|
|
|
of rncroExecSmCommand:
|
|
|
|
# TODO: implemenmt command handling
|
|
|
|
discard
|
|
|
|
of rncroRequestSmState:
|
|
|
|
if RaftNodeIsLeader(node):
|
2023-09-04 09:47:27 +00:00
|
|
|
return RaftNodeClientResponse(nodeId: node.id, error: rncreSuccess, state: RaftNodeStateGet(node))
|
|
|
|
else:
|
|
|
|
return RaftNodeClientResponse(nodeId: node.id, error: rncreNotLeader, currentLeaderId: node.currentLeaderId)
|
|
|
|
else:
|
|
|
|
raiseAssert "Unknown client request operation."
|
2023-08-09 10:06:34 +00:00
|
|
|
|
|
|
|
# Abstract State Machine Ops
|
2023-08-14 20:49:21 +00:00
|
|
|
func RaftNodeSmStateGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): SmStateType =
|
2023-09-07 02:41:05 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
|
|
|
node.stateMachine.state
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-08-14 20:49:21 +00:00
|
|
|
proc RaftNodeSmInit[SmCommandType, SmStateType](stateMachine: var RaftNodeStateMachine[SmCommandType, SmStateType]) =
|
2023-08-09 10:06:34 +00:00
|
|
|
mixin RaftSmInit
|
|
|
|
RaftSmInit(stateMachine)
|
|
|
|
|
2023-08-14 20:49:21 +00:00
|
|
|
proc RaftNodeSmApply[SmCommandType, SmStateType](stateMachine: RaftNodeStateMachine[SmCommandType, SmStateType], command: SmCommandType) =
|
2023-08-09 10:06:34 +00:00
|
|
|
mixin RaftSmApply
|
2023-09-07 02:41:05 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
|
|
|
RaftSmApply(stateMachine, command)
|
2023-08-10 07:17:46 +00:00
|
|
|
|
2023-09-06 16:18:02 +00:00
|
|
|
# Private Abstract Timer creation
|
|
|
|
template RaftTimerCreate(timerInterval: int, timerCallback: RaftTimerCallback): Future[void] =
|
2023-08-10 08:38:09 +00:00
|
|
|
mixin RaftTimerCreateCustomImpl
|
2023-09-06 16:18:02 +00:00
|
|
|
RaftTimerCreateCustomImpl(timerInterval, timerCallback)
|
2023-08-10 07:17:46 +00:00
|
|
|
|
2023-09-02 22:59:35 +00:00
|
|
|
# Timers scheduling stuff etc.
|
2023-09-07 02:04:27 +00:00
|
|
|
proc RaftNodeScheduleHeartBeat*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
|
2023-09-17 00:47:29 +00:00
|
|
|
node.heartBeatTimer = RaftTimerCreate(node.heartBeatTimeout, proc() = RaftNodeSendHeartBeat(node))
|
2023-09-02 22:59:35 +00:00
|
|
|
|
2023-09-17 00:47:29 +00:00
|
|
|
proc RaftNodeSendHeartBeat*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
|
2023-09-06 19:27:22 +00:00
|
|
|
debug "Raft Node sending Heart-Beat to peers", node_id=node.id
|
2023-09-02 22:59:35 +00:00
|
|
|
for raftPeer in node.peers:
|
2023-09-17 00:47:29 +00:00
|
|
|
let msgHrtBt = RaftMessage[SmCommandType, SmStateType](
|
2023-09-06 16:18:02 +00:00
|
|
|
op: rmoAppendLogEntry, senderId: node.id, receiverId: raftPeer.id,
|
2023-09-02 22:59:35 +00:00
|
|
|
senderTerm: RaftNodeTermGet(node), commitIndex: node.commitIndex,
|
2023-09-06 16:18:02 +00:00
|
|
|
prevLogIndex: RaftNodeLogIndexGet(node) - 1, prevLogTerm: if RaftNodeLogIndexGet(node) > 0: RaftNodeLogEntryGet(node, RaftNodeLogIndexGet(node) - 1).term else: 0
|
2023-09-02 22:59:35 +00:00
|
|
|
)
|
2023-09-17 00:47:29 +00:00
|
|
|
let r = node.msgSendCallback(msgHrtBt)
|
2023-09-07 02:04:27 +00:00
|
|
|
discard r
|
|
|
|
RaftNodeScheduleHeartBeat(node)
|
2023-09-06 16:18:02 +00:00
|
|
|
|
2023-09-07 02:04:27 +00:00
|
|
|
proc RaftNodeScheduleElectionTimeout*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
|
2023-09-09 18:07:12 +00:00
|
|
|
node.electionTimeoutTimer = RaftTimerCreate(node.electionTimeout + rand(node.electionTimeout), proc =
|
2023-09-06 16:18:02 +00:00
|
|
|
asyncSpawn RaftNodeStartElection(node)
|
|
|
|
)
|
2023-09-02 22:59:35 +00:00
|
|
|
|
|
|
|
# Raft Node Control
|
2023-09-07 02:41:05 +00:00
|
|
|
proc RaftNodeCancelAllTimers*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
|
2023-09-06 19:27:22 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
|
|
|
if node.heartBeatTimer != nil:
|
2023-09-07 03:56:35 +00:00
|
|
|
asyncSpawn cancelAndWait(node.heartBeatTimer)
|
2023-09-07 02:04:27 +00:00
|
|
|
if node.electionTimeoutTimer != nil:
|
2023-09-07 03:56:35 +00:00
|
|
|
asyncSpawn cancelAndWait(node.electionTimeoutTimer )
|
2023-09-06 19:27:22 +00:00
|
|
|
if node.appendEntriesTimer != nil:
|
2023-09-07 03:56:35 +00:00
|
|
|
asyncSpawn cancelAndWait(node.appendEntriesTimer)
|
2023-09-03 00:53:48 +00:00
|
|
|
|
2023-09-02 22:59:35 +00:00
|
|
|
proc RaftNodeStop*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
|
2023-09-03 00:53:48 +00:00
|
|
|
# Try to stop gracefully
|
2023-09-06 19:27:22 +00:00
|
|
|
withRLock(node.raftStateMutex):
|
2023-09-07 02:41:05 +00:00
|
|
|
# Abort election if in election
|
|
|
|
if node.state == rnsCandidate:
|
|
|
|
RaftNodeAbortElection(node)s
|
2023-09-06 16:18:02 +00:00
|
|
|
node.state = rnsStopped
|
2023-09-03 00:53:48 +00:00
|
|
|
# Cancel pending timers (if any)
|
2023-09-06 16:18:02 +00:00
|
|
|
RaftNodeCancelAllTimers(node)
|
2023-09-02 22:59:35 +00:00
|
|
|
|
|
|
|
proc RaftNodeStart*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
|
2023-09-03 00:53:48 +00:00
|
|
|
node.state = rnsFollower
|
2023-09-06 19:27:22 +00:00
|
|
|
debug "Start Raft Node", node_id=node.id, state=node.state
|
2023-09-07 02:04:27 +00:00
|
|
|
RaftNodeScheduleElectionTimeout(node)
|