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