當前位置: 首頁>>代碼示例>>Golang>>正文


Golang PaxosMessage.ProposeResponse方法代碼示例

本文整理匯總了Golang中proto-ascent/paxos/classic.PaxosMessage.ProposeResponse方法的典型用法代碼示例。如果您正苦於以下問題:Golang PaxosMessage.ProposeResponse方法的具體用法?Golang PaxosMessage.ProposeResponse怎麽用?Golang PaxosMessage.ProposeResponse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在proto-ascent/paxos/classic.PaxosMessage的用法示例。


在下文中一共展示了PaxosMessage.ProposeResponse方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ProposeRPC

// ProposeRPC handles ClassicPaxos.Propose rpc.
func (this *Paxos) ProposeRPC(header *msgpb.Header,
	request *thispb.ProposeRequest) (status error) {

	if !this.IsProposer() {
		this.Errorf("this paxos instance is not a proposer; rejecting %s", header)
		return errs.ErrInvalid
	}

	// OPTIMIZATION If this object is also a learner and already knows the
	// consensus result, we don't need to perform expensive proposal.
	if this.IsLearner() {
		lock := this.ctlr.ReadLock("learner")
		defer lock.Unlock()

		if this.chosenValue != nil {
			response := thispb.ProposeResponse{}
			response.ChosenValue = this.chosenValue
			message := thispb.PaxosMessage{}
			message.ProposeResponse = &response
			errSend := msg.SendResponseProto(this.msn, header, &message)
			if errSend != nil {
				this.Errorf("could not send known chosen value as the propose "+
					"response: %v", errSend)
				return errSend
			}
			return nil
		}
		lock.Unlock()
	}

	var chosen []byte
	proposal := request.GetProposedValue()
	for ii := 0; chosen == nil && msg.RequestTimeout(header) > 0; ii++ {
		if ii > 0 {
			time.Sleep(this.opts.ProposeRetryInterval)
		}

		// Get the next proposal ballot number.
		ballot, errNext := this.GetNextProposalBallot(msg.RequestTimeout(header))
		if errNext != nil {
			this.Errorf("could not select higher ballot: %v", errNext)
			return errNext
		}
		this.Infof("using ballot number %d for the proposal", ballot)

		lock := this.ctlr.ReadLock("config")
		phase1AcceptorList := this.getPhase1AcceptorList(ballot)
		lock.Unlock()

		// Collect phase1 promises from majority number of acceptors.
		votedValue, acceptorList, errPhase1 := this.doPhase1(header, ballot,
			phase1AcceptorList)
		if errPhase1 != nil {
			this.Warningf("could not complete paxos phase1: %v", errPhase1)
			continue
		}

		// If a value was already voted, it may have been chosen, so propose it
		// instead.
		value := proposal
		if votedValue != nil {
			value = votedValue
		}

		// Collect phase2 votes from majority number of acceptors.
		errPhase2 := this.doPhase2(header, ballot, value, acceptorList)
		if errPhase2 != nil {
			this.Warningf("could not complete paxos phase2: %v", errPhase2)
			continue
		}

		// A value is chosen, break out of the loop.
		chosen = value
		break
	}

	if chosen == nil {
		this.Errorf("could not propose value %s", proposal)
		return errs.ErrRetry
	}

	// If local node is a learner, update him with the consensus result directly.
	defer func() {
		if this.IsLearner() {
			lock, errLock := this.ctlr.Lock("learner")
			if errLock != nil {
				return
			}
			defer lock.Unlock()

			change := thispb.LearnerChange{}
			change.ChosenValue = chosen
			if err := this.doUpdateLearner(&change); err != nil {
				this.Warningf("could not update local learner with the consensus "+
					"result (ignored): %v", err)
			}
		}
	}()

//.........這裏部分代碼省略.........
開發者ID:bvk,項目名稱:ascent,代碼行數:101,代碼來源:paxos.go


注:本文中的proto-ascent/paxos/classic.PaxosMessage.ProposeResponse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。