Merge pull request #112 from goraft/minor-refactor-add-remove-peer

Minor refactor of AddPeer/RemovePeer.
pull/820/head
Xiang Li 2013-08-19 09:03:27 -07:00
commit b71d2ecae9
1 changed files with 26 additions and 29 deletions

View File

@ -927,17 +927,14 @@ func (s *Server) processRequestVoteRequest(req *RequestVoteRequest) (*RequestVot
// Adds a peer to the server. // Adds a peer to the server.
func (s *Server) AddPeer(name string, connectiongString string) error { func (s *Server) AddPeer(name string, connectiongString string) error {
s.debugln("server.peer.add: ", name, len(s.peers)) s.debugln("server.peer.add: ", name, len(s.peers))
defer s.writeConf()
// Do not allow peers to be added twice. // Do not allow peers to be added twice.
if s.peers[name] != nil { if s.peers[name] != nil {
return nil return nil
} }
// Skip the Peer if it has the same name as the Server // Skip the Peer if it has the same name as the Server
if s.name == name { if s.name != name {
return nil
}
peer := newPeer(s, name, connectiongString, s.heartbeatTimeout) peer := newPeer(s, name, connectiongString, s.heartbeatTimeout)
if s.State() == Leader { if s.State() == Leader {
@ -945,8 +942,10 @@ func (s *Server) AddPeer(name string, connectiongString string) error {
} }
s.peers[peer.Name] = peer s.peers[peer.Name] = peer
}
s.debugln("server.peer.conf.write: ", name) // Write the configuration to file.
s.writeConf()
return nil return nil
} }
@ -955,14 +954,8 @@ func (s *Server) AddPeer(name string, connectiongString string) error {
func (s *Server) RemovePeer(name string) error { func (s *Server) RemovePeer(name string) error {
s.debugln("server.peer.remove: ", name, len(s.peers)) s.debugln("server.peer.remove: ", name, len(s.peers))
defer s.writeConf() // Skip the Peer if it has the same name as the Server
if name != s.Name() {
if name == s.Name() {
// when the removed node restart, it should be able
// to know it has been removed before. So we need
// to update knownCommitIndex
return nil
}
// Return error if peer doesn't exist. // Return error if peer doesn't exist.
peer := s.peers[name] peer := s.peers[name]
if peer == nil { if peer == nil {
@ -975,6 +968,10 @@ func (s *Server) RemovePeer(name string) error {
} }
delete(s.peers, name) delete(s.peers, name)
}
// Write the configuration to file.
s.writeConf()
return nil return nil
} }