Curator Source code parsing (iii) Access interface analysis

Source: Internet
Author: User

Next, the Access Interface section of the test program is analyzed. 2 Calling the Zookeeper Access interface

Initialization and start analysis is done, the operation interface call code is as follows:

String Path = Zkpaths.makepath (path, name); byte[] bytes =args[1].getbytes (); try{    Client.setdata (). Forpath (Path, bytes);} catch (Keeperexception.nonodeexception e) {   client.create (). creatingparentsifneeded (). Forpath (path,bytes);}

The actual operation of the zookeeper node is called the SetData method of the Curatorframeworkimpl class that implements the Curatorframework interface. Defined as follows:

@Override   publicsetdatabuildersetdata ()   {        preconditions.checkstate (getState () = = Curatorframeworkstate.started, "instance must bestarted before calling this method");         return new Setdatabuilderimpl (this);}

In addition, there are other zookeeper operating interfaces:

@Override   publicgetdatabuilder getData ()   {        preconditions.checkstate (getState () = = Curatorframeworkstate.started, "instance must bestarted before calling this method");         return new Getdatabuilderimpl (this);   }    @Override   Publicgetchildrenbuildergetchildren ()   {        preconditions.checkstate (getState () = = Curatorframeworkstate.started, "instance must bestarted before calling this method");         Returnnew Getchildrenbuilderimpl (this);   }    @Override   publicgetaclbuilder getacl ()   {        preconditions.checkstate (getState () = = Curatorframeworkstate.started, "instance must bestarted before calling this method");         return new Getaclbuilderimpl (this);}

So the Curatorframeworkimpl class not only encapsulates the Curatorzookeeperclient class, but also provides a similar zookeeper-style operation interface, the following concrete look at how the SetData method is implemented.

You can see that each operation interface returns a corresponding action class, such as Deletebuilderimpl,getdatabuilderimpl,setdatabuilderimpl, and each operation class implements a method called Forpath, The following is an example of Setdatabuilderimpl's Forpath method:

@Override   publicstat Forpath (String path,byte[] data) throwsexception   {        if (compress)        {            data = Client.getcompressionprovider (). Compress (Path,data);        }         Path = client.fixfornamespace (path);         Stat resultstat = null;               If the Inbackground () method is called to set the asynchronous operation, execute this branch        if (Backgrounding.inbackground ()  )        {            Client.processbackgroundoperation (newoperationanddata<pathandbytes> (this,new PathAndBytes (path, data), Backgrounding.getcallback (), Null,backgrounding.getcontext ()), null);        }        else        {            //Otherwise execute this branch            resultstat = Pathinforeground (Path,data);        }        Returnresultstat;}

Here we only focus on synchronous operation, the code is as follows:

Private Stat Pathinforeground (final String path,final byte[]data) throwsexception   {        Stat  resultstat= Retryloop.callwithretry        (           client.getzookeeperclient (),            new callable<stat> ()            {                @ Override public                Stat call () throws Exception                {                    returnclient.getzookeeper (). SetData (Path, data, version) ;                }            }        );        Trace.commit ();        return resultstat;    }

You can see that the corresponding operation interface is ultimately performed by calling the Retryloop.callwithretry method to perform the native zookeeper. Do you want to encapsulate it in retryloop.callwithretry? This involves the curator encapsulation zookeeper the most important point, is the internal encapsulation of the complex client-to-zookeeper cluster connection and retry mechanism, detailed in the next section.

Curator Source code parsing (iii) Access interface analysis

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.