Database Reference
In-Depth Information
In the Watcher method, first the event is checked to see if it is a
ZooKeeper status event ( EventType.Node ). If it is a connection event,
then a leadership position is requested. If it is not a connection event,
the path is checked. If it is the same path as the one registered when
creating the object, a list of children is obtained. In this case, the
callback version of the event is used so there only needs to be a single
location where leadership status is checked. The watch is also reset
when getChildren is called.
When leadership is requested, first the main path is created if it does
not exist. This call can produce errors because each client tries to create
the path. If the path already exists by the time the client calls the
create method, the error can be safely ignored. Next, an ephemeral
and sequential node is created in this path. This node is the server's
node and is stored for all future transactions. Before the node is
created, the object registers itself for changes to the main path so that
the act of creating a new node registers a notification in the event that
only one server is started. This is all implemented in
requestLeadership , shown here:
public void requestLeadership() throws Exception {
Stat stat = client.exists(path, false );
if (stat == null ) {
try {
client.create(path, new byte [0],
ZooDefs.Ids. OPEN_ACL_UNSAFE ,
CreateMode. PERSISTENT );
} catch (KeeperException.NodeExistsException e)
{ }
}
client.getChildren(path, true , this , null );
node = client.create(path+"/n_", new byte [0],
ZooDefs.Ids. OPEN_ACL_UNSAFE ,
CreateMode. EPHEMERAL_SEQUENTIAL );
System. out .println("My node is: "+node);
}
Search WWH ::




Custom Search