Database Reference
In-Depth Information
WARNING
The sync operation is not like fsync() in POSIX filesystems. As mentioned earlier, writes in
ZooKeeper are atomic, and a successful write operation is guaranteed to have been written to persistent
storage on a majority of ZooKeeper servers. However, it is permissible for reads to lag the latest state of
the ZooKeeper service, and the sync operation exists to allow a client to bring itself up to date. This
topic is covered in more detail in Consistency .
Multiupdate
There is another ZooKeeper operation, called multi , that batches together multiple prim-
itive operations into a single unit that either succeeds or fails in its entirety. The situation
where some of the primitive operations succeed and some fail can never arise.
Multiupdate is very useful for building structures in ZooKeeper that maintain some global
invariant. One example is an undirected graph. Each vertex in the graph is naturally rep-
resented as a znode in ZooKeeper, and to add or remove an edge we need to update the
two znodes corresponding to its vertices because each has a reference to the other. If we
used only primitive ZooKeeper operations, it would be possible for another client to ob-
serve the graph in an inconsistent state, where one vertex is connected to another but the
reverse connection is absent. Batching the updates on the two znodes into one multi op-
eration ensures that the update is atomic, so a pair of vertices can never have a dangling
connection.
APIs
There are two core language bindings for ZooKeeper clients, one for Java and one for C;
there are also contrib bindings for Perl, Python, and REST clients. For each binding,
there is a choice between performing operations synchronously or asynchronously. We've
already seen the synchronous Java API. Here's the signature for the exists operation,
which returns either a Stat object that encapsulates the znode's metadata or null if the
znode doesn't exist:
public Stat exists ( String path , Watcher watcher ) throws
KeeperException ,
InterruptedException
The asynchronous equivalent, which is also found in the ZooKeeper class, looks like
this:
public void exists ( String path , Watcher watcher , StatCallback cb ,
Object ctx )
Search WWH ::




Custom Search