2023-09-03 03:27:27 +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.
|
|
|
|
|
|
|
|
import types
|
2023-10-13 06:03:42 +00:00
|
|
|
import chronicles
|
2023-09-03 03:27:27 +00:00
|
|
|
|
|
|
|
# Private Log Ops
|
2023-10-13 04:24:35 +00:00
|
|
|
proc raftNodeLogIndexGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType]): RaftLogIndex =
|
2023-09-11 16:55:30 +00:00
|
|
|
len(node.log.logData)
|
2023-09-03 03:27:27 +00:00
|
|
|
|
2023-10-13 04:24:35 +00:00
|
|
|
proc raftNodeLogEntryGet*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], logIndex: RaftLogIndex): RaftNodeLogEntry[SmCommandType] =
|
2023-09-11 16:55:30 +00:00
|
|
|
if logIndex > 0:
|
2023-10-20 00:35:42 +00:00
|
|
|
result = node.log.logData[logIndex - 1]
|
2023-09-07 02:41:05 +00:00
|
|
|
|
2023-10-13 04:24:35 +00:00
|
|
|
proc raftNodeLogAppend*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], logEntry: RaftNodeLogEntry[SmCommandType]) =
|
|
|
|
node.log.logData.add(logEntry)
|
2023-09-07 02:41:05 +00:00
|
|
|
|
2023-10-13 06:03:42 +00:00
|
|
|
proc raftNodeLogTruncate*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], truncateIndex: RaftLogIndex) =
|
|
|
|
debug "Truncating log to index: ", truncateIndex=truncateIndex, ld=repr(node.log.logData)
|
|
|
|
# node.log.logData = node.log.logData[:truncateIndex]
|
2023-10-13 04:24:35 +00:00
|
|
|
|
2023-10-20 01:20:44 +00:00
|
|
|
proc raftNodeApplyLogEntry*[SmCommandType, SmStateType](node: RaftNode[SmCommandType, SmStateType], entryIndex: RaftLogIndex) =
|
2023-10-13 04:24:35 +00:00
|
|
|
mixin raftNodeSmApply
|
|
|
|
|
2023-10-20 01:20:44 +00:00
|
|
|
let logEntry = raftNodeLogEntryGet(node, entryIndex)
|
|
|
|
|
|
|
|
if entryIndex > node.lastApplied:
|
|
|
|
debug "Applying log entry: ", node_id=node.id, entryIndex=entryIndex, entry=repr(logEntry)
|
2023-10-20 01:41:10 +00:00
|
|
|
raftNodeSmApply(node.stateMachine, raftNodeLogEntryGet(node, entryIndex).data.get)
|
2023-10-20 01:20:44 +00:00
|
|
|
node.lastApplied = entryIndex
|
|
|
|
|
|
|
|
else:
|
|
|
|
debug "Log entry already applied: ", node_id=node.id, entryIndex=entryIndex, entry=repr(logEntry)
|