Repository: Migorithm/duva (rust)

Issue:
Bug: client request hangs when first election is made
## Setup
- Node A, B, C.
- At time t0, Node A is the leader.
- Node A goes down.
- A new leader is elected from B or C.
- A client sends a request (e.g., a write) to the new leader.

Here, client request hangs. 

Pull request description:
fix: client hang after election
closed #625 

Patch:
diff --git a/duva/src/domains/cluster_actors/consensus/log.rs b/duva/src/domains/cluster_actors/consensus/log.rs
index 5e74fe73..598df712 100644
--- a/duva/src/domains/cluster_actors/consensus/log.rs
+++ b/duva/src/domains/cluster_actors/consensus/log.rs
@@ -38,7 +38,7 @@ impl LogConsensusVoting {
         replica_count: usize,
         session_req: Option<SessionRequest>,
     ) -> Self {
-        Self { callback, cnt: 0, voters: Vec::with_capacity(replica_count), session_req }
+        Self { callback, cnt: 1, voters: Vec::with_capacity(replica_count), session_req }
     }
 
     pub(crate) fn increase_vote(&mut self, voter: PeerIdentifier) {
@@ -47,11 +47,11 @@ impl LogConsensusVoting {
     }
 
     pub(crate) fn get_required_votes(&self) -> u8 {
-        let replica_count = self.voters.capacity() as u8;
-        (replica_count + 1).div_ceil(2)
+        let total_nodes = self.voters.capacity() as u8 + 1;
+        (total_nodes + 1).div_ceil(2)
     }
 
     pub(crate) fn votable(&self, voter: &PeerIdentifier) -> bool {
-        !self.voters.iter().any(|v| v == voter)
+        !self.voters.contains(voter)
     }
 }


Test patch:
diff --git a/duva/src/domains/cluster_actors/actor/tests/replications.rs b/duva/src/domains/cluster_actors/actor/tests/replications.rs
index e7d72dbb..c00257d9 100644
--- a/duva/src/domains/cluster_actors/actor/tests/replications.rs
+++ b/duva/src/domains/cluster_actors/actor/tests/replications.rs
@@ -486,8 +486,7 @@ async fn test_consensus_voting_deleted_when_consensus_reached() {
 
     let (cluster_sender, _) = tokio::sync::mpsc::channel(100);
 
-    // - add followers to create quorum
-
+    // - add 4 followers to create quorum - so 2 votes are needed to reach consensus
     let follower_buffs = (0..4).map(|_| FakeReadWrite::new()).collect::<Vec<_>>();
     cluster_member_create_helper(
         &mut cluster_actor,
@@ -511,12 +510,12 @@ async fn test_consensus_voting_deleted_when_consensus_reached() {
         rej_reason: RejectionReason::None,
         from: PeerIdentifier("".into()),
     };
+    // Leader already has 1 vote, so we only need 1 more votes to reach consensus
     cluster_actor.track_replication_progress(follower_res.clone().set_from("repl1"));
-    cluster_actor.track_replication_progress(follower_res.clone().set_from("repl2"));
 
     // up to this point, tracker hold the consensus
     assert_eq!(cluster_actor.consensus_tracker.len(), 1);
-    assert_eq!(cluster_actor.consensus_tracker.get(&1).unwrap().voters.len(), 2);
+    assert_eq!(cluster_actor.consensus_tracker.get(&1).unwrap().voters.len(), 1);
 
     // ! Majority votes made
     cluster_actor.track_replication_progress(follower_res.set_from("repl3"));