The ZooKeeper operations

ZooKeeper's data model and its API support the following nine basic operations:

Operation

Description

create

Creates a znode in a specified path of the ZooKeeper namespace

delete

Deletes a znode from a specified path of the ZooKeeper namespace

exists

Checks if a znode exists in the path

getChildren

Gets a list of children of a znode

getData

Gets the data associated with a znode

setData

Sets/writes data into the data field of a znode

getACL

Gets the ACL of a znode

setACL

Sets the ACL in a znode

sync

Synchronizes a client's view of a znode with ZooKeeper

Let's look at the ZooKeeper operations mentioned in the preceding table using ZooKeeper Java shell:

  1. Create a znode called root with ThisIsTheRootNode as its data:
    [zk: localhost(CONNECTED) 0] create /root "ThisIsTheRootNode"
    Created /root
    
  2. Get the content of the just created znode root:
    [zk: localhost(CONNECTED) 1] get /root
    "ThisIsTheRootNode"
    …… ……
    …… ……
    
  3. Create a child znode child-1 for root with ThisIsChild-1 as its data:
    [zk: localhost(CONNECTED) 2] create /root/child-1 "ThisIsChild-1"
    Created /root/child-1
    
  4. Create a child znode child-2 for root with ThisIsChild-2 as its data:
    [zk: localhost(CONNECTED) 3] create /root/child-2 "ThisIsChild-2"
    Created /root/child-2
    
  5. List the children of root:
    [zk: localhost(CONNECTED) 4] ls /root
    [child-2, child-1]
    
  6. Get the access control listing for root:
    [zk: localhost(CONNECTED) 5] getAcl /root
    'world,'anyone
    : cdrwa
    
  7. Deleting the root is not allowed as root has 2 child znodes:
    [zk: localhost(CONNECTED) 6] delete /root
    Node not empty: /root
    
  8. Delete child-1:
    [zk: localhost(CONNECTED) 7] delete /root/child-1
    
  9. Delete child-2:
    [zk: localhost(CONNECTED) 8] delete /root/child-2
    
  10. List the content of root:
    [zk: localhost(CONNECTED) 9] ls2 /root
    []
    …… ……
    …… ……
    
  11. Delete root:
    [zk: localhost(CONNECTED) 10] delete /root
    

Apart from the operations described so far, ZooKeeper also supports batch updates to znodes with an operation called multi. This batches together multiple primitive operations into a single unit. A multi operation is also atomic in nature, which means that either all the updates succeed or the whole bunch of updates fails in its entirety.

ZooKeeper does not allow partial writes or reads of the znode data. When setting the data of a znode or reading it, the content of the znode is replaced or read entirely. Update operations in ZooKeeper, such as a delete or setData operation, have to specify the version number of the znode that is being updated. The version number can be obtained by using the exists() call. The update operation will fail if the specified version number does not match the one in the znode. Also, another important thing to note is that updates in ZooKeeper are non-blocking operations.

The read and write operations in ZooKeeper are shown in the following image:

The ZooKeeper operations

An image that shows the read and write operations in ZooKeeper

From the preceding image, we need to take note of the following two crucial aspects of these operations:

  • Read requests: These are processed locally in the ZooKeeper server to which the client is currently connected
  • Write requests: These are forwarded to the leader and go through majority consensus before a response is generated

    Tip

    The read and write processing by ZooKeeper is described in more detail in the ZooKeeper implementation section later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.120.159