2023-08-09 10:06:34 +00: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
|
|
|
|
# those terms.
|
|
|
|
|
2023-09-04 09:47:27 +00:00
|
|
|
# **************************** #
|
2023-08-14 20:49:21 +00:00
|
|
|
# Raft Protocol definition #
|
2023-09-04 09:47:27 +00:00
|
|
|
# **************************** #
|
2023-08-09 10:06:34 +00:00
|
|
|
import types
|
|
|
|
|
|
|
|
type
|
2023-09-17 00:47:29 +00:00
|
|
|
RaftMessage*[SmCommandType, SmStateType] = ref object of RaftMessageBase[SmCommandType, SmStateType]
|
|
|
|
senderTerm*: RaftNodeTerm # Sender Raft Node Term
|
|
|
|
case op*: RaftMessageOps
|
|
|
|
of rmoRequestVote:
|
|
|
|
lastLogTerm*: RaftNodeTerm
|
|
|
|
lastLogIndex*: RaftLogIndex
|
|
|
|
of rmoAppendLogEntry:
|
|
|
|
prevLogIndex*: RaftLogIndex
|
|
|
|
prevLogTerm*: RaftNodeTerm
|
|
|
|
commitIndex*: RaftLogIndex
|
|
|
|
logEntries*: Option[seq[RaftNodeLogEntry[SmCommandType]]] # Optional log entry(ies). Will be empty for a Heart-Beat
|
|
|
|
of rmoInstallSnapshot:
|
|
|
|
discard
|
|
|
|
|
|
|
|
RaftMessageResponse*[SmCommandType, SmStateType] = ref object of RaftMessageResponseBase[SmCommandType, SmStateType]
|
|
|
|
case op*: RaftMessageOps
|
|
|
|
of rmoRequestVote:
|
|
|
|
granted*: bool # Is vote granted by the Raft node, from we requested vote?
|
|
|
|
of rmoAppendLogEntry:
|
|
|
|
success*: bool
|
|
|
|
lastLogIndex*: RaftLogIndex
|
|
|
|
state*: Option[SmStateType] # Optional Raft Abstract State Machine State
|
|
|
|
of rmoInstallSnapshot:
|
|
|
|
discard
|
2023-08-09 10:06:34 +00:00
|
|
|
|
|
|
|
# Raft Node Client Request/Response definitions
|
2023-08-14 20:49:21 +00:00
|
|
|
RaftNodeClientRequestOps* = enum
|
|
|
|
rncroRequestSmState = 0,
|
|
|
|
rncroExecSmCommand = 1
|
|
|
|
|
2023-09-02 21:16:26 +00:00
|
|
|
RaftNodeClientResponseError* = enum
|
2023-08-22 01:04:47 +00:00
|
|
|
rncreSuccess = 0,
|
|
|
|
rncreFail = 1,
|
2023-10-20 00:35:42 +00:00
|
|
|
rncreNotLeader = 2,
|
|
|
|
rncreStopped = 3
|
2023-08-09 10:06:34 +00:00
|
|
|
|
2023-08-14 20:49:21 +00:00
|
|
|
RaftNodeClientRequest*[SmCommandType] = ref object
|
2023-08-09 10:06:34 +00:00
|
|
|
op*: RaftNodeClientRequestOps
|
2023-08-31 20:52:52 +00:00
|
|
|
nodeId*: RaftNodeId
|
2023-08-14 20:49:21 +00:00
|
|
|
payload*: Option[SmCommandType] # Optional RaftMessagePayload carrying a Log Entry
|
2023-08-09 10:06:34 +00:00
|
|
|
|
|
|
|
RaftNodeClientResponse*[SmStateType] = ref object
|
2023-09-04 09:47:27 +00:00
|
|
|
nodeId*: RaftNodeId
|
2023-08-14 20:49:21 +00:00
|
|
|
error*: RaftNodeClientResponseError
|
2023-08-09 10:06:34 +00:00
|
|
|
state*: Option[SmStateType] # Optional Raft Abstract State Machine State
|
|
|
|
raftNodeRedirectId*: Option[RaftNodeId] # Optional Raft Node ID to redirect the request to in case of failure
|