preface

As a distributed coordination service for distributed applications, Zookeeper provides API calls of C and Java languages. The service middleware itself, developed in Java, has some core concepts that we need to understand to make better use of the service middleware. This time will start from the following parts:

  • The Session Session

  • Watch

  • The data model

Session

As shown in the figure, ZK is a C/S architecture, including a client and a server. The client connects to the server, which is called a session.

The process and features of establishing a session are as follows:

  1. A client connects to a session and is assigned a unique session ID by ZK

  2. The client sends a heartbeat at a specific interval to keep the session valid, which is set by the tickTime parameter

  3. If the server does not receive the client heartbeat message within the session timeout period, the server determines that the client is invalid and has failed.

  4. MinSessionTimeout Minimum timeout period for a session (default is twice tickTime)

  5. MaxSessionTimeout Maximum timeout duration of the session (default is 20 times tickTime)

  6. Requests in the session are executed in FIFO order, similar to a queue

The data model

As a distributed coordination service, ZK is also a centralized storage service. Data is stored in the content. It has the characteristics of high performance and low latency and depends on its own data model.

Hierarchical namespace
  • Similar to the Unix file system, with (/) as the root image

  • Difference: A node can contain data associated with it as well as child nodes (which are both files and folders in the view of the file system)

  • The path of a node is always represented as a canonical, absolute, slash-separated path

For each node, as shown in the figure, you can set the corresponding value

Each node is called zNode.

znode
  • Unique name, naming convention

  • Node types: Persistent, Sequential, temporary, and temporary

  • Node data composition

Znode – Naming convention

Node names can use any Unicode character except for the following restrictions

  1. The null character (\u0000) cannot be part of the pathname

  2. The following characters cannot be used because they cannot be explicitly friendly, \u0001 – \u0019 and \u007F – \u009F

  3. The following characters are not allowed: \ ud800-uf8fff,\uFFF0 – \uFFFF

  4. The “.” character can be used as part of another name, but the “. And “..” Zookeeper does not apply to nodes with absolute paths. For example, “/a/b/./c” “c/a/b/.. /” are both invalid node names

  5. Zookeeper Has a reserved node, zooKeeper.

Znode – Indicates the node type

There are four types of nodes

Persistent node

create /test1 test
Copy the code

Temporary node

create -e /test1 /abd abds
Copy the code

Order node

create -s /test1/abc 908
Copy the code

Temporary sequential node

create -e -s /test1/abd/ 90
Copy the code

Where, the sequential nodes, which are 10-bit decimal numbers, each parent node carries a counter, zK is developed by Java, the counter is a signed int (4 bytes), the number is 2147483647, after which the overflow will occur, resulting in the name “-2147483648”

As shown in the figure, nodes of the four types are created, and the decimal numbers of the sequential nodes are displayed

At the same time, the temporary node, whose validity range is within the validity period of the current session, closes the current session and restarts a client

In the figure, you can see the current sessionid 0x100002011b80003

Check the node again. You can find that temporary nodes C and F0000000005 do not exist. Temporary nodes are widely used in coordination services.

Znode – Data composition

Node data: Stored coordination data (status information, configuration, location information, etc.)

Node metadata (stat structure)

The maximum data volume is 1 MB

Zk is a distributed coordination service. It is not a storage database and is only used for distributed coordination. Therefore, the data volume is small and high performance is guaranteed.

Znode – Metadata stat structure

On the ZK client, you can run the stat command to view node metadata. Including the following data:

Stat structure field describe
cZxid Create the zxID (transaction ID) of this node
mZxid Finally change the zxID of the node
pZxid The zxID of the last updated child node of the current node
ctime Node Creation Time
mtime Last modified time of the node
dataVersion Number of times node data has been modified
cversion Number of node node changes
dataLength Node data length
numChildren Node Number of nodes
ephemeralOwner The node is temporary and the owner session ID, or 0 if it is not temporary
aclVersion Number of node ACL changes

The client provides the following commands to use the access control function

create [-s] [-e] [-c] [-t ttl] path [data] [acl]

setAcl [-s] [-v version] [-R] path acl

getAcl [-s] path

Provides setting format to (scheme:expression, perms)

Schemes including

  • World is accessible to any client.

  • Auth has authorized clients.

  • Digest Use username:base64 encoded SHA1 password digest.

  • IP IP: 127.0.0.1, read

  • x509

Perms including

  • CREATE: Child nodes can be created

  • READ: The node data and the list of child nodes can be obtained.

  • WRITE: Sets node data

  • DELETE: deletes a child node

  • ADMIN: sets the permission

Time in Zookeeper

Zk keeps track of time in a variety of ways.

  1. Each change operation in Zookeeper will correspond to a unique transaction ID, called Zxid, which is a globally ordered timestamp.

  2. Version Numbers The Version number is increased for each node change operation

  3. Ticks When the ZK cluster is used, the server uses tick to define the event time, such as status upload, session timeout, and connection timeout between peers. The ticktime is disclosed only indirectly through the minimum session timeout (twice the ticktime by default). If the session timeout set by the client is less than the minimum session timeout set by the server, the server will inform the client of the actual session timeout, that is, the configured time of the server will be used and the result will be notified to the client. If the session timeout configured on the client is greater than that configured on the server, the client setting is used.

  4. Real Time Zookeeper does not use Real time or clock time except to place timestamps in stat structures when zNode creates and modifies time

Watch Monitoring Mechanism

On the client, you can set the Watch on the ZNode to monitor the changes of the ZNode in real time

  • Data Watch listens for data changes

  • Child Watch listens for child node changes

First, add a watch listener to the node, and then change the node data. All clients will trigger the watch time and listen to the change.

addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] – default is PERSISTENT_RECURSIVE

After Zookeeper 3.6.0, the addWatch method can be used to set the watch monitoring mode to PERSISTENT_RECURSIVE, which can be triggered multiple times

Zookeeper features

  1. Sequential consistency: Ensure that client operations take effect in sequence

  2. Atomicity: Updates succeed or fail with no partial results

  3. Single system image: Clients see the same no matter which cluster server they are connected to

  4. Reliability: Data changes are not lost unless overwritten by the client

  5. When data changes occur in ZK, transaction records will be carried out first, and consistency algorithm will be processed in the cluster to finally achieve consistency and reliability

  6. Timeliness: Ensure that the data read by the client of the system is up to date