Zookeeper Basics (a)

First, what Zookeeper that?

ZooKeeper is an open source distributed coordination service, which is a cluster manager, monitors the status of each node in the cluster nodes based on feedback submitted, the next step is reasonable operation. Finally, the easy to use interface and efficient performance, function and stability of the system to the user.
Distributed applications, such as data released under Zookeeper realization / subscribe, load balancing, name services, distributed coordination / notification, cluster management, Master election, distributed locking and distributed queue functions. The main use: to solve the single point of failure.
Zookeeper distributed to ensure the consistency of the following characteristics:

  • Sequential Consistency: Update from the client will be in strict accordance with the order sent by the client process
  • Atomicity: update or success or failure, partial success, or failure of the portion of the scene does not exist
  • Single view: no matter to which the client connects to the server to see is the same view
  • Reliability: Once an update to take effect, it will remain there until the update again
  • Real-time: within a specific period of time, to change any system can be seen by the client, or are listening to

Client read request may be processed in any machine cluster. For written requests, which will be simultaneously sent to other machines and the zookeeper agreement, the request will return success. Therefore, with the increase zookeeper cluster machine, read request throughput increases but write request throughput will decrease.
Orderliness is a very important characteristic of a zookeeper, all updates are global ordered, each update has a unique time stamp, the time stamp called zxid (Zookeeper Transaction Id). But only with respect to the read request to update orderly, that is, the read request will return results with the latest zxid the zookeeper.

Second, the common API and command

1.API
Create /path data
Delete /path
Exists /path
setData /path data
getData /path
getChildren /path

java: reference Apache curator (Netflix open source), or zk comes zkClient
Curator framework for packaging the client on the basis of native zk, zk offers a variety of scenarios, such as: distributed locks, cluster leader election, shared counter, caching, distributed queues.
Curator mainly from the following aspects to reduce the complexity of zk used:
retry mechanism: all captured recoverable abnormal (later speak) configured to retry retry strategy a strategy also provides several internal standard (exponential compensation)
connection status monitoring: Curator after initialization will always be connected to zk listening, once the state changes make the appropriate treatment
zk client instance management: Curator will zk client-to-server connections cluster management and reconstruction zk when needed example, the cluster connection reliability zk.

2. command
start: ./ zkServer.sh start
to see zk operating status: ./ zkServer.sh status
client connections zk: ./zkClis.sh
LS: View
get: obtaining node data and update
cZxid: creation of nodes the above mentioned id
ctime: created nodes
mZxid: modify the node the above mentioned id
mtime: time to modify node
pZxid: id child nodes
cversion: version child nodes
dataVersion: the current version of the node data
aclVersion: version rights
ephemeralOwner: to determine whether the temporary node
dataLength: data the length
numChildren: the number of child nodes
stat: obtaining node updates
create -e: create a temporary node
create -s: create order node
delete / path: delete
stat path watch: set the watch events
get path watch: watch event set

Third, authority control Zookeeper

ACL access control:
ZK nodes There are five operating authority: CREATE, READ, WRITE, DELETE , ADMIN is, add, delete, change, search, manage permissions, the five permissions abbreviated as crwda (ie: each word of the first character abbreviation).
Note: these five permissions, delete permissions refers to delete the child node, the other four kinds of rights refers to the operating authority of its own node

Identity authentication There are four ways:

  • world: the default mode, the equivalent of the whole world can access
  • auth: user representatives have been certified by the (cli can by addauth digest user: to add authorized users in the current context pwd)
  • digest: namely Username: Password authentication in this way, which is the most commonly used business systems
  • ip: Ip address authentication using
    the command:
    getacl: get the right information to a node
    setAcl: Set permissions

Four, Znode node

  1. Persistent node
  2. Temporary node
  3. Persistent ordered
  4. Temporary ordered
    Here Insert Picture Description
    the so-called persistent node refers to the node is created, it exists until there is a delete operation to remove the active node - the node will not create client sessions fail disappear.
    The basic characteristics of such a node above node types is the same. Additional features are, in ZK, each parent node maintains a timing for his first-level child node, the order will be recorded each child node created. Based on this characteristic, when creating the child nodes, you can set this property, then the node creation process, ZK will be automatically given node name with a numeric suffix, as the new node name. This figure range is the maximum integer suffix.
    And lasting node is different, and the life cycle of a client node session temporary binding. In other words, if the client session fails, then the node will automatically be removed. Note that, here mentioned session failover, rather than disconnected. In addition, you can not create a child node in a temporary node.
    Life cycle and client sessions temporary node binding. In other words, if the client session fails, then the node will automatically be removed. Note created node will automatically add numbers.

