Esempio n. 1
0
File: paxos.go Progetto: bvk/ascent
// Phase1RPC handles ClassicPaxos.Phase1 rpc.
func (this *Paxos) Phase1RPC(header *msgpb.Header,
	request *thispb.Phase1Request) (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.Phase1Response{}
		response.PromisedBallot = proto.Int64(this.promisedBallot)
		if this.votedBallot >= 0 {
			response.VotedBallot = proto.Int64(this.votedBallot)
			response.VotedValue = this.votedValue
		}
		message := thispb.PaxosMessage{}
		message.Phase1Response = &response
		errSend := msg.SendResponseProto(this.msn, header, &message)
		if errSend != nil {
			this.Errorf("could not send phase1 response to %s: %v", clientID,
				errSend)
			return errSend
		}
		return nil
	}

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

	if ballot == this.promisedBallot {
		this.Warningf("duplicate phase1 request from client %s with an already "+
			"promised ballot number %d", clientID, ballot)
		return respond()
	}

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

	this.Infof("this acceptor has now promised higher ballot %d from %s", ballot,
		clientID)
	return respond()
}
Esempio n. 2
0
File: paxos.go Progetto: bvk/ascent
// doPhase1 performs classic paxos phase1 steps.
func (this *Paxos) doPhase1(ctxHeader *msgpb.Header, ballot int64,
	phase1AcceptorList []string) ([]byte, []string, error) {

	phase1request := thispb.Phase1Request{}
	phase1request.BallotNumber = proto.Int64(ballot)
	message := thispb.PaxosMessage{}
	message.Phase1Request = &phase1request

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

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

	var acceptorList []string
	maxVotedBallot := int64(-1)
	var maxVotedValue []byte
	responseMap := make(map[string]*thispb.Phase1Response)

	for ii := 0; ii < count && len(responseMap) < this.MajoritySize(); ii++ {
		message := thispb.PaxosMessage{}
		resHeader, errRecv := msg.ReceiveProto(this.msn, reqHeader, &message)
		if errRecv != nil {
			this.Warningf("could not receive more phase1 responses for %s: %v",
				reqHeader, errRecv)
			break
		}

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

		if message.Phase1Response == nil {
			this.Warningf("phase1 response data is empty from %s (ignored)",
				acceptor)
			continue
		}

		response := message.GetPhase1Response()
		if response.PromisedBallot == nil {
			this.Warningf("phase1 response from %s has no promise ballot", acceptor)
			continue
		}

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

		if promisedBallot < ballot {
			this.Errorf("acceptor %s did not promise this ballot %d", acceptor,
				ballot)
			continue
		}

		// We received a promise from this acceptor.
		acceptorList = append(acceptorList, acceptor)
		responseMap[acceptor] = response

		// If there was a voted value already, we need to pick the max voted value.
		if response.VotedBallot != nil {
			votedBallot := response.GetVotedBallot()
			if votedBallot > maxVotedBallot {
				maxVotedBallot = votedBallot
				maxVotedValue = response.GetVotedValue()
			}
		}
	}

	if len(responseMap) < this.MajoritySize() {
		this.Warningf("could not get majority phase1 votes %v for ballot %d",
			responseMap, ballot)
		return nil, nil, errs.ErrRetry
	}

	if maxVotedValue == nil {
		this.Infof("no prior value was chosen as per phase1 responses %v",
			responseMap)
	} else {
		this.Infof("value [%s] could have been chosen as per phase1 responses %v",
			maxVotedValue, responseMap)
	}

	return maxVotedValue, acceptorList, nil
}