Example #1
0
//
// run election
//
func (s *Coordinator) runElection() (leader string, err error) {

	host := s.getHostUDPAddr()
	peers := s.getPeerUDPAddr()

	// Create an election site to start leader election.
	logging.Debugf("Coordinator.runElection(): Local Coordinator %s start election", host)
	logging.Debugf("Coordinator.runElection(): Peer in election")
	for _, peer := range peers {
		logging.Debugf("	peer : %s", peer)
	}

	s.site, err = protocol.CreateElectionSite(host, peers, s.factory, s, false)
	if err != nil {
		return "", err
	}

	// blocked until leader is elected. coordinator.Terminate() will unblock this.
	resultCh := s.site.StartElection()
	leader, ok := <-resultCh
	if !ok {
		return "", NewError(ERROR_COOR_ELECTION_FAIL, NORMAL, COORDINATOR, nil,
			fmt.Sprintf("Index Coordinator Election Fails"))
	}

	return leader, nil
}
Example #2
0
//
// run election
//
func (s *Server) runElection() (leader string, err error) {

	host := GetHostUDPAddr()
	peers := GetPeerUDPAddr()

	// Create an election site to start leader election.
	log.Current.Debugf("Server.runElection(): Local Server %s start election", host)
	log.Current.Debugf("Server.runElection(): Peer in election")
	for _, peer := range peers {
		log.Current.Debugf("	peer : %s", peer)
	}

	s.site, err = protocol.CreateElectionSite(host, peers, s.factory, s.handler, false)
	if err != nil {
		return "", err
	}

	resultCh := s.site.StartElection()
	if resultCh == nil {
		return "", common.NewError(common.SERVER_ERROR, "Election Site is in progress or is closed.")
	}

	leader, ok := <-resultCh // blocked until leader is elected
	if !ok {
		return "", common.NewError(common.SERVER_ERROR, "Election Fails")
	}

	return leader, nil
}