【20140422】paxos的一次总结
2014-04-22 23:13
295 查看
今天零压力爬南山,得一棒槌公仔,算是没有工作,晚上吃了喜欢的兰州料理,总之还不错,现在有些困,而及时总结的督促挥之不去,还是写一些。
最近两个月,一直在思考一致性的问题,看了paxos算法和zookeeper,有了一些感悟。
自认为理解paxos的关键在于:它提供了一致的过程来保证一致性。
先假设存在不会出错的环境,即使不用paxos算法,一致性也是可以保证的,比如就是固定发一台机器然后再由这台机器分发给其他机器。在现实中,这个假设是不成立的,出错的情况可分为以下几种:
1 当机器故障的时候
2 当故障机器恢复的时候
3 当消息不可达的时候
4 当消息重复达到的时候
5 其他任何出错
paxos的神妙之处就在于:无论什么出错情况,都可以使用相同的算法执行过程来保持一致性,也就是说,只要按规定好的过程的执行就好,其他不用考虑,也体现了简单性和可用性。按照这个思路想,我感觉可以领悟一些算法的内涵。
下面描述一下paxos算法这个神妙的过程:引用自http://en.wikipedia.org/wiki/Paxos_algorithm
A 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.
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.
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.
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
4000
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.
最近两个月,一直在思考一致性的问题,看了paxos算法和zookeeper,有了一些感悟。
自认为理解paxos的关键在于:它提供了一致的过程来保证一致性。
先假设存在不会出错的环境,即使不用paxos算法,一致性也是可以保证的,比如就是固定发一台机器然后再由这台机器分发给其他机器。在现实中,这个假设是不成立的,出错的情况可分为以下几种:
1 当机器故障的时候
2 当故障机器恢复的时候
3 当消息不可达的时候
4 当消息重复达到的时候
5 其他任何出错
paxos的神妙之处就在于:无论什么出错情况,都可以使用相同的算法执行过程来保持一致性,也就是说,只要按规定好的过程的执行就好,其他不用考虑,也体现了简单性和可用性。按照这个思路想,我感觉可以领悟一些算法的内涵。
下面描述一下paxos算法这个神妙的过程:引用自http://en.wikipedia.org/wiki/Paxos_algorithm
Phase 1a: Prepare
A 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 returna 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 acceptedany 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 acceptit 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
4000
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.
相关文章推荐
- C# 实现程序只启动一次(总结)
- 一次失败的面试——学会总结
- 在团800运维工作总结之记一次lvs坑
- 项目总结报告之假如历史重新再来一次
- 百度UEditor上传图片-再总结一次
- 一次android编程的经验总结
- 【面试总结】--记录一次面试经历
- 一次电话Java面试的问题总结(JDK8新特性、哈希冲突、HashMap原理、线程安全、Linux查询命令、Hadoop节点)
- 一次小比赛的总结
- 数据结构面试题总结5——数组:找出数组中唯一一个出现一次的元素
- 记一次CSR上线及总结
- 百度UEditor上传图片-再总结一次
- 一次组队赛总结
- 再做一次总结:中小企业为什么留不住人才
- DG 一次大意操作总结:
- 经历一次人际关系危机---总结,改进
- 编程生涯的一次思想总结
- 程序员面试金典——解题总结: 9.18高难度题 18.10给定两个字典里的单词,长度相等。编写一个方法,将一个单词变换成另一个单词,一次只改动一个字母。
- 一次大面积线上cpu使用率过载报警总结
- Java开发规范总结(两周至少看一次)