"Go" ZooKeeper data model

Source: Internet
Author: User

Original link http://coolxing.iteye.com/blog/1871328

The zookeeper data structure is very similar to the normal file system. See:

Picture references from Developerworks

Each node in the diagram is called a znode. Each znode consists of 3 parts:

    • Stat. This is the status information that describes the Znode version, permissions, and so on.
    • Data. The data associated with the Znode.
    • Children. The child node under the Znode.
Zookeeper command

Before delving into the various parts of Znode, you need to familiarize yourself with some common zookeeper commands first.

Connect to Server

Bash code
    1. Bin/zkcli.sh-server 10.1.39.43:4180

Lists the child node of the specified node

Bash code
    1. [Zk:10.1.39.43:4180 (CONNECTED) 9] LS/
    2. [Hello, Filesync, zookeeper, Xing, server, group, log]
    3. [Zk:10.1.39.43:4180 (CONNECTED)] Ls/hello
    4. []

Create the Znode node and specify the associated data

Bash code
    1. Create/hello World

Create the node/hello and associate the string "World" to the node.

Get data and status information for Znode

Bash code
    1. [Zk:10.1.39.43:4180 (CONNECTED) 7] Get/hello
    2. World
    3. Czxid = 0x10000042c
    4. CTime = Fri 17:57:33 CST 2013
    5. Mzxid = 0x10000042c
    6. Mtime = Fri 17:57:33 CST 2013
    7. Pzxid = 0x10000042c
    8. cversion = 0
    9. dataversion = 0
    10. aclversion = 0
    11. Ephemeralowner = 0x0
    12. Datalength = 5
    13. Numchildren = 0

Delete Znode

Bash code
    1. [Zk:localhost:4180 (CONNECTED)] delete/xing/item0000000001
    2. [Zk:localhost:4180 (CONNECTED)] Delete/xing
    3. Node not empty:/xing

Use the Delete command to delete the specified znode. When the Znode has a child znode, it must first delete all its child znode, otherwise the operation will fail. The RMR command can be used in place of the Delete command, RMR is a recursive delete command, and the RMR command removes the child nodes first if the specified node has child nodes.

Status information for the Znode node

When you use the GET command to get the data for a specified node, it also returns the state information for that node, called Stat. It contains the following fields:

    • Czxid. The Zxid when the node is created.
    • Mzxid. The Zxid of the node when the latest update occurs.
    • CTime. Timestamp when the node was created.
    • Mtime. Timestamp when the node's latest update occurred.
    • Dataversion. The number of times the node data was updated.
    • Cversion. The number of updates to its child nodes.
    • Aclversion. The number of updates to the node ACL (authorization information).
    • Ephemeralowner. If the node is a ephemeral node, the Ephemeralowner value represents the session ID that is bound to the node. If the node is not a ephemeral node, the Ephemeralowner value is 0. As for what is the ephemeral node, look at the following narration.
    • Datalength. The number of bytes of node data.
    • Numchildren. The number of child nodes.
Zxid

The status information for the Znode node contains Czxid and Mzxid, so what is ZXID?
Each change in the zookeeper state corresponds to an increment Transaction id , which is called Zxid. Because of the increment of Zxid, if ZXID1 is less than Zxid2, then zxid1 must precede zxid2. Create any node, or update the data of any node, or deleting any node will cause the zookeeper state to change, resulting in an increase in the value of ZXID.

Session

Before client and server communication, you first need to establish a connection, which is called the session. After the connection is established, if a connection timeout occurs, authorization fails, or the connection is explicitly closed, the connection is in the closed state, at which point the session ends.

Node type

When you tell the Ephemeralowner field of a node state, the nodes that are mentioned are ephemeral nodes, and some are not. So what types of nodes do they have? What are the characteristics of each type of node?
persistent. The persistent node is not bound to a specific session and does not disappear with the end of the session that created the node, but persists until the node is explicitly deleted.
ephemeral. The ephemeral node is temporary, and if the session that created the node ends, the node is automatically deleted. The ephemeral node cannot have child nodes. Although the ephemeral node is bound to the session that created it, the other session can read and write the associated data in that node as long as the node is not deleted. Use the-e parameter to specify the create ephemeral node.

Bash code
    1. [Zk:localhost:4180 (CONNECTED) 4] create-e/xing/ei World
    2. Created/xing/ei

sequence. Strictly speaking, sequence is not one of the node types. The sequence node can be either ephemeral or persistent. When you create a sequence node, ZooKeeper server adds a sequence of numbers after the specified node name, which is incremented. As a result, you can create the same sequence node multiple times and get different nodes. Use the-s parameter to specify the create sequence node.

Bash code
    1. [zk:localhost:4180 (CONNECTED) 0] create-s/xing/item World
    2. created/xing/item0000000001
    3. [Zk:localhost:4180 (CONNECTED) 1] create-s/xing/item World
    4. created/xing/item0000000002
    5. [Zk:localhost:4180 (CONNECTED) 2] create-s/xing/item World
    6. created/xing/item0000000003
    7. [Zk:localhost:4180 (CONNECTED) 3] create-s/xing/item World
    8. created/xing/item0000000004

Watch

Watch means listening to events of interest. On the command line, the following commands can specify whether to listen for the appropriate event.

ls command. The first parameter of the LS command specifies Znode, and the second parameter, if true, indicates the increase or decrease of the child nodes listening to the Znode, and the delete event for the Znode itself.

Bash code
    1. [Zk:localhost:4180 (CONNECTED)] ls/xing true
    2. []
    3. [Zk:localhost:4180 (CONNECTED)] Create/xing/item item000
    4. Watcher::
    5. Watchedevent state:syncconnected type:nodechildrenchanged path:/xing
    6. Created/xing/item

Get command. The first parameter of the GET command specifies Znode, and the second parameter, if True, describes the update and delete events that are listening for the znode.

Bash code
  1. [Zk:localhost:4180 (CONNECTED)] get/xing true
  2. World
  3. Czxid = 0x100000066
  4. CTime = Fri 22:30:01 CST 2013
  5. Mzxid = 0x100000066
  6. Mtime = Fri 22:30:01 CST 2013
  7. Pzxid = 0x100000066
  8. cversion = 0
  9. dataversion = 0
  10. aclversion = 0
  11. Ephemeralowner = 0x0
  12. Datalength = 5
  13. Numchildren = 0
  14. [Zk:localhost:4180 (CONNECTED)] Create/xing/item item000
  15. Created/xing/item
  16. [Zk:localhost:4180 (CONNECTED)] Rmr/xing
  17. Watcher::
  18. Watchedevent state:syncconnected type:nodedeleted path:/xing

Stat command. The stat command is used to obtain status information for Znode. The first parameter specifies Znode, and if the second argument is true, the update and delete events for that node are monitored.

"Go" ZooKeeper data model

Related Article

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.