Zookeeper basic features and the Linux-based ZK client command line learning

Source: Internet
Author: User
Tags zookeeper

Zookeeper common command-line operations
    1. To open the ZK client via zkcli.sh:
[[email protected] ~]# zkCli.sh[zk: localhost:2181(CONNECTED) 0]
    1. LS and LS2 command:
[zk: localhost:2181(CONNECTED) 0] ls /  # ls命令用于查看节点,类似于Linux中的查看目录[zookeeper][zk: localhost:2181(CONNECTED) 1] ls2 /  # ls2命令用于查看节点以及该节点状态的详细信息[zookeeper]cZxid = 0x0ctime = Thu Jan 01 08:00:00 CST 1970mZxid = 0x0mtime = Thu Jan 01 08:00:00 CST 1970pZxid = 0x0cversion = -1dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 0numChildren = 1[zk: localhost:2181(CONNECTED) 2]
    1. Get with stat command:
[zk: localhost:2181(CONNECTED) 2] stat /  # stat命令用于查看节点状态的详细信息cZxid = 0x0ctime = Thu Jan 01 08:00:00 CST 1970mZxid = 0x0mtime = Thu Jan 01 08:00:00 CST 1970pZxid = 0x0cversion = -1dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 0numChildren = 1[zk: localhost:2181(CONNECTED) 3] get /  

A description of the node State property information, as in the following table:

Properties Description
Czxid ZXID format timestamp corresponding to the creation time of the node
CTime Time the node was created
Mzxid ZXID format timestamp for the last modification time of the node
Mtime Last modified time of the node
Pzxid The ZXID format timestamp for the most recent creation/deletion of the node's child node (or node)
Cversion The node has child nodes that are modified by the version number, delete or add child nodes, and the version number is increased from
Dataversion Version number of the current node data, data write operation, version number incremented
Aclversion Node ACL permission version, permission write operation, version number incremented
Ephemeralowner The transaction ID of the temporary node when it was created, and a value of 0 if the node is a persistent node
Datalength Node data length in bytes, Chinese 3 bytes
Numchildren Number of child nodes
The basic principle of session and the use of the Create command

The basic principle of ZK characteristic-session:

    • A session exists between the client and the server connection
    • Each session can be set to a time-out
    • The heartbeat is over and the session expires
    • Session expires, temporary node Znode will be discarded
    • Heartbeat mechanism: Client ping packet request to server

Use of the Create command:

[zk: localhost:2181(CONNECTED) 7] create /testDir test-data  # 创建一个节点,节点的数据为test-dataCreated /testDir[zk: localhost:2181(CONNECTED) 8] ls /[zookeeper, testDir][zk: localhost:2181(CONNECTED) 9] get /testDirtest-datacZxid = 0x4ctime = Sun Apr 22 18:17:56 CST 2018mZxid = 0x4mtime = Sun Apr 22 18:17:56 CST 2018pZxid = 0x4cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 9numChildren = 0[zk: localhost:2181(CONNECTED) 10]

The nodes created by this creation are persisted, that is, persistent nodes (persistent). The so-called persistent node is the existence of a node after it has been created, until a delete operation is taken to proactively clear the node--it will not disappear because the client session that created the node fails. In addition to persistent nodes, we can also create temporary nodes (ephemeral), so let's look at how to create a temporary node:

[zk: localhost:2181(CONNECTED) 11] create -e /testDir/tmp tmp-data  # -e指定创建的节点是临时节点Created /testDir/tmp[zk: localhost:2181(CONNECTED) 12] get /testDirtest-datacZxid = 0x4ctime = Sun Apr 22 18:17:56 CST 2018mZxid = 0x4mtime = Sun Apr 22 18:17:56 CST 2018pZxid = 0x5cversion = 1  # 由于在testDir下创建了一个子节点,所以 cversion 的值就会进行累加dataVersion = 0aclVersion = 0ephemeralOwner = 0x0  # 表示持久节点dataLength = 9numChildren = 1[zk: localhost:2181(CONNECTED) 16] get /testDir/tmp tmp-datacZxid = 0x5ctime = Sun Apr 22 18:20:26 CST 2018mZxid = 0x5mtime = Sun Apr 22 18:20:26 CST 2018pZxid = 0x5cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x10000052f5b0000  

