2023-08-09 10:17:03 +03:00
|
|
|
# nim-raft
|
2023-07-27 23:49:03 +03:00
|
|
|
# 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.
|
2023-07-29 06:32:15 +03:00
|
|
|
|
2023-07-29 15:54:41 +03:00
|
|
|
import types
|
|
|
|
import protocol
|
|
|
|
|
2023-07-29 08:22:02 +03:00
|
|
|
export types, protocol
|
2023-07-27 23:49:03 +03:00
|
|
|
|
|
|
|
# RAFT Node Public API procedures / functions
|
2023-08-09 10:17:03 +03:00
|
|
|
proc RAFTNodeCreateNew*[LogEntryDataType, SMStateType]( # Create New RAFT Node
|
|
|
|
id: RAFTNodeId, peers: RAFTNodePeers,
|
|
|
|
persistent_storage: RAFTNodePersistentStorage,
|
|
|
|
msg_send_callback: RAFTMessageSendCallback): RAFTNode[LogEntryDataType, SMStateType] =
|
2023-07-27 23:49:03 +03:00
|
|
|
discard
|
|
|
|
|
2023-08-09 10:17:03 +03:00
|
|
|
proc RAFTNodeLoad*[LogEntryDataType, SMStateType](
|
|
|
|
persistent_storage: RAFTNodePersistentStorage, # Load RAFT Node From Storage
|
|
|
|
msg_send_callback: RAFTMessageSendCallback): Result[RAFTNode[LogEntryDataType, SMStateType], string] =
|
2023-07-27 23:49:03 +03:00
|
|
|
discard
|
|
|
|
|
2023-07-29 07:03:20 +03:00
|
|
|
proc RAFTNodeStop*(node: RAFTNode) =
|
|
|
|
discard
|
|
|
|
|
2023-08-09 10:17:03 +03:00
|
|
|
proc RAFTNodeStart*(node: RAFTNode) =
|
2023-07-29 07:03:20 +03:00
|
|
|
discard
|
|
|
|
|
2023-07-27 23:49:03 +03:00
|
|
|
func RAFTNodeIdGet*(node: RAFTNode): RAFTNodeId = # Get RAFT Node ID
|
|
|
|
discard
|
|
|
|
|
|
|
|
func RAFTNodeStateGet*(node: RAFTNode): RAFTNodeState = # Get RAFT Node State
|
|
|
|
discard
|
|
|
|
|
|
|
|
func RAFTNodeTermGet*(node: RAFTNode): RAFTNodeTerm = # Get RAFT Node Term
|
|
|
|
discard
|
|
|
|
|
|
|
|
func RAFTNodePeersGet*(node: RAFTNode): RAFTNodePeers = # Get RAFT Node Peers
|
|
|
|
discard
|
|
|
|
|
|
|
|
func RAFTNodeIsLeader*(node: RAFTNode): bool = # Check if RAFT Node is Leader
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc RAFTNodeMessageDeliver*(node: RAFTNode, raft_message: RAFTMessageBase): RAFTMessageResponse {.discardable.} = # Deliver RAFT Message to the RAFT Node
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc RAFTNodeRequest*(node: RAFTNode, req: RAFTNodeClientRequest): RAFTNodeClientResponse = # Process RAFTNodeClientRequest
|
2023-07-29 15:54:41 +03:00
|
|
|
discard
|
|
|
|
|
2023-08-09 10:17:03 +03:00
|
|
|
proc RAFTNodeLogIndexGet*(node: RAFTNode): RAFTLogIndex =
|
|
|
|
node.log_index
|
2023-07-29 15:54:41 +03:00
|
|
|
discard
|
|
|
|
|
2023-08-09 10:17:03 +03:00
|
|
|
proc RAFTNodeLogEntryGet*(node: RAFTNode, log_index: RAFTLogIndex): Result[RAFTNodeLogEntry, string] =
|
2023-07-29 15:54:41 +03:00
|
|
|
discard
|
|
|
|
|
2023-08-09 10:17:03 +03:00
|
|
|
# Abstract State Machine Ops
|
|
|
|
func RAFTNodeSMStateGet*[LogEntryDataType, SMStateType](node: RAFTNode[LogEntryDataType, SMStateType]): SMStateType =
|
|
|
|
node.state_machine.state
|
|
|
|
|
|
|
|
proc RAFTNodeSMInit[LogEntryDataType, SMStateType](state_machine: var RAFTNodeStateMachine[LogEntryDataType, SMStateType]) =
|
|
|
|
mixin RAFTSMInit
|
|
|
|
RAFTSMInit(state_machine)
|
2023-07-29 15:54:41 +03:00
|
|
|
|
2023-08-09 10:17:03 +03:00
|
|
|
proc RAFTNodeSMApply[LogEntryDataType, SMStateType](state_machine: RAFTNodeStateMachine[LogEntryDataType, SMStateType], log_entry: LogEntryDataType) =
|
|
|
|
mixin RAFTSMApply
|
|
|
|
RAFTSMApply(state_machine, log_entry)
|