【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.



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