Unlike persistent nodes, the life cycle of a temporary node and client session binding. In other words, if the client session fails, the node is automatically erased. Note that the session invalidation is mentioned here, not the disconnection, and of course the disconnection also causes the session to fail, but not the main reason. In addition, child nodes cannot be created under temporary nodes. As we mentioned above, the end of the heartbeat, the session will expire, and the session expires, the temporary node Znode will be discarded. So let's disconnect from the server and see if the temporary node is cleared:

[zk: localhost:2181(CONNECTED) 17] quit  # 退出Quitting...2018-04-22 18:25:44,884 [myid:] - INFO  [main:[email protected]] - Session: 0x10000052f5b0000 closed  # session关闭了2018-04-22 18:25:44,885 [myid:] - INFO  [main-EventThread:[email protected]] - EventThread shut down for session: 0x10000052f5b0000[[email protected] ~]# zkCli.sh  # 重新连接服务端[zk: localhost:2181(CONNECTED) 0] ls /testDir  # 可以看到,tmp节点就消失了[][zk: localhost:2181(CONNECTED) 1]

Above we demonstrate the creation of persistent nodes and temporary nodes, let's take a look at the creation of persistent sequential nodes (persistent_sequential):

[zk: localhost:2181(CONNECTED) 1] create -s /testDir/sec seq  # -s指定创建持久顺序节点Created /testDir/sec0000000001  # 会自动为给定节点名加上一个数字后缀[zk: localhost:2181(CONNECTED) 2] ls /testDir[sec0000000001][zk: localhost:2181(CONNECTED) 3] create -s /testDir/sec seqCreated /testDir/sec0000000002  # 再次创建节点数字就会递增[zk: localhost:2181(CONNECTED) 4] ls /testDir               [sec0000000001, sec0000000002]  # 这时就会有两个节点[zk: localhost:2181(CONNECTED) 7] create -s /testDir/test seq  # 创建前缀不同的节点,数字也会递增Created /testDir/test0000000003[zk: localhost:2181(CONNECTED) 8] ls /testDir                

The basic characteristics of such nodes are consistent with the persistent node types. The extra feature is that in ZK, each parent node maintains a time sequence for his first-level child node, which records the order in which each child node is created. Based on this feature, when you create a child node, you can set this property, and in the process of creating the node, ZK automatically adds a number suffix to the given node name, as the new one. The range of this number suffix is the maximum value of the integer type.

When the-S is used with the-e option to create a temporary order node (ephemeral_sequential), this node belongs to the temporary node, but with the order, as with the temporary node, the session expiration node disappears. The end of the client session connection will also cause the session to expire, so the same node will disappear, and this type of node is generally used to implement distributed locks. Here's a demonstration of how the temporary sequential nodes are created:

[zk: localhost:2181(CONNECTED) 15] create /testTmp testTmp-data  # 创建一个持久节点Created /testTmp[zk: localhost:2181(CONNECTED) 16] create -s -e /testTmp/secTmp secTmp-data  # 在该节点下,创建临时顺序节点Created /testTmp/secTmp0000000000[zk: localhost:2181(CONNECTED) 17] create -s -e /testTmp/secTmp secTmp-dataCreated /testTmp/secTmp0000000001[zk: localhost:2181(CONNECTED) 18] create -s -e /testTmp/testTmp secTmp-dataCreated /testTmp/testTmp0000000002[zk: localhost:2181(CONNECTED) 19] ls /testTmp[testTmp0000000002, secTmp0000000001, secTmp0000000000][zk: localhost:2181(CONNECTED) 20]

Disconnect the client from the server and see if the staging node is cleared:

