Super detailed explanation of Paxos protocol + simple example

Reprinted from: https://blog.csdn.net/cnh294141800/article/details/53768464
Super detailed explanation of Paxos protocol + simple example

Basic-Paxos algorithm (you can look at the actual example later and then look at the specific introduction section above)

Purpose of Paxos Algorithm

The purpose of the Paxos algorithm is to solve the problem of consistency in a distributed environment.

    Multiple nodes manipulate data concurrently, how to ensure the consistency of data in the process of reading and writing, and the solution must be able to adapt to the unreliability in a distributed environment ( how does the system achieve unity on a value )

Two components of Paxos

To propose

The proposal originator, which handles the client request, sends the client's request to the cluster in order to decide whether the value can be approved.

Acceptor

Proposal approvers, responsible for processing received proposals, whose reply is a vote. will store some state to decide whether to receive a value or not

Paxos has two principles

1) Security principle---guarantee that nothing can be done wrong

a) The voting for an instance can only have one value approved, and there cannot be a situation where an approved value is overwritten by another value; (Assuming that a value is approved by a majority of Acceptors, then this value can only be learned )

b) Each node can only learn values ​​that have been approved, and cannot learn values ​​that have not been approved.

2) Survival principle - as long as most servers survive and can communicate with each other, the following things must be done in the end:

a) will eventually approve a proposed value;

b) A value is approved and other servers will eventually learn this value.

Paxos specific flow chart

 

 

The first stage (prepare)

1). Get a proposal number, n;

2). The proposer broadcasts a prepare(n) request to all nodes;

3). Receivers (Acceptors are more fickle, if they have not finally approved a value, it will continue to agree with the proposal with the largest proposal number) Compare n and minProposal, if n>minProposal, it means that there is an updated proposal minProposal=n; If the acceptor does not accept a final value at this time, then accept the proposal and return OK. If there is already an acceptedValue at this time, it will return (acceptedProposal,acceptedValue);

4). After the proposer receives more than half of the requests, if it finds that an acceptedValue is returned, it means that there is an approved proposal, and saves the acceptedValue with the highest acceptedProposal number to the local

 

The second stage (Accept)

5) Broadcast accept(n, value) to all nodes;

6). The receiver compares n and minProposal, if n>=minProposal, then acceptedProposal=minProposal=n, acceptedValue=value, after local persistence, return;

Otherwise, reject and return minProposal

7). After the proposer receives more than half of the requests, if it finds that there is a return value > n, it means that there is an updated proposal, and jumps to 1 (re-initiating the proposal); otherwise, the value is agreed.

 

 

Paxos proposal ID generation algorithm

       Such a method is given in Google's Chubby paper: Assuming there are n proposers, each numbered ir (0<=ir<n), any value s of the proposal number should be greater than its known maximum value, and satisfy:

     s %n = ir    =>     s = m*n + ir

    The maximum known value of the proposer comes from two parts: the value obtained by the proposer after the self-increment of the number and the value obtained after receiving the rejection of the acceptor.

Example: Take 3 proposers P1, P2, P3 as an example, start m=0, and the numbers are 0, 1, and 2 respectively.

1) When P1 is submitted, it is found that P2 has been submitted, and the number of P2 is 1 > 0 of P1, so P1 recalculates the number: new P1 = 1*3+1 = 4;

2) P3 is submitted with number 2 and found to be less than 4 of P1, so P3 is renumbered: new P3 = 1*3+2 = 5.

 

Paxos principle

Any two legal sets must have a common member. This property is the basic guarantee that Paxos is effective.

 

livelock

     When a proposal submitted by a proposer is rejected, it may be because the acceptor promises to return a proposal with a larger number, so the proposer increases the number and continues to submit. If both proposers find that their number is too low and propose a proposal with a higher number, it will lead to an infinite loop, which is also called livelock.

       For example, when the proposal of proposer1 is 3, the proposal of proposer2 is 4, but the number of the acceptor commitment is 5, then proposer1 and proposer2 will increase the number to 6 and 7 respectively, and try to connect with the acceptor. If it is accepted, then Proposal 5 and Proposal 6 will be renumbered and submitted, thus continuing an infinite loop.

 

Exceptions - Persistent Storage

     In the process of algorithm execution, there will be many abnormal situations: proposer downtime, acceptor downtime after receiving proposal, proposer downtime after receiving message, acceptor downtime after accepting, learn downtime, storage failure, and so on.

     In order to ensure the correctness of the paxos algorithm, proposer, acceptor, and learn all implement persistent storage, so that the server can still correctly participate in paxos processing after recovery.

    propose stores the submitted maximum proposal number and resolution number (instance id).

    The acceptor stores the maximum number of the promise, the maximum number of the accepted (accept) and the value, and the resolution number.

    learn stores learned resolutions and numbers

Specific examples:

Hypothetical 3 Army Problem

 

1) 1 Red Army camped in the valley, and 3 Blue Army camped on the surrounding hillsides;

2) The Red Army is stronger than any one Blue Army; if one Blue Army fights alone, the Red Army wins; if two or more Blue Army attack at the same time, the Blue Army wins;

3) The three blue armies need to synchronize their attack times; but their only medium of communication is to send a signal trooper on foot into the valley where they may be captured and thus lose information; or may stay in the valley for a long time to avoid capture time;

4) Each army has 1 staff responsible for proposing the attack time; each army also has 1 general to approve the attack time proposed by the staff; obviously, the attack time proposed by 1 staff needs to be approved by at least 2 generals to make sense;

