2023-08-09 13:06:34 +03: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
|
2023-08-14 23:49:21 +03:00
|
|
|
# those terms.
|
|
|
|
|
2023-09-04 12:47:27 +03:00
|
|
|
{.hint[XDeclaredButNotUsed]: off.}
|
|
|
|
|
|
|
|
import types
|
|
|
|
import protocol
|
|
|
|
import log_ops
|
|
|
|
import chronicles
|
2023-08-14 23:49:21 +03:00
|
|
|
|
2023-09-03 20:52:35 +03:00
|
|
|
proc RaftNodeStartElection*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) {.async.} =
|
2023-09-04 12:47:27 +03:00
|
|
|
withLock(node.raftStateMutex):
|
|
|
|
debug "Raft Node started election. Node ID: ", node_id=node.id
|
|
|
|
node.currentTerm.inc
|
|
|
|
node.state = rnsCandidate
|
|
|
|
node.votedFor = node.id
|
2023-09-03 06:27:27 +03:00
|
|
|
|
2023-09-04 12:47:27 +03:00
|
|
|
for peer in node.peers:
|
|
|
|
peer.hasVoted = false
|
|
|
|
node.votesFuts.add(node.msgSendCallback(
|
|
|
|
RaftMessageRequestVote(lastLogTerm: RaftNodeLogEntryGet(node, RaftNodeLogIndexGet(node)).value.term, lastLogIndex: RaftNodeLogIndexGet(node), senderTerm: node.currentTerm)
|
|
|
|
)
|
2023-09-03 06:27:27 +03:00
|
|
|
)
|
|
|
|
|
2023-09-03 20:52:35 +03:00
|
|
|
# Process votes (if any)
|
|
|
|
for voteFut in node.votesFuts:
|
2023-09-04 12:47:27 +03:00
|
|
|
var
|
|
|
|
r: RaftMessageRequestVoteResponse
|
|
|
|
|
|
|
|
r = RaftMessageRequestVoteResponse(waitFor voteFut)
|
|
|
|
|
|
|
|
debug "voteFut.finished", voteFut_finished=voteFut.finished
|
|
|
|
|
|
|
|
withLock(node.raftStateMutex):
|
2023-09-03 06:27:27 +03:00
|
|
|
for p in node.peers:
|
2023-09-04 12:47:27 +03:00
|
|
|
debug "voteFut: ", Response=repr(r)
|
|
|
|
debug "senderId: ", sender_id=r.senderId
|
|
|
|
debug "granted: ", granted=r.granted
|
|
|
|
if p.id == r.senderId:
|
|
|
|
p.hasVoted = r.granted
|
2023-09-03 20:52:35 +03:00
|
|
|
|
2023-09-04 12:47:27 +03:00
|
|
|
withLock(node.raftStateMutex):
|
|
|
|
node.votesFuts.clear
|
2023-08-14 23:49:21 +03:00
|
|
|
|
2023-09-03 06:27:27 +03:00
|
|
|
proc RaftNodeAbortElection*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
|
2023-09-04 12:47:27 +03:00
|
|
|
withLock(node.raftStateMutex):
|
|
|
|
for fut in node.voteFuts:
|
|
|
|
if not fut.finished and not fut.failed:
|
|
|
|
cancel(fut)
|
|
|
|
|
|
|
|
proc RaftNodeProcessRequestVote*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], msg: RaftMessageRequestVote): RaftMessageRequestVoteResponse =
|
|
|
|
withLock(node.raftStateMutex):
|
|
|
|
result = RaftMessageRequestVoteResponse(msgId: msg.msgId, senderId: msg.senderId, receiverId: msg.reciverId, granted: false)
|
|
|
|
if msg.senderTerm > node.term:
|
|
|
|
if msg.lastLogIndex >= RaftNodeLogIndexGet(node) and msg.lastLogTerm >= RaftNodeLogEntryGet(RaftNodeLogIndexGet(node)).term:
|
|
|
|
result.granted = true
|
2023-08-14 23:49:21 +03:00
|
|
|
|
2023-09-04 12:47:27 +03:00
|
|
|
proc RaftNodeProcessAppendEntries*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], msg: RaftMessageAppendEntries): RaftMessageAppendEntriesResponse =
|
|
|
|
discard
|
2023-09-03 06:27:27 +03:00
|
|
|
|
2023-09-04 12:47:27 +03:00
|
|
|
proc RaftNodeProcessHeartBeat*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], msg: RaftMessageAppendEntries): RaftMessageAppendEntriesResponse =
|
2023-09-01 05:55:55 +03:00
|
|
|
discard
|
|
|
|
|
2023-09-01 06:28:43 +03:00
|
|
|
proc RaftNodeQuorumMin[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): bool =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc RaftNodeReplicateSmCommand*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], cmd: SmCommandType) =
|
|
|
|
discard
|
|
|
|
|
2023-09-03 01:59:35 +03:00
|
|
|
proc RaftNodeScheduleRequestVotesCleanUpTimeout*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
|
2023-09-03 03:53:48 +03:00
|
|
|
discard
|
|
|
|
|
|
|
|
proc RaftNodeLogAppend[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], logEntry: RaftNodeLogEntry[SmCommandType]) =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc RaftNodeLogTruncate[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], truncateIndex: uint64) =
|
2023-08-14 23:49:21 +03:00
|
|
|
discard
|