Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 3个PD的集群坏掉一个PD后,剩下两个PD还支持读写吧,它们是怎么选leader的?
Why is there no split-brain issue?
Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 3个PD的集群坏掉一个PD后,剩下两个PD还支持读写吧,它们是怎么选leader的?
Why is there no split-brain issue?
There shouldn’t be any split-brain issue probably because TiDB is CP. I don’t understand the process of selecting a Leader.
You can look up the Raft leader election process.
The remaining two PDs can support read and write operations, but to be on the safe side, it’s best to have three nodes.
Having an odd number of nodes is a basic requirement… So there won’t be a split-brain scenario…
When to Initiate an Election
At the start of the cluster, all servers are followers. When a server does not receive a valid message from the leader or candidate within a specified time, it initiates an election. This specified time is called the election timeout, which is a random value (e.g., 200ms-500ms). What constitutes a valid message? A valid message from the leader is a heartbeat message, and from a candidate, it is a vote request message. Two state variables are introduced here: election timeout and heartbeat time interval (the interval at which the leader sends heartbeat messages). It is required that the heartbeat time interval << min(election timeout) to avoid followers from initiating unnecessary votes.
Voting Process
The follower increments its term.
The follower changes its state to candidate.
The candidate votes for itself.
The candidate sends a vote request (RequestVote) to other machines in the cluster.
The candidate ends its state under the following conditions:
When Followers Agree
If the vote request contains a term greater than or equal to the current term and the log information is not older than the candidate’s log information, the follower will agree. Log-related information will be discussed in log replication.
How Terms are Updated
All recipients of requests and responses must update their term upon receiving a larger term. This ensures that a leader can eventually be elected.
Why is there no split-brain issue?
Because with 3 nodes, 2 remaining nodes can only elect a leader with a majority of 2 votes, which is defined by the protocol to ensure that a split-brain scenario is impossible.
How do they elect a leader?
You can refer to previous articles for the general logic:
When there are two PDs left, it can still read and write normally. However, if one more fails, leaving only one, it can no longer read or write, and the entire cluster will go down.
Here’s an animation to see how Raft elects a leader:
https://raft.github.io/raftscope/index.html
It can simulate node failures.
The Raft protocol is also used for elections in Redis Cluster.
Only when the two can no longer communicate should they split.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.