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 election
Leader, copied by leaders
FollowerThe whole algorithm is also divided into two parts: how to elect leaders and how to execute the log instructions safely.
RaftIn the algorithm, a node can only be at any time
FollowerAt 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
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 elected
leaderThe term of office is
1, after the new leader is elected, the term of office is the previous term plus
1。 When the node changes from follower to candidate, the term number should also be increased
Tenure serves as a logical clock
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 current
Term of office, transition state to
- 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
- Win the election and become a leader;
- Be informed that other nodes are elected leaders and return to follower status by themselves;
- 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…
He was elected
LeaderThe 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 is
Same initial state + same input = same final state, leaders package client requests one by one
log entryTake these
log entriesSend to all follower nodes, and then apply in the same order
log entryThe status must be consistent.
- Leaders receive requests packaged to
- Leaders send in parallel
log entryTo all nodes of the cluster;
- Leaders get it most followers get it
- Leader application
log 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 same
log entryCommand to achieve the state machine consistent with itself;
log 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 phase
commitedAnd the second stage
applyTo 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 (▔▔▔▔ㄏ).