Raft of distributed consistency protocol (1)


  RaftThe core problem that the algorithm solves is how to keep the consistency of cluster state in the distributed environment. In short, it is a group of services, given a group of operations, and finally get a consistent result.
  RaftAlgorithm through electionLeader, copied by leadersLog (log)reachFollowerThe whole algorithm is also divided into two parts: how to elect leaders and how to execute the log instructions safely.

Leader election

  RaftIn the algorithm, a node can only be at any timeLeaderCandidateFollowerAt the same time, it emphasizes the strong leadership to simplify the whole process, so its log data flow can only be copied from the leader to the follower, and the followers can’t transfer logs.

Generally, there is only one leader in the system and all other nodes are followers. Followers are passive. They will not send any requests, but simply respond to requests from leaders or candidates

Picture description
When the     node is started, it is in the follower state. If it does not receive the heartbeat from the leader for a period of time, it will change from the follower to the candidate and initiate the election. If it receives the majority vote containing itself, it will change to the leader. If it finds that other nodes are newer than itself, it will switch to the follower state. To make sure that other nodes are newer than themselves,RaftIt has been introduced again.Term of officeThe concept.

Starting from   algorithm, the term of office is 0, when a node is electedleaderThe term of office is1, after the new leader is elected, the term of office is the previous term plus1。 When the node changes from follower to candidate, the term number should also be increased1

Tenure serves as a logical clock

Election process

When a follower node does not receive a leader heartbeat packet for a long time, it is speculated that the leader node may hang up and initiate a new leader node election.

  • Add your currentTerm of office, transition state toCandidate
  • Vote for themselves, and send requests to other nodes to vote for themselves;
  • Wait for other nodes to reply. In the process of waiting, the following situations may occur

    1. Win the election and become a leader;
    2. Be informed that other nodes are elected leaders and return to follower status by themselves;
    3. If not enough votes are received within the timeout period, the whole election process will be repeated;

The following GIF shows this process; (Tucao doesn’t support git, blog connects http://qyuan.top/2019/07/14/r…

Log replication

He was electedLeaderThe system can provide external services. The client sends the request to the cluster. If the follower receives the request, it will be forwarded to the leader for unified processing. The leader will schedule these requests and inform all followers in order to ensure the status of all nodes is consistent.
  RaftIt is based on replication state machine, and its core idea isSame initial state + same input = same final state, leaders package client requests one by onelog entryTake theselog entriesSend to all follower nodes, and then apply in the same orderlog entryThe status must be consistent.

Replication process
  • Leaders receive requests packaged tolog entry
  • Leaders send in parallellog entryTo all nodes of the cluster;
  • Leaders get it most followers get itlog entryReply;
  • Leader applicationlog entryThe command in it goes to its own state machine, that is, to execute the command;
  • Leaders respond to followers and let them do the samelog entryCommand to achieve the state machine consistent with itself;

Every onelog entryIn, in addition to the commands that need to be executed, there is the tenure number of the leader node, which is used to handle exceptions. At the same time, we can see that when the log is copied to most nodes, a successful message can be returned to the client. Once the result is returned, it must be ensured that the system will not send a rollback in any exception.
  RaftThrough the first phasecommitedAnd the second stageapplyTo ensure the consistency of state machine.

Of course, there are various abnormal situations. Let’s talk about the details in the next part(╹▽╹)

Welcome to pay attention to my blog (qyuan. Top), irregularly share some blockchain underlying technical articles, and make the blog layout better (▔▔▔▔ㄏ).

Recommended Today

Notes on tensorflow 2 deep learning (I) tensorflow Foundation

This series of notes records the process of learning tensorflow2, mainly based on https://github.com/dragen1860/Deep-Learning-with-TensorFlow-book Learning First of all, it needs to be clear that tensorflow is a scientific computing library for deep learning algorithm, and the internal data is stored in theTensor objectAll operations (OPS) are also based on tensor objects. data type Fundamentals in […]