zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Rosenstrauch <dar...@darose.net>
Subject Re: zookeeper utils
Date Wed, 03 Mar 2010 22:41:14 GMT
On 03/03/2010 01:43 AM, Henry Robinson wrote:
> Just to illustrate one of the primitives you're looking for: an
> AtomicInteger equivalent would be fairly easy to construct, with nearly
> identical semantics to the Java version.
> Let's say a given znode has four bytes of data that represent an integer
> value. Get operations or set operations are easy, as ZK will make sure that
> all operations are atomic, so they happen in a linearizable order. A
> get-and-set operation can be performed by reading the znode via getData, and
> performing a conditional update using the version number of the znode that
> was returned as part of the getData operation. ZooKeeper's setData operation
> takes an optional version number (set it to -1 to ignore it) which tells the
> operation to succeed only if the znode's version hasn't changed since. Other
> operations can use this procedure as a base.
> This is exactly how Java's getAndSet is implemented - neither implementation
> is wait-free, but they are still lock-free: some process will always make
> progress.
> Hope this helps - let me know if you'd like more detail on exactly how to
> build this.
> Henry

Ah!  Didn't see that "version" parm on the setData call.  Yes, that 
makes atomic operations incredibly easy!

Thanks much for the explanation, Henry!


View raw message