1

What I want:

  1. I'd like to be able to use transactions with ZooKeeper
  2. I'd like for them to actually complete

Now I first tried using the Apache (nee Netflix) Curator library, but when following the examples to do a transaction like so:

curator.inTransaction().create().forPath(path, data)
      .and().commit();

and it would just get stuck there (I realize there's only one item in the transaction so a txn is unnecessary, but I'm trying to keep things simple). So I figured I'd write a more complete test scenario using the raw ZooKeeper API

final CountDownLatch connect = new CountDownLatch(1);
ZooKeeper z = new ZooKeeper("localhost:2181", 60000, new Watcher() {
  @Override
  public void process(WatchedEvent arg0) {
    System.err.println("EVENT! " + arg0);
    connect.countDown();
  }
});

List<ACL> acl = ImmutableList.of(new ACL(0777, new Id("world", "anyone")));
connect.await();
System.err.println("CREATING");
z.multi(ImmutableList.of(
    Op.create("/foo", "DATA".getBytes(), acl, CreateMode.PERSISTENT)));

It also hangs like a dead man after printing

EVENT! WatchedEvent state:SyncConnected type:None path:null
CREATING

As for the zookeeper server, it's running as a single node, so there are no other nodes it should be quarreling with. /foo never shows up in the tree - using zooinspector to look at it. If I use ZK without transactions, everything works fine. I hunted around the webz and found nothing relevant. Am I missing something obvious?

4

1 回答 1

4

事实证明你必须使用 ZooKeeper >= 3.4,否则你将度过糟糕的一天。ZooKeeper 3.3 在给定事务时只会闲逛,什么也不做。

于 2014-02-10T23:05:13.743 回答