This commit is contained in:
Raycho Mukelov 2023-11-05 18:14:18 +02:00
parent 02d220afa3
commit 3b3e9b2765
1 changed files with 7 additions and 6 deletions

View File

@ -18,6 +18,7 @@ type
VotingTimeout, VotingTimeout,
ElectionTimeout, ElectionTimeout,
HeartbeatTimeout, HeartbeatTimeout,
AppendEntriesTimeout
HeartbeatReceived, HeartbeatReceived,
HeartbeatSent, HeartbeatSent,
AppendEntriesReceived, AppendEntriesReceived,
@ -28,14 +29,14 @@ type
ClientRequestProcessed ClientRequestProcessed
# Define callback to use with Terminals. Node states are updated/read in-place in the node object # Define callback to use with Terminals. Node states are updated/read in-place in the node object
ConsensusFsmTransActionType*[NodeType] = proc(node: NodeType) {.gcsafe.} ConsensusFsmTransitionActionType*[NodeType] = proc(node: NodeType) {.gcsafe.}
# Define logical functions (conditions) computed from our NodeType etc. (Truth Table) # Define logical functions (conditions) computed from our NodeType etc. (Truth Table)
LogicalConditionValueType* = bool LogicalConditionValueType* = bool
LogicalCondition*[NodeTytpe, RaftMessageBase] = LogicalCondition*[NodeTytpe, RaftMessageBase] =
proc(n: NodeTytpe, msg: Option[RaftMessageBase]): LogicalConditionValueType proc(node: NodeTytpe, msg: Option[RaftMessageBase]): LogicalConditionValueType
LogicalConditionsLut*[RaftNodeState, EventType, NodeType, RaftMessageBase] = LogicalConditionsLut*[RaftNodeState, EventType, NodeType, RaftMessageBase] =
Table[(RaftNodeState, EventType), seq[LogicalCondition[NodeType, Option[RaftMessageBase]]]] Table[(RaftNodeState, EventType), seq[LogicalCondition[NodeType, RaftMessageBase]]]
# Define Terminals as a tuple of a Event and a sequence of logical functions (conditions) and their respective values computed from NodeType, NodeTytpe and RaftMessageBase # Define Terminals as a tuple of a Event and a sequence of logical functions (conditions) and their respective values computed from NodeType, NodeTytpe and RaftMessageBase
# (kind of Truth Table) # (kind of Truth Table)
@ -45,8 +46,8 @@ type
# Define State Transition Rules LUT of the form ( NonTerminal -> Terminal ) -> NonTerminal ) # Define State Transition Rules LUT of the form ( NonTerminal -> Terminal ) -> NonTerminal )
# NonTerminal is a NodeState and Terminal is a TerminalSymbol - the tuple (EventType, seq[LogicalConditionValueType]) # NonTerminal is a NodeState and Terminal is a TerminalSymbol - the tuple (EventType, seq[LogicalConditionValueType])
StateTransitionsRulesLut*[RaftNodeState, EventType, NodeType, RaftMessageBase] = Table[ StateTransitionsRulesLut*[RaftNodeState, EventType, NodeType, RaftMessageBase] = Table[
(RaftNodeState, TerminalSymbol[NodeType, EventType, Option[RaftMessageBase]]), (RaftNodeState, TerminalSymbol[NodeType, EventType, RaftMessageBase]),
(RaftNodeState, Option[ConsensusFsmTransActionType]) (RaftNodeState, Option[ConsensusFsmTransitionActionType])
] ]
# FSM type definition # FSM type definition
@ -70,7 +71,7 @@ proc addFsmTransition*[RaftNodeState, EventType, NodeType, RaftMessageBase](
fromState: RaftNodeState, fromState: RaftNodeState,
termSymb: TerminalSymbol[EventType, NodeType, RaftMessageBase], termSymb: TerminalSymbol[EventType, NodeType, RaftMessageBase],
toState: RaftNodeState, toState: RaftNodeState,
action: Option[ConsensusFsmTransActionType]) = action: Option[ConsensusFsmTransitionActionType]) =
fsm.stateTransitionsLut[(fromState.state, termSymb)] = (toState, action) fsm.stateTransitionsLut[(fromState.state, termSymb)] = (toState, action)