\

1. What is ZooKeeper

Zookeeper is a distributed, source developed distributed application coordination service. It is the manager of the cluster, monitoring each node of the cluster according to the feedback submitted by the node for the next reasonable operation

\

2. What does ZooKeeper offer

File system, notification mechanism

\

3. Zookeeper file system

Zookeeper provides a multi-level node namespace (named ZNode). Different from file systems, these nodes can set associated data. Zookeeper maintains this tree structure in memory to ensure high throughput and low latency. This feature prevents ZooKeeper from storing large amounts of data, with a maximum of 1 MB per node

\

4. Znode4 types

Persistent – Persistent directory nodes that persist when the client session ends

Persistent_sequential node, and ZooKeeper automatically generates sequential numbers for you

Ephemeral is the temporary directory node. When the client terminates the session, the temporary directory node is deleted. If the session is terminated normally, the temporary directory node is deleted without delay, while if the session is forcibly closed, the temporary directory node is deleted with some delay

Ephemeral_sequential, with sequential directory nodes, zooKeeper automatically generates serial numbers (10 digits).

\

5. Zookeeper notification mechanism

The client sets a Watcher event for a ZNode. When the Znode changes, zK notifies the client of the zNode change. Note that the Watcher event is a one-time operation and does not always listen

\

6. What does ZooKeeper do

Naming services, configuration management, cluster management, distributed locks, queue management

6.1 Naming Service (File System)

Naming service refers to obtaining the resource or service address by specifying the name. Zk is used to create a global path. The path is unique and cannot have the same directory name at the same layer

6.2 Configuration Management

The program is distributed to multiple machines, and the configuration file information is put on a Znode. When the configuration file changes, ZK will notify the client that set watcher zNode, and the client will obtain the latest configuration file from the Znode for configuration

6.3 Cluster Management

Cluster new machines joining with old machines exiting, machine master electing

All machines agree to create a temporary directory node in the parent directory, listen for changes in the child nodes of the temporary node in the parent directory, and other machines will be notified when a machine joins or goes down

6.4 Distributed Lock (File system, notification mechanism)

Zk is a consistent file system, and directories are unique. Zk locks are divided into exclusive locks and sequential locks

Exclusive lock: a znode on a zk is considered a lock. Create a temporary directory with createZNode. When all clients attempt to create a znode, the temporary directory will be unlocked

Sequential locking: Under an existing zNode, all clients create a temporary sequential directory zNode. The node with the smallest sequence number gets the lock first. When it is used, the lock is released and the next sequence number gets the lock

Client set watcher on ZNode to listen for node changes

6.5 Queue Management (File System, notification mechanism)

1, synchronization queue, when all the members of the queue are complete, can start to use, otherwise wait for the members to complete, in the agreed directory to create a temporary node, monitor whether all the child nodes under the temporary node are complete

Persistent_sequential node creates a persistent_sequential node in a specific directory. Watcher monitors directory changes and the queue deletes the node with the smallest serial number for consumption. Znode is used for message storage. The data stored in a ZNode is the message content in a queue, and sequential is the consumption number of the queue. Because a persistent_sequential node is created, it is not afraid of data loss

\

7. Replicate ZooKeeper data

Zk as a cluster to provide external consistency services, so the data synchronization between the cluster machines, data replication benefits are as follows: \

1. Fault tolerance: When a node goes down, the entire cluster cannot provide services

2. Improve the expansion capacity of the system: add nodes to improve the load capacity

3, improve performance: let the client locally access the nearest node, improve the access speed

Data cluster replication is classified into the following two types:

1. Write master: Data modification must be submitted to the specified server. There is no limit to read data, and any node can read data

2, write arbitrary: data modification can be submitted to any server

Zk uses write arbitrary, and by adding machines, read and respond ability will improve, but write ability will decrease (which is why Obeserver was built)

\

8. Working principle of ZooKeeper

Zk is the core of the atom broadcasting, this mechanism is the key to keep the data synchronization between each server, realize the mechanism of the protocol is zab, zab protocol has two modes, respectively is to restore mode (optional) and radio (synchronous data state), when the service starts or master after the collapse, zab into recovery mode to choose the main, The master node is elected and the recovery mode ends after most flows and masters synchronize data state. State synchronization ensures that master and follow have the same system state

\

9. Zookeeper keeps things in order

Zk is marked by increasing thing ids. All proposals are presented with zxID, a 64-bit number with the epoch 32 bits higher; Era; The world; If a new master is elected, the epoch is incremented, and the lower 32 bits are used to increment the count. When a new proposal is created, it sends a transaction execution request to other servers. If more than half of the servers can execute it successfully, the execution begins

\

10. Working status of ZooKeeper

1, Looking: do not know who the master is, looking for the master

2, leading: The current server is the elected master

3, Following: The master has been elected, and the current server is synchronized with it

 

\

11. Zookeeper synchronization

When zab completes recovery mode, it enters broadcast mode, starting synchronization between master and follow

1, the master waits for the follow connection

2, follow connects to the master and sends its Max zxID to the master

3. Master determines the synchronization point according to the zxID of follow

4. After the synchronization is complete, notify follow that the status has become uptodate

5. After receiving the uptodate status, follow can provide service to the client request again

\

12. Functions of the primary node in ZooKeeper

In a distributed environment, some service logic of the ZooKeeper cluster is calculated by the master, and other follows share the calculation results, greatly reducing repeated data calculation and improving performance

\

13. Zookeeper breaks down

By default, there are at least three servers in a zookeeper cluster :(2n+1). When servers are down, the active server can be selected as long as at least (2n+1) /2 servers survive to provide external services. Data is not lost because the broadcast mode ensures that data copies exist on each server

Give a [look], is the biggest support for IT elder brotherCopy the code