Add DefaultUUID(00000000-0000-0000-0000-000000000000) etc.

This commit is contained in:
Raycho Mukelov 2023-09-01 02:56:15 +03:00
parent 497b309379
commit ad5dd67560
5 changed files with 32 additions and 23 deletions

View File

@ -1,3 +0,0 @@
* testbasictimers - 503 milliseconds, 739 microseconds, and 790 nanoseconds
* testbasicstatemachine - 1 millisecond, 551 microseconds, and 593 nanoseconds
* testbasiccluster - 1 millisecond, 333 microseconds, and 100 nanoseconds

View File

@ -16,23 +16,23 @@ export types, protocol, consensus_module
proc RaftNodeSmInit[SmCommandType, SmStateType](stateMachine: var RaftNodeStateMachine[SmCommandType, SmStateType])
# Raft Node Public API procedures / functions
proc RaftNodeCreateNew*[SmCommandType, SmStateType]( # Create New Raft Node
id: RaftNodeId, peersIds: seq[RaftNodeId],
proc new*[SmCommandType, SmStateType](T: type RaftNode[SmCommandType, SmStateType]; # Create New Raft Node
id: RaftNodeId; peersIds: seq[RaftNodeId];
# persistentStorage: RaftNodePersistentStorage,
msgSendCallback: RaftMessageSendCallback): RaftNode[SmCommandType, SmStateType] =
msgSendCallback: RaftMessageSendCallback): T =
var
sm: RaftNodeStateMachine[SmCommandType, SmStateType]
peers: RaftNodePeers
RaftNodeSmInit[SmCommandType, SmStateType](sm)
for peerId in peersIds:
peers.add(RaftNodePeer(id: peerId, nextIndex: 0, matchIndex: 0, hasVoted: false, canVote: true))
result = RaftNode[SmCommandType, SmStateType](
result = T(
id: id, state: rnsFollower, currentTerm: 0, peers: peers, commitIndex: 0, lastApplied: 0,
stateMachine: sm, msgSendCallback: msgSendCallback
stateMachine: sm, msgSendCallback: msgSendCallback, votedFor: DefaultUUID, currentLeaderId: DefaultUUID
)
RaftNodeSmInit[SmCommandType, SmStateType](result.stateMachine)
initLock(result.raftStateMutex)
proc RaftNodeLoad*[SmCommandType, SmStateType](
@ -44,13 +44,13 @@ proc RaftNodeStop*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmS
discard
proc RaftNodeStart*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]) =
discard
debugEcho "StartNode: ", node.id
func RaftNodeIdGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeId = # Get Raft Node ID
node.id
func RaftNodeIdGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeId {.gcsafe.} = # Get Raft Node ID
result = node.id
func RaftNodeStateGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeState = # Get Raft Node State
node.state
node.state
func RaftNodeTermGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftNodeTerm = # Get Raft Node Term
node.currentTerm

View File

@ -17,6 +17,9 @@ import uuids
export results, options, locks, uuids
const
DefaultUUID* = initUUID(0, 0) # 00000000-0000-0000-0000-000000000000
type
RaftNodeState* = enum
rnsUnknown = 0,
@ -141,15 +144,15 @@ type
id*: RaftNodeId # This Raft Node ID
state*: RaftNodeState # This Raft Node State
currentTerm*: RaftNodeTerm # Latest term this Raft Node has seen (initialized to 0 on first boot, increases monotonically)
votedFor*: RaftNodeId # Candidate RaftNodeId that received vote in current term (or nil/zero if none),
# also used to redirect Client Requests in case this Raft Node is not the leader
log: RaftNodeLog[SmCommandType] # This Raft Node Log
votedFor*: RaftNodeId # Candidate RaftNodeId that received vote in current term (or DefaultUUID if none),
# also used to redirect Client Requests in case this Raft Node is not the leader
log: RaftNodeLog[SmCommandType] # This Raft Node Log
stateMachine*: RaftNodeStateMachine[SmCommandType, SmStateType] # Not sure for now putting it here. I assume that persisting the State Machine's
# state is enough to consider it 'persisted'
peers*: RaftNodePeers # This Raft Node Peers IDs. I am not sure if this must be persistent or volatile but making it persistent
# makes sense for the moment
# makes sense for the moment
# Volatile state
commitIndex*: RaftLogIndex # Index of highest log entry known to be committed (initialized to 0, increases monotonically)
lastApplied*: RaftLogIndex # Index of highest log entry applied to state machine (initialized to 0, increases monotonically)
currentLeaderId: RaftNodeId # The ID of the cirrent leader Raft Node or 0/nil if None is leader (election is in progress etc.)
currentLeaderId*: RaftNodeId # The ID of the current leader Raft Node or DefaultUUID if None is leader (election is in progress etc.)

View File

@ -9,7 +9,6 @@
import basic_timers
import basic_state_machine
import ../raft/raft_api
export raft_api
@ -25,12 +24,22 @@ proc BasicRaftClusterRaftMessageSendCallbackCreate(cluster: BasicRaftCluster): R
nodeIdx: int = -1
for i in 0..cluster.nodes.len:
if cluster.nodes[i].id == msg.receiverId:
if RaftNodeIdGet(cluster.nodes[i]) == msg.receiverId:
nodeIdx = i
break
cluster.nodes[nodeIdx].RaftNodeMessageDeliver(msg)
proc BasicRaftClusterStart*(cluster: BasicRaftCluster) =
for node in cluster.nodes:
RaftNodeStart(node)
proc BasicRaftClusterGetLeader*(cluster: BasicRaftCluster): UUID =
result = DefaultUUID
for node in cluster.nodes:
if RaftNodeIsLeader(node):
return RaftNodeIdGet(node)
proc BasicRaftClusterClientRequest*(cluster: BasicRaftCluster, req: RaftNodeClientRequest): RaftNodeClientResponse =
discard
@ -41,5 +50,5 @@ proc BasicRaftClusterInit*(nodesIds: seq[RaftNodeId]): BasicRaftCluster =
peersIds = nodesIds
peersIds.del(peersIds.find(nodeId))
result.nodes.add(RaftNodeCreateNew[SmCommand, SmState](nodeId, peersIds, BasicRaftClusterRaftMessageSendCallbackCreate(result)))
result.nodes.add(BasicRaftNode.new(nodeId, peersIds, BasicRaftClusterRaftMessageSendCallbackCreate(result)))

View File

@ -28,7 +28,7 @@ proc basicClusterMain*() =
discard
test "Start Basic Raft Cluster And wait it to converge (Elect a Leader)":
discard
BasicRaftClusterStart(cluster)
test "Simulate Basic Raft Cluster Client SmCommands Execution / Log Replication":
discard