【问题标题】:Ignore lock-nodes when calling getChildren on a Zookeeper node在 Zookeeper 节点上调用 getChildren 时忽略锁定节点
【发布时间】:2016-08-15 16:24:41
【问题描述】:

当我在带有 curator 的 Zookeeper 节点上调用 getChildren() 时,有没有办法忽略代表锁的子节点?

我想用一个特定节点的数据读取所有子节点。因此,我首先调用 getChildren() 并遍历返回的 List 并对每个这样的孩子调用 getData()。为了避免孩子们在两者之间发生变化,我首先需要一个 InterProcessMutex。不幸的是,孩子的列表也包含这个互斥体。

InterProcessMutex mutex = new InterProcessMutex(client, parentNodePath);

mutex.acquire();

try {
  List<String> children = client.getChildren().forPath(parentNodePath);

  for (String child : children) {
    // do something
    // ignore the lock-node
  }

} finally {
  mutex.release();
}

有没有更聪明的方法来做到这一点?或者只是忽略锁定节点?

【问题讨论】:

  • 无论如何,即使我手动过滤掉锁定孩子,这也不起作用。这在创建具有不同客户端的节点 1-2 次后挂断。也许我正在陷入无限循环,因为以这种方式获取和释放锁会触发所有其他客户端上的 getChildren-watchers,这些客户端也尝试获取互斥锁等等。当我删除互斥体时,一切都按预期工作。

标签: java apache-zookeeper apache-curator


【解决方案1】:

为锁定节点使用不同的基数,这样实际数据就不会与锁定数据混合。锁不必知道它们在锁定什么,因此无需为锁机制提供相同的父基。

InterProcessMutex mutex = new InterProcessMutex(client, "/lock-base/lock-");

然后按照你做的方式做剩下的事情

mutex.acquire();

try {
  List<String> children = client.getChildren().forPath(parentNodePath);

  for (String child : children) {
    // do something
    // no need to worry about lock nodes
  }

} finally {
  mutex.release();
}

只要确保你对所有试图访问 parentNodePath 的应用程序使用相同的锁节点库就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多