5) Question: Is there a protocol that would allow the Blues to synchronize their attack times?

 

Next, BasicPaxos is played with two hypothetical scenarios; staff and generals need to follow some basic rules

1) The staff initiates a proposal in a two-stage submission (prepare/commit) way, and a number needs to be given in the prepare stage;

2) If a conflict occurs in the prepare stage, the general will decide by the size of the number, and the staff with the larger number will win;

3) If the staff officer receives the accepted attack time returned by the general in the prepare phase, the returned attack time must be used in the commit phase;

Scenario proposed by two staff officers successively

 

1) Staff 1 initiates a proposal to send a signal soldier to send a letter to the 3 generals, the content is (number 1);

2) The 3 generals received the proposal of Staff 1. Since they have not saved any number before, they saved (number 1) to avoid forgetting; at the same time, let the signal soldier bring the letter back with the content (ok);

3) Staff 1 receives replies from at least 2 generals, and again sends a signal trooper to send letters to 3 generals, with the content (number 1, attack time 1);

4) When the 3 generals receive the time of staff 1, they save (number 1, attack time 1) to avoid forgetting; at the same time, let the signal soldier bring the letter back with the content (Accepted);

5) Staff 1 receives the (Accepted) content of at least 2 generals, confirming that the attack time has been accepted by everyone;

 

6) Staff 2 initiates a proposal to send a signal soldier to send a letter to the 3 generals, the content is (No. 2);

7) The 3 generals received the proposal of Staff 2. Since (No. 2) was larger than (No. 1), they saved (No. 2) to avoid forgetting; and because they had already accepted the proposal of Staff 1, they let the signal soldier lead Letter back, the content is (number 1, attack time 1);

8) Staff 2 received replies from at least 2 generals. Since the reply brought the content of the accepted proposal from Staff 1, Staff 2 no longer proposed a new attack time and accepted the time proposed by Staff 1;

A scene where two staff officers cross proposals

 

 

1) Staff 1 initiates a proposal to send a signal soldier to send a letter to the 3 generals, the content is (number 1);

2) The situation of the 3 generals is as follows

a) General 1 and General 2 received the proposal from Staff 1, and General 1 and General 2 recorded (number 1), if any other staff officer proposed a lower number, it would be rejected; at the same time, let the signal soldier bring back the letter, the content is (ok);

b) The signal trooper responsible for notifying General 3 was arrested, so General 3 did not receive the proposal from Staff 1;

 

3) Staff 2 also initiated a proposal at the same time to send a signal trooper to send a letter to the 3 generals with the content (No. 2);

4) The situation of the 3 generals is as follows

a) Generals 2 and 3 received the proposal from Staff 2, and Generals 2 and 3 recorded (number 2), if other staffs proposed a lower number, they would be rejected; at the same time, the signal soldiers would be sent back with the letter, which reads (ok);

b) The signal trooper responsible for notifying General 1 was arrested, so General 1 did not receive the proposal from Staff 2;

 

5) Staff 1 receives replies from at least 2 generals, and again sends a signal trooper to send a letter to the 2 generals who have responded, with the content (number 1, attack time 1);

6) The situation of 2 generals is as follows

a) General 1 received (number 1, attack time 1), which is the same as the number saved by himself, so save (number 1, attack time 1); at the same time, let the signal soldier bring the letter back, the content is (Accepted);

b) General 2 received (number 1, attack time 1), because (number 1) is smaller than the saved (number 2), so let the signal soldier bring the letter back, the content is (Rejected, number 2);

 

7) Staff 2 received replies from at least 2 generals, and sent a signal trooper to send a letter to the 2 generals who responded, with the content (number 2, attack time 2);

8) General 2 and General 3 received (number 2, attack time 2), which is the same as the number they saved, so save (number 2, attack time 2), and let the signal trooper take the letter back, the content is (Accepted) ;

9) Staff 2 receives the (Accepted) content of at least 2 generals, confirming that the attack time has been accepted by the majority;

 

10) Staff 1 only received the (Accepted) content of 1 general, and received one (Rejected, No. 2) at the same time; Staff 1 re-initiated a proposal and sent a signal soldier to send a letter to 3 generals, the content is (No. 3);

11) The situation of the 3 generals is as follows

a) General 1 received the proposal from Staff 1. Since (No. 3) was larger than the previously saved (No. 1), he saved (No. 3); since General 1 had accepted the previous proposal from Staff 1, he let the signal trooper lead Letter back, the content is (number 1, attack time 1);

b) General 2 received the proposal from Staff 1. Since (No. 3) was larger than the previously saved (No. 2), he saved (No. 3); since General 2 had accepted Staff 2's proposal, he asked the signal trooper to bring the letter back. , the content is (number 2, attack time 2);

c) The signal trooper responsible for notifying General 3 was arrested, so General 3 did not receive the proposal from Staff 1;

12) Staff 1 has received replies from at least 2 generals, compares the numbers of the two replies, and selects the attack time corresponding to the larger number as the latest proposal; Staff 1 again sends a signal trooper to send a letter to the 2 generals who have responded, and the content is: (number 3, attack time 2);

13) General 1 and General 2 received (number 3, attack time 2), which is the same as the number saved by themselves, so save (number 3, attack time 2), and at the same time let the signal soldier bring the letter back, the content is (Accepted);

14) Staff 1 received at least 2 generals' (accepted) content, confirming that the attack time has been accepted by the majority;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324961399&siteId=291194637
Recommended