[zk: localhost:2181(CONNECTED) 21] quit  # 退出Quitting...2018-04-22 19:07:13,527 [myid:] - INFO  [main:[email protected]] - Session: 0x10000052f5b0001 closed2018-04-22 19:07:13,528 [myid:] - INFO  [main-EventThread:[email protected]] - EventThread shut down for session: 0x10000052f5b0001[[email protected] ~]# zkCli.sh[zk: localhost:2181(CONNECTED) 1] ls /testTmp  
Use of Set and delete commands

You can modify a node by using the SET command:

[zk: localhost:2181(CONNECTED) 2] set /testDir new-data  # 修改testDir节点的数据cZxid = 0x4ctime = Sun Apr 22 18:17:56 CST 2018mZxid = 0x12mtime = Sun Apr 22 19:24:41 CST 2018pZxid = 0xacversion = 5dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 8numChildren = 3[zk: localhost:2181(CONNECTED) 3] get /testDir         new-data   # 可以看到数据更新了cZxid = 0x4ctime = Sun Apr 22 18:17:56 CST 2018mZxid = 0x12mtime = Sun Apr 22 19:24:41 CST 2018pZxid = 0xacversion = 5dataVersion = 1  # 此时数据版本就会递增为1aclVersion = 0ephemeralOwner = 0x0dataLength = 8numChildren = 3[zk: localhost:2181(CONNECTED) 4]

We can use the data version dataversion to achieve an optimistic lock effect, so each time we modify the node data, we should add this dataversion value to modify, so as not to cause inconsistent data in the concurrency:

[zk: localhost:2181(CONNECTED) 4] set /testDir OneVerstion-data 1  指定版本去修改数据cZxid = 0x4ctime = Sun Apr 22 18:17:56 CST 2018mZxid = 0x13mtime = Sun Apr 22 19:29:29 CST 2018pZxid = 0xacversion = 5dataVersion = 2aclVersion = 0ephemeralOwner = 0x0dataLength = 16numChildren = 3[zk: localhost:2181(CONNECTED) 5] set /testDir OneVerstion-data 1  

You can delete a node by using the Delete command:

[zk: localhost:2181(CONNECTED) 8] ls /testDir[test0000000003, sec0000000001, sec0000000002][zk: localhost:2181(CONNECTED) 9] delete /testDir/sec0000000001  # 删除节点[zk: localhost:2181(CONNECTED) 10] ls /testDir                  [test0000000003, sec0000000002][zk: localhost:2181(CONNECTED) 11] delete /testDir/sec0000000002 0  # 通过指定版本号来删除节点[zk: localhost:2181(CONNECTED) 12] ls /testDir                    [test0000000003][zk: localhost:2181(CONNECTED) 13]
Watcher mechanism

Watcher is a more important feature of ZK and is defined as follows:

    • For each node operation, there will be a supervisor, watcher
    • Trigger watcher event when a monitored object (znode) has changed
    • In simple terms, watcher is similar to a trigger in SQL
    • The Watcher in ZK is disposable and destroyed immediately after triggering.
    • Parent node, the child node can trigger its watcher
    • For different types of operations, the watcher events that are triggered are also different:
      • (child) Node creation events
      • (Child) Node Delete event
      • (child) node data Change event
Parent Node Watcher Event

Watcher Command Line learning:

    • The watcher can be set through get path [watcher], and other functions such as stat, LS, LS2 command can also be set watcher
    • Parent node Delete and change operation Trigger watcher
    • Sub-node deletion and modification operation trigger watcher

Watcher Event Type-parent node:

    • Create parent node to trigger nodecreated event
    • Modify parent node data to trigger Nodedatachanged events
    • Delete parent node triggering nodedeleted event

Create a parent node to trigger the Nodecreated event, example:

[zk: localhost:2181(CONNECTED) 19] stat /testWatch watch  # 在节点创建之前,我们可以通过 stat 命令去设置watcherNode does not exist: /testWatch[zk: localhost:2181(CONNECTED) 20] create /testWatch test-data  # 创建父节点WATCHER::WatchedEvent state:SyncConnected type:NodeCreated path:/testWatch  # 触发 NodeCreated 事件Created /testWatch[zk: localhost:2181(CONNECTED) 21]

Modify parent node data to trigger Nodedatachanged events, Example:

