private void judge_Election() { StopThread(); switch (ServerHandler_.election_state_) { case ElectionState.ElectionFailed: ServerHandler_.ToBeFollower(); break; case ElectionState.ElectionSuccess: ServerHandler_.ToBeLeader(); ServerHandler_.reInitLeader(); break; case ElectionState.HigherTerm: ServerHandler_.ToBeFollower(); break; case ElectionState.Stop: ServerHandler_.ToBeFollower(); break; } SwitchThread(); StartThread(); ServerHandler_.is_election_done_ = false; }
private void commit() { while (!node_should_stop_) { ServerHandler_.commit(); } }
private void judge_Timeout() { if (ServerHandler_.role == NodeRole.Candidate) { lock (this) { StopThread(); SwitchThread(); StartThread(); ServerHandler_.ToBeRunning(); } } else { ServerHandler_.LOG(String.Format("get Timeout state")); } }
private void judge_FindLeader() { if (ServerHandler_.role == NodeRole.Follower) { lock (this) { StopThread(); SwitchThread(); StartThread(); ServerHandler_.ToBeRunning(); } } else { ServerHandler_.LOG(String.Format("get FindLeader state")); } }
private void judge_HigherTerm() { // 引发higher term的时候就已经把role改为了follower if (ServerHandler_.role != NodeRole.Follower) { // do nothing } else { lock (this) { StopThread(); ServerHandler_.ToBeFollower(); SwitchThread(); StartThread(); ServerHandler_.ToBeRunning(); } } }
private void state_checker() { Console.WriteLine("state checker thread running..."); while (!node_should_stop_) { if (ServerHandler_.is_election_done_) { lock (this) { judge_Election(); } } lock (this) { switch (ServerHandler_.state) //judge internal state(state can be verify in heartbeat and vote) { case ServerState.Running: // everything is ok! break; case ServerState.FindLeader: judge_FindLeader(); break; case ServerState.Timeout: judge_Timeout(); break; case ServerState.HigherTerm: judge_HigherTerm(); break; default: ServerHandler_.LOG(String.Format("Unknown state {0}", ServerHandler_.state)); break; } } } Console.WriteLine("{0} state checker thread existed!", ServerHandler_.id); }