Hi Avinash, getChildren returns a list in lexicographic order, so if you are
updating the children of the election node concurrently, then you may get a
different first node with different clients. If you are using the sequence flag
to create nodes, then you may consider stripping the prefix of the node name
and using the sufix value to determine order.
Hope it helps.
----- Original Message ----
From: Avinash Lakshman <[EMAIL PROTECTED]>
Sent: Friday, July 11, 2008 7:20:06 AM
Subject: [Zookeeper-user] Leader election
I am trying to elect leader among 50 nodes. There is always one odd guy who
seems to think that someone else distinct from what some other nodes see as
leader. Could someone please tell me what is wrong with the following code for
public void electLeader()
ZooKeeper zk = StorageService.instance().getZooKeeperHandle();
String path = "/Leader";
String createPath = path + "/L-";
while( true )
/* Get all znodes under the Leader znode */
List<String> values = zk.getChildren(path, false);
* Get the first znode and if it is the
* pathCreated created above then the data
* in that znode is the leader's identity.
if ( leader_ == null )
leader_ = new AtomicReference<EndPoint>(
EndPoint.fromBytes( zk.getData(path + "/" + values.get(0), false, null) ) );
leader_.set( EndPoint.fromBytes( zk.getData(path + "/"
+ values .get(0), false, null) ) );
/* Disseminate the state as to who the leader is. */
logger_.debug("Elected leader is " + leader_ + " @ znode "
+ ( path + "/" + values.get(0) ) );
/* We need only the last portion of this znode */
String peices = pathCreated_.split("/");
int index = Collections.binarySearch(values,
peices[peices.length - 1]);
if ( index > 0 )
String pathToCheck = path + "/" + values.get(index - 1);
Stat stat = zk.exists(pathToCheck, true);
if ( stat != null )
logger_.debug("Awaiting my turn ...");
logger_.debug("Checking to see if leader is around
catch ( InterruptedException ex )
catch ( KeeperException ex )
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
Zookeeper-user mailing list