refactor(find_cluster): make it simpler

release-0.4
Yicheng Qin 2014-05-09 02:08:50 -07:00
parent baadf63912
commit 765cd5d8b3
1 changed files with 15 additions and 31 deletions

View File

@ -211,43 +211,26 @@ func (s *PeerServer) FindCluster(discoverURL string, peers []string) (toStart bo
// Attempt cluster discovery // Attempt cluster discovery
if discoverURL != "" { if discoverURL != "" {
discoverPeers, discoverErr := s.handleDiscovery(discoverURL) discoverPeers, discoverErr := s.handleDiscovery(discoverURL)
// It is registered in discover url // It is not registered in discover url
if discoverErr == nil { if discoverErr != nil {
// start as a leader in a new cluster log.Warnf("%s failed to connect discovery service[%v]: %v", name, discoverURL, discoverErr)
if len(discoverPeers) == 0 { if len(peers) == 0 {
s.isNewCluster = true err = fmt.Errorf("%s, the new instance, must register itself to discovery service as required", name)
log.Debugf("%s is starting a new cluster via discover service", name)
toStart = true
return return
} }
log.Debugf("%s is joining peers %v from -peers flag", name, peers)
} else {
log.Debugf("%s is joining a cluster %v via discover service", name, discoverPeers) log.Debugf("%s is joining a cluster %v via discover service", name, discoverPeers)
if rejected, ierr := s.startAsFollower(discoverPeers, s.Config.RetryTimes); rejected { peers = discoverPeers
log.Debugf("%s should work as standby for the cluster %v: %v", name, discoverPeers, ierr)
possiblePeers = discoverPeers
} else if ierr != nil {
log.Warnf("%s cannot connect to existing cluster %v: %v", name, discoverPeers, ierr)
err = ierr
} else {
toStart = true
}
return
}
log.Warnf("%s failed to connect discovery service[%v]: %v", name, discoverURL, discoverErr)
if len(peers) == 0 {
err = fmt.Errorf("%s, the new instance, must register itself to discovery service as required", name)
return
} }
} }
possiblePeers = peers
if len(peers) > 0 { if len(possiblePeers) > 0 {
log.Debugf("%s is joining peers %v from -peers flag", name, peers) if rejected, ierr := s.startAsFollower(possiblePeers, s.Config.RetryTimes); rejected {
if rejected, ierr := s.startAsFollower(peers, s.Config.RetryTimes); rejected { log.Debugf("%s should work as standby for the cluster %v: %v", name, possiblePeers, ierr)
log.Debugf("%s should work as standby for the cluster %v: %v", name, peers, ierr)
possiblePeers = peers
} else if ierr != nil { } else if ierr != nil {
log.Warnf("%s cannot connect to existing peers %v: %v", name, peers, ierr) log.Warnf("%s cannot connect to existing peers %v: %v", name, possiblePeers, ierr)
err = ierr err = ierr
} else { } else {
toStart = true toStart = true
@ -255,8 +238,9 @@ func (s *PeerServer) FindCluster(discoverURL string, peers []string) (toStart bo
return return
} }
// start as a leader in a new cluster
s.isNewCluster = true s.isNewCluster = true
log.Infof("%s is starting a new cluster.", s.Config.Name) log.Infof("%s is starting a new cluster", s.Config.Name)
toStart = true toStart = true
return return
} }