Zookeeperclient (based on Zookeepernetex re-encapsulation)

Source: Internet
Author: User
Tags zookeeper
Overview

Zookeeper is essentially a distributed, small file storage system. Originally a component of Apache Hadoop, it is now split into a standalone subproject of Hadoop.

As a distributed service framework, zookeeper is mainly used to solve the problem of consistency of application system in distributed cluster, it can provide data storage based on directory node tree like file system, but zookeeper is not used to store data specifically. It is primarily used to maintain and monitor the state changes in your stored data. Data-based cluster management can be achieved by monitoring changes in the state of these data.

The zookeeperclient realizes the monitoring of the disconnection, the session expiration, the permanent listening, and the change of the sub node data. and added common functions, such as distributed locks, leader elections, distributed queue usage Instructions

First, create Zkclient object
Two, add, update, delete and acquire three nodes,
Rights Management
four, monitoring related
There are two ways to create a Zkclient object to easily create a Zkclient object

1. Using constructors to create

string address = "localhost:2181"; 
    Zkclient zkClient1 = new Zkclient (address);     
    Zkclient ZkClient2 = new Zkclient (address, Timespan.frommilliseconds (10000));   
    Zkclient ZkClient3 = new Zkclient (address, timespan.frommilliseconds (10000), Timespan.frommilliseconds (10000));   
    Zkclient zkClient4 = new Zkclient (address, timespan.frommilliseconds (30000), Timespan.frommilliseconds (10000), new Serializableserializer ());      
    Zkclient zkClient5 = new Zkclient (address, timespan.frommilliseconds (30000), Timespan.frommilliseconds (10000), new Serializableserializer (), Timespan.frommilliseconds (60000));  

2. Create using a secondary class

  string address = "localhost:2181";  
    Zkclient zkclient = zkclientbuilder.newzkclient (address)  
                               . Sessiontimeout (30000)//optional  
                               . Serializer (new Serializableserializer ())//optional  
                               . Retrytimeout (60000)//optional  
                               . ConnectionTimeout (10000)//optional  
                               . Build (); Create an instance
new, update, delete, and fetch of nodes new Node

1. General new node

The parent node does not exist throws an exception

    Await Zkclient.createasync ("/test1", "123", createmode.ephemeral);  
    Await Zkclient.createasync ("/test1-1", 123, createmode.ephemeral_sequential);  
    Await Zkclient.createasync ("/test1-2", 123, createmode.persistent);  
    Await Zkclient.createasync ("/test1-3", 123, createmode.persistent_sequential);

2. Recursive new node (new node and its parent node)

If the parent node does not exist, it is created together.
For persistent types of nodes, recursive creation, parent nodes, and child nodes are created as persistent.
For ephemeral types of nodes, recursive creation, parent nodes are persistent types, and the last level node is the ephemeral type. (because ephemeral cannot own child nodes)
Note: The second parameter is the value of the node, the value of the last level node.

     String path = "/TEST8/1/2/3";
     Recursively creates nodes and parent nodes
     await Zkclient.createrecursiveasync (path, "abc", createmode.persistent);
     Await Zkclient.createrecursiveasync (path, "123", ZooDefs.Ids.CREATOR_ALL_ACL, createmode.persistent);

3. Special Ephemeral type node

A special type of ephemeral node that is automatically created when a session fails to be deleted.

       String path = "/TEST8/1/2/3";
       Ephemeral type node
       await zkclient.createephemeralasync (path);
       Await Zkclient.createephemeralasync (path, "123");
       Ephemeral_sequential type
       String retpath = await Zkclient.createephemeralsequentialasync (path, "456");
Update node Data
   String path = "/test";
   Await Zkclient.setdataasync (path, "456");
   Updates with the expected version number, if the true version number is inconsistent with the expected version number, will fail to update and throw an exception
   await Zkclient.setdataasync (path, "123", 2);

Delete a node

1. General deletion

    BOOL flag = await Zkclient.deleteasync ("/test");//delete any version
    bool flag = await Zkclient.deleteasync ("/test", 1); Delete the specified version

2. Recursive deletion (delete node and child node)

    String path = "/test";
    Await Zkclient.deleterecursiveasync (path);//If there are multiple child nodes under/test, they will be deleted together.
Get Node Data
    String path = "/test";
    Await zkclient.getdataasync<string> (path); If the node does not exist throw an exception
    await zkclient.getdataasync<string> (path, true);//If the node does not exist return null
    Stat Stat = (await Zkclient.getzkdataasync<string> (Path)). Stat; Access to data and stat information
