zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Strachan" <james.strac...@gmail.com>
Subject [PATCH] a simple Leader Election or exclusive Write Lock protocol/policy
Date Thu, 17 Jul 2008 15:42:09 GMT
So having recently discovered ZooKeeper, I'm really liking it - good job folks!

I've seen discussions of building high level features from the core ZK
library and had not seen any available on the interweb so figured I'd
have a try creating a simple one. Feel free to ignore it if a ZK ninja
can think of a neater way of doing it - I've basically followed the
protocol defined in the recent ZK presentation...

I've submitted the code as a patch here...

I figured the Java Client might as well come with some helper code to
make doing things like exclusive locks or leader elections easier; we
could always spin them out into a separate library if and when
required etc. Right now its one fairly simple class :)

Currently its a simple class where you can register a Runnable to be
invoked when you have the lock; or you can just keep asking if you
have the lock now and again as you see fit etc.

WriteLock locker = new WriteLock(zookeeper, "/foo/bar");
locker.setWhenOwner(new Runnable() {...}); // fire this code when owner...

// lets try own it

// I may or may not have the lock now
if (locker.isOwner()) {....}

// time passes



Open Source Integration

View raw message