async fn leader_selection_deamon(&self) {
        loop {
            sleep(generate_election_timeout()).await;
            {
                let mut raft = self.inner.lock().unwrap();
                // if self is leader, do not issue leader selection
                // if self is not leader and already recv heartbeat, do not issue leader selection
                if raft.state.is_leader() || raft.recv_hb_from_last_check {
                    raft.recv_hb_from_last_check = false;
                    continue;
                } else {
                    raft.state.role = Role::Candidate;
                }
            }
            let mut rpcs = self.inner.lock().unwrap().send_vote_request();
            self.persist().await.expect("failed to persist");
            let inner = Arc::clone(&self.inner);
            let this = self.clone();
            task::spawn(async move {
                let mut counter: usize = 1;
                while let Some(res) = rpcs.next().await {
                    if let Ok(res) = res {
                        let mut raft = inner.lock().unwrap();
                        if !raft.check_incoming_term(res.term) || raft.state.role != Role::Candidate
                        {
                            break;
                        }
                        if res.vote_granted {
                            counter += 1;
                        }
                        if counter > raft.peers.len() / 2 {
                            //TODO send heartbeat immediately after becoming leader
                            info!("[Vote] Server {} become leader", raft.me);
                            raft.state.role = Role::Leader;
                            // initialize nextIndex and matchIndex after becoming leaders
                            let last_log_index = raft.last_log_index();
                            for item in raft.next_index.iter_mut() {
                                *item = last_log_index + 1;
                            }
                            for item in raft.match_index.iter_mut() {
                                *item = 0;
                            }
                            drop(raft);
                            task::spawn(async move { this.leader_append_deamon().await }).detach();
                            break;
                        }
                    }
                }
            })
            .detach();
        }
    }
/// used for checking leader selection timeout periodically
/// Note: this deamon will never terminate after this server is initialized
used for checking leader selection timeout periodically
Note: this deamon will never terminate after this server is initialized
used for checking leader selection timeout periodically Note : this deamon will never terminate after this server is initialized
async fn leader_selection_deamon(&self) {
        loop {
            sleep(generate_election_timeout()).await;
            {
                let mut raft = self.inner.lock().unwrap();
                // if self is leader, do not issue leader selection
                // if self is not leader and already recv heartbeat, do not issue leader selection
                if raft.state.is_leader() || raft.recv_hb_from_last_check {
                    raft.recv_hb_from_last_check = false;
                    continue;
                } else {
                    raft.state.role = Role::Candidate;
                }
            }
            let mut rpcs = self.inner.lock().unwrap().send_vote_request();
            self.persist().await.expect("failed to persist");
            let inner = Arc::clone(&self.inner);
            let this = self.clone();
            task::spawn(async move {
                let mut counter: usize = 1;
                while let Some(res) = rpcs.next().await {
                    if let Ok(res) = res {
                        let mut raft = inner.lock().unwrap();
                        if !raft.check_incoming_term(res.term) || raft.state.role != Role::Candidate
                        {
                            break;
                        }
                        if res.vote_granted {
                            counter += 1;
                        }
                        if counter > raft.peers.len() / 2 {
                            //TODO send heartbeat immediately after becoming leader
                            info!("[Vote] Server {} become leader", raft.me);
                            raft.state.role = Role::Leader;
                            // initialize nextIndex and matchIndex after becoming leaders
                            let last_log_index = raft.last_log_index();
                            for item in raft.next_index.iter_mut() {
                                *item = last_log_index + 1;
                            }
                            for item in raft.match_index.iter_mut() {
                                *item = 0;
                            }
                            drop(raft);
                            task::spawn(async move { this.leader_append_deamon().await }).detach();
                            break;
                        }
                    }
                }
            })
            .detach();
        }
    }
async fn leader_selection_deamon ( & self ) { loop { sleep ( generate_election_timeout ( ) ) . await ; { let mut raft = self . inner . lock ( ) . unwrap ( ) ; // if self is leader, do not issue leader selection // if self is not leader and already recv heartbeat, do not issue leader selection if raft . state . is_leader ( ) || raft . recv_hb_from_last_check { raft . recv_hb_from_last_check = false ; continue ; } else { raft . state . role = Role :: Candidate ; } } let mut rpcs = self . inner . lock ( ) . unwrap ( ) . send_vote_request ( ) ; self . persist ( ) . await . expect ( " " ) ; let inner = Arc :: clone ( & self . inner ) ; let this = self . clone ( ) ; task :: spawn ( async move { let mut counter : usize = 1 ; while let Some ( res ) = rpcs . next ( ) . await { if let Ok ( res ) = res { let mut raft = inner . lock ( ) . unwrap ( ) ; if ! raft . check_incoming_term ( res . term ) || raft . state . role != Role :: Candidate { break ; } if res . vote_granted { counter += 1 ; } if counter > raft . peers . len ( ) / 2 { //TODO send heartbeat immediately after becoming leader info ! ( " " raft me ) ; raft . state . role = Role :: Leader ; // initialize nextIndex and matchIndex after becoming leaders let last_log_index = raft . last_log_index ( ) ; for item in raft . next_index . iter_mut ( ) { * item = last_log_index + 1 ; } for item in raft . match_index . iter_mut ( ) { * item = 0 ; } drop ( raft ) ; task :: spawn ( async move { this . leader_append_deamon ( ) . await } ) . detach ( ) ; break ; } } } } ) . detach ( ) ; } }
used for checking leader selection timeout periodically
Note: this deamon will never terminate after this server is initialized
used for checking leader selection timeout periodically Note : this deamon will never terminate after this server is initialized
&self
&self