Zookeeper Election Leader Source Code Analysis (2)

Maven version should be above version 3.5, jdk1.8

 1. Election process

If the sid of other servers is smaller than your own sid, close the socket connection directly. The reason for this is to reduce the connection

 If they are the same, print a warning message. If the remote sid is greater than your own sid, create a sending thread and a receiving thread

 

 sendWorker.start, directly look at the logic of the run method:
get the message from the blocking queue

1.2 recver. run method

Read the remote server socker message

 

The overall vote logic, the business layer throws the sendqueue-workerSender-queueSendMap to send the queue vote message, and then sends the vote information to the remote server and its own recvWorker receiving->recvQueue queue to generate the vote. 

 

wokersender builds ballots

 

 mysid is equal to sid and added to the transport layer queue, if it is not equal, it is thrown into queuesendMap

 

 fastleaderElection election algorithm: initialization state looking

 Real ballot logic: newEpoch is equal to curEpoch, first compare zxid and compare newID, curId,

updateProposal: update the ballot, and vote for the ballot that is competitive next time

sendNotifications: Send selected ballots to other machines

 

 

 termPredicadate: Put the selected votes into voteSet and compare half of them

 

 

 Half comparison logic: compare whether the number of voting machines is greater than half

half: number of votes/2

 

 

 

  After returning true, if you win the PK, set yourself as leading, otherwise, set the current state as following

 

 When the state changes after an election, look at leading and following. When the third machine receives the ballot, it will compare whether the current state is looking.

 

 When the third machine comes in, it will follow follwoing and leading, first compare the current cycle, and perform the above vote comparison logic. Finally set the current election state

 

 

Guess you like

Origin blog.csdn.net/qq_21575929/article/details/126217768