在 paxos 算法的第二阶段,如果接受者之前已经选择了一个值,那么提议者会发出一个接受请求,其中包含从接受者那里获得的数字n
和值。v
我的问题是为什么提议者这样做?因为一旦选择了一个值,它就是永久的并且不能更改,所以在这种情况下,提议者只是在学习选择的值,该值是在准备请求的响应中发送的。为什么它会要求接受已经接受的值?
1 回答
选择的值必须与最后一个领导者提出的值一致,否则可能会丢失已选择的值。一个有用的思考方式是新提议者选择与旧提议者合作。如果它不协作,那么可能会发生矛盾,我们可能会在分布式系统中获得不一致。
例子:
考虑节点 A、B 和 C 充当 multi-paxos 的所有角色。节点 A 是领导者并提出 V1。想象一下网络出现故障,只有节点 A 和 B 能够通信,并且只有最少数量的消息通过,节点 A 才知道选择了 V1。
当节点 A 收到节点 B 的消息时,它知道选择了 V1,因为它拥有多数(节点 A 和 B)。它向节点 B 和 C 发送消息,表示选择了该值,但是如本例中所述,节点 A 没有进一步的消息通过。节点 A 执行业务操作,例如从金额为 V1 的银行账户中支付资金。然后节点 A 崩溃。
节点 C 现在成为领导者,不知道正确的银行账户余额,也不知道任何关于付款建议的事实。节点 B 知道 V1 中建议了付款,但不知道是否选择了它,因为它从未从节点 A 听到结果。因此节点 B 也不知道正确的银行账户余额。
您描述的机制正是节点 C 如何与死节点 A 协作选择值 V1。如果没有进一步的消息丢失,B 和 C 都将进入一致的状态,即他们同意从银行账户中支付的金额。
显然,如果节点 C 不是通过节点 B 发现价值 V1 而是提出一些新价值,我们就会产生矛盾。银行账户将被损坏,因为付款 V1 不会反映在节点 B 和 C 上的账户余额中。
讨论
在我的博客文章中详细讨论了您所询问的机制,其中描述了领导者接管阶段。
There are some standard implementation details assumed in the events as I describe them above. For example, one might say "don't move the money from the bank account without more messages to confirm that all nodes are aware the value is chosen". Yet Paxos is proven to only need the minimum number of messages to be safe and crashes should only be rare. This means that when implementing Paxos it is usually optimal to use the minimum number of messages during normal running and to rely on the algorithm to recover a consistent state across the system during failure scenarios.
It is interesting that a value can be chosen yet no living node knows about it. In the above example Node A runs long enough to see the messages from Node B and move money between bank accounts. Yet it might have crashed before hearing from Node A. It will have accepted V1, in addition to Node B, yet no Node knows that the value has been chosen until Node C discovers V1 and also chooses it.
It is interesting that the clients of the system, those things observing the bank account, or other systems receiving a payment from the bank account, are also a part of the distributed system. Were it the case that no payments were made from bank accounts in the example then loosing V1 would not be a problem. Yet it is fairly normal that there are side effects of values being chosen. The 3rd party systems, or user web browsers that observe the system, are actually part of the distributed system.