Analysis of Zookeeper Election Leader Source Code (1)

Table of contents

 1. Source code download

 2. Start the ledaer election process


1. Start or leader downtime election process

2. Client-server interaction process (NIO or Netty)

3. ZAB consistency protocol for writing data (how to ensure the order of messages)

4. Watch monitoring trigger mechanism

 1. Source code download

 https://github.com/apache/zookeeper.git

 Cut from the code, cut from the zkServer.sh file 

 

 Specify the program parameters and start the main function of QuorumPeerMain.java

 

 The second way to start

ZooKeeperMain, add the following startup parameters

 

Build a zookeeper cluster, and when it is opened normally, it will prompt each one which is the leader and which one is the flower

 

 2. Start the ledaer election process

1. The first machine votes for itself and sends it to other machines, and other machines will also vote for itself and send it to other machines to see which one has the highest zxid, but the zxid of the first round of voting is all 0, so continue to compare myids Which one is bigger, the one with myid=2 voted in the first round, the winner enters the next round of election

2. Both myid=1 and myid=2 vote for the machine with myid 2, and determine (2,0) as the winner

 

 Source code entry

  Parse the configuration file to the memory, clean up the snapshot file task, and the core startup process runFromConfig method.

By default, NIO is used to load the configuration, and it can also be started with netty as shown in the figure below

 Initialize the Qrorum parameters, internally use the node object to generate

start up

 

 

 Load netty parameters

 

Start jetty by default, port 8080, used by operation and maintenance

 

 Start leader election

 Default server status: looking

 

 

 See the final election algorithm

 Skip by default, see the election algorithm of 3

 

Start an election port for listening to vote information 

 

 

Two threads sending out ballots

 

 

 The above are some methods of thread monitoring, super.start is the real election method.

 Core election logic:

        1. Start status: Looking

 

Core election strategy: fastLeaderElection 

 

 Update the ballot message and send the message. When debugging, the sendqueue.offer method may be lost. We need to know that there is a poll method above to get the message in the queue. 

 

 

 

 manager.connectAll: establish a socket connection

 

 

 

Guess you like

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