Wait for node to create
    String path = "/test";
    Wait until the timeout or node creation succeeds.
    await Zkclient.waituntilexistsasync (path, Timespan.frommilliseconds (5000));
Rights Management

Zookeeper's rights management, which is the ACL control function, is coordinated through server and client ends:
Server End:
A zookeeper node (Znode) stores two pieces of content: Data and state, and the state contains ACL information. Creating a znode creates an ACL list in which each ACL includes the authentication mode (scheme) specific content (ID) (When scheme= "Digest", the ID is the username password, for example, " root:j0sty9bcukubtk1y8pkbl7qoxsw= ") Permission (perms)

Zookeeper provides the following authentication modes (scheme): Digest:client driven by username and password authentication, for example, user:password,digest password generation is Sha1 of base64 summary auth: Do not use any IDs That represents any confirmed user. ip:client driven by IP address verification, such as 172.2.0.0/24 World: Fixed user for anyone, open to all client side super: In this scheme case, the corresponding ID has Super permission, can do Anything (Cdrwa)

Note that the exists operation and the GETACL operation are not subject to ACL admission Control, so any client can query the state of the node and the ACL of the node.

The

node's permissions (perms) are mainly as follows: Create allows child node create operations Read allows GetChildren and GetData operations on this node Write permission to SetData operations Delete Allow child node delete operation Admin permission to SETACL operations on this node Znode ACL permissions are represented by an int-type numeric perms, and the perms 5 bits represent SetACL, delete, create, write, read, respectively. Like 0x1f=adcwr,0x1=--r,0x15=a-c-r.

