Zookeeper (three) internal principle

Zookeeper (three) internal principle

Election mechanism

  • Half mechanism: More than half of the machines in the cluster survive and the cluster is available. So Zookeeper is suitable for installing an odd number of servers
  • Although Master and Slave are not specified in the configuration file. However, when Zookeeper works, one node is Leader (leader), and the other is Follower (worker). Leader is temporarily generated through the internal election mechanism

Insert picture description here

  1. Server1 voted first, and voted for itself . It has 1 vote. No more than half of it . It is impossible to become the leader at all. The number of votes was voted for Server2 whose id is greater than itself.
  2. Server2 also voted for itself, plus the number of votes given by Server1, the total number of votes is 2 votes, no more than half, and can not become the leader, also learn Server1, follow the boat, give all of their votes to id than itself Big Server3
  3. Server3 got two votes from Server1 and Server2, plus one vote for itself. 3 votes more than half, successfully became the leader
  4. Both Server4 and Server5 vote for themselves, but cannot change the number of votes for Server3, so they have to let their fate admit that Server3 is the leader

It can be understood that the election starts from the first, until the number of votes is more than half as the leader (leader)

Node type

  • Persistent (persistent)

    • After the persistent directory node (persistent) client disconnects from zookeeper, the node still exists
    • The persistent_sequential directory node (persistent_sequential) still exists after the client is disconnected from zookeeper. The sequence identifier is set when the znode is created. A value will be appended to the znode name. The sequence number is a monotonically increasing counter, which is determined by the parent node Maintenance, for example: Znode001, Znode002...
  • Ephemeral

    • After the ephemeral client and server are disconnected, the created node is automatically deleted
    • The ephemeral_sequential directory node (ephemeral_sequential) is deleted after the client is disconnected from zookeeper. The sequence identifier is set when the znode is created. A value is appended to the znode name. The sequence number is a monotonically increasing counter, maintained by the parent node , For example: Znode001, Znode002...

Note: The serial number is equivalent to i++, which is similar to self-growth in the database

Listener principle

Insert picture description here

  1. Two threads are created when the Zookeeper client is created in the main method, one is responsible for network connection communication, and the other is responsible for monitoring

  2. Monitoring events will be sent to zookeeper via network communication

  3. After zookeeper obtains the registered monitoring event, it immediately adds the monitoring event to the monitoring list

  4. Zookeeper monitors data changes or path changes, and sends this message to the listening thread

    • Common monitoring
      1. Monitor changes in node data: get path [watch]
      2. Monitor the changes of child nodes: ls path [watch]
  5. The listening thread will call the process method internally (we need to implement the process method content)

Write data flow

Insert picture description here

  1. Client wants to write data to ZooKeeper Server1, it must send a write request first
  2. If Server1 is not the leader, then Server1 will forward the received request to the leader.
  3. This leader will broadcast the write request to each server, and each server will notify the leader when the write is successful.
  4. When the leader receives more than half of the server data and writes successfully, then the data is written successfully.
  5. Subsequently, Leader will tell Server1 that the data was successfully written.
  6. Server1 will inform the Client that the data is written successfully, and the whole process ends

Guess you like

Origin blog.csdn.net/weixin_49741990/article/details/112502682