[zk: localhost:2181(CONNECTED) 21] get /testWatch watch  # 因为zk事件是一次性的,所以我们还需要通过 get 命令设置 watchertest-datacZxid = 0x19ctime = Sun Apr 22 23:37:08 CST 2018mZxid = 0x19mtime = Sun Apr 22 23:37:08 CST 2018pZxid = 0x19cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 9numChildren = 0[zk: localhost:2181(CONNECTED) 22] set /testWatch new-data  # 修改父节点数据WATCHER::WatchedEvent state:SyncConnected type:NodeDataChanged path:/testWatch  

Delete parent node triggering nodedeleted event, example:

[zk: localhost:2181(CONNECTED) 23] ls /testWatch watch  # 通过 ls 命令来设置 watcher[][zk: localhost:2181(CONNECTED) 24] delete /testWatch  # 删除父节点WATCHER::WatchedEvent state:SyncConnected type:NodeDeleted path:/testWatch  # 触发 NodeDeleted 事件[zk: localhost:2181(CONNECTED) 25]
Child Node Watcher event

Watcher event Type-child node:

    • Use the LS command to set watcher for the parent node, which triggers the nodechildrenchanged event when the child node is created
    • Use the LS command to set watcher for a parent node, and also to trigger a nodechildrenchanged event when a child node is deleted
    • Using the LS command to set watcher for a parent node, no events are triggered when modifying child node data
    • Set watcher for child nodes using the GET command, which triggers nodedatachanged events when child node data is modified

Using the LS command to set watcher for the parent node, the Nodechildrenchanged event is triggered when the child node is created, as an example:

[zk: localhost:2181(CONNECTED) 29] create /testWatch test-data  # 创建父节点Created /testWatch[zk: localhost:2181(CONNECTED) 30] ls /testWatch watch  # 使用 ls 命令为父节点设置watcher[][zk: localhost:2181(CONNECTED) 31] create /testWatch/testChildren children-data  # 创建子节点WATCHER::WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/testWatch  

Using the LS command to set watcher for the parent node, the Nodechildrenchanged event is also triggered when the child node is deleted, as an example:

[zk: localhost:2181(CONNECTED) 32] ls /testWatch watch   # 使用 ls 命令为父节点设置watcher[testChildren][zk: localhost:2181(CONNECTED) 33] delete /testWatch/testChildren   # 删除子节点          WATCHER::WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/testWatch   

Simply explain why creating and deleting child nodes is the trigger nodechildrenchanged event, because child nodes are hung under the parent node, and we are setting the watcher to the parent node, not to the child node watcher, regardless of whether the child node is deleted or created, is a process of change, so it's all triggering the same event.

Using the LS command to set watcher for the parent node, no events are triggered when the child node data is modified, example:

[zk: localhost:2181(CONNECTED) 35] create /testWatch/testChildren children-data  # 创建子节点[zk: localhost:2181(CONNECTED) 36] ls /testWatch watch   # 使用 ls 命令为父节点设置watcher        [testChildren][zk: localhost:2181(CONNECTED) 37] set /testWatch/testChildren new-children-data  

The event is not triggered because the watcher is set on the parent node, so the watcher event set by the parent node is not triggered when the child node data is modified.

Use the Get command to set watcher for child nodes, and to modify child node data to trigger the Nodedatachanged event, example:

[zk: localhost:2181(CONNECTED) 40] get /testWatch/testChildren watch  # 使用 get 命令为子节点设置watchernew-children-datacZxid = 0x1fctime = Sun Apr 22 23:58:44 CST 2018mZxid = 0x21mtime = Mon Apr 23 00:01:41 CST 2018pZxid = 0x1fcversion = 0dataVersion = 2aclVersion = 0ephemeralOwner = 0x0dataLength = 17numChildren = 0[zk: localhost:2181(CONNECTED) 41] set /testWatch/testChildren new-children-data2  # 修改子节点数据WATCHER::WatchedEvent state:SyncConnected type:NodeDataChanged path:/testWatch/testChildren   

Zookeeper basic features and the Linux-based ZK client command line learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.