Zkclient.addauthinfo (Digest, Encoding.Default.GetBytes (Allauth));
            if (await Zkclient.existsasync (Testnode)) {await zkclient.deleteasync (testnode);
            } list<acl> ACLS = new list<acl> (); ACLs.
            ADD (New ACL ((int) Perms.all, new Id (Digest, Allauth)); ACLs.
            ADD (New ACL ((int) perms.read, new Id (Digest, Readauth)); ACLs.
            ADD (New ACL ((int) perms.write, new Id (Digest, Writeauth)); ACLs.
            ADD (New ACL ((int) perms.delete, new Id (Digest, Deleteauth)); ACLs.

            ADD (New ACL ((int) perms.admin, new Id (Digest, Adminauth));

            Await Zkclient.createpersistentasync (Testnode, "Test-data", ACLs);
            try {await zkclient.getdataasync<string> (testnode);//There is no authentication information, read error} catch (Exception e) {} try {Zkclient.addauthinfo (Digest, Encoding.default.
     GetBytes (Adminauth));           Await zkclient.getdataasync<string> (testnode);//admin permissions do not match read rights and read errors} catch (Exception e)
                {} try {Zkclient.addauthinfo (Digest, Encoding.Default.GetBytes (Readauth)); Await zkclient.getdataasync<string> (testnode);//Only authenticated information for Read permission to read} catch (E Xception e) {} try {await Zkclient.setdataasync (Testnode, "new-data");/No authentication information Write failure} catch (Exception e) {} try {Zkclient.addauthinf
                O (Digest, Encoding.Default.GetBytes (Writeauth));

            Await Zkclient.setdataasync (Testnode, "new-data");//After adding authentication information, write normal} catch (Exception e) {}
                try {Zkclient.addauthinfo (Digest, Encoding.Default.GetBytes (Readauth));
      Await zkclient.getdataasync<string> (Testnode)//Read new value validation      catch (Exception e) {} try {Zkclient.addauthinfo (Digest, En Coding.
                Default.getbytes (Deleteauth));
            Await Zkclient.deleteasync (Testnode); catch (Exception e) {}//Note: Zkclient.setacl method View source can be found, called ReadData, SetACL two methods//So to repair To change the ACL properties of a node, you must also have read, admin two permissions try {Zkclient.addauthinfo (Digest, Encoding .
                Default.getbytes (Adminauth));

                Zkclient.addauthinfo (Digest, Encoding.Default.GetBytes (Readauth));
                list<acl> acls1 = new list<acl> (); Acls1.
                ADD (New ACL ((int) Perms.all, new Id (Digest, Adminauth));
                Await Zkclient.setaclasync (Testnode, ACLS1);
            Aclresult Aclresult = await zkclient.getaclasync (testnode); catch (Exception e) {}
Monitor related

Note: Session expiration due to too long disconnect time, because the server side will delete the client settings listening after the session expires.

Even if the client automatically connects successfully after the session expires, the node that the client listens on may still change during session expiration

And what changed or did not change, the client is unable to perceive.

To avoid losing any data-changing events, all listeners have a callback method (Sessionexpiredhandler) to handle the special case of session expiration.

    Sessionexpiredhandler = Async (path) =>
                {
                    await Task.run (() =>
                    {
                        Console.WriteLine (path);
                    });
                };
information changes for subscription nodes (create nodes, delete nodes, add child nodes)
Izkchildlistener Childlistener = new Zkchildlistener ();
    Child node content changes
    Childlistener.childchangehandler = Async (Parentpath, currentchilds) =>
         {
               await task.run () =>
               {
                     Console.WriteLine (parentpath);
                     Console.WriteLine (String. Join (".", Currentchilds));
               };
     Number of child nodes changes
     Childlistener.childcountchangedhandler = Async (Parentpath, currentchilds) =>
         {
               await Task.run (() =>
               {
                     Console.WriteLine (parentpath);
                     Console.WriteLine (String. Join (".", Currentchilds));
               };
     "/testusernode" monitoring nodes, can be existing or can not exist 
     zkclient.subscribechildchanges ("/testusernode3", Childlistener);   
changes in the content of the subscription node's data
    Izkdatalistener Datalistener = new Zkdatalistener (); Node creation and node content changes Datalistener.datacreatedorchangehandler = Async (datapath, data) => {await
                Task.run (() => {Console.WriteLine (DataPath + ":" + convert.tostring (data));
        });
     };
                 Node deletion datalistener.datadeletedhandler = Async (DataPath) => {await task.run () =>
                 {Console.WriteLine (datapath);

         });
      }; Node Creation Datalistener.datacreatedhandler = Async (datapath, data) => {await Task.run
                   (() => {Console.WriteLine (DataPath + ":" + convert.tostring (data));
           });
       }; Node content Changes Datalistener.datachangehandler = Async (datapath, data) => {await Tas
              K.run (() =>      {Console.WriteLine (datapath);

            });
      }; Zkclient.subscribedatachanges ("/testusernode", Datalistener);
Client State Listening
    Izkstatelistener Statelistener = new Zkstatelistener ();
                Status Change Statelistener.statechangedhandler = Async (state) => {await task.run () => {Console.WriteLine (state.
                ToString ());
         });
      }; Session Invalidation Statelistener.sessionexpiredhandler = Async (path) => {await task.run () =&G
                  T
                  {Console.WriteLine (path);
           });
       }; Create Session Statelistener.newsessionhandler = Async () => {await task.run () =>{
             });
        }; Session Failure Statelistener.sessionestablishmenterrorhandler = Async (ex) => {await Ta Sk. Run (() => {Console.WriteLine, ex.
                  message);

             });
         }; Zkclient.subscribestatechanges (Statelistener);
Extended Functionality Distributed Locks
using (var zkclient = new Zkclient (testutil.zkservers))
 {    
         await zkclient.createrecursiveasync ("/zk/lock", NULL, createmode.persistent);

         Create a distributed lock, not a thread-safe class, and each thread creates a separate instance.
         var _lock = new Zkdistributedlock (zkclient, "/zk/lock");

         Await _lock. Lockasync (); Get lock

         //do someting

         await _lock. Unlockasync ()//Free lock
}
leader Elections
using (var zkclient = new Zkclient (testutil.zkservers))
 {          
         await zkclient.createrecursiveasync ("/zk/leader", NULL, createmode.persistent);

         var listener = new Zkleaderselectorlistener ();
         Listener.takeleadership = Async (client, selector) =>
                                 {                 
                                     Console.WriteLine ("I am the leader-" + await selector.) Getleaderasync ());
                                     Selector. Close ();
                                 };
         var selector = new Zkleaderselector ("id", True, Zkclient, "/zk/leader", listener);
        Initiate and participate in leader election
         selector. Start ();

         Gets the ID await selector of the current primary service
         . Getleaderasync ();

         If you want to quit leader election
         selector. Close ();
}
Distributed Queues
using (var zkclient = new Zkclient (testutil.zkservers)) {await Zkclient.createrecursiv

         Easync ("/zk/queue", null, createmode.persistent); var queue = new Zkdistributedqueue<long> (new Zkclient (testutil.zkservers), "/zk/queue") await queue. Offerasync ("123");//put element var value = await queue. Pollasync ()//delete and get top element var value = await queue. Peekasync (); Gets the top element and does not delete the} 

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.