【20140422】paxos的一次总结

今天零压力爬南山,得一棒槌公仔,算是没有工作,晚上吃了喜欢的兰州料理,总之还不错,现在有些困,而及时总结的督促挥之不去,还是写一些。

最近两个月,一直在思考一致性的问题,看了paxos算法和zookeeper,有了一些感悟。

自认为理解paxos的关键在于:它提供了一致的过程来保证一致性。

先假设存在不会出错的环境,即使不用paxos算法,一致性也是可以保证的,比如就是固定发一台机器然后再由这台机器分发给其他机器。在现实中,这个假设是不成立的,出错的情况可分为以下几种:

1 当机器故障的时候

2 当故障机器恢复的时候

3 当消息不可达的时候

4 当消息重复达到的时候

5 其他任何出错

paxos的神妙之处就在于:无论什么出错情况,都可以使用相同的算法执行过程来保持一致性,也就是说,只要按规定好的过程的执行就好,其他不用考虑,也体现了简单性和可用性。按照这个思路想,我感觉可以领悟一些算法的内涵。


下面描述一下paxos算法这个神妙的过程:引用自http://en.wikipedia.org/wiki/Paxos_algorithm

Phase 1a: Prepare


Proposer (the leader) creates a proposal identified with a number N. This number must be greater than any previous proposal number used by this Proposer. Then, it sends a Prepare message containing this proposal to a Quorum of Acceptors. The Proposer decides who is in the Quorum.

Phase 1b: Promise

If the proposal's number N is higher than any previous proposal number received from any Proposer by the Acceptor, then the Acceptor must return a promise to ignore all future proposals having a number less than N. If the Acceptor accepted a proposal at some point in the past, it must include the previous proposal number and previous value in its response to the Proposer.

Otherwise, the Acceptor can ignore the received proposal. It does not have to answer in this case for Paxos to work. However, for the sake of optimization, sending a denial (Nack) response would tell the Proposer that it can stop its attempt to create consensus with proposal N.

Phase 2a: Accept Request

If a Proposer receives enough promises from a Quorum of Acceptors, it needs to set a value to its proposal. If any Acceptors had previously accepted any proposal, then they'll have sent their values to the Proposer, who now must set the value of its proposal to the value associated with the highest proposal number reported by the Acceptors. If none of the Acceptors had accepted a proposal up to this point, then the Proposer may choose any value for its proposal.

The Proposer sends an Accept Request message to a Quorum of Acceptors with the chosen value for its proposal.

Phase 2b: Accepted


If an Acceptor receives an Accept Request message for a proposal N, it must accept it if and only if it has not already promised to only consider proposals having an identifier greater than N. In this case, it should register the corresponding value v and send an Accepted message to the Proposer and every Learner. Else, it can ignore the Accept Request.

Rounds fail when multiple Proposers send conflicting Prepare messages, or when the Proposer does not receive a Quorum of responses (Promise or Accepted). In these cases, another round must be started with a higher proposal number.

Notice that when Acceptors accept a request, they also acknowledge the leadership of the Proposer. Hence, Paxos can be used to select a leader in a cluster of nodes.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章