Zookeeper listener principle and election mechanism

Composition of Stat structure:

	(1)czxid-Create transaction for node zxid
	Each modification ZooKeeper Status will receive a zxid Form of timestamp, that is ZooKeeper affair ID. 
	affair ID yes ZooKeeper The general order of all modifications in the. Each modification has a unique zxid,If zxid1 less than zxid2,that zxid1 stay zxid2 It happened before.
	(2)ctime - znode Number of milliseconds created(Since 1970)
	(3)mzxid - znode Last updated transaction zxid
	(4)mtime - znode Last modified milliseconds(Since 1970)
	(5)pZxid-znode Last updated child node zxid
	(6)cversion - znode Child node change number, znode Modification times of child nodes
	(7)dataversion - znode Data change number
	(8)aclVersion - znode Change number of access control list
	(9)ephemeralOwner- If it is a temporary node, this is znode Owner's session id. 0 if it is not a temporary node.
	(10)dataLength- znode Data length of
	(11)numChildren - znode Number of child nodes

Listener principle

1) First, create a main() thread
2) Create a Zookeeper client in the main thread. At this time, two threads will be created, one for network connection communication and the other for listener
3) Send the registered listening event to Zookeeper through the connect thread
4) Add the registered listening events to the list in the registered listener list of Zooleeper.
5) Zookeeper will send this message to the stener thread when it detects data or path changes
6) The process() method was called inside the listener thread

Common monitoring
1) Monitor changes in node data
get path [watch]
2) Increase or decrease of child nodes
ls path [watch]

Election mechanism

1) Half mechanism: more than half of the machines in the cluster survive and the cluster is available. All zookeepers are suitable for installing an odd number of servers
2) Zookeeper does not specify Master and Slave in the configuration file. However, when zookeeper works, one node is Leader and the other is Follower. The Leader is temporarily selected through the internal election mechanism.
3) Take a simple example to illustrate the whole election process
Suppose there is a Zookeeper cluster composed of five servers. Their IDs range from 1 to 5. At the same time, they are all newly started, that is, there is no historical data. In terms of the amount of data stored, they are the same. Let's assume these servers start up in sequence and see what happens

	(1)Server 1 starts and initiates an election. Server 1 voted for itself. At this time, server 1 has one vote, less than half (3 votes), the election cannot be completed, and the state of server 1 remains as LOOKING;
	(2)Server 2 starts and initiates another election. Servers 1 and 2 vote for themselves and exchange vote information: at this time, server 1 finds the vote of server 2 ID It is larger than (server 1) you are currently voting and recommending. Change the vote to recommendation server 2. At this time, there are 0 votes for server 1 and 2 votes for server 2. Without more than half of the results, the election cannot be completed, and the status of server 1 and 2 remains unchanged LOOKING
	(3)Server 3 starts and initiates an election. Both servers 1 and 2 change to server 3. The voting results: 0 votes for server 1, 0 votes for server 2 and 3 votes for server 3. At this time, server 3 has more than half of the votes, and server 3 is elected Leader. Server 1, 2 change status to FOLLOWING,Server 3 changes status to LEADING;
	(4)Server 4 starts and initiates an election. At this time, servers 1, 2 and 3 are no longer LOOKING Status, and the ballot information will not be changed. Result of exchange of ballot information: server 3 has 3 votes and server 4 has 1 vote. At this time, server 4 obeys the majority, changes the vote information to server 3, and changes the status to FOLLOWING;
	(5)Server 5 starts up and becomes a younger brother like 4.

Tags: Zookeeper

Posted by Eclectic on Sun, 22 May 2022 07:15:09 +0300