nim-raft/raft/raft_api.nim

119 lines
5.1 KiB
Nim
Raw Normal View History

# 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
export types, protocol, consensus_module
2023-08-31 14:05:41 +00:00
proc RaftNodeSmInit[SmCommandType, SmStateType](stateMachine: var RaftNodeStateMachine[SmCommandType, SmStateType])
# Raft Node Public API procedures / functions
2023-08-31 14:05:41 +00:00
proc RaftNodeCreateNew*[SmCommandType, SmStateType]( # Create New Raft Node
id: RaftNodeId, peers: RaftNodePeers,
persistentStorage: RaftNodePersistentStorage,
2023-08-14 20:49:21 +00:00
msgSendCallback: RaftMessageSendCallback): RaftNode[SmCommandType, SmStateType] =
2023-08-31 14:05:41 +00:00
var
sm = RaftNodeStateMachine[SmCommandType, SmStateType]
RaftNodeSmInit[SmCommandType, SmStateType](sm)
result = RaftNode[SmCommandType, SmStateType](
id: id, state: rnsFollower, currentTerm: 0, votedFor: nil, peers: peers, commitIndex: 0, lastApplied: 0,
stateMachine: sm, msgSendCallback: msgSendCallback
)
2023-08-14 20:49:21 +00:00
proc RaftNodeLoad*[SmCommandType, SmStateType](
persistentStorage: RaftNodePersistentStorage, # Load Raft Node From Storage
2023-08-14 20:49:21 +00:00
msgSendCallback: RaftMessageSendCallback): Result[RaftNode[SmCommandType, SmStateType], string] =
discard
2023-08-14 20:49:21 +00:00
proc RaftNodeStop*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
discard
2023-08-14 20:49:21 +00:00
proc RaftNodeStart*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
discard
2023-08-14 20:49:21 +00:00
func RaftNodeIdGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeId = # Get Raft Node ID
2023-08-31 14:05:41 +00:00
node.id
2023-08-14 20:49:21 +00:00
func RaftNodeStateGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeState = # Get Raft Node State
2023-08-31 14:05:41 +00:00
node.state
2023-08-14 20:49:21 +00:00
func RaftNodeTermGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeTerm = # Get Raft Node Term
2023-08-31 14:05:41 +00:00
node.currentTerm
2023-08-14 20:49:21 +00:00
func RaftNodePeersGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodePeers = # Get Raft Node Peers
2023-08-31 14:05:41 +00:00
node.peers
2023-08-14 20:49:21 +00:00
func RaftNodeIsLeader*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): bool = # Check if Raft Node is Leader
2023-08-31 14:05:41 +00:00
node.state == rnsLeader
2023-08-14 20:49:21 +00:00
# Deliver Raft Message to the Raft Node and dispatch it
proc RaftNodeMessageDeliver*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], raftMessage: RaftMessageBase): RaftMessageResponseBase =
discard
2023-08-14 20:49:21 +00:00
# Process RaftNodeClientRequests
proc RaftNodeClientRequest*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], req: RaftNodeClientRequest[SmCommandType]): RaftNodeClientResponse[SmStateType] =
discard
# Abstract State Machine Ops
2023-08-14 20:49:21 +00:00
func RaftNodeSmStateGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): SmStateType =
node.stateMachine.state
2023-08-14 20:49:21 +00:00
proc RaftNodeSmInit[SmCommandType, SmStateType](stateMachine: var RaftNodeStateMachine[SmCommandType, SmStateType]) =
mixin RaftSmInit
RaftSmInit(stateMachine)
2023-08-14 20:49:21 +00:00
proc RaftNodeSmApply[SmCommandType, SmStateType](stateMachine: RaftNodeStateMachine[SmCommandType, SmStateType], command: SmCommandType) =
mixin RaftSmApply
2023-08-14 20:49:21 +00:00
RaftSmApply(stateMachine, command)
2023-08-14 20:49:21 +00:00
# Private Abstract Timer manipulation Ops
2023-08-25 09:00:40 +00:00
template RaftTimerCreate(timerInterval: int, oneshot: bool, timerCallback: RaftTimerCallback): RaftTimer =
2023-08-10 08:38:09 +00:00
mixin RaftTimerCreateCustomImpl
2023-08-25 09:00:40 +00:00
RaftTimerCreateCustomImpl(timerInterval, oneshot, timerCallback)
template RaftTimerCancel(timer: RaftTimer) =
2023-08-10 08:38:09 +00:00
mixin RaftTimerCancelCustomImpl
RaftTimerCancelCustomImpl(timer)
template RaftTimerStart() =
mixin RaftTimerStartCustomImpl
RaftTimerStartCustomImpl()
template RaftTimerStop() =
mixin RaftTimerStopCustomImpl
RaftTimerStopCustomImpl()
2023-08-14 20:49:21 +00:00
# Private Log Ops
proc RaftNodeLogAppend[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], logEntry: RaftNodeLogEntry[SmCommandType]) =
discard
2023-08-31 14:05:41 +00:00
proc RaftNodeLogTruncate[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], truncateIndex: uint64) =
2023-08-14 20:49:21 +00:00
discard
2023-08-31 14:05:41 +00:00
proc RaftNodeLogIndexGet[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftLogIndex =
discard
proc RaftNodeLogEntryGet[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], logIndex: RaftLogIndex): Result[RaftNodeLogEntry[SmCommandType], string] =
2023-08-14 20:49:21 +00:00
discard
# Private Timers Create Ops
proc RaftNodeScheduleHeartBeat[SmCommandType, SmStateType, TimerDurationType](node: RaftNode[SmCommandType, SmStateType]) =
2023-08-14 20:49:21 +00:00
discard
proc RaftNodeScheduleHeartBeatTimeout[SmCommandType, SmStateType, TimerDurationType](node: RaftNode[SmCommandType, SmStateType]) =
2023-08-14 20:49:21 +00:00
discard
proc RaftNodeScheduleElectionTimeOut[SmCommandType, SmStateType, TimerDurationType](node: RaftNode[SmCommandType, SmStateType]) =
2023-08-14 20:49:21 +00:00
discard
proc RaftNodeScheduleRequestVoteTimeout[SmCommandType, SmStateType, TimerDurationType](node: RaftNode[SmCommandType, SmStateType]) =
2023-08-14 20:49:21 +00:00
discard