当前位置: 首页>>代码示例>>Golang>>正文


Golang PaxosMessage.LearnRequest方法代码示例

本文整理汇总了Golang中proto-ascent/paxos/classic.PaxosMessage.LearnRequest方法的典型用法代码示例。如果您正苦于以下问题:Golang PaxosMessage.LearnRequest方法的具体用法?Golang PaxosMessage.LearnRequest怎么用?Golang PaxosMessage.LearnRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在proto-ascent/paxos/classic.PaxosMessage的用法示例。


在下文中一共展示了PaxosMessage.LearnRequest方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NotifyAllLearners

// NotifyAllLearners sends the current vote to all learners.
func (this *Paxos) NotifyAllLearners() (status error) {
	if !this.IsConfigured() || !this.IsAcceptor() {
		return nil
	}

	defer func() {
		if status != nil && !errs.IsClosed(status) {
			now := time.Now()
			next := now.Add(this.opts.LearnRetryInterval)
			_ = this.alarm.ScheduleAt(this.uid, next, this.NotifyAllLearners)
		}
	}()

	rlock := this.ctlr.ReadLock("acceptor", "config")
	// Stop notifications when all learners know the consensus value.
	if len(this.doneLearnerSet) == len(this.learnerList) {
		rlock.Unlock()
		return nil
	}
	// Make a copy of what we need: learners and the vote map.
	numLearners := len(this.learnerList)
	learnerList := append([]string{}, this.learnerList...)
	votedValueMap := make(map[int64][]byte)
	for ballot, value := range this.votedValueMap {
		if ackMap := this.learnerAckMap[ballot]; len(ackMap) < numLearners {
			votedValueMap[ballot] = value
		}
	}
	rlock.Unlock()

	request := thispb.LearnRequest{}
	for ballot, value := range votedValueMap {
		request.VotedBallotList = append(request.VotedBallotList, ballot)
		request.VotedValueList = append(request.VotedValueList, value)
	}
	message := thispb.PaxosMessage{}
	message.LearnRequest = &request

	// Send notification to all learners.
	reqHeader := this.msn.NewRequest(this.namespace, this.uid,
		"ClassicPaxos.Learn", this.opts.LearnTimeout)
	defer this.msn.CloseMessage(reqHeader)

	count, errSend := msg.SendAllProto(this.msn, learnerList, reqHeader,
		&message)
	if errSend != nil {
		this.Errorf("could not send learn request to all learners: %v", errSend)
		return errSend
	}

	// Wait for responses from all learners.
	for ii := 0; ii < count; ii++ {
		message := thispb.PaxosMessage{}
		resHeader, errRecv := msg.ReceiveProto(this.msn, reqHeader, &message)
		if errRecv != nil {
			this.Warningf("could not receive learner responses: %v", errRecv)
			break
		}

		learner := resHeader.GetMessengerId()
		if message.LearnResponse == nil {
			continue
		}
		response := message.GetLearnResponse()

		// Save the learner acknowledgment to the wal.
		change := thispb.AcceptorChange{}
		change.AckedLearner = proto.String(learner)
		if response.GetKnowsChosenValue() {
			change.AckedChosenValue = proto.Bool(true)
		} else {
			for ballot := range votedValueMap {
				change.AckedBallotList = append(change.AckedBallotList, ballot)
			}
		}
		if err := this.UpdateAcceptor(&change); err != nil {
			this.Errorf("could not update acceptor state: %v", err)
			return err
		}
	}
	return nil
}
开发者ID:bvk,项目名称:ascent,代码行数:83,代码来源:paxos.go


注:本文中的proto-ascent/paxos/classic.PaxosMessage.LearnRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。