// Dispatch handles all incoming rpc requests for this object. func (this *Election) Dispatch(header *msgpb.Header, data []byte) error { request := header.GetRequest() objectID := request.GetObjectId() // Dispatch classic paxos rpcs to the paxos instances. if objectID != this.uid { if this.IsPaxosUID(objectID) { return this.DispatchPaxos(header, data) } this.Errorf("rpc request %s doesn't belong to this instance", header) return errs.ErrInvalid } message := thispb.ElectionMessage{} if err := proto.Unmarshal(data, &message); err != nil { this.Errorf("could not parse incoming message %s", header) return err } switch { case message.ElectRequest != nil: return this.ElectRPC(header, message.GetElectRequest()) case message.StatusRequest != nil: return this.StatusRPC(header, message.GetStatusRequest()) default: this.Errorf("unknown/invalid rpc reqest %s", header) return errs.ErrInvalid } }
// StatusRPC implements the Election.Status rpc. func (this *Election) StatusRPC(reqHeader *msgpb.Header, request *thispb.StatusRequest) error { if !this.InCommittee() { this.Errorf("this election object is not a committee member") return errs.ErrNotExist } lock := this.ctlr.ReadLock("election") round := this.CurrentRound() winner := this.currentWinner lock.Unlock() // Committee members always have up to date election information. response := thispb.StatusResponse{} response.ElectionRound = proto.Int64(round) response.ElectionWinner = proto.String(winner) message := thispb.ElectionMessage{} message.StatusResponse = &response clientID := reqHeader.GetMessengerId() if err := msg.SendResponseProto(this.msn, reqHeader, &message); err != nil { this.Errorf("could not send status response to %s: %v", clientID, err) return err } return nil }
// ElectRPC implements the Election.Elect rpc. func (this *Election) ElectRPC(reqHeader *msgpb.Header, request *thispb.ElectRequest) (status error) { if !this.InCommittee() { return errs.ErrInvalid } round := request.GetElectionRound() current := this.CurrentRound() if round != current+1 { this.Errorf("could not begin election round %d because current round "+ "is only at %d", round, current) return errs.ErrStale } uid := this.PaxosUIDFromElectionRound(round) paxos, errGet := this.GetPaxosInstance(uid) if errGet != nil { this.Errorf("could not find paxos instance for uid %s: %v", uid, errGet) return errGet } this.Infof("starting new election for round %d using paxos instance %s", round, uid) clientID := reqHeader.GetMessengerId() proposedValue := []byte(clientID) chosenValue, errPropose := paxos.Propose(proposedValue, msg.RequestTimeout(reqHeader)) if errPropose != nil { this.Errorf("could not propose %s as the leader for election round %d: %v", proposedValue, round, errPropose) return errPropose } winner := string(chosenValue) // Send the election result. response := thispb.ElectResponse{} response.ElectionRound = proto.Int64(round) response.ElectionWinner = proto.String(winner) message := thispb.ElectionMessage{} message.ElectResponse = &response if err := msg.SendResponseProto(this.msn, reqHeader, &message); err != nil { this.Errorf("could not send election response to %s: %v", clientID, err) return err } return nil }
// ElectLeader performs a leader election for the next round. func (this *Election) ElectLeader(timeout time.Duration) ( string, int64, error) { lock := this.ctlr.ReadLockAll() target := this.msn.UID() if !this.InCommittee() { target = this.committee[rand.Intn(len(this.committee))] } lock.Unlock() request := thispb.ElectRequest{} request.ElectionRound = proto.Int64(this.CurrentRound() + 1) message := thispb.ElectionMessage{} message.ElectRequest = &request reqHeader := this.msn.NewRequest(this.namespace, this.uid, "Election.Elect", timeout) defer this.msn.CloseMessage(reqHeader) if err := msg.SendProto(this.msn, target, reqHeader, &message); err != nil { this.Errorf("could not send elect request to %s: %v", target, err) return "", -1, err } message.Reset() _, errRecv := msg.ReceiveProto(this.msn, reqHeader, &message) if errRecv != nil { this.Errorf("could not receive elect response from %s: %v", target, errRecv) return "", -1, errRecv } if message.ElectResponse == nil { this.Errorf("elect response from %s is empty", target) return "", -1, errs.ErrCorrupt } response := message.GetElectResponse() change := thispb.ElectionChange{} change.ElectionRound = response.ElectionRound change.ElectionWinner = response.ElectionWinner if err := this.UpdateElection(&change); err != nil { this.Errorf("could not update to new election round: %v", err) return "", -1, err } newRound := response.GetElectionRound() newLeader := response.GetElectionWinner() return newLeader, newRound, nil }
// RefreshLeader queries committee members for the latest election information. func (this *Election) RefreshLeader(timeout time.Duration) ( string, int64, error) { lock := this.ctlr.ReadLock("config") committee := append([]string{}, this.committee...) lock.Unlock() request := thispb.StatusRequest{} message := thispb.ElectionMessage{} message.StatusRequest = &request reqHeader := this.msn.NewRequest(this.namespace, this.uid, "Election.Status", timeout) defer this.msn.CloseMessage(reqHeader) count, errSend := msg.SendAllProto(this.msn, committee, reqHeader, &message) if errSend != nil && count < this.MajoritySize() { this.Errorf("could not send status request to committee: %v", errSend) return "", -1, errSend } // Wait for majority number of status responses and pick the winner for the // largest election round. It may also happen that no election round exists // because this instance is just created. maxElectionRound := int64(-1) maxElectionWinner := "" remoteSet := make(map[string]struct{}) for ii := 0; ii < count && len(remoteSet) < this.MajoritySize(); ii++ { message := thispb.ElectionMessage{} resHeader, errRecv := msg.ReceiveProto(this.msn, reqHeader, &message) if errRecv != nil { this.Warningf("could not receive any more status responses for %s: %v", reqHeader, errRecv) break } memberID := resHeader.GetMessengerId() if _, ok := remoteSet[memberID]; ok { this.Warningf("duplicate status response from %s (ignored)", memberID) continue } // TODO Check the response header for errors. if message.StatusResponse == nil { this.Warningf("status response data is empty from %s (ignored)", memberID) continue } response := message.GetStatusResponse() if response.ElectionRound != nil { round := response.GetElectionRound() if round > maxElectionRound { maxElectionRound = round maxElectionWinner = response.GetElectionWinner() } } remoteSet[memberID] = struct{}{} } if len(remoteSet) < this.MajoritySize() { this.Errorf("could not get majority responses %v in finding leader", remoteSet) return "", -1, errs.ErrRetry } if maxElectionRound > this.CurrentRound() { change := thispb.ElectionChange{} change.ElectionRound = proto.Int64(maxElectionRound) change.ElectionWinner = proto.String(maxElectionWinner) if err := this.UpdateElection(&change); err != nil { this.Errorf("could not update election status: %v", err) return maxElectionWinner, maxElectionRound, err } } this.Infof("found %s as the last known leader in round %d", maxElectionWinner, maxElectionRound) return maxElectionWinner, maxElectionRound, nil }