Data stored in memory, so the high throughput, low latency, also proved zk file node should not be too large to store data.

Fifth, monitoring and notification

1, polling View
Here Insert Picture Description
2, Watcher mechanism
Here Insert Picture Description
zookeeper this mechanism, reducing the overhead of multiple network in Figure 1-1 2.
In Fig 1-2 is disposed monitor point watch process:
1: c2 monitor changes in the client after the connection is created in the listening C1 / tasks ordered incrementing a directory node Task1
2: C2 receive notification watch
3: C2 to zk Register / task again
question: in the third step, adding a new watch C2 node, if there is a new client connections after c3, to add a new task / tasks, client C2 is not to miss this change?
Answer: Before setting up a new watch to be a node in step 3 to read the next state

Note: Try to watch several changes into one event, watch a single event, does not always succeed, watch is kept in memory only, and will not persist to the hard disk, the client and server is disconnected, need to reset the watch

Using watch points to note:

  • Watches are one-time notification, registration must be repeated.
  • After CONNECTIONLOSS happen, as long as the connection (ie SESSIONEXPIRED not happen) again within the session_timeout, then the connection is still registered in watches.
  • Version node data change triggers NodeDataChanged, note that this version is specifically describes the change. There is a case, as long as the successful implementation of the setData () method, regardless of whether the content is the same before and will trigger NodeDataChanged.
  • Sign up for a node watch, but the node is removed, then register on this node watches will be removed.
  • Zk with a client on the same watch one node registered, you will only receive a notification.
  • Watcher对象只会保存在客户端,不会传递到服务端在,具体流程上,简单讲,客户端在向 ZooKeeper 服务器注册 Watcher 的同时,会将 Watcher 对象存储在客户端的 WatchManager 中。当ZooKeeper 服务器触发 Watcher 事件后,会向客户端发送通知,客户端线程从 WatchManager 的实现类中取出对应的 Watcher 对象来执行回调逻辑。

六、版本与运行模式

Here Insert Picture Description
版本控制并发
举例:Client1和Client2每次setData后,会保存服务器的版本号在客户端,下次变更会携带版本号,两个调用以版本号作为传入参数,只有传入参数和服务器版本号一致,才能变更(CAS)

Zookeeper两种运行模式:
独立模式、仲裁模式
仲裁模式下有法定人数的概念,一般最少设置为(n/2+1)后续讲解

七、会话

Session refers to the ZooKeeper server and client session. In the ZooKeeper, it refers to a client connection between a client and server TCP connection length. When the client starts, first establish a TCP connection with the server, the first connection is established from the beginning of the life cycle of the client session began. Can be transmitted through this connection, the client can maintain a valid session with the server by the heartbeat Zookeeper the request and the server accepts the response, while also Watch receives event notifications from the server through the connection. Session sessionTimeout value used to set a timeout client session. When too much pressure due to a server, a network failure or client interrupts the connection, and other causes of client connection is lost, as long as within the specified time sessionTimeout able to reconnect on any server in the cluster, you create before the session is still valid.
Before you create a session for the client, the server will first assign a sessionID for each client. Since sessionID is important to identify a Zookeeper session, many related to the session sessionID operation mechanism are based on this, so no matter which server is assigned to the client sessionID, are sure to guarantee a globally unique.

Here Insert Picture Description
Session Lifecycle:

If a client and server because the timeout, the client remains connecting until sessionTimeout. If set sessionTimeout = t, then at time t / 3, the client will reconnect, and if that fails, at the time of 2t / 3, and will look for other servers, try to connect.
Status of the new connection to the server can not be behind the original server. So to decide whether to use zxid behind.
Here Insert Picture Description

Published 55 original articles · won praise 14 · views 20000 +

Guess you like

Origin blog.csdn.net/qq422243639/article/details/98599494