This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

preface

Through the learning of the first two chapters, I believe that friends have a preliminary understanding of Zookeeper. Today, we will learn the core parts of Zookeeper together, such as what types of nodes we can create? What do parameters in ZNode stand for? What is the monitoring mechanism? What are the features of ZK?

This series of problems in today’s article, flower elder brother will explain one by one, have not learned the first two chapters of the friends, click on the following through train can learn oh.

Zookeeper in plain English, really easy to understand

Zookeeper Single-node deployment, mandatory commands, and actual deployment scenarios

Session

We can see that the Server can accept the connection of multiple clients, and each Client connected to the Server will generate a session, and ZK will generate a unique session ID.

It is important to note that the Leader does not allow client connections unless the leaderServes parameter is displayed set.

To keep the session between the client and the server valid, the client sends heartbeat messages at a specific time frequency. If the server does not receive heartbeat messages from the client after the session timeout period (tickTime twice by default), the server is considered as hung up.

  • Id of the session generated when the client connects

The client maintains a TCP connection through which it can send requests, receive server responses, get listening events, and so on.

Znode Node type

As we have learned in the previous article, Zookeeper is composed of one node (Znode), and Znode contains various types, as listed below.

The node type For example, describe
Persistent node create /testData 100 Once created, it is persisted
Order node ① create -s /testData/name huage ② create -s /testData/name The ZK appends 10 digits to the path, for example, name40000000007. The number starts from 1 and increases gradually. The maximum value is 2^32-1
Temporary node create -e /testData 100 After the client is disconnected, it will be deleted
Temporary sequence node create -e -s /testData/age 18 A combination of temporary nodes + sequential nodes

Each node can store a maximum of 1 MB data.

Znode data structure

Knowing that there are several node types, let’s take a look at the composition of each node.

Each Znode consists of four parts: storage data (Data), access permission (ACL), child node reference (children), and status information (stat).

The name of the describe
data Stored service data
acl Client access permission to Znode
children Child node references of the current node
stat Contains Znode status information, such as transaction ID and version number

The four more complex types are node metadata (STAT). You can run the get command to view the node information. You can see that there are many other parameters besides the value of the node.

The above parameters are metadata, and the following table is posted directly, so you must understand them all.

The name of the describe
cZxid Create the transaction ID of the node
mZxid Finally, change the transaction ID of the object
pZxid The last updated transaction ID of the znode child node
ctime Creation time of the node
mtime Time when this object was last modified
cversion Version number of the child node of the node. The initial value is -1. The value is automatically increased each time the child node is modified
dataVersion The number of times that the node is modified. The initial version is 0. The number of times that the node is modified automatically increases after each operation
aclVersion Acl version of the current node
ephemeraOwner The owner session ID of the temporary node. If it is not a temporary node, the default is 0
dataLength The data length of this object
numChildren Number of child nodes

If feel see form too disgusting……. Flower brother’s thoughtful demonstration wave.

First, create a new node and use GET to view the node’s data.

We directly open another client and modify this node. Do we find that the last modified transaction ID (mZxid), the last modified time (Mtime) and the number of modifications (dattends) have changed?

If you operate on child nodes, what will happen? A small question, please try it yourself.

Watch monitoring mechanism

In yesterday’s article, we also used examples to demonstrate the use of watch, and today we will focus on explaining it.

If the node is modified, the client on the listening side will receive the message.

You can also use ls -w to listen for new or deleted node changes.

GetData (), getChildren(), exists() can also be used in Java to listen for events.

Watch has the following two features:

  1. One-time trigger: After the trigger, the watch will be deleted. If you want to receive message notification continuously, you need to set the Watch continuously.
  2. Orderliness: The client receives a watch notification before viewing node changes.

One point needs to be pointed out: Since watch is a one-time trigger, after receiving a change notification, there will be a certain delay when the watch request is initiated again. If the node changes again at this time, the change will be lost.

The characteristics of the ZooKeeper

Finally, let’s take a look at the features of ZooKeeper

  • Sequential consistency: The Leader server will generate transaction ids according to the order of requests to ensure that client operations take effect in sequence.
  • Atomicity: all transaction requests are processed with either success or failure, with no partial results;
  • Single system image: The data is the same regardless of which server the client is connected to;
  • Reliability: data will not be lost once it is changed;
  • Real-time: Ensure that the data read by the client is up to date.

Wrote last

We’ve covered a lot of stuff today, so it’s time to show some real technology, and tomorrow we’ll take a look at Zookeeper in action to see what it does and where it’s used. If there are any mistakes in the article, I hope you can point out and learn together.