nim-raft/raft/protocol.nim

48 lines
2.4 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.
# #
2023-08-09 11:44:15 +03:00
# Raft Messages Protocol definition #
# #
2023-07-29 15:54:41 +03:00
import types
2023-08-09 11:44:15 +03:00
import options
type
2023-08-09 11:44:15 +03:00
# Raft Node Messages OPs
RaftMessageOps* = enum
rmoRequestVote = 0,
rmoAppendLogEntry = 1,
rmoInstallSnapshot = 2 # For dynamic adding of new Raft Nodes
RaftMessagePayloadChecksum* = object # Checksum probably will be a SHA3 hash not sure about this at this point
RaftMessagePayload*[LogEntryDataType] = ref object
data*: RaftNodeLogEntry[LogEntryDataType]
checksum*: RaftMessagePayloadChecksum
RaftMessage*[LogEntryDataType] = ref object of RaftMessageBase
2023-08-09 11:57:30 +03:00
op*: RaftMessageOps # Message Op - Ask For Votes, Append Entry(ies), Install Snapshot etc.
2023-08-09 11:44:15 +03:00
payload*: Option[seq[RaftMessagePayload[LogEntryDataType]]] # Optional Message Payload(s) - e.g. log entry(ies). Will be empty for a Heart-Beat # Heart-Beat will be a message with Append Entry(ies) Op and empty payload
RaftMessageResponse*[SmStateType] = ref object of RaftMessageBase
2023-08-09 11:21:58 +03:00
success*: bool # Indicates success/failure
2023-08-09 11:44:15 +03:00
state*: Option[SmStateType] # Raft Abstract State Machine State
2023-08-09 11:44:15 +03:00
# Raft Node Client Request/Response definitions
RaftNodeClientRequestOps = enum
rncroRequestState = 0,
rncroAppendNewEntry = 1
2023-08-09 11:44:15 +03:00
RaftNodeClientRequest*[LogEntryDataType] = ref object
op*: RaftNodeClientRequestOps
payload*: Option[RaftMessagePayload[LogEntryDataType]] # Optional RaftMessagePayload carrying a Log Entry
2023-08-09 11:44:15 +03:00
RaftNodeClientResponse*[SmStateType] = ref object
2023-08-09 11:21:58 +03:00
success*: bool # Indicate succcess
2023-08-09 11:44:15 +03:00
state*: Option[SmStateType] # Optional Raft Abstract State Machine State
2023-08-09 11:57:30 +03:00
raftNodeRedirectId*: Option[RaftNodeId] # Optional Raft Node ID to redirect the request to in case of failure