Merge pull request #112 from goraft/minor-refactor-add-remove-peer
Minor refactor of AddPeer/RemovePeer.pull/820/head
commit
b71d2ecae9
55
server.go
55
server.go
|
@ -927,26 +927,25 @@ 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)
|
||||||
|
|
||||||
|
if s.State() == Leader {
|
||||||
|
peer.startHeartbeat()
|
||||||
|
}
|
||||||
|
|
||||||
|
s.peers[peer.Name] = peer
|
||||||
}
|
}
|
||||||
|
|
||||||
peer := newPeer(s, name, connectiongString, s.heartbeatTimeout)
|
// Write the configuration to file.
|
||||||
|
s.writeConf()
|
||||||
if s.State() == Leader {
|
|
||||||
peer.startHeartbeat()
|
|
||||||
}
|
|
||||||
|
|
||||||
s.peers[peer.Name] = peer
|
|
||||||
|
|
||||||
s.debugln("server.peer.conf.write: ", name)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -955,26 +954,24 @@ 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() {
|
||||||
|
// Return error if peer doesn't exist.
|
||||||
|
peer := s.peers[name]
|
||||||
|
if peer == nil {
|
||||||
|
return fmt.Errorf("raft: Peer not found: %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
if name == s.Name() {
|
// Stop peer and remove it.
|
||||||
// when the removed node restart, it should be able
|
if s.State() == Leader {
|
||||||
// to know it has been removed before. So we need
|
peer.stopHeartbeat(true)
|
||||||
// to update knownCommitIndex
|
}
|
||||||
return nil
|
|
||||||
}
|
delete(s.peers, name)
|
||||||
// Return error if peer doesn't exist.
|
|
||||||
peer := s.peers[name]
|
|
||||||
if peer == nil {
|
|
||||||
return fmt.Errorf("raft: Peer not found: %s", name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop peer and remove it.
|
// Write the configuration to file.
|
||||||
if s.State() == Leader {
|
s.writeConf()
|
||||||
peer.stopHeartbeat(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(s.peers, name)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue