Recently, distributed system learned the Paxos algorithm, saying that it did not understand ... I have to make up my own evil. Here is Paxos made Simple notes. In this article, Lamport describes in detail why the Paxos algorithm is this logic. Lamport is still very humorous, the first version of Paxos inside the humor can not be accepted, so Paxos made simple inside the first sentence is: The Paxos algorithm, when presentd in plain 中文版, is very simple. I just want to say: "Dry, I didn't feel." Must be my English is not good, read do not understand.
Assume
The operation of any system or algorithm needs to meet some conditions, here Paxos algorithm satisfies the following several conditions:
- If there is no Byzantine general problem, there will be no traitor. This hypothesis can be assumed to be reasonable, after all, now a variety of calibration algorithms, error, and then send again
- All the machines can hang up "Cool" at any time.
- All messages can be transmitted repeatedly, and the arrival time is uncertain "Also Cool"
Role
Each machine can play three characters: Proposer,acceptor and learner. This is a democratic society. Proposer is a variety of motions, for example, I think machine x cow break, should be when leader this. Acceptor to decide whether to accept the proposal. Learner is when a proposal is passed, it is necessary to learn that this is the God horse proposal. Probably on this division of labor.
Ideas
So first proposer will make a proposal, and then when most acceptor accept it, the proposal is passed. We are all strict people, so that is blurred: God horse is called the majority? The definition given here is that if each acceptor at the same time can accept at most one proposal, then for any two "majority" set, at least one of the Accetpor must intersect. For example, if it is 5 acceptor, we can define 3 for the majority (4 is also), because any set of three out of the collection must have the intersection. Ok so, you can not understand the blame your own Chinese is not good.
Then we assume thatP1: Every acceptor will accept the first proposal received . It's better than nothing to have a proposal on hand. Then, the problem comes, there are a lot of proposer, many acceptor, so it's probably not going to come up with a majority of accepted proposals. This would require acceptor to be able to accept multiple proposals. This is also reasonable, the previous proposal pits, can not force people always in the pit, just like people do not hang in a tree dead. To track the proposal, we add a number (ID) to the proposal, and each proposal has a message who is leader (v). The different proposal numbers must not be the same, but the leader can be the same (proposer heart). If one (Id_k,v_k) is accepted by most acceptor, then we say that the proposal passes.
Okay, here's the point,P2: We can get multiple proposals to pass, but we have to make sure that the proposal passed is the same as the information about who is leader . For example (Id_1,v_1) and (id_2,v_2) have been passed, then we must guarantee: V_1 = v_2. This is also reasonable, if not the same, and two of the proposals are passed, you let learner learn a fluffy.
Now we consider another situation,P2A: If a proposal of one (N,V) is passed, then any offer that is arbitrarily numbered >n and is accepted by any acceptor must have the same v. If P2A is satisfied, then we can find P2 is satisfied, namely P2A=>P2. Why is it? First, if the proposal is to be passed, it must be accepted by most acceptor. Then if the proposal accepted by any acceptor is satisfied with the same V, then it satisfies the P2 condition. It is also necessary to use the mathematical induction method to prove any number >n this point. Just fill your brain.
At first glance there is no problem, but in fact the pit is often there, whether you find it or not. First of all, according to the hypothesis, we can have the following situation: some proposer hang up (we assume that P), and then full of blood full state resurrection, such as he believed in Brother Chun, and then some acceptor very lonely (we assume that a), no proposer play with him, for example, he lived in a very far Pittsburg , and the proposer are in two times, the basic contact is not. Then because P Xin Chun brother, so p can contact a. According to P1, We know that a will definitely accept P's proposal (perhaps a is more lonely), even if the proposal only out of leader and others, this situation is not satisfied with the P2A. In other words, because of the existence of P1 will lead to P2A, resulting in P2 not set up. So here we need to take P1 into consideration, P2B: If a proposal (N,V) is passed, the proposed number >n by any proposer must have the same v. All right, here we are directly forcing the proposal made by P above to agree with the proposal that has been accepted, thus solving the problem.
How to meet P2B?
The idea is that if we have a mechanism that can satisfy the p2b in the hypothetical case, then we can meet the P2. Lamport's idea is that if we want to satisfy a certain condition, we can see how this condition can be satisfied.
Assuming (M,V) has been passed, note that the majority has accepted the offer. Then there is a new offer (N,v_n), here n>m, then we need to prove v_n = v. If this is to be proved, what conditions do we need? If we use mathematical induction, then we can assume the offer of M. (n-1) The proposed leader are all V, then the idea sub-proof v_n = v.
if (m,v) is passed, then there must be a set C that can be called the majority of Acceptor accepted the proposal, and then according to the hypothetical conditions, we have: for any of the acceptor in C, they must have accepted a proposal (I,V), here I again M and N-1, And the value is v. At the same time we find that for an arbitrary majority of the set S (referring to most of the above explanations), at least one acceptor must be in set C .
Now proposed P2C,p2c: for arbitrary N and V, if there is a proposal (N,V) Then there must be a majority of the set S, satisfies 1) the set S does not have any acceptor accept any id<n proposal, or 2) V is the largest value of all the proposals that acceptor has accepted in this s. If the p2c is satisfied then P2B can also be satisfied. Before that, we had
- Offer M.. (n-1) The proposed leader are all V
- For any of the majority of the set S, where there must be a acceptor accepted leader is V
In this way, we need to ensure that the V proposed by N is the largest v that has been accepted in S. So when a proposer needs to make a motion, he must know the largest one that has been or will be accepted in any of the most collections. It is easier to know that the largest V has been accepted, and it is difficult to predict a V that will be accepted. Rather than predicting, add some mechanism: assuming that all acceptor will not accept any id<n proposal (I am feature).
About Proposer
Finally, we have the first phase of the algorithm "It's really hard to learn English":
- Proposer selects a new proposal N, and then sends N to all acceptor, requiring acceptor to respond as follows (prepare request )
- will not accept any <n offer.
- Returns an already accepted proposal with the highest ID and returns NULL if no proposal is accepted
- If most acceptor have a response, then proposer will propose (N,V) here V is the largest of all the received proposals. If all of the responses are returned empty, then proposer can choose one (for example).
At this point, proposer finally has his own proposal, and then can send a proposal to acceptor, request its acceptance. Visible when a proposer is also not easy, who makes you a democratic society. In addition proposer can always abandon a proposal, and then start a new proposal
About acceptor
For acceptor there are two kinds of requests, 1) prepare requests, 2) accept requests. For prepare requests, acceptor can always respond. At the same time, for an accept request, he is always acceptable, if he does not promise not to accept (proposal ID). Or,P1a: A acceptor can accept a proposal numbered N, as long as he has not responded to a request for a numbered >n. visible p1a are included with P1.
Here's a little optimization, don't be nervous. Not convex optimization: Since acceptor does not accept the proposal that the number is less than it has already responded to, then the prepare request phase can ignore the request directly because it will not be accepted even if it responds.
This acceptor needs to remember the following two messages: 1) The highest proposed number that has been responded to, 2) the highest proposed number that has been accepted inside the offer information. because the P2C must ensure that the two messages are correct, the persistent memory must be saved so that it can be used even if the machine is hung and restarted.
Finally Paxos
- First Stage
- Proposer Select a number n, send prepare request to acceptor
- If Acceptor accepts a prepare request and the number n is greater than all previously-responded numbers, he will guarantee that it will not respond and accept any number <n requests, and will send proposal back with the largest number accepted. If no proposal has been accepted, the send is empty.
- Phase II
- If proposer received a response from most acceptor prepare request, he would write his own proposal: N, the maximum value of the received response. The accept request is then sent. If all the values of the response are empty (such as the initial phase), then you can choose a value (such as yourself) to send.
- If Acceptor receives an accept request, and he does not promise not to accept the request (see number), then accept it.
If you think this is over, then I can only say too young, too simple, sometimes naive .... Although there is a little bit of a sense of existence, but ... You see or not, he's there ...
Learner
For a node, he needs to know that the value was chosen as eldest brother, after all, with the wrong eldest brother is a very tragic thing. A naïve algorithm is that for each acceptor, as long as he accepts a request, he sends a call to all nodes. But it's a little bit too, isn't it? Lamport won't do that.
Another way is that all acceptor will send a request to an outstanding learner (such as IQ), and the outstanding learner will tell the result to another learner. Yes, well, it sounds like a little bit of SB ... After all, this outstanding learner may be too hard to study, then hang up, and then a variety of low IQ learner on his knees ...
Finally, we can cultivate a number of outstanding learner, and then acceptor can tell these people, who is the eldest brother ... A lot of people, hanging off one or two, in a short time letter Chun brother, saved on the line (Restart) ...
Other
Can be found that the Paxos here may be infinite loop ... There are some variant algorithms that can avoid this, but I'm not sure. In this way, the homework will be written.
Paxos Study Notes