Пример #1
0
// Phase2RPC handles ClassicPaxos.Phase2 rpc.
func (this *Paxos) Phase2RPC(header *msgpb.Header,
	request *thispb.Phase2Request) (status error) {

	if !this.IsAcceptor() {
		this.Errorf("this paxos instance is not an acceptor; rejecting %s", header)
		return errs.ErrInvalid
	}

	lock, errLock := this.ctlr.TimedLock(msg.RequestTimeout(header), "acceptor")
	if errLock != nil {
		return errLock
	}
	defer lock.Unlock()

	clientID := header.GetMessengerId()
	respond := func() error {
		response := thispb.Phase2Response{}
		response.PromisedBallot = proto.Int64(this.promisedBallot)
		if this.votedBallot >= 0 {
			response.VotedBallot = proto.Int64(this.votedBallot)
			response.VotedValue = this.votedValue
		}
		message := thispb.PaxosMessage{}
		message.Phase2Response = &response
		errSend := msg.SendResponseProto(this.msn, header, &message)
		if errSend != nil {
			this.Errorf("could not send phase2 response to %s: %v", clientID,
				errSend)
			return errSend
		}
		return nil
	}

	ballot := request.GetBallotNumber()
	if ballot < this.promisedBallot {
		this.Warningf("phase2 request from %s is ignored due to stale ballot %d",
			clientID, ballot)
		return respond()
	}

	if ballot > this.promisedBallot {
		this.Errorf("phase2 request from client %s without acquiring a prior "+
			"promise", clientID)
		return respond()
	}
	value := request.GetProposedValue()

	// Save the phase2 vote into the wal.
	change := thispb.AcceptorChange{}
	change.VotedBallot = proto.Int64(ballot)
	change.VotedValue = value
	if err := this.doUpdateAcceptor(&change); err != nil {
		this.Errorf("could not update acceptor state: %v", err)
		return err
	}

	this.Infof("this acceptor has voted for %d in ballot %s", ballot, value)
	if err := respond(); err != nil {
		return err
	}

	// Schedule a notification to all learners.
	_ = this.alarm.ScheduleAt(this.uid, time.Now(), this.NotifyAllLearners)
	return nil
}
Пример #2
0
// doPhase2 performs classic paxos phase2 steps.
func (this *Paxos) doPhase2(ctxHeader *msgpb.Header, ballot int64,
	value []byte, acceptorList []string) error {

	phase2request := thispb.Phase2Request{}
	phase2request.BallotNumber = proto.Int64(ballot)
	phase2request.ProposedValue = value
	message := thispb.PaxosMessage{}
	message.Phase2Request = &phase2request

	header := msg.NewNestedRequest(this.msn, ctxHeader, this.namespace, this.uid,
		"ClassicPaxos.Phase2")
	defer this.msn.CloseMessage(header)

	count, errSend := msg.SendAllProto(this.msn, acceptorList, header, &message)
	if errSend != nil && count < this.MajoritySize() {
		this.Errorf("could not send phase2 request to majority acceptors: %v",
			errSend)
		return errSend
	}
	this.Infof("send phase2 request %s to acceptors: %v", header, acceptorList)

	responseMap := make(map[string]*thispb.Phase2Response)
	for ii := 0; ii < count && len(responseMap) < this.MajoritySize(); ii++ {
		message := thispb.PaxosMessage{}
		resHeader, errRecv := msg.ReceiveProto(this.msn, header, &message)
		if errRecv != nil {
			break
		}

		acceptor := resHeader.GetMessengerId()
		if _, ok := responseMap[acceptor]; ok {
			this.Warningf("duplicate phase2 response from %s (ignored)", acceptor)
			continue
		}

		if message.Phase2Response == nil {
			this.Warningf("phase2 response data is empty from %s (ignored)",
				acceptor)
			continue
		}
		response := message.GetPhase2Response()
		promisedBallot := response.GetPromisedBallot()

		if promisedBallot < ballot {
			this.Errorf("as per phase2 response, acceptor %s seems to have rolled "+
				"back on his phase1 promise to ballot %d", acceptor, ballot)
			continue
		}

		if promisedBallot > ballot {
			this.Warningf("acceptor %s has moved on to higher ballot %d", acceptor,
				promisedBallot)
			break
		}

		responseMap[acceptor] = response
	}

	if len(responseMap) < this.MajoritySize() {
		this.Warningf("could not get majority phase2 votes %v for value [%s] "+
			"ballot %d", responseMap, value, ballot)
		return errs.ErrRetry
	}

	this.Infof("value [%s] is chosen by phase2 responses %v", value, responseMap)
	return nil
}