From 98f06836548d744b199ab5faf083093e52abb050 Mon Sep 17 00:00:00 2001 From: Raycho Mukelov Date: Fri, 20 Oct 2023 07:51:37 +0300 Subject: [PATCH] Simulate Network delay in the basic cluster tests --- raft/consensus_module.nim | 4 ++++ raft/raft_api.nim | 3 ++- tests/basic_cluster.nim | 5 ++++- tests/test_basic_cluster_election.nim | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/raft/consensus_module.nim b/raft/consensus_module.nim index ec84019..4106f54 100644 --- a/raft/consensus_module.nim +++ b/raft/consensus_module.nim @@ -181,6 +181,10 @@ proc raftNodeReplicateSmCommand*[SmCommandType, SmStateType](node: RaftNode[SmCo raftNodeLogAppend(node, logEntry) + for fut in node.replicateFuts: + discard fut + node.replicateFuts.clear + for peer in node.peers: var msg: RaftMessage[SmCommandType, SmStateType] = RaftMessage[SmCommandType, SmStateType]( diff --git a/raft/raft_api.nim b/raft/raft_api.nim index c8675c9..fed61cf 100644 --- a/raft/raft_api.nim +++ b/raft/raft_api.nim @@ -20,7 +20,8 @@ export protocol, consensus_module, log_ops, - chronicles + chronicles, + random # Forward declarations proc raftNodeSmInit*[SmCommandType, SmStateType](stateMachine: var RaftNodeStateMachine[SmCommandType, SmStateType]) diff --git a/tests/basic_cluster.nim b/tests/basic_cluster.nim index 23da196..4609420 100644 --- a/tests/basic_cluster.nim +++ b/tests/basic_cluster.nim @@ -19,9 +19,11 @@ type BasicRaftCluster* = ref object nodes*: Table[RaftNodeId, BasicRaftNode] nodesLock*: RLock + networkDelay*: int proc basicRaftClusterRaftMessageSendCallbackCreate[SmCommandType, SmStateType](cluster: BasicRaftCluster): RaftMessageSendCallback[SmCommandType, SmStateType] = proc (msg: RaftMessageBase[SmCommandType, SmStateType]): Future[RaftMessageResponseBase[SmCommandType, SmStateType]] {.async, gcsafe.} = + await raftTimerCreate(rand(cluster.networkDelay), proc()=discard) # Simulate network delay result = await cluster.nodes[msg.receiverId].raftNodeMessageDeliver(msg) proc basicRaftClusterStart*(cluster: BasicRaftCluster) = @@ -46,7 +48,7 @@ proc basicRaftClusterClientRequest*(cluster: BasicRaftCluster, req: RaftNodeClie of rncroExecSmCommand: discard -proc basicRaftClusterInit*(nodesIds: seq[RaftNodeId], electionTimeout: int=150, heartBeatTimeout: int=150, appendEntriesRespTimeout: int=20, votingRespTimeout: int=20, +proc basicRaftClusterInit*(nodesIds: seq[RaftNodeId], networkDelay: int=25, electionTimeout: int=150, heartBeatTimeout: int=150, appendEntriesRespTimeout: int=20, votingRespTimeout: int=20, heartBeatRespTimeout: int=10): BasicRaftCluster = new(result) for nodeId in nodesIds: @@ -54,6 +56,7 @@ proc basicRaftClusterInit*(nodesIds: seq[RaftNodeId], electionTimeout: int=150, peersIds = nodesIds peersIds.del(peersIds.find(nodeId)) + result.networkDelay = networkDelay result.nodes[nodeId] = BasicRaftNode.new(nodeId, peersIds, basicRaftClusterRaftMessageSendCallbackCreate[SmCommand, SmState](result), electionTimeout, heartBeatTimeout, appendEntriesRespTimeout, votingRespTimeout, heartBeatRespTimeout) diff --git a/tests/test_basic_cluster_election.nim b/tests/test_basic_cluster_election.nim index 7d858fd..7d37f37 100644 --- a/tests/test_basic_cluster_election.nim +++ b/tests/test_basic_cluster_election.nim @@ -20,7 +20,7 @@ proc basicClusterElectionMain*() = test "Basic Raft Cluster Init (5 nodes)": for i in 0..4: nodesIds[i] = genUUID() - cluster = basicRaftClusterInit(nodesIds, 150, 150, 20, 20, 10) + cluster = basicRaftClusterInit(nodesIds, 25, 150, 150, 20, 20, 10) check cluster != nil test "Start Basic Raft Cluster and wait it to converge for a 2 seconds interval (Elect a Leader)":